In the first tutorial I taught you how to create an OpenGL window. In this tutorial I will teach you how to create both Polygons and Quads. We will create a triangle using GL_POLYGON, and a square using GL_QUADS.

Using the code from the first tutorial, we will be adding to the DrawGLScene procedure. I will rewrite the entire procedure below, so if you plan to modify the last lesson, you can replace the DrawGLScene procedure with the code below, or just add code to the DrawGLScene procedure that is not already in the last tutorial.

GLvoid DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
	glLoadIdentity();						// Reset The View

When you do a glLoadIdentity() what you are doing is moving back to the center of the screen with the X axis running left to right, the Y axis moving up and down, and the Z axis moving into, and out of the screen. The center of an OpenGL screen is 0.0 on the X, Y, and Z axis. To the left of center would be a negative number. To the right would be a positive number. Moving towards the top of the screen would be a positive number, moving to the bottom of the screen would be a negative number. Moving deeper into the screen is a negative number, moving towards the viewer would be a positive number.

glTranslatef moves along the X, Y and Z axis, in that order. The line of code below moves left on the X axis 1.5 units. It does not move on the Y axis at all (0.0), and it moves into the screen 6.0 units. When you translate, you are not moving a set amount from the center of the screen, you are moving a set amount from wherever you currently were on the screen.

	glTranslatef(-1.5f,0.0f,-6.0f);			// Move Left 1.5 Units And Into The Screen 6.0

Now that we have moved to the left half of the screen, and we've set the view deep enough into the screen (6.0) that we can see our entire scene we will create the Triangle. glBegin(GL_POLYGON) means we want to start drawing a many sided polygon, and glEnd() tells OpenGL we are done creating the polygon. We can put as many points as we want between glBegin and glEnd, but there has to be at least 3 points in order to create a filled polygon.

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 right one unit on the X axis and down one unit on the Y axis. This gives us the bottom right point of the triangle. The third glVertex moves left one unit, and down one unit. This gives us the bottom left point of the triangle. glEnd will tell OpenGL there are no more points. The filled triangle will be displayed.

	glBegin(GL_POLYGON);
		glVertex3f( 0.0f, 1.0f, 0.0f);		// Top
		glVertex3f( 1.0f,-1.0f, 0.0f);		// Bottom Right
		glVertex3f(-1.0f,-1.0f, 0.0f);		// Bottom Left
	glEnd();

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, just to get to the center we'd have to move right 1.5 units. So to get 1.5 units to the right of center, we'd have to move 3.0 units in total.

	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 that used to create a triangle. The only difference is the use of GL_QUADS, and an extra glVertex3f for the 4th point of the square. We draw the square top left, top right, bottom right, bottom left.

	glBegin(GL_QUADS);
		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();
}

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 Delphi Code For This Lesson. ( Conversion by Brad Choate )
* DOWNLOAD Linux Code For This Lesson. ( Conversion by Richard Campbell )
* DOWNLOAD BeOS Code For This Lesson. ( Conversion by Chris Herborth )