Help end child hunger

Creating a Shader

Prev: Setup for GLSL Next: Creating a Program
 

The following figure shows the necessary steps to create a shader.

 

The first step is creating an object which will act as a shader container. The function available for this purpose returns a handle for the container.

The OpenGL 2.0 syntax for this function is as follows:


GLuint glCreateShader(GLenum shaderType);

Parameter:

shaderType – GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.

The ARB extensions syntax for this function is as follows:


GLhandleARB glCreateShaderObjectARB(GLenum shaderType);

Parameter:

shaderType – GL_VERTEX_SHADER_ARB or GL_FRAGMENT_SHADER_ARB.

You can create as many shaders as you want to add to a program, but remember that there can only be a main function for the set of vertex shaders and one main function for the set of fragment shaders in each single program.

The following step is to add some source code. The source code for a shader is a string array, although you can use a pointer to a single string.

The syntax of the function to set the source code, in OpenGL 2.0 syntax, for a shader is:


void glShaderSource(GLuint shader, int numOfStrings, const char **strings, int *lenOfStrings);

 

Parameters:

  • shader – the handler to the shader.
  • numOfStrings – the number of strings in the array.
  • strings – the array of strings.
  • lenOfStrings – an array with the length of each string, or NULL, meaning that the strings are NULL terminated.

And using the ARB extensions:


void glShaderSourceARB(GLhandleARB shader, int numOfStrings, const char **strings, int *lenOfStrings);

 

Parameters:

  • shader – the handler to the shader.
  • numOfStrings – the number of strings in the array.
  • strings – the array of strings.
  • lenOfStrings – an array with the length of each string, or NULL, meaning that the strings are NULL terminated.

Finally, the shader must be compiled. The function to achieve this using OpenGL 2.0 is:


void glCompileShader(GLuint shader);

Parameters:

shader – the handler to the shader.

And using the ARB extensions:


void glCompileShaderARB(GLhandleARB shader);

Parameters:

shader – the handler to the shader.

 

Prev: Setup for GLSL Next: Creating a Program
 
© 2014 Lighthouse3d.com Suffusion theme by Sayontan Sinha