OpenGL
From DaphneWiki
Contents |
OpenGL Shading Language Reference
Variable Qualifiers
Qualifiers cannot be initialized to any value!
attribute
frequently changing values from application to vertex shader
uniform
infrequently changing values from application to any shader (vertex or fragment) To set the value of a uniform variable do the following:
- Call glUseProgram(GLhandle program) to activate your shader program.
- Call glGetUniformLocation(GLhandle program, const GLchar *pszName) to get an ID (a GLint) for the variable 'pszName'.
- Call glUniform1{f|i}(GLint id, GLint your_val) to set the variable to have 'your_val', where 'id' is the id you got from the last call.
varying
interpolated variables from vertex shader to fragment shader
Built-In Variables
If it begins with "gl_" it is a built-in variable. Example is gl_FragColor.
Variable Types
Scalars (regular C-styled variables)
float, int, bool are all supported and can be initialized to any value. Integers are fairly limited and don't behave like they do in C. See orange book, page 66. const is also supported.
Vectors
vec2, vec3, or vec4 (all floating point)
Matrices
mat2, mat3, mat4 (2x2, 3x3, 4x4 matrix respectively)
Texture Map Readers
sampler1D for 1D texture map, sampler2D for 2D texture map, higher dimensions are apparently supported. Samplers cannot be modified by the shader! Samplers are read using the texture lookup functions texture1D, texture2D, etc.
Arrays
Arrays of any type can be created. There are no pointers; to access an array, you must use square brackets.
Texture Access Functions
Texture coordinates are accessed via the built-in array declared as: varying vec4 gl_TexCoord[]; Use the texture1D function to access 1Dimensional textures. Use texture2D function to access 2D textures. The first argument is the sampler, which will be a uniform variable defined by the application. The second argument is a vec2 which is the coordinate on the texture from which to do the lookup. The coordinate is given to you in the gl_TexCoord but you don't have to necessarily use these provided values.
To set uniform sampler variable in your shader from your application
See previous instructions on how to set a uniform variable. Call glUniform1i and for your value, use the number of the texture unit that you will use to access the texture. The texture unit is set by calling glActiveTexture. For example, to set texture unit 0, you would call glActiveTexture(GL_TEXTURE0). After setting the texture unit, I believe that you must call glBindTexture to copy the texture from the cpu's memory into the gpu's memory.