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

480
D. Vertex Array Class
VA->CollapseCommonVertices( true );
VA->UseBufferObjects( true );
VA->SetTol( .001f );
VA->glBegin( GL_QUADS );
for( int i = 0; i < 6; i++ )
{
for( int j = 0; j < 4; j++ )
{
GLuint k = CubeIndices[i][j];
VA->glColor3fv( CubeColors[k] );
VA->glVertex3fv( CubeVertices[k] );
}
}
VA->glEnd( );
VA->Print( ); // verify that vertices were really collapsed
. . .
// this goes in the display-callback part of the program:
VA->Draw( );
Figure D.1. The cube drawn by this code example, with axes added to show how the colors
correspond to the vertex coordinates.
481
This next example shows drawing gridlines on a terrain map. The
already-dened
Heights[ ] array holds the terrain heights. This is a good
example of using the
RestartPrimitive( ) method so that the next grid
line doesn’t have to be in a new line strip. In this way, the entire grid is
saved as a single line strip and is drawn by blasting a single VA / VBO into
the graphics pipeline.
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
VA->CollapseCommonVertices( true );
VA->UseBufferObjects( true );
VA->SetTol( .001f );
int x, y; // loop indices
float ux, uy; // utm coords
VA->glBegin( GL_LINE_STRIP );
for( y = 0, uy = meteryMin; y < NumLats; y++, uy += meteryStep )
{
VA->RestartPrimitive( );
for( x = 0, ux = meterxMin; x < NumLngs x++, ux += meterxStep
)
{
float uz = Heights[ y*NumLngs + x ];
VA->glColor3f( 1., 1., 0. ); // single color = yellow
VA->glVertex3f( ux, uy, uz );
}
}
for( x = 0, ux = meterxMin; x < NumLngs; x++, ux += meterxStep )
{
VA->RestartPrimitive( );
for( y = 0, uy = meteryMin; y < NumLats; y++, uy +=
meteryStep )
{
float uz = Heights[ y*NumLngs + x ];
VA->glColor3f( 1., 1., 0. );
The Program Body
482
D. Vertex Array Class
VA->glVertex3f( ux, uy, uz );
}
}
VA->glEnd( );
. . .
// this goes in the display-callback part of the program:
VA->Draw( );
Figure D.2. A wireframe terrain map drawn as a single line
strip in a vertex buer object.
2
2 If you’re interested in timing, this dataset had 1024 × 569 grid points, and displayed at 1,000 FPS on
an NVIDIA GTX 480.

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