Difference between revisions of "DPS905 Kaitlyn ParticleEffects"

From CDOT Wiki
Jump to: navigation, search
(Created page with ' ************************************************************************************************** ** Particles …')
 
([A] - INSTALLATION)
 
(40 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
  **************************************************************************************************
 
  **************************************************************************************************
  
 +
[[Image:Kaitlyn_particles_April2011_v01.jpg]]
  
INSTALLATION
 
  
1)
+
Contents:
 +
* [[#installation|[A] INSTALLATION]]
 +
* [[#using|[B] HOW TO USE]]
  
Add the following files to your project:
+
 
 +
<div id="installation"></div>
 +
 
 +
==[A] - INSTALLATION==
 +
 
 +
 
 +
1) Add the following files to your project:
  
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/Particle.cpp Particle.cpp]
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/Particle.cpp Particle.cpp]
Line 20: Line 28:
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/ParticleVertexList.cpp ParticleVertexList.cpp]
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/ParticleVertexList.cpp ParticleVertexList.cpp]
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/ParticleVertexList.h ParticleVertexList.h]
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/ParticleVertexList.h ParticleVertexList.h]
 +
* Optionally, you can get a simple texture for you to try making particle effects with here: [http://matrix.senecac.on.ca/~dacallow/DPS905/bubble.tga bubble.tga]
  
  
2)
 
  
Append onto the bottom of your existing Effects.fx file the contents of the following file:
+
2) Append onto the bottom of your existing Effects.fx file the contents of the following file:
 +
 
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/effects.fx Effects.fx]
 
* [http://matrix.senecac.on.ca/~dacallow/DPS905/effects.fx Effects.fx]
  
  
Modify your files making the following changes:
 
  
3)
+
3) Modify iObject.h:
 
 
Modify iObject.h:
 
  
 
* Add to Class iObject...
 
* Add to Class iObject...
Line 46: Line 52:
 
  float _endsize, float _frequencysize, Vector _startgravity, Vector _endgravity, float _frequencygravityvertical,
 
  float _endsize, float _frequencysize, Vector _startgravity, Vector _endgravity, float _frequencygravityvertical,
 
  float _frequencygravityhorizontal, float _startalpha, float _endalpha, float _frequencyalpha, Colour* c, float* p);
 
  float _frequencygravityhorizontal, float _startalpha, float _endalpha, float _frequencyalpha, Colour* c, float* p);
 +
 +
 +
 +
4) Modify Object.h
 +
 +
* Add to Class Object...
 +
 +
void update(int now) { /* do nothing, added for particle effects & animations */ }
 +
 +
 +
 +
5) Modify Display.cpp
 +
 +
* Add an include at the top..
 +
 +
#include "ParticleVertexList.h" // for access to ParticleVertexList::connectDevice()
 +
 +
 +
* Then add inside of the method: bool Display::setup(void* hwnd)
 +
 +
near line 360 (beneath the comment "// setup successful") add...
 +
ParticleVertex::device = d3dd;
 +
ParticleVertexList::connectDevice(d3dd);
 +
 +
 +
 +
6) Modify Configuration.h
 +
 +
* Adding into the existing typedef enum Category (line 375) a new type of:
 +
 +
PARTICLES
 +
 +
 +
 +
7) Modify Object.cpp
 +
 +
* Updating the Object::Object(category, colour, iGraphic*, int, int) method..
 +
 +
Change from..
 +
// store reflectivity, shininess, and texture pointers
 +
material = new Colour[nSubsets];
 +
shine    = new float[nSubsets];
 +
texture  = new iTexture**[nSubsets];
 +
for (int i = 0; i < nSubsets; i++) {
 +
  material[i] = c[i];
 +
  if (p && p[i])
 +
    shine[i] = p[i];
 +
  else
 +
    shine[i] = DEFAULT_SHININESS;
 +
  if (material[i].a != 1.0f)
 +
    category = TRANSLUCENT_OBJECT;
 +
  texture[i] = new iTexture*[nStages];
 +
  for (int j = 0; j < nStages; j++)
 +
    texture[i][j] = NULL;
 +
}
 +
 +
And change it to..:
 +
// store reflectivity, shininess, and texture pointers
 +
material = new Colour[nSubsets];
 +
shine    = new float[nSubsets];
 +
texture  = new iTexture**[nSubsets];
 +
