Sunteți pe pagina 1din 8

An overview of how to use the glVertex commands to draw triangle strips, quads or triangle fans.

Vertexes, Triangles and the glVertex Command Every object in a 3D world is made up of many triangles all stitched together. To draw these triangles you need to specify a list of vertexes (points) to draw. OpenGl provides an easy way of drawing vertexes to the screen in the form of the glBegin, glVertex, glEnd commands. These commands are not very efficient for large sets of vertexes (then you'd rather want to use a display list or vertex buffer object) but are perfect for drawing small objects and graphical user interfaces. To tell OpenGL you're ready to send a list of vertexes to draw, you start with the glBegin(mode) command. Now OpenGL has numerous ways of interpreting the list of vertexes you give it, so it will draw different things depending on what mode you set when you call glBegin. There are about 7 common modes to choose from:

GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS

Firstly we need to explain the glVertex command:

The glVertex Command The glVertex command specifies a bunch of coordinates in whatever dimensions we are currently using. Furthermore, the coordinates can be specified in different data types. So there are a lot of different versions of glVertex which allow you to specify a certain number of coordinates and the datatype of the coordinates. For example, we normally use either 2 or 3 dimensions and either integers (whole numbers) or floating point numbers (decimals):
glVertex3f(1.2, 4.5, 5.3); //Indicates a 3D vertex with floating point coordinates glVertex2i(1, 5); //Indicates a 2D vertex with integer coordinates glVertex3d(1.2, 4.5, 5.3); //Indicates a 3D vertex with double-precision floating point coordinates //These are twice as accurate as normal floating point coordinates.

From now on, I'll just use glVertex to indicate one of the above. Now we'll examine the different modes for drawing stuff in OpenGL:

GL_POINTS This is the simplest form of drawing and does just what you'd expect: it draws each vertex you give it as a single dot on the screen. You can set the size of this dot using the glPointSize() command. For example, this code:
glBegin(GL_POINTS); glVertex glVertex glVertex glVertex glVertex //Draw 5 vertexes glEnd(); //NB! Dont forget the glEnd, it could crash your program if you do!

Will produce the following output:

GL_LINES The GL_LINES mode will draw lines inbetween your vertexes, but it doesnt join them up. It takes vertexes in pairs and draws a single line between those two. Again, the size of the line can be changed using glLineWidth() For example, this code:

glBegin(GL_LINES); glVertex glVertex glVertex glVertex glVertex glEnd(); //5 vertexes again

Will produce the following output:

Notice how the 5th vertex is ignored because it doesnt have another vertex to link up with.

GL_LINE_STRIP and GL_LINE_LOOP These two modes will draw lines from the first vertex, through every following vertex until the end vertex. The vital difference here between GL_LINE_STRIP and GL_LINE_LOOP is that the GL_LINE_LOOP mode will also draw a line connecting your first and last vertexes, completeing a line loop. We'll look at the GL_LINE_LOOP command in this example:
glBegin(GL_LINE_LOOP); glVertex glVertex glVertex glVertex glVertex glEnd(); //5 vertexes again

Will produce the following output:

GL_TRIANGLES Ok, onto the good stuff: Triangles! Every three vertexes specified here will turn into one triangle. If there is one or two vertexes left over at the end, they are discarded. For example, this code:
glBegin(GL_TRIANGLES); glVertex glVertex glVertex glVertex glVertex glVertex glEnd(); //6 vertexes this time!

Will produce the following output:

The vertexes are shown here for clarity. In OpenGL they are not drawn.

GL_QUADS A quad or quadrilateral is just a four sided shape. Every 4 vertexes specified here will turn into one quad. If there is one, two or three vertexes left over at the end, they are discarded. Also it now becomes important to note the order in which you specify the vertexes. The OpenGL default is Counter Clockwise Winding meaning that you first specify the top left corner, then the bottom left, then bottom right, then finally top right (working counter clockwise around from the top left corner). You can change this setting using the glFrontFace() command, but it's not viewed as a good programming practice to do so. Take special note of the order in the following example:
glBegin(GL_QUADS); glVertex glVertex glVertex glVertex glVertex glEnd();

Will produce the following output:

Vertex 5 is discarded and is not drawn.

GL_TRIANGLE_STRIP This is the most common drawing mode used in OpenGL. It draws 1 triangle using the first three vertexes, then for each vertex after that, another triangle is drawn using the last two vertexes and the one new vertex. This time, you order your vertexes almost like a sewing machine would sew up a seam: 1, 2 in a straight verticle line, then move across, 3, 4, move across, 5, 6, move across etc. Observe:
glBegin(GL_TRIANGLE_STRIP); glVertex glVertex glVertex glVertex glVertex glEnd();

Will produce the following output:

GL_TRIANGLE_FAN And finally: the triangle fan mode which is usually useful in special cases. It begins by drawing a triangle using the first three vertexes as normal, but then each vertex after that creates a new triangle using the very first vertex, the previous vertex and this vertex. This eventually makes a fan shaped polygon. Observe:
glBegin(GL_TRIANGLE_FAN); glVertex glVertex glVertex glVertex glVertex glEnd();

Will produce the following output:

And thats the end of the tutorial!

S-ar putea să vă placă și