GameProgrammer.org
tutorials for dummies bug source let me google it for you my blog all about me
OpenGL Primitives

Introduction

Here we are actually going to start using OpenGL and draw some shapes. OpenGL can draw a few primitives like points, lines, triangles and polygons. Primitives are the only visible things on a scene and you should draw everything using them. We start drawing some simple shapes in this tutorial.

Primitives

In order to draw a shape using a 2D graphics API like GDI, you need to specify the screen coordinates of your shape. For example to draw a rectangle you need to specify the start point and the end point:

DrawRectangle(100, 100, 200, 200);

By using OpenGL you don't need to worry about the screen coordinates, you just need to specify the shape on a Cartesian coordinate system and setup the camera. OpenGL will handle the projection on the screen coordinates internally.

For example consider we want to draw a quad using OpenGL (look at the picture).

Open the project from the previous tutorial and change the Application::Render() function like below, then compile and run the code.

void Application::Render()
{
    glClear(GL_COLOR_BUFFER_BIT);

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

    glBegin(GL_QUADS);
    glVertex2f(-0.5,-0.5);
    glVertex2f( 0.5,-0.5);
    glVertex2f( 0.5, 0.5);
    glVertex2f(-0.5, 0.5);
    glEnd();

    window.Swap();
}

First we cleared the color buffer like the previous tutorial, then we set the current color by glColor3f() command. Notice that OpenGL is like a state machine and it keeps the old states until you change it, so after calling this function every shape you draw will be filled by the set color.

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

glColor3f() takes three float arguments indicating red, green and blue components of the color. Before continuing our main discussion let's talk a little about OpenGL command syntax. Various groups of OpenGL commands perform the same operation but they are different in arguments. Common OpenGL commands syntax is:

gl + <function name> + <a class='x3dmainlink'rguments number> + <a class='x3dmainlink'rguments type> + <vector>

For example glVertex2f() specifies that this command will take 2 float parameters. And command glColor3fv() indicates that the command takes a vector containing 3 float parameters.

After setting the color we need to specify the shape and vertices coordinates:

glBegin(GL_QUADS);
glVertex2f(-0.5,-0.5);
glVertex2f( 0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();

We can use glBegin()/glEnd() block to indicate the primitive type. You can specify vertices inside this block by glVertex() command. glBegin() takes a parameter specifying the primitive type. Here we used GL_QUADS which means that we are going to draw some four sided shapes, then we used glVertex2f() command four times to indicate the quad corners. Note that the order in which we call glVertex() command is very important. Here we specified the vertices in a counter clock wise order. The importance rises from the fact that OpenGL can cull the back faces and speed up the rendering procedure as a result.

glEnable(GL_CULL_FACE);

This command will activate cull facing in OpenGL. So you can write this line in the Initialize() function and run the application. Of course nothing will happen because we specified the vertices in the correct order. If you reverse the order and run the application, you'll notice that the quad is disappeared. But yet you can make it visible by glFrontFace() command.

glFrontFace(GL_CW);

This command specifies the order of front faces. Passing GL_CW as the argument means that every face with clock wise order will be drawn as front face.

Let's go back to the discussion about the glBegin()/glEnd() block. You learned how to use it to draw a quad. As we said before OpenGL can draw some other primitives. Here we have a table that shows the valid values which we can use as the argument of glBegin() function. Note that the meaning of vertex sequence depends on the primitive you choose.

GL_POINTS

Treats each vertex as a single point.

Example: Draw a vertex in the center of screen.

glBegin(GL_POINTS);
glVertex2f(0, 0);
glEnd();

GL_LINES

Treats each pair of vertices as an independent line segment.

Example: Draw a horizontal and a vertical line

glBegin(GL_LINES);
// Horizontal
glVertex2f(-1, 0);
glVertex2f( 1, 0);
// Vertical
glVertex2f(0, -1);
glVertex2f(0, 1);
glEnd();

GL_LINE_STRIP

Draws a connected group of line segments from the first vertex to the last.

Example: Draw a wire quad in the center of screen.

glBegin(GL_LINE_STRIP);
glVertex2f(-0.5,-0.5);
glVertex2f( 0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.5,-0.5);
glEnd();

GL_LINE_LOOP

Draws a connected group of line segments from the first vertex to the last, then back to the first.

Example: Draw a wire quad in the center of screen.

glBegin(GL_LINE_STRIP);
glVertex2f(-0.5,-0.5);
glVertex2f( 0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();

GL_TRIANGLES

Treats each triplet of vertices as an independent triangle.

Example: Draw a quad in the center of screen.

glBegin(GL_TRIANGLES);
// Down triangle
glVertex2f( 0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5,-0.5);
// Up triangle
glVertex2f(-0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();

GL_TRIANGLE_STRIP

Draws a connected group of triangles using vertices v1, v2, v3 then v3 v2, v4 then v4, v3, v5 and so on.

Example: Draw a quad in the center of screen.

glBegin(GL_TRIANGLE_STRIP);
glVertex2f( 0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5,-0.5);
glVertex2f(-0.5, 0.5);
glEnd();

GL_QUADS

Treats each group of four vertices as an independent quadrilateral.

Example: Draw a quad in the center of screen.

glBegin(GL_QUADS);
glVertex2f(-0.5,-0.5);
glVertex2f( 0.5,-0.5);
glVertex2f( 0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();

GL_QUAD_STRIP

Draws a connected group of quadrilaterals using vertices v1, v2, v4, v3 then v3, v4, v5, v6 and so on.

GL_POLYGON

Draws a single, convex polygon.

Example: Draw a five sided polygon.

glBegin(GL_POLYGON);
glVertex2f( 0.0f, 0.5f);
glVertex2f(-0.4f, 0.1f);
glVertex2f(-0.2f,-0.5f);
glVertex2f( 0.2f,-0.5f);
glVertex2f( 0.4f, 0.1f);
glEnd();

That's all. Those are the only things that OpenGL can draw and In fact those are more than enough, because almost all the games and applications just use triangles and you can draw everything using them. But how we can draw more complex shapes with these primitives? This is an interesting field of Computer Graphics which is called "Tessellation". How ever game programmers don't care about that much because most often you just import the model from an art package, so the tessellation is already done. It's a good practice to try drawing a sphere or cylinder using triangle strips.

Good Luck

Download Source

Posted on : 10 Sep 2004
Vahid Kazemi

Copyright © 2003-2010 Vahid Kazemi, GameProgrammer.org

iPhone Games