for (int i = 0; i < nSubsets; i++) {
 +
  material[i] = c[i];
 +
  if (p && p[i])
 +
    shine[i] = p[i];
 +
  else
 +
    shine[i] = DEFAULT_SHININESS;
 +
  if (material[i].a != 1.0f '''&& category != PARTICLES''')
 +
    category = TRANSLUCENT_OBJECT;
 +
  texture[i] = new iTexture*[nStages];
 +
  for (int j = 0; j < nStages; j++)
 +
    texture[i][j] = NULL;
 +
}
 +
 +
 +
 +
8) Modify DeviceTexture.cpp
 +
 +
* Inside the method ( void DeviceTexture::create(iScene* scene, const Matrix& view, const Matrix& projection, const Vector& viewpoint) )
 +
Near line 190...
 +
 +
Change from:
 +
d3dd->BeginScene();
 +
scene->draw(SKYBOX);
 +
scene->draw(OPAQUE_OBJECT); 
 +
scene->draw(TRANSLUCENT_OBJECT);
 +
d3dd->EndScene();
 +
 +
And change it to..:
 +
d3dd->BeginScene();
 +
scene->draw(SKYBOX);
 +
scene->draw(OPAQUE_OBJECT); 
 +
scene->draw(TRANSLUCENT_OBJECT);
 +
'''scene->draw(PARTICLES);'''
 +
d3dd->EndScene();
 +
 +
 +
 +
9) Modify Design.cpp
 +
 +
* Modify void Design::draw()
 +
 +
Changing from:
 +
scene->draw(MIRROR);
 +
scene->draw(SKYBOX);
 +
scene->draw(OPAQUE_OBJECT);
 +
scene->draw(TRANSLUCENT_OBJECT);
 +
hud->draw(HUD_ALPHA);
 +
 +
Changing to:
 +
scene->draw(MIRROR);
 +
scene->draw(SKYBOX);
 +
scene->draw(OPAQUE_OBJECT);
 +
scene->draw(TRANSLUCENT_OBJECT);
 +
'''scene->draw(PARTICLES);'''
 +
hud->draw(HUD_ALPHA);
 +
 +
 +
 +
10) Modify iVertexList.h
 +
 +
* Add to Class iVertexList
 +
 +
virtual void clear() = 0;
 +
 +
* Add to the bottom of the file iVertexList.h
 +
 +
extern "C"
 +
iVertexList* CreateParticleVertexList(PrimitiveType t, int np, bool flip = false);
 +
 +
 +
 +
11) Modify VertexList.h
 +
 +
* Add to Class VertexList
 +
 +
void clear() {nVertices = 0;} /* added for particle effects */
 +
 +
* At the bottom of VertexList.h add to the Class StockMesh
 +
 +
void clear() {}
 +
 +
 +
 +
<div id="using"></div>
 +
 +
== [B] - HOW TO USE ==
 +
 +
To create a particle:
 +
 +
1) Include in ParticleGenerator.h
 +
 +
2) Use the following suggested code (maybe in Design.cpp inside Design::initialize(int now)):
 +
 +
    //VARIABLES
 +
    float generatorlifespan        = 0.0f;                          // how long for the generator to run for (0.0f is forever) in seconds
 +
    float particlelifespan        = 3.0f;                          // how long particles live for
 +
    float particlespawnrate        = 180.0f;                        // how many particles can spawn, per second, up to the MAX_PARTICLES limit
 +
    float spawndispersion          = 6.0f;                          // how varied are the spawn positions (0.0f means spawn on the same exact positon initially)
 +
    float theta                    = 80.0f;                        // how varied is the initial velocities, 180.0f is a full sphere, 90.0f is a hemisphere, 40.0f is a cone, etc
 +
    float startingspeed            = 110.0f;                        // initial speed applied to the velocities
 +
    Colour startcolour            = Colour(2.0f,0.0f,0.0f);
 +
    Colour endcolour              = Colour(0.0f,0.0f,1.0f);
 +
    float frequencycolour          = 0.0f; 
 +
    float startsize                = 5.0f;
 +
    float endsize                  = 0.0f;
 +
    float frequencysize            = 0.0f;
 +
    Vector startgravity            = Vector(0.0f, -29.8f, 0.0f);
 +
    Vector endgravity              = Vector(0.0f, -199.8f, 0.0f);
 +
    float frequencygravityvertical  = 0.0f;
 +
    float frequencygravityhorizontal = 0.0f;
 +
    float startalpha              = 1.0f;
 +
    float endalpha                = 0.0f;
 +
    float frequencyalpha          = 0.0f;
 +
    Colour c                      = Colour(1.0f,0.0f,0.0f);        //for object (material type)... not actually used
 +
    float p                        = 0.5f;                          //for object (reflectivity)... not actually used
 +
 +
 +
    iObject* particleEffect1 = CreateParticleGenerator(generatorlifespan, particlelifespan, particlespawnrate, spawndispersion, theta,
 +
      startingspeed, startcolour, endcolour, frequencycolour, startsize, endsize, frequencysize, startgravity, endgravity, frequencygravityvertical,
 +
      frequencygravityhorizontal, startalpha, endalpha, frequencyalpha, &c, &p);
 +
 +
    //Add your desired texture on
 +
    iTexture* particle_texture = CreateTexture(L"bubble.tga");
 +
    particleEffect1->attach(particle_texture,0);
 +
 +
    //Position your particle generator
 +
    particleEffect1->translate(250,70,250);
 +
 +
    //Optionally particles may be set to attract to other objects or positions in the world
 +
    //((ParticleGenerator*)particleEffect1)->AttractTo(someObject,250.0f);
 +
    //((ParticleGenerator*)particleEffect1)->AttractTo(&Vector(50,50,50),250.0f);
 +
 +
 +
