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

353
The GLSL API
14
We have spent a lot of time talking about shaders outside the context of graph-
ics applications that would use them. This is, of course, not the way the real
world works, and in Chapter 15, we will see a number of exciting and impor-
tant ways that shaders can contribute to creating meaningful images. To do
that, however, you must integrate shader programming with your other
graphics programming.
Shaders in the OpenGL Programming Process
So far, this book has focused on just writing the shader code itself, and not on
the API boilerplate that goes around it. If you have been following along using
354
14. The GLSL API
glman, you probably realize that glman is handling a lot of infrastructure for
you. Well, it’s time to pull back the curtain and talk about how to hook GLSL
shaders into an application.
As we have seen, shader programs replace xed-function graphics opera-
tions that handle vertex, geometry, and fragment processing. When we use
shaders, we must not only provide these les, but we must also carry out sev-
eral steps, diagrammed in Figure 14.1, to integrate the shaders with our appli-
cation. These steps are
1. Create the necessary shader source le(s).
2. Read each shader source le into a null-terminated text string to be com-
piled.
3. Create an empty shader object for each shader.
4. Give each shader object the text string of its shader source.
5. Compile each shader object.
6. Create an overall shader program.
7. Aach the shader objects to the program.
8. Link the shader program.
9. Specify that the shader program is to be used in place of the xed-func-
tion pipeline.
When these are done, OpenGL will replace the xed-function processing
with your shaders until the application nishes or you deactivate or delete the
shader program.
Figure 14.1. The GLSL shader-creation process from shader source to shader use.
355
How Is a GLSL Shader Program Created?
Handling OpenGL Extensions
At this time, shader programming with GLSL is new enough that many of the
GLSL API calls are handled through OpenGL extensions. In order to manage
extensions in a cross-platform way, we can use the OpenGL Extension Wrangler
Library (GLEW). GLEW provides ecient run-time mechanisms for determin-
ing which OpenGL extensions are supported on the target platform. OpenGL
core and extension functionality is exposed in a single header le. GLEW
changes often to keep up with OpenGL developments. You can download
GLEW from hp://glew.sourceforge.net
you should check for new GLEW
releases frequently.
In this chapter, we will refer to some GLSL functions that may be either
EXT or ARB functions (that is, may not yet be fully integrated into the OpenGL
standard), but GLEW will handle that and will replace a function name like
glCreateProgram( ) with glCreateProgramEXT( ) or glCreateProgramARB( )
if either of those is the appropriate one for your system. In this chapter, we will
only use the general function names and will leave
EXT or ARB details up to
GLEW.
You need to initialize GLEW in your application, probably in the func-
tion where you initialize your OpenGL system. The code below will do that
for you.
#include “glew.h“
. . .
GLenum err = glewInit( );
if( err != GLEW_OK )
{
fprintf( stderr, “glewInitError\n” );
exit( 1 );
}
fprintf( stderr,“GLEW initialized OK\n” );
fprintf( stderr,”Status: Using GLEW %s\n”,glewGetString(GLEW_
VERSION) );
How Is a GLSL Shader Program Created?
The usual way of creating shader functionality is to create a collection of dier-
ent types of shaders (e.g., vertex, tessellation, geometry, fragment) and collect

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