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

* 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 Power Basic Code For This Lesson. (Conversion by Angus Law)

* DOWNLOAD Python Code For This Lesson. (Conversion by John)

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

* DOWNLOAD Visual Basic Code For This Lesson. (Conversion by Ross Dawson)

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

Lesson 05

Expanding on the last tutorial, we'll now make the object into TRUE 3D object, rather than 2D objects in a 3D world. We will do this by adding a left, back, and right side to the triangle, and a left, right, back, top and bottom to the square. By doing this, we turn the triangle into a pyramid, and the square into a cube.

We'll blend the colors on the pyramid, creating a smoothly colored object, and for the square we'll color each face a different color.

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(-1.5f,0.0f,-6.0f); // Move Left And Into The Screen

 glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Pyramid On It's Y Axis

 glBegin(GL_TRIANGLES); // Start Drawing The Pyramid 

A few of you have taken the code from the last tutorial, and made 3D objects of your own. One thing I've been asked quite a bit is "how come my objects are not spinning on their axis? It seems like they are spinning all over the screen". In order for your object to spin around an axis, it has to be designed AROUND that axis. You have to remember that the center of any object should be 0 on the X, 0 on the Y, and 0 on the Z.

The following code will create the pyramid around a central axis. The top of the pyramid is one high from the center, the bottom of the pyramid is one down from the center. The top point is right in the middle (zero), and the bottom points are one left from center, and one right from center.

Note that all triangles are drawn in a counterclockwise rotation. This is important, and will be explained in a future tutorial, for now, just know that it's good practice to make objects either clockwise or counterclockwise, but you shouldn't mix the two unless you have a reason to.

We start off by drawing the Front Face. Because all of the faces share the top point, we will make this point red on all of the triangles. The color on the bottom two points of the triangles will alternate. The front face will have a green left point and a blue right point. Then the triangle on the right side will have a blue left point and a green right point. By alternating the bottom two colors on each face, we make a common colored point at the bottom of each face.

  glColor3f(1.0f,0.0f,0.0f); // Red

  glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)

  glColor3f(0.0f,1.0f,0.0f); // Green

  glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)

  glColor3f(0.0f,0.0f,1.0f); // Blue

  glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)

Now we draw the right face. Notice then the two bottom point are drawn one to the right of center, and the top point is drawn one up on the y axis, and right in the middle of the x axis. causing the face to slope from center point at the top out to the right side of the screen at the bottom.

Notice the left point is drawn blue this time. By drawing it blue, it will be the same color as the right bottom corner of the front face. Blending blue outwards from that one corner across both the front and right face of the pyramid.

Notice how the remaining three faces are included inside the same glBegin(GL_TRIANGLES) and glEnd() as the first face. Because we're making this entire object out of triangles, OpenGL will know that every three points we plot are the three points of a triangle. Once it's drawn three points, if there are three more points, it will assume another triangle needs to be drawn. If you were to put four points instead of three, OpenGL would draw the first three and assume the fourth point is the start of a new triangle. It would not draw a Quad. So make sure you don't add any extra points by accident.

  glColor3f(1.0f,0.0f,0.0f); // Red

  glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)

  glColor3f(0.0f,0.0f,1.0f); // Blue

  glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)

  glColor3f(0.0f,1.0f,0.0f); // Green

  glVertex3f( 1.0f,-1.0f, –1.0f); // Right Of Triangle (Right)

Now for the back face. Again the colors switch. The left point is now green again, because the corner it shares with the right face is green.

  glColor3f(1.0f,0.0f,0.0f); // Red

  glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)

  glColor3f(0.0f,1.0f,0.0f); // Green

  glVertex3f( 1.0f,-1.0f, –1.0f); // Left Of Triangle (Back)

  glColor3f(0.0f,0.0f,1.0f); // Blue

  glVertex3f(-1.0f,-1.0f, –1.0f); // Right Of Triangle (Back)

Finally we draw the left face. The colors switch one last time. The left point is blue, and blends with the right point of the back face. The right point is green, and blends with the left point of the front face.

We're done drawing the pyramid. Because the pyramid only spins on the Y axis, we will never see the bottom, so there is no need to put a bottom on the pyramid. If you feel like experimenting, try adding a bottom using a quad, then rotate on the X axis to see if you've done it correctly. Make sure the color used on each corner of the quad matches up with the colors being used at the four corners of the pyramid.

  glColor3f(1.0f,0.0f,0.0f); // Red

  glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)

  glColor3f(0.0f,0.0f,1.0f); // Blue

  glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)

  glColor3f(0.0f,1.0f,0.0f); // Green

  glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)

 glEnd(); // Done Drawing The Pyramid

Now we'll draw the cube. It's made up of six quads. All of the quads are drawn in a counter clockwise order. Meaning the first point is the top right, the second point is the top left, third point is bottom left, and finally bottom right. When we draw the back face, it may seem as though we are drawing clockwise, but you have to keep in mind that if we were behind the cube looking at the front of it, the left side of the screen is actually the right side of the quad, and the right side of the screen would actually be the left side of the quad.