Particle engine - Need help

Started by
8 comments, last by Scorp07 16 years, 11 months ago
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.
Advertisement
first thought: try a little data dump to screen or file and check whether the particles have valid positions.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Wrote the code so that it writes the values of the variables to a file or screen using console in program type. But that crashes the application means that either I am programming wrong or something else. I'll try something else

I debug and get values for all 1000 variable which do look correct, some other problem.

Found something!!!
when I mod part It would not work otherwise it will!
so there is a problem there
I don't know which IDE you're using, but I use VS 2005, and in a situation such as yours, I simply set a breakpoint at the end of an initialization, for example, and after the break, inspect the values of the member variables of the object to see if it's doing what I expect. It's a little trickier when there's a bug that occurs deep into loops, but then I just set a breakpoint that breaks when a certain variable meets a given value, then inspect.

I've been looking at the code, but I'm not experienced in OpenGL, which leaves me somewhat limited to syntax review. If I can spot something I'll update, however.

Scorp
Since you remind me I'll specify, I am using Dev C++ on windows and open gl with a graphics card of which i do not have drivers.

Fixed it actually, it works now but not like Nehe's so I think ill come back to this. The particles actually look like glow worms :)
lol I guess I was switching between apps and didn't realize you had posted that you had it narrowed down when I posted. Ahh well. Very nice on getting it working. I've always found particle systems fun because there are so many different things you can do with them.

Wouldn't mind seeing what yours looks like if you have any screenies to send or post.

Happy (particling?) lol
Scorp
Well, you seem so interested so I'll show you, here

http://img177.imageshack.us/my.php?image=76831078wg3.png

That looks interesting doesn't it? I am still particling to get some other good effects, this was with zero gravity values what do you think it looks like? to me it looks like the close up of a star :)
Hey that looks good, I like it! That would work great for effects in space without the gravity, or a ton of uses with gravity in a ground-based scene.

Nice work on that :)
Scorp
Thanks
I had to show you this, it looks like real fire

http://img520.imageshack.us/img520/566/25232591ex2.png
Oh yeah, that's awesome. Nice fire effect, or would be a great comet effect in a space sim, too. Maybe if the OP in the other thread for the scaled down solar system creates that, they could use something like that to give it a few frills, perhaps? Frills are always nice lol.

Keep up the good work!
Scorp

This topic is closed to new replies.

Advertisement