Game Development Reference
In-Depth Information
glGetShaderInfoLog(ID, 1024, &Len, Error);
printf("%s\n", Error);
exit (-1);
}
}
As a bonus, you can also check the compilation status using the API call glGetShaderiv . It
takes as arguments a shader ID, a query constant ( GL_COMPILE_STATUS , in this case, to check
the compilation status), and the status of the query. If the status is not GL_TRUE , then the
compilation errors can be extracted by calling glGetShaderInfoLog with the ID of the shader
and a string buffer that described the nature of the error. The next step is to attach the
shader to a program.
Attaching to the Shader
To attach your shader to the main program, use the API call glAttachShader . It takes as
arguments the ID of the program object to which a shader object will be attached plus the
shader object that is to be attached, as shown in the following fragment:
glAttachShader(Program, Shader[0]);
glAttachShader(Program, Shader[1]);
glBindAttribLocation(Program, 0, "Position");
glBindAttribLocation(Program, 1, "Normal");
You also use glBindAttribLocation to associate a user-defined attribute variable in the
program object with a generic vertex attribute index. The name of the user-defined attribute
variable is passed as a null-terminated string in the last argument. This allows the developer
to declare variables in the master program and bind them to variables in the shader code.
Linking the Shader Program
To use the shaders, you must link the program that contains them by calling glLinkProgram
with the reference ID of the program. Behind the scenes, OpenGL creates an executable that
will run on the programmable fragment processor.
// Link
glLinkProgram(Program);
Getting the Link Status
The status of the link operation is stored as part of the program object's state. It is always
a good idea to check for errors by getting the status of the link using glGetProgramiv ,
very similar to the way you checked the compilation status but using the GL_LINK_STATUS
constant in this particular case. The following fragment demonstrates how to do so:
// Validate our work thus far
int ShaderStatus;
glGetProgramiv(Program, GL_LINK_STATUS, &ShaderStatus);
if (ShaderStatus != GL_TRUE) {
 
Search WWH ::




Custom Search