Graphics Reference
In-Depth Information
There is no question that using
glBegin-glEnd
is convenient, especially when
beginning to learn OpenGL. With this in mind, here is a C++ class1 that looks
like the application is using
glBegin-glEnd
, but inside its data structures are
preparing to use VAs and VBOs when the class's
Draw( )
method is called:
void CollapseCommonVertices( bool );
void Draw( );
void glBegin( GLenum );
void glColor3f( GLfloat, GLfloat, GLfloat );
void glColor3fv( GLfloat * );
void glEnd( );
void glNormal3f( GLfloat, GLfloat, GLfloat );
void glNormal3fv( GLfloat * );
void glTexCoord2f( GLfloat, GLfloat );
void glTexCoord2fv( GLfloat * );
void glVertex2f( GLfloat, GLfloat );
void glVertex2fv( GLfloat * );
void glVertex3f( GLfloat, GLfloat, GLfloat );
void glVertex3fv( GLfloat * );
void Print( FILE * = stderr );
void RestartPrimitive( );
void SetTol( float );
void SetVerbose( bool );
void UseBufferObjects( bool );
The
UseBufferObjects( )
method declares whether a VBO should be
used instead of a VA. As VBOs are stored in the graphics card memory and
thus only ever need to be transmited from host memory once, VBOs are
almost always preferable.
Passing a true to the
CollapseCommonVertices( )
method says that you
want any vertices closer to each other than the distance specified in
SetTol( )
collapsed to be treated as a single vertex. The advantage to this is that the
single vertex only gets transformed once. The disadvantage is that the collaps-
ing process takes time, especially for large lists of vertices.
The
RestartPrimitive( )
method invokes an OpenGL-ism that restarts
the current primitive topology without starting a new VA or VBO. This saves
overhead. It is especially handy for “never-ending” topologies such as triangle
strips and line strips.
Here is an example of using the
VertexArray
class and the image it pro-
duces (see Figure D.1):
1.
The source for this class is available on the topic's web site: htp://www.cgeducation.org