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

We bind the explosion texture before we draw the textured quad. Again, the quad is drawn counter-clockwise.

 glBindTexture(GL_TEXTURE_2D, textures[5].texID); // Select The Explosion Texture

 glBegin(GL_QUADS); // Begin Drawing A Quad

  glTexCoord2f(ex ,1.0f-(ey )); glVertex3f(-1.0f,-1.0f,0.0f); // Bottom Left

  glTexCoord2f(ex+0.25f,1.0f-(ey )); glVertex3f( 1.0f,-1.0f,0.0f); // Bottom Right

  glTexCoord2f(ex+0.25f,1.0f-(ey+0.25f)); glVertex3f( 1.0f, 1.0f,0.0f); // Top Right

  glTexCoord2f(ex ,1.0f-(ey+0.25f)); glVertex3f(-1.0f, 1.0f,0.0f); // Top Left

 glEnd(); // Done Drawing Quad

As I mentioned above, the value of frame should not exceed 63 otherwise the animation will start over again. So we increase the value of frame and then we check to see if the value is greater than 63. If it is, we call InitObject(num) which destroys the object and gives it new values to create an entirely new object.

 object[num].frame+=1; // Increase Current Explosion Frame

 if (object[num].frame>63) // Have We Gone Through All 16 Frames?

 {

  InitObject(num); // Init The Object (Assign New Values)

 }

}

This section of code draws all of the targets (objects) to the screen. We start off by resetting the modelview matrix. We then translate 10 units into the screen and set up a loop from 0 to the players current level.

void DrawTargets(void) // Draws The Targets (Needs To Be Seperate)

{

 glLoadIdentity(); // Reset The Modelview Matrix

 glTranslatef(0.0f,0.0f,-10.0f); // Move Into The Screen 20 Units

 for (int loop=0; loop<level; loop++) // Loop Through 9 Objects

 {

The first line of code is the secret to picking individual objects. What it does is assigns a name (number) to each object. The first object drawn will be 0. The second object will be 1, etc… If the loop was to hit 29, the last object drawn would be given the name 29. After assigning a name to the object, we push the modelview matrix onto the stack. It's important to note the calls to glLoadName() are ignored if the program is not in selection mode.

We then move to the location on the screen where we want our object to be drawn. We use object[loop].x to position on the x-axis, object[loop].y to position on the y-axis and object[loop].distance to position the object on the z-axis (depth into the screen). We have already translated 10 units into the screen, so the actual distance at which the object will be drawn is going to be object[loop].distance-10.0f.

  glLoadName(loop); // Assign Object A Name (ID)

  glPushMatrix(); // Push The Modelview Matrix

  glTranslatef(object[loop].x,object[loop].y,object[loop].distance); // Position The Object (x,y)

Before we draw the object, we have to check if it's been hit or not. We do this by checking to see if object[loop].hit is TRUE. if it is, we jump to Explosion(loop) which will draw the explosion animation instead of the actual object. If the object was not hit, we spin the object on it's z-axis by object[loop].spin degrees before we call Object().

Object takes 3 parameters. The first one is the width, the second one is the height and the third one is the number of the texture to use. To get the width and height, we use the array size[object[loop].texid].w and size[object[loop].texid].h. This look up the width and height from our predefined object size array at the beginning of this program. The reason we use object[loop].texid is because it represents the type of object we are drawing. A texid of 0 is always the blueface… a texid of 3 is always the coke can, etc.

After drawing an object, we pop the matrix resetting the view, so our next object is drawn at the proper location on the screen.

  if (object[loop].hit) // If Object Has Been Hit

  {

   Explosion(loop); // Draw An Explosion

  } else // Otherwise

  {

   glRotatef(object[loop].spin,0.0f,0.0f,1.0f); // Rotate The Object

   Object(size[object[loop].texid].w,size[object[loop].texid].h,object[loop].texid); // Draw The Object

  }

  glPopMatrix(); // Pop The Modelview Matrix

 }

}

This is where the drawing occurs. We start off by clearing the screen, and resetting our modelview matrix.

void Draw(void) // Draw Our Scene

{

 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

 glLoadIdentity(); // Reset The Modelview Matrix

Next we push the modelview matrix onto the stack and select the sky texture (texture 7). The sky is made up of 4 textured quads. The first 4 vertices draw the sky way in the distance from the ground straight up. The texture on this quad will roll fairly slowly. The next 4 vertices draw the sky again at the exact same location but the sky texture will roll faster. The two textures will blend together in alpha blending mode to create a neat multilayered effect.

 glPushMatrix(); // Push The Modelview Matrix

 glBindTexture(GL_TEXTURE_2D, textures[7].texID); // Select The Sky Texture

 glBegin(GL_QUADS); // Begin Drawing Quads

  glTexCoord2f(1.0f,roll/1.5f+1.0f); glVertex3f( 28.0f,+7.0f,-50.0f); // Top Right

  glTexCoord2f(0.0f,roll/1.5f+1.0f); glVertex3f(-28.0f,+7.0f,-50.0f); // Top Left

  glTexCoord2f(0.0f,roll/1.5f+0.0f); glVertex3f(-28.0f,-3.0f,-50.0f); // Bottom Left

  glTexCoord2f(1.0f,roll/1.5f+0.0f); glVertex3f( 28.0f,-3.0f,-50.0f); // Bottom Right

  glTexCoord2f(1.5f,roll+1.0f); glVertex3f( 28.0f,+7.0f,-50.0f); // Top Right

  glTexCoord2f(0.5f,roll+1.0f); glVertex3f(-28.0f,+7.0f,-50.0f); // Top Left

  glTexCoord2f(0.5f,roll+0.0f); glVertex3f(-28.0f,-3.0f,-50.0f); // Bottom Left

  glTexCoord2f(1.5f,roll+0.0f); glVertex3f( 28.0f,-3.0f,-50.0f); // Bottom Right

To give the illusion that the sky is coming towards the viewer, we draw two more quads, but this time we draw them from way in the distance coming toward the viewer. The first 4 verticies draw slow rolling clouds and the remaining 4 draw faster moving clouds. The two layers will blend together in alpha blending mode to create a multilayered effect. The second layer of clouds is offset by 0.5f so that the two textures don't line up. Same with the two layers of clouds above. The second layer is offset by 0.5f.

The final effect of all 4 quads is a sky that appears to move up way out in the distance and then toward the viewer up high. I could have used a textured half sphere for the sky, but I was too lazy, and the effect is still pretty good as is.

  glTexCoord2f(1.0f,roll/1.5f+1.0f); glVertex3f( 28.0f,+7.0f,0.0f); // Top Right

  glTexCoord2f(0.0f,roll/1.5f+1.0f); glVertex3f(-28.0f,+7.0f,0.0f); // Top Left

  glTexCoord2f(0.0f,roll/1.5f+0.0f); glVertex3f(-28.0f,+7.0f,-50.0f); // Bottom Left

  glTexCoord2f(1.0f,roll/1.5f+0.0f); glVertex3f( 28.0f,+7.0f,-50.0f); // Bottom Right

  glTexCoord2f(1.5f,roll+1.0f); glVertex3f( 28.0f,+7.0f,0.0f); // Top Right

  glTexCoord2f(0.5f,roll+1.0f); glVertex3f(-28.0f,+7.0f,0.0f); // Top Left

  glTexCoord2f(0.5f,roll+0.0f); glVertex3f(-28.0f,+7.0f,-50.0f); // Bottom Left