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

If the spacebar was pressed or rainbow mode is on, and delay is greater than 25, we'll let the computer know that space has been pressed by making sp equal true. Then we'll set the delay back to 0 so that it can start counting back up to 25. Finally we'll increase the variable col so that the color will change to the next color in the color table.

     sp=true; // Set Flag Telling Us Space Is Pressed

     delay=0; // Reset The Rainbow Color Cycling Delay

     col++; // Change The Particle Color

If the color is greater than 11, we reset it back to zero. If we didn't reset col to zero, our program would try to find a 13th color. We only have 12 colors! Trying to get information about a color that doesn't exist would crash our program.

     if (col>11) col=0; // If Color Is To High Reset It

    }

Lastly if the spacebar is no longer being pressed, we let the computer know by setting the variable sp to false.

    if (!keys[' ']) sp=false; // If Spacebar Is Released Clear Flag

Now for some control over the particles. Remember that we created 2 variables at the beginning of our program? One was called xspeed and one was called yspeed. Also remember that after the particle burned out, we gave it a new moving speed and added the new speed to either xspeed or yspeed. By doing that we can influence what direction the particles will move when they're first created.

For example. Say our particle had a moving speed of 5 on the x axis and 0 on the y axis. If we decreased xspeed until it was –10, we would be moving at a speed of –10 (xspeed) + 5 (original moving speed). So instead of moving at a rate of 10 to the right we'd be moving at a rate of –5 to the left. Make sense?

Anyways. The line below checks to see if the up arrow is being pressed. If it is, yspeed will be increased. This will cause our particles to move upwards. The particles will move at a maximum speed of 200 upwards. Anything faster than that doesn't look to good.

    // If Up Arrow And Y Speed Is Less Than 200 Increase Upward Speed

    if (keys[VK_UP] && (yspeed<200)) yspeed+=1.0f;

This line checks to see if the down arrow is being pressed. If it is, yspeed will be decreased. This will cause the particles to move downward. Again, a maximum downward speed of 200 is enforced.

    // If Down Arrow And Y Speed Is Greater Than –200 Increase Downward Speed

    if (keys[VK_DOWN] && (yspeed>-200)) yspeed-=1.0f;

Now we check to see if the right arrow is being pressed. If it is, xspeed will be increased. This will cause the particles to move to the right. A maximum speed of 200 is enforced.

    // If Right Arrow And X Speed Is Less Than 200 Increase Speed To The Right

    if (keys[VK_RIGHT] && (xspeed<200)) xspeed+=1.0f;

Finally we check to see if the left arrow is being pressed. If it is… you guessed it… xspeed is decreased, and the particles start to move left. Maximum speed of 200 enforced.

    // If Left Arrow And X Speed Is Greater Than –200 Increase Speed To The Left

    if (keys[VK_LEFT] && (xspeed>-200)) xspeed-=1.0f;

The last thing we need to do is increase the variable delay. Like I said above, delay is used to control how fast the colors change when you're using rainbow mode.

    delay++; // Increase Rainbow Mode Color Cycling Delay Counter

Like all the previous tutorials, make sure the title at the top of the window is correct.

    if (keys[VK_F1]) // Is F1 Being Pressed?

    {

     keys[VK_F1]=FALSE; // If So Make Key FALSE

     KillGLWindow(); // Kill Our Current Window

     fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode

     // Recreate Our OpenGL Window

     if (!CreateGLWindow("NeHe's Particle Tutorial", 640, 480, 16, fullscreen)) {

      return 0; // Quit If Window Was Not Created

     }

    }

   }

  }

 }

 // Shutdown

 KillGLWindow(); // Kill The Window

 return (msg.wParam); // Exit The Program

}

In this lesson, I have tried to explain in as much detail, all the steps required to create a simple but impressive particle system. This particle system can be used in games of your own to create effects such as Fire, Water, Snow, Explosions, Falling Stars, and more. The code can easily be modified to handle more parameters, and new effects (fireworks for example).

Thanks to Richard Nutman for suggesting that the particles be positioned with glVertex3f() instead of resetting the Modelview Matrix and repositioning each particle with glTranslatef(). Both methods are effective, but his method will reduce the amount of work the computer has to do before it draws each particle, causing the program to run even faster.

Thanks to Antoine Valentim for suggesting triangle strips to help speed up the program and to introduce a new command to this tutorial. The feedback on this tutorial has been great, I appreciate it!

I hope you enjoyed this tutorial. If you had any problems understanding it, or you've found a mistake in the tutorial please let me know. I want to make the best tutorials available. Your feedback is important!

Jeff Molofee (NeHe)

* DOWNLOAD Visual C++ Code For This Lesson.

* DOWNLOAD Borland C++ Code For This Lesson. (Conversion by Patrick Salmons)

* DOWNLOAD Cygwin Code For This Lesson. (Conversion by Stephan Ferraro)

* DOWNLOAD Delphi Code For This Lesson. (Conversion by Marc Aarts)

* DOWNLOAD Game GLUT Code For This Lesson. (Conversion by Milikas Anastasios)

* DOWNLOAD Irix Code For This Lesson. (Conversion by Dimitrios Christopoulos)

* DOWNLOAD Java Code For This Lesson. (Conversion by Jeff Kirby)

* DOWNLOAD Jedi-SDL Code For This Lesson. (Conversion by Dominique Louis)

* DOWNLOAD Linux Code For This Lesson. (Conversion by Ken Rockot)

* DOWNLOAD Linux/GLX Code For This Lesson. (Conversion by Mihael Vrbanec)

* DOWNLOAD Linux/SDL Code For This Lesson. (Conversion by Ti Leggett)

* DOWNLOAD Mac OS Code For This Lesson. (Conversion by Owen Borstad)

* DOWNLOAD Mac OS X/Cocoa Code For This Lesson. (Conversion by Bryan Blackburn)