Graphics Reference
In-Depth Information
Creating and Compiling Shader Objects
Shader objects are unique objects: code groups that are downloaded from your
application to the appropriate section of your graphics processor, where they
can be used. There is a bit of processing involved in seting up a shader object.
We cover that in this short section.
The process of creating a compiled shader object has three steps:
1. You must first create empty vertex, tessellation, geometry, or fragment
shader objects. These will be identified with GLuint variables called han-
dles that let you access them later. (The actual value of the handle has no
meaning to your graphics application. Print it if you're curious. Just keep
track of it and don't ever change it.)
2. You must next read the shader source string into the shader object. We
described how you could create this string in the previous section.
3. Finally, you compile the shader object and check that the compilation
was successful. This checking step is needed because shader compilation
is not like standard language compilation; there is no automatic report-
ing of compilation problems.
The code fragment below shows how this is done for a vertex shader.
The code to create and compile a geometry or fragment shader is made by
simply replacing every reference to vertex shader by a reference to geometry
or fragment shader, as you will see in the examples later in this chapter. This
is a simpler version of the same operations in the full GLSLProgram class source
listed in Appendix A; it shows the flow of activities needed to create and com-
pile shaders.
int status;
int logLength;
// create an empty shader object
GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
// read the string into shader object
glShaderSource( vertShader, 1, (const GLchar **)&str, NULL );
// str is no longer needed and the memory can be freed
delete [ ] str;
// compile the shader object
glCompileShader( vertShader );
Search WWH ::




Custom Search