Game Development Reference
In-Depth Information
referenced. The following fragment creates two shaders to load a vertex and fragment
shaders to draw an icosahedron (described later in this chapter):
int Shader[2]
// Create 2 shader programs
Shader[0] = glCreateShader(GL_VERTEX_SHADER);
Shader[1] = glCreateShader(GL_FRAGMENT_SHADER);
// Load VertexShader: It has the GLSL code
LoadShader((char *)VertexShader, Shader[0]);
// Load fragment shader: FragmentShaderBlue has the GLSL code
LoadShader((char *)FragmentShaderBlue, Shader[1]);
// Create the program and attach the shaders & attributes
int Program = glCreateProgram();
You also make an API call to glCreateProgram that creates an empty program object and
returns a non-zero value by which it can be referenced. Shaders must be attached to a
program. This provides a mechanism to specify the shader objects that will be linked to
create a program. It also provides a means for checking the compatibility of the shaders that
will be used to create a program. Next, you load it.
Loading the Shader
A shader object is used to maintain the source code strings that define a shader. For this
purpose, you can create a load function that invokes glShaderSource and glCompileShader .
glShaderSource takes as arguments the ID of the shader, the number of elements, a string
containing the source code to be loaded, and an array of string lengths ( NULL in this case).
glCompileShader compiles the shader described by its reference ID. The following fragment
describes the load function that will be used to draw the icosahedron for an upcoming project:
// Simple function to create a shader
void LoadShader(char *Code, int ID)
{
// Compile the shader code
glShaderSource (ID, 1, (const char **)&Code, NULL);
glCompileShader (ID);
// Verify that it worked
int ShaderStatus;
glGetShaderiv(ID, GL_COMPILE_STATUS, &ShaderStatus);
// Check the compile status
if (ShaderStatus != GL_TRUE) {
printf("Error: Failed to compile GLSL program\n");
int Len = 1024;
char Error[1024];
 
Search WWH ::




Custom Search