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

Notice we move the cube a little further into the screen in this lesson. By doing this, the size of the cube appears closer to the size of the pyramid. If you were to move it only 6 units into the screen, the cube would appear much larger than the pyramid, and parts of it might get cut off by the sides of the screen. You can play around with this setting, and see how moving the cube further into the screen makes it appear smaller, and moving it closer makes it appear larger. The reason this happens is perspective. Objects in the distance should appear smaller :)

 glLoadIdentity();

 glTranslatef(1.5f,0.0f,-7.0f); // Move Right And Into The Screen

 glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Cube On X, Y & Z

 glBegin(GL_QUADS); // Start Drawing The Cube

We'll start off by drawing the top of the cube. We move up one unit from the center of the cube. Notice that the Y axis is always one. We then draw a quad on the Z plane. Meaning into the screen. We start off by drawing the top right point of the top of the cube. The top right point would be one unit right, and one unit into the screen. The second point would be one unit to the left, and unit into the screen. Now we have to draw the bottom of the quad towards the viewer. so to do this, instead of going into the screen, we move one unit towards the screen. Make sense?

  glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green

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

  glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)

  glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)

  glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

The bottom is drawn the exact same way as the top, but because it's the bottom, it's drawn down one unit from the center of the cube. Notice the Y axis is always minus one. If we were under the cube, looking at the quad that makes the bottom, you would notice the top right corner is the corner closest to the viewer, so instead of drawing in the distance first, we draw closest to the viewer first, then on the left side closest to the viewer, and then we go into the screen to draw the bottom two points.

If you didn't really care about the order the polygons were drawn in (clockwise or not), you could just copy the same code for the top quad, move it down on the Y axis to –1, and it would work, but ignoring the order the quad is drawn in can cause weird results once you get into fancy things such as texture mapping.

  glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange

  glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)

  glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)

  glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)

  glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)

Now we draw the front of the Quad. We move one unit towards the screen, and away from the center to draw the front face. Notice the Z axis is always one. In the pyramid the Z axis was not always one. At the top, the Z axis was zero. If you tried changing the Z axis to zero in the following code, you'd notice that the corner you changed it on would slope into the screen. That's not something we want to do right now :)

  glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red

  glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)

  glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)

  glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)

  glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)

The back face is a quad the same as the front face, but it's set deeper into the screen. Notice the Z axis is now minus one for all of the points.

  glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow

  glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)

  glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)

  glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)

  glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)

Now we only have two more quads to draw and we're done. As usual, you'll notice one axis is always the same for all the points. In this case the X axis is always minus one. That's because we're always drawing to the left of center because this is the left face.

  glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue

  glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)

  glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)

  glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)

  glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)

This is the last face to complete the cube. The X axis is always one. Drawing is counter clockwise. If you wanted to, you could leave this face out, and make a box :)

Or if you felt like experimenting, you could always try changing the color of each point on the cube to make it blend the same way the pyramid blends. You can see an example of a blended cube by downloading Evil's first GL demo from my web page. Run it and press TAB. You'll see a beautifully colored cube, with colors flowing across all the faces.

  glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet

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

  glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)

  glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)

  glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)

 glEnd(); // Done Drawing The Quad

 rtri+=0.2f; // Increase The Rotation Variable For The Triangle

 rquad-=0.15f; // Decrease The Rotation Variable For The Quad

 return TRUE; // Keep Going

}

By the end of this tutorial, you should have a better understanding of how objects are created in 3D space. You have to think of the OpenGL screen as a giant piece of graph paper, with many transparent layers behind it. Almost like a giant cube made of of points. Some of the points move left to right, some move up and down, and some move further back in the cube. If you can visualize the depth into the screen, you shouldn't have any problems designing new 3D objects.

If you're having a hard time understanding 3D space, don't get frustrated. It can be difficult to grasp right off the start. An object like the cube is a good example to learn from. If you notice, the back face is drawn exactly the same as the front face, it's just further into the screen. Play around with the code, and if you just can't grasp it, email me, and I'll try to answer your questions.

Jeff Molofee (NeHe)

* DOWNLOAD Visual C++ Code For This Lesson.

* DOWNLOAD ASM Code For This Lesson. (Conversion by Foolman)

* DOWNLOAD Borland C++ Builder 5.0 Code For This Lesson. (Conversion by Neil Flynn)

* DOWNLOAD Code Warrior 5 Code For This Lesson. (Conversion by Scott Lupton)