OpenGL Basics

Updated: 22-May-2006
Author: John Niclasen

Contents:

1. Command Suffixes and Argument Data Types
2. Drawing Survival Kit
2.1 Clearing Buffers
2.2 Specifying a Color
2.3 Forcing completion of Drawing
3. Points, Lines, and Polygons
3.1 Rectangle
3.2 Vertices
3.3 OpenGL Geometric Drawing Primitives
3.4 Basic State Management
3.5 Displaying Points, Lines, and Polygons

1. Command Suffixes and Argument Data Types

Suffix

Data Type

C-Language Type

OpenGL Type

b

8-bit integer

signed char

GLbyte

s

16-bit integer

short

GLshort

i

32-bit integer

long

GLint, GLsizei

f

32-bit floating-point

float

GLfloat, GLclampf

d

64-bit floating-point

double

GLdouble, GLclampd

ub

8-bit unsigned integer

unsigned char

GLubyte, GLboolean

us

16-bit unsigned integer

unsigned short

GLushort

ui

32-bit unsigned integer

unsigned long

GLuint, GLenum, GLbitfield

Some OpenGL commands can take a final letter v, which indicates that the command takes a pointer to a vector (or array) of values rather than a series of individual arguments.

2. Drawing Survival Kit

2.1 Clearing Buffers

To clear the color buffer (using RGBA):

glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);

To clear color and depth buffers:

glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth (1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Buffer

Name

Color buffer

GL_COLOR_BUFFER_BIT

Depth buffer

GL_DEPTH_BUFFER_BIT

Accumulation buffer

GL_ACCUM_BUFFER_BIT

Stencil buffer

GL_STENCIL_BUFFER_BIT

2.2 Specifying a Color

Specifying RGB values as floats between 0.0 and 1.0, this will specify red:

glColor3f (1.0, 0.0, 0.0);

2.3 Forcing completion of Drawing

Use this call at the end of each frame:

glFlush();

This call will wait for the drawing to finish:

glFinish();

3. Points, Lines, and Polygons

3.1 Rectangle

void glRect{sifd} (TYPEx1, TYPEy1, TYPEx2, TYPEy2);
void glRect{sifd}v (TYPE*v1, TYPE*v2);

3.2 Vertices

This function is only effective between a glBegin() and glEnd() pair.

void glVertex{234}{sifd}[v] (TYPEcoords);

Examples:

glVertex2s (2, 3);
glVertex3d (0.0, 0.0, 3.1415926535898);
glVertex4f (2.3, 1.0, -2.2, 2.0);

GLdouble dvect[3] = {5.0, 9.0, 1992.0};
glVertex3dv (dvect);

3.3 OpenGL Geometric Drawing Primitives

Geometric primitives are drawn by bracket each set of vertices between a call to glBegin() and a call to glEnd().

Example:

glBegin(GL_POLYGON);
   glVertex2f(0.0, 0.0);
   glVertex2f(0.0, 3.0);
   glVertex2f(3.0, 3.0);
   glVertex2f(4.0, 1.5);
   glVertex2f(3.0, 0.0);
glEnd();

The ten possible glBegin() arguments:

Value

Meaning

GL_POINTS

individual points

GL_LINES

pairs of vertices interpreted as individual line segments

GL_POLYGON

boundary of a simple, convex polygon

GL_TRIANGLES

triples of vertices interpreted as triangles

GL_QUADS

quadruples of vertices interpreted as four-sided polygons

GL_LINE_STRIP

series of connected line segments

GL_LINE_LOOP

same as above, with a segment added between last and first vertices

GL_TRIANGLE_STRIP

linked strip of triangles

GL_TRIANGLE_FAN

linked fan of triangles

GL_QUAD_STRIP

linked strip of quadrilaterals

As you read the following descriptions, assume that n vertices (v0, v1, v2, ... , vn-1) are described between a glBegin() and glEnd() pair.

 GL_POINTSDraws a point at each of the n vertices.
 GL_LINESDraws a series of unconnected line segments. Segments are drawn between v0 and v1, between v2 and v3, and so on. If n is odd, the last segment is drawn between vn-3 and vn-2, and vn-1 is ignored.
 GL_LINE_STRIPDraws a line segment from v0 to v1, then from v1 to v2, and so on, finally drawing the segment from vn-2 to vn-1. Thus, a total of n-1 line segments are drawn. Nothing is drawn unless n is larger than 1. There are no restrictions on the vertices describing a line strip (or a line loop); the lines can intersect arbitrarily.
 GL_LINE_LOOPSame as GL_LINE_STRIP, except that a final line segment is drawn from vn-1 to v0, completing a loop.
 GL_TRIANGLESDraws a series of triangles (three-sided polygons) using vertices v0, v1, v2, then v3, v4, v5, and so on. If n isn't an exact multiple of 3, the final one or two vertices are ignored.
 GL_TRIANGLE_STRIPDraws a series of triangles (three-sided polygons) using vertices v0, v1, v2, then v2, v1, v3 (note the order), then v2, v3, v4, and so on. The ordering is to ensure that the triangles are all drawn with the same orientation so that the strip can correctly form part of a surface. n must be at least 3 for anything to be drawn.
 GL_TRIANGLE_FANSame as GL_TRIANGLE_STRIP, except that the vertices are v0, v1, v2, then v0, v2, v3, then v0, v3, v4, and so on.
 GL_QUADSDraws a series of quadrilaterals (four-sided polygons) using vertices v0, v1, v2, v3, then v4, v5, v6, v7, and so on. If n isn't a multiple of 4, the final one, two, or three vertices are ignored.
 GL_QUAD_STRIPDraws a series of quadrilaterals (four-sided polygons) beginning with v0, v1, v3, v2, then v2, v3, v5, v4, then v4, v5, v7, v6, and so on. n must be at least 4 before anything is drawn, and if n is odd, the final vertex is ignored.
 GL_POLYGONDraws a polygon using the points v0, ... , vn-1 as vertices. n must be at least 3, or nothing is drawn. In addition, the polygon specified must not intersect itself and must be convex. If the vertices don't satisfy these conditions, the results are unpredictable.

3.3.1 Restrictions on Using glBegin() and glEnd()

Valid Commands between glBegin() and glEnd():

Command

Purpose of Command

glVertex*()

set vertex coordinates

glColor*()

set current color

glIndex*()

set current color index

glNormal*()

set normal vector coordinates

glEvalCoord*()

generate coordinates

glCallList(), glCallLists()

execute display list(s)

glTexCoord*()

set texture coordinates

glEdgeFlag*()

control drawing of edges

glMaterial*()

set material properties

3.4 Basic State Management

States can be turned on and off with:

void glEnable (GLenum capability);
void glDisable (GLenum capability);

To check a state:

GLboolean glIsEnabled (GLenum capability);

Routines to find out which values are set for many states:

void glGetBooleanv (GLenum pname, GLboolean *params);
void glGetIntegerv (GLenum pname, GLint *params);
void glGetFloatv (GLenum pname, GLfloat *params);
void glGetDoublev (GLenum pname, GLdouble *params);
void glGetPointerv (GLenum pname, GLvoid **params);

3.5 Displaying Points, Lines, and Polygons

3.5.1 Point Details

void glPointSize (GLfloat size);

3.5.2 Line Details

void glLineWidth (GLfloat width);

3.5.3 Polygon Details

void glPolygonMode (GLenum face, GLenum mode);

The parameter face can be GL_FRONT_AND_BACK, GL_FRONT, or GL_BACK; mode can be GL_POINT, GL_LINE, or GL_FILL. By default, both the front and back faces are drawn filled.

For example, you can have the front faces filled and the back faces outlined with two calls to this routine:

glPolygonMode (GL_FRONT, GL_FILL);
glPolygonMode (GL_BACK, GL_LINE);

3.5.4 Reversing and Culling Polygon Faces

void glFrontFace (GLenum mode);

By default, mode is GL_CCW, which corresponds to a counterclockwise orientation of the ordered vertices of a projected polygon in window coordinates. If mode is GL_CW, faces with a clockwise orientation are considered front-facing.

void glCullFace (GLenum mode);

Indicates which polygons should be discarded (culled) before they're converted to screen coordinates. The mode is either GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK to indicate front-facing, back-facing, or all polygons. To take effect, culling must be enabled using glEnable() with GL_CULL_FACE; it can be disabled with glDisable() and the same argument.


NicomDoc - 22-May-2006