O'Reilly logo

Graphics Shaders, 2nd Edition by Steve Cunningham, Mike Bailey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

475
The Program Body
Vec3 ma = -a;
ma.Print( “-a = “ );
Vec3 e = a + b;
e.Print( “e =” );
e *= 3.;
e.Print( “e =” );
float f = ( a + b ).Length( );
fprintf( stderr, “f = %8.3f\n”, f );
float g = a.Dot( (b+c).Unit( ) );
fprintf( stderr, “g = %8.3f\n”, g );
Vec3 h = a.Cross(b);
h.Print( “axb =” );
float i = c.Dot( a.Cross(b) );
fprintf( stderr, “c.(axb) = %8.3f\n”, i );
Point3 Q( 1., 2., 0. );
Point3 R( 5., 3., 0. );
Point3 S( 3., 6., 0. );
Vec3 normal = ( R-Q ).Cross( S-Q );
normal.Print( “normal = “ );
float area = normal.Length( ) / 2.;
fprintf( stderr, “triangle area = %8.3f\n”, area );
This page intentionally left blankThis page intentionally left blank
477
Vertex Array Class
D
OpenGL encourages you to use vertex arrays (VAs) and vertex buer objects
(VBOs) instead of
glBegin-glEnd for three reasons:
1. VAs and VBOs are much more ecient than using
glBegin-glEnd.
2.
glBegin-glEnd has been deprecated in OpenGL-desktop from version
3.0 onward, and might actually go away at some time in the future.
3.
glBegin-glEnd has been completely eliminated from OpenGL-ES 2.0 and
so cannot be used if you want your application to run on both desktop
and mobile platforms.
478
D. Vertex Array Class
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 transmied 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 specied 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 book’s web site: hp://www.cgeducation.org
479
The Program Body
#include “vertexarray.h”
GLfloat CubeVertices[ ][3] =
{
{ -1., -1., -1. },
{ 1., -1., -1. },
{ -1., 1., -1. },
{ 1., 1., -1. },
{ -1., -1., 1. },
{ 1., -1., 1. },
{ -1., 1., 1. },
{ 1., 1., 1. }
};
GLfloat CubeColors[ ][3] =
{
{ 0., 0., 0. },
{ 1., 0., 0. },
{ 0., 1., 0. },
{ 1., 1., 0. },
{ 0., 0., 1. },
{ 1., 0., 1. },
{ 0., 1., 1. },
{ 1., 1., 1. }
};
GLuint CubeIndices[ ][4] =
{
{ 0, 2, 3, 1 },
{ 4, 5, 7, 6 },
{ 1, 3, 7, 5 },
{ 0, 4, 6, 2 },
{ 2, 6, 7, 3 },
{ 0, 1, 5, 4 }
};
VertexArray *VA;
. . .
// this goes in the part of the program where graphics things
// get initialized once:
VA = new VertexArray( ); // create an instance of the class
// the real “constructor” is in the glBegin method

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required