Sign in to follow this  

Particle engine - Need help

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i].toggle=true;
        part[i].age=1.0f;
        part[i].fade=(rand()%100)/1000.0f+0.003f;
        part[i].col=pcol;
        part[i].size.x=0.01f;
        part[i].size.y=0.01f;
        part[i].size.z=0.01f;
        part[i].inc.x=(rand()%50)-25.0f;
        part[i].inc.y=(rand()%50)-25.0f;
        part[i].inc.z=(rand()%50)-25.0f;
        part[i].grav.x=0.0f;
        part[i].grav.y=-0.8f;
        part[i].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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this