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

465
GLSLProgram C++ Class
A
The act of creating, compiling, linking, using, and passing parameters to shad-
ers is very repetitive. For some of our own work, we have found it helpful
to create a C++ class called
GLSLProgram that implements this process. This
class has the tools to manage all the steps of shader program development
and use, including source le opening, loading, and compilation. Some of the
individual methods were presented in Chapter 14. It also has methods that
implement seing aribute and uniform variables. This source is available on
the book’s website.
The following methods are supported by the class
bool Create( char *, char * = NULL, char * = NULL,
char * = NULL, char * = NULL );
bool IsValid( );
466
A. GLSLProgram C++ Class
void SetAttribute( char *, int );
void SetAttribute( char *, float );
void SetAttribute( char *, float, float, float );
void SetAttribute( char *, float * );
void SetAttribute( char *, Vec3& );
void SetGstap( bool );
void SetInputTopology( GLenum );
void SetOutputTopology( GLenum );
void SetUniform( char *, int );
void SetUniform( char *, float );
void SetUniform( char *, float, float, float );
void SetUniform( char *, float[3] );
void SetUniform( char *, Vec3& );
void SetUniform( char *, Matrix4& );
void SetVerbose( bool );
void Use( );
void UseFixedFunction( );
The Create( ) method takes up to ve shader le names as arguments.
From the lename extension, it gures out what type of shader it is, loads it,
compiles it, and links them all together. All errors are wrien to stderr. The
IsValid( ) method can be called if your application wants to know if every-
thing succeeded or not.
The
SetAttribute( ) methods set aribute variables, destined for the
vertex shader. The
SetUniform( ) methods set uniform variables, destined for
any of the shaders.
The
Use( ) method makes this shader program active, so that it aects
any subsequent drawing that you do.
UseFixedFunction( ) returns the state
of the pipeline to use the xed-functionality (if it’s available).
The
SetGstap( ) method is there to give you the option to have the
gstap.h code included automatically. Just pass true as the argument. Call this
before you call the
Create( ) method.
Here is an example of using the GLSLProgram class.
#include “glslprogram.h”
float Ad, Bd, NoiseAmp, NoiseFreq, Tol;
GLSLProgram * Ovals;
. . .
// set everything up once:
Ovals = new GLSLProgram( );
Ovals->SetVerbose( true );
467
The Program Body
Ovals->SetGstap( true );
bool good = Ovals->Create( “ovalnoise.vert”, “ovalnoise.frag” );
if( ! good )
{
fprintf( stderr, “GLSL Program Ovals wasn’t created.\n” );
. . .
}
. . .
// do this in the display callback:
Ovals->Use( );
Ovals->SetUniform( “uAd”, Ad );
Ovals->SetUniform( “uBd”, Bd );
Ovals->SetUniform( “uNoiseAmp”, NoiseAmp );
Ovals->SetUniform( “uNoiseFreq”, NoiseFreq );
Ovals->SetUniform( “uTol”, Tol );
glColor3f( 0., 1., 0. );
glutSolidTeapot( 1. );
Ovals->UseFixedFunction( );
. . .
This page intentionally left blankThis page intentionally left blank
469
Matrix4 C++ Class
B
One of the trends in OpenGL is to have the application developer bear the
responsibility to manipulate and provide the needed transformation matrices.
We have created a 4 × 4 matrix class called
Matrix4 to handle a lot of this work.
The following methods are supported by the class
Matrix4( );
Matrix4( const Matrix4& );
Matrix4( float, float, float, float, float, float, float, float,
float, float, float, float, float, float, float, float );
Matrix4( float [4][4] );
Matrix4& Frustum( float, float, float, float, float, float );
Matrix4& FrustumZ( float, float, float, float, float, float,
float );

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