Files
GDevelop/Extensions/ParticleSystem/ParticleEmitterObject.cpp
T
Florian ff109c71d8 Working particle system rendered with OpenGL. Just a problem with other 3D object : Their texture are not showed.
git-svn-id: svn://localhost@273 8062f311-0dae-4547-b526-b8ab9ac864a5
2010-10-14 19:53:18 +00:00

575 lines
19 KiB
C++

/**
Game Develop - Particle System Extension
Copyright (c) 2010 Florian Rival (Florian.Rival@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include <SFML/Graphics.hpp>
#include "GDL/Object.h"
#include "GDL/ImageManager.h"
#include "GDL/tinyxml.h"
#include "GDL/FontManager.h"
#include "GDL/Position.h"
#include "GDL/XmlMacros.h"
#include "ParticleEmitterObject.h"
#include <SPK_GL.h>
#ifdef GDE
#include <wx/wx.h>
#include "GDL/CommonTools.h"
#include "GDL/ResourcesMergingHelper.h"
#include "GDL/MainEditorCommand.h"
#include "ParticleEmitterObjectEditor.h"
#endif
bool ParticleEmitterObject::SPKinitialized = false;
ParticleEmitterObject::ParticleEmitterObject(std::string name_) :
Object(name_),
baseParticleSystemID(SPK::NO_ID),
particleSystem(NULL),
particleModel(NULL),
emitter(NULL),
group(NULL),
rendererType(Point),
rendererParam1(1.0f),
rendererParam2(1.0f),
additive(true),
tank(-1),
flow(300),
emitterForceMin(70.0f),
emitterForceMax(170.0f),
particleGravityX(0.0f),
particleGravityY(0.0f),
particleGravityZ(100.0f),
friction(2.0f),
particleLifeTimeMin(0.5f),
particleLifeTimeMax(2.5f),
redParam(Enabled),
greenParam(Random),
blueParam(Random),
alphaParam(Mutable),
particleRed1(1.0f),
particleRed2(1.0f),
particleGreen1(0.2f),
particleGreen2(0.8f),
particleBlue1(0.2f),
particleBlue2(0.0f),
particleAlpha1(0.8f),
particleAlpha2(0.0f),
opacity( 255 ),
colorR( 255 ),
colorG( 255 ),
colorB( 255 ),
angle(0)
{
if ( !SPKinitialized )
{
SPK::randomSeed = static_cast<unsigned int>(time(NULL));
SPK::System::setClampStep(true,0.1f); // clamp the step to 100 ms
SPK::System::useAdaptiveStep(0.001f,0.01f); // use an adaptive step from 1ms to 10ms (1000fps to 100fps)
SPK::SFML::SFMLRenderer::setZFactor(1.0f);
SPK::SFML::setCameraPosition(SPK::SFML::CAMERA_CENTER,SPK::SFML::CAMERA_BOTTOM,static_cast<float>(800),0.0f);
SPKinitialized = true;
}
}
void ParticleEmitterObject::LoadFromXml(const TiXmlElement * elem)
{
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("tank", tank);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("flow", flow);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("emitterForceMin", emitterForceMin);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("emitterForceMax", emitterForceMax);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleGravityX", particleGravityX);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleGravityY", particleGravityY);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleGravityZ", particleGravityZ);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("friction", friction);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleLifeTimeMin", particleLifeTimeMin);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleLifeTimeMax", particleLifeTimeMax);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleRed1", particleRed1);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleRed2", particleRed2);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleGreen1", particleGreen1);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleGreen2", particleGreen2);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleBlue1", particleBlue1);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleBlue2", particleBlue2);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleAlpha1", particleAlpha1);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_FLOAT("particleAlpha2", particleAlpha2);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_BOOL("additive", additive);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_STRING("textureParticleName", textureParticleName);
{
std::string result;
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_STRING("rendererType", result);
if ( result == "Line") rendererType = Line;
else if ( result == "Quad") rendererType = Quad;
else rendererType = Point;
}
{
std::string result;
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_STRING("redParam", result);
if ( result == "Mutable") redParam = Mutable;
else if ( result == "Random") redParam = Random;
else redParam = Enabled;
}
{
std::string result;
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_STRING("greenParam", result);
if ( result == "Mutable") greenParam = Mutable;
else if ( result == "Random") greenParam = Random;
else greenParam = Enabled;
}
{
std::string result;
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_STRING("blueParam", result);
if ( result == "Mutable") blueParam = Mutable;
else if ( result == "Random") blueParam = Random;
else blueParam = Enabled;
}
{
std::string result;
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_STRING("alphaParam", result);
if ( result == "Mutable") alphaParam = Mutable;
else if ( result == "Random") alphaParam = Random;
else alphaParam = Enabled;
}
int r,g,b;
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_INT("colorR", r);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_INT("colorG", g);
GD_CURRENT_ELEMENT_LOAD_ATTRIBUTE_INT("colorB", b);
colorR = r; colorG = g; colorB = b;
}
#if defined(GDE)
void ParticleEmitterObject::SaveToXml(TiXmlElement * elem)
{
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("tank", tank);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("flow", flow);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("emitterForceMin", emitterForceMin);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("emitterForceMax", emitterForceMax);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleGravityX", particleGravityX);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleGravityY", particleGravityY);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleGravityZ", particleGravityZ);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("friction", friction);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleLifeTimeMin", particleLifeTimeMin);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleLifeTimeMax", particleLifeTimeMax);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleRed1", particleRed1);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleRed2", particleRed2);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleGreen1", particleGreen1);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleGreen2", particleGreen2);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleBlue1", particleBlue1);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleBlue2", particleBlue2);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleAlpha1", particleAlpha1);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_FLOAT("particleAlpha2", particleAlpha2);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_BOOL("additive", additive);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_STRING("textureParticleName", textureParticleName);
std::string rendererTypeStr = "Point";
if ( rendererType == Line ) rendererTypeStr = "Line";
else if ( rendererType == Quad ) rendererTypeStr = "Quad";
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_STRING("rendererType", rendererTypeStr);
std::string redParamStr = "Enabled";
if ( redParam == Mutable ) redParamStr = "Mutable";
else if ( redParam == Random ) redParamStr = "Random";
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_STRING("redParam", redParamStr);
std::string greenParamStr = "Enabled";
if ( greenParam == Mutable ) greenParamStr = "Mutable";
else if ( greenParam == Random ) greenParamStr = "Random";
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_STRING("greenParam", greenParamStr);
std::string blueParamStr = "Enabled";
if ( blueParam == Mutable ) blueParamStr = "Mutable";
else if ( blueParam == Random ) blueParamStr = "Random";
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_STRING("blueParam", blueParamStr);
std::string alphaParamStr = "Enabled";
if ( alphaParam == Mutable ) alphaParamStr = "Mutable";
else if ( alphaParam == Random ) alphaParamStr = "Random";
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE_STRING("alphaParam", alphaParamStr);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE("colorR", colorR);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE("colorG", colorG);
GD_CURRENT_ELEMENT_SAVE_ATTRIBUTE("colorB", colorB);
}
#endif
// creates and register the base system
SPK::SPK_ID ParticleEmitterObject::CreateBaseParticleSystem()
{
// Creates the model
particleModel = SPK::Model::create(SPK::FLAG_RED | SPK::FLAG_GREEN | SPK::FLAG_BLUE | SPK::FLAG_ALPHA,
SPK::FLAG_ALPHA,
SPK::FLAG_RED | SPK::FLAG_GREEN | SPK::FLAG_BLUE);
particleModel->setParam(SPK::PARAM_ALPHA,1.0f,0.0f); // This makes the particles fade out over time
particleModel->setParam(SPK::PARAM_BLUE,1.0f,0.0f); // NOTE : Modified
particleModel->setParam(SPK::PARAM_RED,0.0f,1.0f); // NOTE : Modified
particleModel->setLifeTime(1.0f,2.0f);
// Create the renderer
SPK::GL::GLRenderer* renderer = NULL;
SPK::GL::GLPointRenderer* pointRenderer = SPK::GL::GLPointRenderer::create();
pointRenderer->setType(SPK::POINT_CIRCLE);
pointRenderer->enableWorldSize(true);
SPK::GL::GLPointRenderer::setPixelPerUnit(45.0f * 3.14159f / 180.f,600.0f); //!!NOTE : HARDCODED SIZE //Note : Fovy and screenheight
pointRenderer->setSize(1.0f);
//pointRenderer->setTexture(textureIndex); //!!NOTE : COMMENTED LINE
renderer = pointRenderer;
renderer->enableBlending(true);
renderer->setBlendingFunctions(GL_SRC_ALPHA,GL_ONE); // additive blending
renderer->setTextureBlending(GL_MODULATE); // the texture is modulated with the particle's color
renderer->enableRenderingHint(SPK::DEPTH_TEST,false); // the depth test is disabled
// Creates the zone
SPK::Point* source = SPK::Point::create();
// Creates the emitter
emitter = SPK::SphericEmitter::create(SPK::Vector3D(0.0f,1.0f,0.0f),3.14159f / 4.0f,3.0f * 3.14159f / 4.0f);
//emitter = SPK::RandomEmitter::create();
emitter->setForce(100,320);
emitter->setZone(source);
emitter->setTank(-1); // NOTE : Modified
emitter->setFlow(500); // Creates all the particles in the tank at the first frame // NOTE : Modified
// Creates the Group
group = SPK::Group::create(particleModel,500); // 500 particles is the maximum capacity of the group
group->addEmitter(emitter);
group->setGravity(SPK::Vector3D(0.0f,-100.0f,0.0f));
group->setFriction(2.0f);
group->setRenderer(renderer);
// Creates the System
SPK::System* system = SPK::System::create();
system->addGroup(group);
// Defines which objects will be shared by all systems
particleModel->setShared(true);
renderer->setShared(true);
// Creates the base and gets a pointer to the base
return system->getSPKID();
}
bool ParticleEmitterObject::LoadRuntimeResources(const ImageManager & imageMgr )
{
// Loads particle texture
textureParticle = imageMgr.GetImage(textureParticleName);
baseParticleSystemID = CreateBaseParticleSystem();
particleSystem = SPK_Copy(SPK::System, baseParticleSystemID);
return true;
}
bool ParticleEmitterObject::LoadResources(const ImageManager & imageMgr)
{
#if defined(GDE)
edittimeIconImage.LoadFromFile("Extensions/particleSystemicon.png");
edittimeIcon.SetImage(edittimeIconImage);
#endif
return true;
}
ParticleEmitterObject::~ParticleEmitterObject()
{
if ( particleSystem ) SPK_Destroy(particleSystem);
}
/**
* Update animation and direction from the inital position
*/
bool ParticleEmitterObject::InitializeFromInitialPosition(const InitialPosition & position)
{
return true;
}
/**
* Render object at runtime
*/
bool ParticleEmitterObject::Draw( sf::RenderWindow& window )
{
//Don't draw anything if hidden
if ( hidden ) return true;
window.RestoreGLStates();
float windowRatio = static_cast<float>(window.GetView().GetSize().x)/static_cast<float>(window.GetView().GetSize().y);
float sizeRatio = 0.3330 //To make base have the same size as a rectangle drawn by SFML
*1/window.GetView().GetSize().y*600.f; //To make size window's size independant
//Get the position of the box
float x = ( (GetX()-window.GetView().GetCenter().x+window.GetView().GetSize().x/2) * 200.f / window.GetView().GetSize().x - 100.f)*windowRatio;
float y = -(GetY()-window.GetView().GetCenter().y+window.GetView().GetSize().y/2) * 200.f / window.GetView().GetSize().y + 100.f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
cout << "x:" << x << "y:" << y <<endl;
glRotatef(window.GetView().GetRotation(), 0, 0, 1);
glTranslatef(x, y, -100); //5 : Arbitrary
particleSystem->render();
window.SaveGLStates();
return true;
}
#ifdef GDE
/**
* Render object at edittime
*/
bool ParticleEmitterObject::DrawEdittime(sf::RenderWindow& renderWindow)
{
edittimeIcon.SetPosition(GetX(), GetY());
renderWindow.Draw(edittimeIcon);
return true;
}
void ParticleEmitterObject::PrepareResourcesForMerging(ResourcesMergingHelper & resourcesMergingHelper)
{
}
bool ParticleEmitterObject::GenerateThumbnail(const Game & game, wxBitmap & thumbnail)
{
thumbnail = wxBitmap("Extensions/particleSystemicon24.png", wxBITMAP_TYPE_ANY);
return true;
}
void ParticleEmitterObject::EditObject( wxWindow* parent, Game & game, MainEditorCommand & mainEditorCommand )
{
ParticleEmitterObjectEditor dialog(parent, game, *this, mainEditorCommand);
dialog.ShowModal();
}
wxPanel * ParticleEmitterObject::CreateInitialPositionPanel( wxWindow* parent, const Game & game_, const Scene & scene_, const InitialPosition & position )
{
return NULL;
}
void ParticleEmitterObject::UpdateInitialPositionFromPanel(wxPanel * panel, InitialPosition & position)
{
}
void ParticleEmitterObject::GetPropertyForDebugger(unsigned int propertyNb, string & name, string & value) const
{
if ( !particleSystem ) return;
if ( propertyNb == 0 ) {name = _("Nombre de particules"); value = ToString(particleSystem->getNbParticles());}
else if ( propertyNb == 1 ) {name = _("Couleur"); value = ToString(GetColorR())+";"+ToString(GetColorG())+";"+ToString(GetColorB());}
else if ( propertyNb == 2 ) {name = _("Opacité"); value = ToString(GetOpacity());}
}
bool ParticleEmitterObject::ChangeProperty(unsigned int propertyNb, string newValue)
{
if ( propertyNb == 0 ) { return false; }
else if ( propertyNb == 1 )
{
string r, gb, g, b;
{
size_t separationPos = newValue.find(";");
if ( separationPos > newValue.length())
return false;
r = newValue.substr(0, separationPos);
gb = newValue.substr(separationPos+1, newValue.length());
}
{
size_t separationPos = gb.find(";");
if ( separationPos > gb.length())
return false;
g = gb.substr(0, separationPos);
b = gb.substr(separationPos+1, gb.length());
}
SetColor(ToInt(r), ToInt(g), ToInt(b));
}
else if ( propertyNb == 2 ) { SetOpacity(ToFloat(newValue)); }
return true;
}
unsigned int ParticleEmitterObject::GetNumberOfProperties() const
{
return 3;
}
#endif
void ParticleEmitterObject::SetTank(float newValue)
{
tank = newValue;
if ( emitter ) emitter->setFlow(tank);
}
void ParticleEmitterObject::SetFlow(float newValue)
{
flow = newValue;
if ( emitter ) emitter->setFlow(flow);
}
void ParticleEmitterObject::SetEmitterForceMin(float newValue)
{
emitterForceMin = newValue;
if ( emitter ) emitter->setForce(emitterForceMin, emitterForceMax);
}
void ParticleEmitterObject::SetEmitterForceMax(float newValue)
{
emitterForceMax = newValue;
if ( emitter ) emitter->setForce(emitterForceMin, emitterForceMax);
}
void ParticleEmitterObject::SetParticleGravityX(float newValue)
{
particleGravityX = newValue;
if ( group ) group->setGravity(SPK::Vector3D(particleGravityX,particleGravityY,particleGravityZ));
}
void ParticleEmitterObject::SetParticleGravityY(float newValue)
{
particleGravityY = newValue;
if ( group ) group->setGravity(SPK::Vector3D(particleGravityX,particleGravityY,particleGravityZ));
}
void ParticleEmitterObject::SetParticleGravityZ(float newValue)
{
particleGravityZ = newValue;
if ( group ) group->setGravity(SPK::Vector3D(particleGravityX,particleGravityY,particleGravityZ));
}
void ParticleEmitterObject::SetFriction(float newValue)
{
friction = newValue;
if ( group ) group->setFriction(friction);
}
void ParticleEmitterObject::OnPositionChanged()
{
/*if ( particleSystem )
particleSystem->SetPosition(GetX(), GetY());*/
}
/**
* Get the real X position of the sprite
*/
float ParticleEmitterObject::GetDrawableX() const
{
return GetX();
}
/**
* Get the real Y position of the text
*/
float ParticleEmitterObject::GetDrawableY() const
{
return GetY();
}
/**
* Width is the width of the current sprite.
*/
float ParticleEmitterObject::GetWidth() const
{
return 32;
}
/**
* Height is the height of the current sprite.
*/
float ParticleEmitterObject::GetHeight() const
{
return 32;
}
/**
* X center is computed with text rectangle
*/
float ParticleEmitterObject::GetCenterX() const
{
return 16;
}
/**
* Y center is computed with text rectangle
*/
float ParticleEmitterObject::GetCenterY() const
{
return 16;
}
/**
* Nothing to do when updating time
*/
void ParticleEmitterObject::UpdateTime(float deltaTime)
{
particleSystem->update (deltaTime);
}
/**
* Change the color filter of the sprite object
*/
void ParticleEmitterObject::SetColor( unsigned int r, unsigned int g, unsigned int b )
{
colorR = r;
colorG = g;
colorB = b;
//if ( particleSystem ) particleSystem->SetColor(sf::Color(colorR, colorG, colorB, opacity));
}
void ParticleEmitterObject::SetOpacity(float val)
{
if ( val > 255 )
val = 255;
else if ( val < 0 )
val = 0;
opacity = val;
//if ( particleSystem ) particleSystem->SetColor(sf::Color(colorR, colorG, colorB, opacity));
}
/**
* Function destroying an extension Object.
* Game Develop does not delete directly extension object
* to avoid overloaded new/delete conflicts.
*/
void DestroyParticleEmitterObject(Object * object)
{
delete object;
}
/**
* Function creating an extension Object.
* Game Develop can not directly create an extension object
*/
Object * CreateParticleEmitterObject(std::string name)
{
return new ParticleEmitterObject(name);
}