3) Add inside Design.cpp in the Design::Update(int now) method your particle generator's update method.
 +
 +
particleEffect1->update(now);
 +
 +
 +
'''PLEASE NOTE:''' The following is not working / implemented at this time: frequency, alpha transparency

Latest revision as of 21:41, 13 April 2011

**************************************************************************************************
**   Particles                                                                                   *
**************************************************************************************************
*    April 2011
*    By Kaitlyn Callow ( KaitlynCallow.com | dacallow@learn.senecac.on.ca )
*    For DPS905, Professor Chris Szalwinski
**************************************************************************************************

Kaitlyn particles April2011 v01.jpg


Contents:


[A] - INSTALLATION

1) Add the following files to your project:


2) Append onto the bottom of your existing Effects.fx file the contents of the following file:


3) Modify iObject.h:

  • Add to Class iObject...
 //added for particle effects / animations
 virtual void update(int now) = 0;
  • At the bottom of the file iObject.h add...
extern "C"
iObject* CreateParticleGenerator(float _generatorlifespan, float _particlelifespan, float _particlespawnrate, float _spawnpositiondispersion,
float _theta, float _startingspeed, Colour _startcolour, Colour _endcolour, float _frequencycolour, float _startsize,
float _endsize, float _frequencysize, Vector _startgravity, Vector _endgravity, float _frequencygravityvertical,
float _frequencygravityhorizontal, float _startalpha, float _endalpha, float _frequencyalpha, Colour* c, float* p);


4) Modify Object.h

  • Add to Class Object...
void update(int now) { /* do nothing, added for particle effects & animations */ }


5) Modify Display.cpp

  • Add an include at the top..
#include "ParticleVertexList.h" // for access to ParticleVertexList::connectDevice()


  • Then add inside of the method: bool Display::setup(void* hwnd)

near line 360 (beneath the comment "// setup successful") add...

ParticleVertex::device = d3dd;
ParticleVertexList::connectDevice(d3dd);


6) Modify Configuration.h

  • Adding into the existing typedef enum Category (line 375) a new type of:
PARTICLES


7) Modify Object.cpp

  • Updating the Object::Object(category, colour, iGraphic*, int, int) method..

Change from..

// store reflectivity, shininess, and texture pointers
material = new Colour[nSubsets];
shine    = new float[nSubsets];
texture  = new iTexture**[nSubsets];
for (int i = 0; i < nSubsets; i++) {
  material[i] = c[i];
  if (p && p[i])
    shine[i] = p[i];
  else
    shine[i] = DEFAULT_SHININESS;
  if (material[i].a != 1.0f)
    category = TRANSLUCENT_OBJECT;
  texture[i] = new iTexture*[nStages];
  for (int j = 0; j < nStages; j++)
    texture[i][j] = NULL;
}

And change it to..:

// store reflectivity, shininess, and texture pointers
material = new Colour[nSubsets];
shine    = new float[nSubsets];
texture  = new iTexture**[nSubsets];
for (int i = 0; i < nSubsets; i++) {
  material[i] = c[i];
  if (p && p[i])
    shine[i] = p[i];
  else
    shine[i] = DEFAULT_SHININESS;
  if (material[i].a != 1.0f && category != PARTICLES)
    category = TRANSLUCENT_OBJECT;
  texture[i] = new iTexture*[nStages];
  for (int j = 0; j < nStages; j++)
    texture[i][j] = NULL;
}


