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

GLUquadricObj *quadratic; // Storage For Our Quadratic Objects ( NEW )

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; // Ambient Light Values

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position

GLuint filter; // Which Filter To Use

GLuint texture[3]; // Storage for 3 textures

GLuint object=0; // Which Object To Draw ( NEW )

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

Okay now move down to InitGL(), We're going to add 3 lines of code here to initialize our quadratic. Add these 3 lines after you enable light1 but before you return true. The first line of code initializes the Quadratic and creates a pointer to where it will be held in memory. If it can't be created it returns 0. The second line of code creates smooth normals on the quadratic so lighting will look great. Other possible values are GLU_NONE, and GLU_FLAT. Last we enable texture mapping on our quadratic. Texture mapping is kind of awkward and never goes the way you planned as you can tell from the crate texture.

 quadratic=gluNewQuadric(); // Create A Pointer To The Quadric Object ( NEW )

 gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals ( NEW )

 gluQuadricTexture(quadratic, GL_TRUE); // Create Texture Coords ( NEW )

Now I decided to keep the cube in this tutorial so you can see how the textures are mapped onto the quadratic object. I decided to move the cube into its own function so when we write the draw function it will appear more clean. Everybody should recognize this code. =P

GLvoid glDrawCube() // Draw A Cube

{

 glBegin(GL_QUADS); // Start Drawing Quads

  // Front Face

  glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Facing Forward

  glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, –1.0f, 1.0f); // Bottom Left Of The Texture and Quad

  glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, –1.0f, 1.0f); // Bottom Right Of The Texture and Quad

  glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad

  glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad

  // Back Face

  glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Facing Away

  glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, –1.0f, –1.0f); // Bottom Right Of The Texture and Quad

  glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, –1.0f); // Top Right Of The Texture and Quad

  glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, –1.0f); // Top Left Of The Texture and Quad

  glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, –1.0f, –1.0f); // Bottom Left Of The Texture and Quad

  // Top Face

  glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Facing Up

  glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, –1.0f); // Top Left Of The Texture and Quad

  glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad

  glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad

  glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, –1.0f); // Top Right Of The Texture and Quad

  // Bottom Face

  glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Facing Down

  glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, –1.0f, –1.0f); // Top Right Of The Texture and Quad

  glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, –1.0f, –1.0f); // Top Left Of The Texture and Quad

  glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, –1.0f, 1.0f); // Bottom Left Of The Texture and Quad

  glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, –1.0f, 1.0f); // Bottom Right Of The Texture and Quad

  // Right face

  glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Facing Right

  glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, –1.0f, –1.0f); // Bottom Right Of The Texture and Quad

  glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, –1.0f); // Top Right Of The Texture and Quad

  glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad

  glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, –1.0f, 1.0f); // Bottom Left Of The Texture and Quad

  // Left Face

  glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Facing Left

  glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, –1.0f, –1.0f); // Bottom Left Of The Texture and Quad

  glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, –1.0f, 1.0f); // Bottom Right Of The Texture and Quad

  glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad

  glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, –1.0f); // Top Left Of The Texture and Quad

 glEnd(); // Done Drawing Quads

}

Next is the DrawGLScene function, here I just wrote a simple if statement to draw the different objects. Also I used a static variable (a local variable that keeps its value everytime it is called) for a cool effect when drawing the partial disk. I'm going to rewrite the whole DrawGLScene function for clarity.

You'll notice that when I talk about the parameters being used I ignore the actual first parameter (quadratic). This parameter is used for all the objects we draw aside from the cube, so I ignore it when I talk about the parameters.

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{

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

 glLoadIdentity(); // Reset The View

 glTranslatef(0.0f,0.0f,z); // Translate Into The Screen

 glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis

 glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis

 glBindTexture(GL_TEXTURE_2D, texture[filter]); // Select A Filtered Texture

 // This Section Of Code Is New ( NEW )

 switch(object) // Check object To Find Out What To Draw

 {

 case 0: // Drawing Object 1

  glDrawCube(); // Draw Our Cube

  break; // Done

The second object we create is going to be a Cylinder. The first parameter (1.0f) is the radius of the cylinder at base (bottom). The second parameter (1.0f) is the radius of the cylinder at the top. The third parameter ( 3.0f) is the height of the cylinder (how long it is). The fouth parameter (32) is how many subdivisions there are "around" the Z axis, and finally, the fifth parameter (32) is the amount of subdivisions "along" the Z axis. The more subdivisions there are the more detailed the object is. By increase the amount of subdivisions you add more polygons to the object. So you end up sacrificing speed for quality. Most of the time it's easy to find a happy medium.

 case 1: // Drawing Object 2

  glTranslatef(0.0f,0.0f,-1.5f); // Center The Cylinder

  gluCylinder(quadratic,1.0f,1.0f,3.0f,32,32); // Draw Our Cylinder

  break; // Done

The third object we create will be a CD shaped disc. The first parameter (0.5f) is the inner radius of the disk. This value can be zero, meaning there will be no hole in the middle. The larger the inner radius is, the bigger the hole in the middle of the disc will be. The second parameter (1.5f) is the outer radius. This value should be larger than the inner radius. If you make this value a little bit larger than the inner radius you will end up with a thing ring. If you make this value alot larger than the inner radius you will end up with a thick ring. The third parameter (32) is the number of slices that make up the disc. Think of slices like the slices in a pizza. The more slices you have, the smoother the outer edge of the disc will be. Finally the fourth parameter (32) is the number of rings that make up the disc. The rings are are similar to the tracks on a record. Circles inside circles. These ring subdivide the disc from the inner radius to the outer radius, adding more detail. Again, the more subdivisions there are, the slow it will run.