Alex
Alex

Reputation: 561

OpenGL particles, help controlling direction

I am trying to modify this Digiben sample in order to get the effect of particles that generate from a spot (impact point) and float upwards kind of like the sparks of a fire. The sample has the particles rotating in a circle... I have tried removing the cosine/sine functions and replace them with a normal glTranslate with increasing Y value but I just can't get any real results... could anyone please point out roughly where I should add/modify the translation in this code to obtain that result?

void ParticleMgr::init(){
    tex.Load("part.bmp");
    GLfloat angle = 0; // A particle's angle
    GLfloat speed = 0; // A particle's speed
    // Create all the particles
    for(int i = 0; i < P_MAX; i++)
    {
        speed = float(rand()%50 + 450); // Make a random speed
        // Init the particle with a random speed
        InitParticle(particle[i],speed,angle);
        angle += 360 / (float)P_MAX; // Increment the angle so when all the particles are
                                     // initialized they will be equally positioned in a
                                     // circular fashion
    }
}
void ParticleMgr::InitParticle(PARTICLE &particle, GLfloat sss, GLfloat aaa)
{
    particle.speed = sss; // Set the particle's speed
    particle.angle = aaa; // Set the particle's current angle of rotation
    // Randomly set the particles color
    particle.red = rand()%255;
    particle.green = rand()%255;
    particle.blue = rand()%255;
}
void ParticleMgr::DrawParticle(const PARTICLE &particle)
{
    tex.Use();
    // Calculate the current x any y positions of the particle based on the particle's
    // current angle -- This will make the particles move in a "circular pattern"
    GLfloat xPos = sinf(particle.angle); 
    GLfloat yPos = cosf(particle.angle);
    // Translate to the x and y position and the #defined PDEPTH (particle depth)
    glTranslatef(xPos,yPos,PDEPTH);
    // Draw the first quad
    glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex3f(-5, 5, 0);
        glTexCoord2f(1,0);
        glVertex3f(5, 5, 0);
        glTexCoord2f(1,1);
        glVertex3f(5, -5, 0);
        glTexCoord2f(0,1);
        glVertex3f(-5, -5, 0);
    glEnd(); // Done drawing quad
    // Draw the SECOND part of our particle
    tex.Use();
    glRotatef(particle.angle,0,0,1); // Rotate around the z-axis (depth axis)
    //glTranslatef(0, particle.angle, 0);
    // Draw the second quad
    glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex3f(-4, 4, 0);
        glTexCoord2f(1,0);
        glVertex3f(4, 4, 0);
        glTexCoord2f(1,1);
        glVertex3f(4, -4, 0);
        glTexCoord2f(0,1);
        glVertex3f(-4, -4, 0);
    glEnd(); // Done drawing quad
    // Translate back to where we began
    glTranslatef(-xPos,-yPos,-PDEPTH);
}
void ParticleMgr::run(){
    for(int i = 0; i < P_MAX; i++)
    {
        DrawParticle(particle[i]);
        // Increment the particle's angle
        particle[i].angle += ANGLE_INC;
    }
}

For now I am adding a glPushMatrix(), glTranslate(x, y, z) in the run() function above, right before the loop, with x,y,z as the position of the enemy for placing them on top of the enemy....is that the best place for that?

Thanks for any input!

Upvotes: 1

Views: 1103

Answers (1)

datenwolf
datenwolf

Reputation: 162367

Using glTranslate and glRotate that way will in fact decrease your program's performance. OpenGL is not a scene graph, so the matrix manipulation functions directly influence the drawing process, i.e. they don't set "object state". The issue you're running into is, that a 4×4 matrix-matrix multiplication involves 64 multiplications and 16 additions. So you're spending 96 times the computing power for moving a particle, than simply update the vertex position directly.

Now to your problem: Like I already told you, glTranslate operates on (a global) matrix state of one of 4 selectable matrices. And the effects accumulate, i.e. each glTranslate will start from the matrix the previous glTranslate left. OpenGL provides a matrix stack, where one can push a copy of the current matrix to work with, then pop to revert to the state before.

However: Matrix manipulation has been removed from OpenGL-3 core and later entirely. OpenGL matrix manipulation never was accelerated (except on one particular graphics workstation made by SGI around 1996). Today it is a anachronism, as every respectable program working with 3D geometry used much more sophisticated matrix manipulation by either own implementation or 3rd party library. OpenGL's matrix stack was just redundant. So I strongly suggest you forget about OpenGL's matrix manipulation functionality and roll your own.

Upvotes: 1

Related Questions