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

  star[loop].angle+=float(loop)/num; // Changes The Angle Of A Star

  star[loop].dist-=0.01f; // Changes The Distance Of A Star

The lines below check to see if the stars have hit the center of the screen or not. When a star hits the center of the screen it's given a new color, and is moved 5 units from the center, so it can start it's journey back to the center as a new star.

  if (star[loop].dist<0.0f) // Is The Star In The Middle Yet

  {

   star[loop].dist+=5.0f; // Move The Star 5 Units From The Center

   star[loop].r=rand()%256; // Give It A New Red Value

   star[loop].g=rand()%256; // Give It A New Green Value

   star[loop].b=rand()%256; // Give It A New Blue Value

  }

 }

 return TRUE; // Everything Went OK

}

Now we're going to add code to check if any keys are being pressed. Go down to WinMain(). Look for the line SwapBuffers(hDC). We'll add our key checking code right under that line. lines of code.

The lines below check to see if the T key has been pressed. If it has been pressed and it's not being held down the following will happen. If twinkle is FALSE, it will become TRUE. If it was TRUE, it will become FALSE. Once T is pressed tp will become TRUE. This prevents the code from running over and over again if you hold down the T key.

  SwapBuffers(hDC); // Swap Buffers (Double Buffering)

  if (keys['T'] && !tp) // Is T Being Pressed And Is tp FALSE

  {

   tp=TRUE; // If So, Make tp TRUE

   twinkle=!twinkle; // Make twinkle Equal The Opposite Of What It Is

  }

The code below checks to see if you've let go of the T key. If you have, it makes tp=FALSE. Pressing the T key will do nothing unless tp is FALSE, so this section of code is very important.

  if (!keys['T']) // Has The T Key Been Released

  {

   tp=FALSE; // If So, make tp FALSE

  }

The rest of the code checks to see if the up arrow, down arrow, page up or page down keys are being pressed.

  if (keys[VK_UP]) // Is Up Arrow Being Pressed

  {

   tilt-=0.5f; // Tilt The Screen Up

  }

  if (keys[VK_DOWN]) // Is Down Arrow Being Pressed

  {

   tilt+=0.5f; // Tilt The Screen Down

  }

  if (keys[VK_PRIOR]) // Is Page Up Being Pressed

  {

   zoom-=0.2f; // Zoom Out

  }

  if (keys[VK_NEXT]) // Is Page Down Being Pressed

  {

   zoom+=0.2f; // Zoom In

  }

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 Textures, Lighting & Keyboard Tutorial", 640, 480, 16, fullscreen)) {

    return 0; // Quit If Window Was Not Created

   }

  }

 }

}

In this tutorial I have tried to explain in as much detail how to load in a gray scale bitmap image, remove the black space around the image (using blending), add color to the image, and move the image around the screen in 3D. I've also shown you how to create beautiful colors and animation by overlapping a second copy of the bitmap on top of the original bitmap. Once you have a good understanding of everything I've taught you up till now, you should have no problems making 3D demos ofyour own. All the basics have been covered!

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 Lakmal Gunasekara)

* 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 Richard Campbell)

* 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 Anthony Parker)

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

* DOWNLOAD MASM Code For This Lesson. (Conversion by Nico (Scalp))

* DOWNLOAD Visual C++ / OpenIL Code For This Lesson. (Conversion by Denton Woods)

* DOWNLOAD Power Basic Code For This Lesson. (Conversion by Angus Law)

* DOWNLOAD Solaris Code For This Lesson. (Conversion by Lakmal Gunasekara)

* DOWNLOAD Visual Basic Code For This Lesson. (Conversion by Peter De Tagyos)

* DOWNLOAD Visual Fortran Code For This Lesson. (Conversion by Jean-Philippe Perois