Graphics Reference
In-Depth Information
them into a shader “program.” This program will then be invoked (“used” in
OpenGL terminology) during the rendering process to have this combination
of shaders replace the fixed-function pipeline. An individual GLSL shader is
exactly what you have been working with when you write vertex, geometry,
or fragment shader files for glman . The only functional difference is in how you
incorporate shaders in your application. A GLSL shader is writen and stored
as a plain text file to be incorporated into an OpenGL-based application, as we
indicate above. You can use any kind of text-editing application to create the
source file.
As you saw in Figure 14.1, there are a number of steps needed to incor-
porate shaders in an OpenGL application. The first step in this process is to
read the shader source file into an ordinary null-terminated text string. This
should be a familiar programming operation, but for completeness, the fol-
lowing example is a C++ source code fragment that reads a file into a null-ter-
minated string whose address is str . From there, it can be compiled, atached
to a shader program, and linked, so it can be used in your application.
#include <stdio.h>
FILE *fp = fopen( filename, “r” );
If ( fp == NULL ) {...} // report failure to open, and fail
// gracefully
fseek( fp, 0, SEEK_END );
int numBytes = ftell( fp ); // length of file
GLchar * str = new GLchar[numBytes+1];
rewind( fp );
fread( str, 1, numBytes, fp );
fclose( fp );
str[numBytes] = '\0'; // end byte string with NULL
The code fragment above uses the C++ new( ) operator, and in general
we use C++ conventions in this chapter. If you are using C, you replace that
line with the two lines
GLchar *str;
. . .
str = (GLchar *) malloc( numBytes + 1 );
and anywhere we use the C++ delete[ ] operator, you should use the C func-
tion free(...) .
Search WWH ::




Custom Search