Graphics Reference
In-Depth Information
Creating a Shader Program and Attaching Shader Objects
The glCreateProgram( ) function is used to create an empty shader program:
GLuint program = glCreateProgram( );
The variable program is just a handle and has no numerical significance to
the application. To atach a shader to this program, use both the program and
shader handles in glAttachShader( ) like this:
glAttachShader ( program, vertShader );
glAttachShader ( program, tesscontrolShader );
glAttachShader ( program, tessevaluationShader );
glAttachShader ( program, geomShader );
glAttachShader ( program, fragShader );
You don't need to have all these types of shaders in every program. The
program will just consist of the shaders you have atached. This code should
be placed in an initialization section of your application program. You can also
create more than one shader program if you want to use different shaders in
different parts of your application.
Linking Shader Programs
Before you can actually use the shader program you have just created, you
must link the individual shader objects together, resolve their common vari-
ables, and link with any built-in support code. The linking uses the function
glLinkProgram( program )
If any shader objects are not included, the shader program will let the
fixed-function processor continue to take on those functions.
Like compilation, linking a shader program can fail, and you should rou-
tinely check that linking is successful before assuming that the program is use-
able. The function
glGetProgramiv( program, GL_LINK_STATUS, &linkStatus )
returns a linkStatus of GL_TRUE if the program linked successfully; otherwise
it returns GL_FALSE . Just as was the case when you compiled shader objects,
you not only need to check for success, you also need to report any errors. The
whole process looks like this:
Search WWH ::




Custom Search