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

The first line after glBegin, sets the first point of our polygon. The first number of glVertex is for the X axis, the second number is for the Y axis, and the third number is for the Z axis. So in the first line, we don't move on the X axis. We move up one unit on the Y axis, and we don't move on the Z axis. This gives us the top point of the triangle. The second glVertex moves left one unit on the X axis and down one unit on the Y axis. This gives us the bottom left point of the triangle. The third glVertex moves right one unit, and down one unit. This gives us the bottom right point of the triangle. glEnd() tells OpenGL there are no more points. The filled triangle will be displayed.

 glBegin(GL_TRIANGLES); // Drawing Using Triangles

  glVertex3f( 0.0f, 1.0f, 0.0f); // Top

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

  glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

 glEnd(); // Finished Drawing The Triangle

Now that we have the triangle displayed on the left half of the screen, we need to move to the right half of the screen to display the square. In order to do this we use glTranslate again. This time we must move to the right, so X must be a positive value. Because we've already moved left 1.5 units, to get to the center we have to move right 1.5 units. After we reach the center we have to move another 1.5 units to the right of center. So in total we need to move 3.0 units to the right.

 glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units

Now we create the square. We'll do this using GL_QUADS. A quad is basically a 4 sided polygon. Perfect for making a square. The code for creating a square is very similar to the code we used to create a triangle. The only difference is the use of GL_QUADS instead of GL_TRIANGLES, and an extra glVertex3f for the 4th point of the square. We'll draw the square top left, top right, bottom right, bottom left (clockwise). By drawing in a clockwise order, the square will be drawn as a back face. Meaning the side of the quad we see is actually the back. Objects drawn in a counter clockwise order will be facing us. Not important at the moment, but later on you will need to know this.

 glBegin(GL_QUADS); // Draw A Quad

  glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left

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

  glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

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

 glEnd(); // Done Drawing The Quad

 return TRUE; // Keep Going

}

Finally change the code to toggle window / fullscreen mode so that the title at the top of the window is proper.

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 ( Modified )

 if (!CreateGLWindow("NeHe's First Polygon Tutorial",640,480,16,fullscreen))

 {

  return 0; // Quit If Window Was Not Created

 }

}

Markus Knauer Adds: In the book ("OpenGL Programming Guide: The Official Guide to Learning OpenGL, Release 1", J. Neider, T. Davis, M. Woo, Addison-Wesley, 1993) the following paragraph will clearly explain what NeHe means when he refers to movement by units in OpenGL:

[I mentioned] inches and millimeters — do these really have anything to do with OpenGL? The answer is, in a word, no. The projection and other transformations are inheritly unitless. If you want to think of the near and far clipping planes as located at 1.0 and 20.0 meters, inches, kilometers, or leagues, it's up to you. The only rule is that you have to use consistent unit of measurement.

In this tutorial I have tried to explain in as much detail, every step involved in drawing polygons, and quads on the screen using OpenGL. If you have comments or questions please email me. If you feel I have incorrectly commented something or that the code could be done better in some sections, please let me know. I want to make the best OpenGL tutorials I can. I'm interested in hearing your feedback.

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 David Dolinar)

* DOWNLOAD Cygwin Code For This Lesson. (Conversion by Stephan Ferraro)

* DOWNLOAD Delphi Code For This Lesson. (Conversion by Peter De Jaegher)

* DOWNLOAD Game GLUT Code For This Lesson. (Conversion by Milikas Anastasios)

* DOWNLOAD GLUT Code For This Lesson. (Conversion by Andy Restad)

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