8) Modify DeviceTexture.cpp

  • Inside the method ( void DeviceTexture::create(iScene* scene, const Matrix& view, const Matrix& projection, const Vector& viewpoint) )

Near line 190...

Change from:

d3dd->BeginScene();
scene->draw(SKYBOX);
scene->draw(OPAQUE_OBJECT);   
scene->draw(TRANSLUCENT_OBJECT);
d3dd->EndScene();

And change it to..:

d3dd->BeginScene();
scene->draw(SKYBOX);
scene->draw(OPAQUE_OBJECT);   
scene->draw(TRANSLUCENT_OBJECT);
scene->draw(PARTICLES);
d3dd->EndScene();


9) Modify Design.cpp

  • Modify void Design::draw()

Changing from:

scene->draw(MIRROR);
scene->draw(SKYBOX);
scene->draw(OPAQUE_OBJECT);
scene->draw(TRANSLUCENT_OBJECT);
hud->draw(HUD_ALPHA);

Changing to:

scene->draw(MIRROR);
scene->draw(SKYBOX);
scene->draw(OPAQUE_OBJECT);
scene->draw(TRANSLUCENT_OBJECT);
scene->draw(PARTICLES);
hud->draw(HUD_ALPHA);


10) Modify iVertexList.h

  • Add to Class iVertexList
virtual void clear() = 0;
  • Add to the bottom of the file iVertexList.h
extern "C"
iVertexList* CreateParticleVertexList(PrimitiveType t, int np, bool flip = false);


11) Modify VertexList.h

  • Add to Class VertexList
void clear() {nVertices = 0;} /* added for particle effects */
  • At the bottom of VertexList.h add to the Class StockMesh
void clear() {}


[B] - HOW TO USE

To create a particle:

1) Include in ParticleGenerator.h

2) Use the following suggested code (maybe in Design.cpp inside Design::initialize(int now)):

   //VARIABLES
   float generatorlifespan        = 0.0f;                          // how long for the generator to run for (0.0f is forever) in seconds
   float particlelifespan         = 3.0f;                          // how long particles live for
   float particlespawnrate        = 180.0f;                        // how many particles can spawn, per second, up to the MAX_PARTICLES limit
   float spawndispersion          = 6.0f;                          // how varied are the spawn positions (0.0f means spawn on the same exact positon initially)
   float theta                    = 80.0f;                         // how varied is the initial velocities, 180.0f is a full sphere, 90.0f is a hemisphere, 40.0f is a cone, etc
   float startingspeed            = 110.0f;                        // initial speed applied to the velocities
   Colour startcolour             = Colour(2.0f,0.0f,0.0f);
   Colour endcolour               = Colour(0.0f,0.0f,1.0f);
   float frequencycolour          = 0.0f;   
   float startsize                = 5.0f;
   float endsize                  = 0.0f;
   float frequencysize            = 0.0f;
   Vector startgravity            = Vector(0.0f, -29.8f, 0.0f);
   Vector endgravity              = Vector(0.0f, -199.8f, 0.0f);
   float frequencygravityvertical   = 0.0f;
   float frequencygravityhorizontal = 0.0f;
   float startalpha               = 1.0f;
   float endalpha                 = 0.0f;
   float frequencyalpha           = 0.0f;
   Colour c                       = Colour(1.0f,0.0f,0.0f);        //for object (material type)... not actually used
   float p                        = 0.5f;                          //for object (reflectivity)... not actually used


   iObject* particleEffect1 = CreateParticleGenerator(generatorlifespan, particlelifespan, particlespawnrate, spawndispersion, theta,
     startingspeed, startcolour, endcolour, frequencycolour, startsize, endsize, frequencysize, startgravity, endgravity, frequencygravityvertical,
     frequencygravityhorizontal, startalpha, endalpha, frequencyalpha, &c, &p);

   //Add your desired texture on
   iTexture* particle_texture = CreateTexture(L"bubble.tga");
   particleEffect1->attach(particle_texture,0);

   //Position your particle generator
   particleEffect1->translate(250,70,250);

   //Optionally particles may be set to attract to other objects or positions in the world
   //((ParticleGenerator*)particleEffect1)->AttractTo(someObject,250.0f);
   //((ParticleGenerator*)particleEffect1)->AttractTo(&Vector(50,50,50),250.0f);


3) Add inside Design.cpp in the Design::Update(int now) method your particle generator's update method.

particleEffect1->update(now);


PLEASE NOTE: The following is not working / implemented at this time: frequency, alpha transparency