Выбрать главу

There are two good reasons to use triangle strips. First, after specifying the first three vertices for the initial triangle, you only need to specify a single point for each additional triangle. That point will be combined with 2 previous vertices to create a triangle. Secondly, by cutting back the amount of data needed to create a triangle your program will run quicker, and the amount of code or data required to draw an object is greatly reduced.

Note: The number of triangles you see on the screen will be the number of vertices you specify minus 2. In the code below we have 4 vertices and we see two triangles.

    glTexCoord2d(1,1); glVertex3f(x+0.5f, y+0.5f, z); // Top Right

    glTexCoord2d(0,1); glVertex3f(x-0.5f, y+0.5f, z); // Top Left

    glTexCoord2d(1,0); glVertex3f(x+0.5f, y-0.5f, z); // Bottom Right

    glTexCoord2d(0,0); glVertex3f(x-0.5f, y-0.5f, z); // Bottom Left

Finally we tell OpenGL that we are done drawing our triangle strip.

   glEnd(); // Done Building Triangle Strip

Now we can move the particle. The math below may look strange, but once again, it's pretty simple. First we take the current particle x position. Then we add the x movement value to the particle divided by slowdown times 1000. So if our particle was in the center of the screen on the x axis (0), our movement variable (xi) for the x axis was +10 (moving us to the right) and slowdown was equal to 1, we would be moving to the right by 10/(1*1000), or 0.01f. If we increase the slowdown to 2 we'll only be moving at 0.005f. Hopefully that helps you understand how slowdown works.

That's also why multiplying the start values by 10.0f made the pixels move alot faster, creating an explosion.

We use the same formula for the y and z axis to move the particle around on the screen.

   particle[loop].x+=particle[loop].xi/(slowdown*1000); // Move On The X Axis By X Speed

   particle[loop].y+=particle[loop].yi/(slowdown*1000); // Move On The Y Axis By Y Speed

   particle[loop].z+=particle[loop].zi/(slowdown*1000); // Move On The Z Axis By Z Speed

After we've calculated where to move the particle to next, we have to apply gravity or resistance. In the first line below, we do this by adding our resistance (xg) to the speed we are moving at (xi).

Lets say our moving speed was 10 and our resistance was 1. Each time our particle was drawn resistance would act on it. So the second time it was drawn, resistance would act, and our moving speed would drop from 10 to 9. This causes the particle to slow down a bit. The third time the particle is drawn, resistance would act again, and our moving speed would drop to 8. If the particle burns for more than 10 redraws, it will eventually end up moving the opposite direction because the moving speed would become a negative value.

The resistance is applied to the y and z moving speed the same way it's applied to the x moving speed.

   particle[loop].xi+=particle[loop].xg; // Take Pull On X Axis Into Account

   particle[loop].yi+=particle[loop].yg; // Take Pull On Y Axis Into Account

   particle[loop].zi+=particle[loop].zg; // Take Pull On Z Axis Into Account

The next line takes some life away from the particle. If we didn't do this, the particle would never burn out. We take the current life of the particle and subtract the fade value for that particle. Each particle will have a different fade value, so they'll all burn out at different speeds.

   particle[loop].life -= particle[loop].fade; // Reduce Particles Life By 'Fade'

Now we check to see if the particle is still alive after having life taken from it.

   if (particle[loop].life < 0.0f) // If Particle Is Burned Out

   {

If the particle is dead (burnt out), we'll rejuvenate it. We do this by giving it full life and a new fade speed.

    particle[loop].life=1.0f; // Give It New Life

    particle[loop].fade=float(rand()%100)/1000.0f+0.003f; // Random Fade Value

We also reset the particles position to the center of the screen. We do this by resetting the x, y and z positions of the particle to zero.

    particle[loop].x=0.0f; // Center On X Axis

    particle[loop].y=0.0f; // Center On Y Axis

    particle[loop].z=0.0f; // Center On Z Axis

After the particle has been reset to the center of the screen, we give it a new moving speed / direction. Notice I've increased the maximum and minimum speed that the particle can move at from a random value of 50 to a value of 60, but this time we're not going to multiply the moving speed by 10. We don't want an explosion this time around, we want slower moving particles.

Also notice that I add xspeed to the x axis moving speed, and yspeed to the y axis moving speed. This gives us control over what direction the particles move later in the program.

    particle[loop].xi = xspeed+float((rand()%60)-32.0f); // X Axis Speed And Direction

    particle[loop].yi = yspeed+float((rand()%60)-30.0f); // Y Axis Speed And Direction

    particle[loop].zi = float((rand()%60)-30.0f); // Z Axis Speed And Direction

Lastly we assign the particle a new color. The variable col holds a number from 0 to 11 (12 colors). We use this variable to look of the red, green and blue intensities in our color table that we made at the beginning of the program. The first line below sets the red (r) intensity to the red value stored in colors[col][0]. So if col was 0, the red intensity would be 1.0f. The green and blue values are read the same way.

If you don't understand how I got the value of 1.0f for the red intensity if col is 0, I'll explain in a bit more detail. Look at the very top of the program. Find the line: static GLfloat colors[12][3]. Notice there are 12 groups of 3 number. The first of the three number is the red intensity. The second value is the green intensity and the third value is the blue intensity. [0], [1] and [2] below represent the 1st, 2nd and 3rd values I just mentioned. If col is equal to 0, we want to look at the first group. 11 is the last group (12th color).

    particle[loop].r=colors[col][0]; // Select Red From Color Table

    particle[loop].g=colors[col][1]; // Select Green From Color Table

    particle[loop].b=colors[col][2]; // Select Blue From Color Table

   }

The line below controls how much gravity there is pulling upward. By pressing 8 on the number pad, we increase the yg (y gravity) variable. This causes a pull upwards. This code is located here in the program because it makes our life easier by applying the gravity to all of our particles thanks to the loop. If this code was outside the loop we'd have to create another loop to do the same job, so we might as well do it here.

   // If Number Pad 8 And Y Gravity Is Less Than 1.5 Increase Pull Upwards

   if (keys[VK_NUMPAD8] && (particle[loop].yg<1.5f)) particle[loop].yg+=0.01f;

This line has the exact opposite affect. By pressing 2 on the number pad we decrease yg creating a stronger pull downwards.

   // If Number Pad 2 And Y Gravity Is Greater Than –1.5 Increase Pull Downwards

   if (keys[VK_NUMPAD2] && (particle[loop].yg>-1.5f)) particle[loop].yg-=0.01f;

Now we modify the pull to the right. If the 6 key on the number pad is pressed, we increase the pull to the right.

   // If Number Pad 6 And X Gravity Is Less Than 1.5 Increase Pull Right

   if (keys[VK_NUMPAD6] && (particle[loop].xg<1.5f)) particle[loop].xg+=0.01f;