Hi all!
Ok so I was reading NeHe's tutorials which I find easier than the other opengl tutorials.
Then I reached that tutorial in which he teaches how to develop a particle system. So I thought I'd add it to my dear old Poly3d engine, so I made every particle in a class so using for loops I initaialise all the particles in the array and then use a function to change the location of that particle and then I check whether a particle is visible or not.
My problem is that the particles would not display at all, can you help me, ofcourse I think the problem is in the display function but can't be sure, for the compiler gives no error.
The code:
// Coordinates structure
typedef struct
{
float x;
float y;
float z;
}Poly3d_coord;
// Colour type.
typedef struct
{
float r;
float g;
float b;
float a;
}Poly3d_col;
class Poly3d_part
{
public:
bool toggle;
float age;
float fade;
float speed;
Poly3d_col col;
Poly3d_coord coords;
Poly3d_coord size;
Poly3d_coord inc;
Poly3d_coord grav;
// Process a particle
void Poly3d_modPart()
{
coords.x=inc.x/speed*1000;
coords.y=inc.y/speed*1000;
coords.z=inc.z/speed*1000;
inc.x+=grav.x;
inc.y+=grav.y;
inc.z+=grav.z;
age-=fade;
}
// Display particle
void Poly3d_dispPart(bool tex=true)
{
if(toggle)
{
glColor4f(col.r,col.g,col.b,age);
float v0[]={ coords.x+size.x,coords.y+size.y,coords.z };
float v1[]={ coords.x,coords.y+size.y,coords.z };
float v2[]={ coords.x+size.x,coords.y,coords.z };
float v3[]={ coords.x,coords.y,coords.z };
if(tex)
{
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(1.0f,1.0f); glVertex3fv(v0);
glTexCoord2f(0.0f,1.0f); glVertex3fv(v1);
glTexCoord2f(1.0f,0.0f); glVertex3fv(v2);
glTexCoord2f(0.0f,0.0f); glVertex3fv(v3);
glEnd();
}
else
{
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(v0);
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);
glEnd();
}
}
}
// Check if particle is alive
bool Poly3d_checkPart()
{
if(age<0.0f)
{
return false;
}
else
{
return true;
}
}
};
So that was the class above. Now the code I use in my programs for particles ( I dont give you the whole code but how I do certain things in the program for I am sure that the code not present here works but even then this is in the order of how it is done )
I set the colour over here
pcol.r=1.0f;
pcol.g=1.0f;
pcol.b=0.0f;
then I initialize every particle
for(int i=0;i<1000;i++)
{
part.toggle=true;
part.age=1.0f;
part.fade=(rand()%100)/1000.0f+0.003f;
part.col=pcol;
part.size.x=0.01f;
part.size.y=0.01f;
part.size.z=0.01f;
part.inc.x=(rand()%50)-25.0f;
part.inc.y=(rand()%50)-25.0f;
part.inc.z=(rand()%50)-25.0f;
part.grav.x=0.0f;
part.grav.y=-0.8f;
part.grav.z=0.0f;
}
and then I let it change the particles values
for(int j=0;j<1000;j++)
{
part[j].Poly3d_modPart();
}
At last I display it
Poly3d_Begin();
glTranslatef(0.0f,0.0f,50.0f);
texture=tex1.Poly3d_RAWtex();
for(int j=0;j<1000;j++)
{
part[j].Poly3d_dispPart();
}
glDeleteTextures(1,&texture);
Poly3d_End();
now only the check if particle still exists part is left
for(int k=0;k<1000;k++)
{
if(!part[k].Poly3d_checkPart())
{
part[k].toggle=true;
part[k].age=1.0f;
part[k].fade=(rand()%100)/1000.0f+0.003f;
part[k].col=pcol;
part[k].size.x=0.01f;
part[k].size.y=0.01f;
part[k].size.z=0.01f;
part[k].inc.x=(rand()%50)-25.0f;
part[k].inc.y=(rand()%50)-25.0f;
part[k].inc.z=(rand()%50)-25.0f;
part[k].grav.x=0.0f;
part[k].grav.y=-0.8f;
part[k].grav.z=0.0f;
}
}
Thats a lot of code and some of it might be useless but I need a lot of help, please help me.