Vizard uses the OpenGL Shading Language (GLSL) for shaders. Using vertex and fragment shaders for rendering real-time graphics has many benefits. Most notably, it moves graphics related operations from the CPU to the GPU. This allows programmers to use the graphics hardware directly to render a wider range of effects and realistic materials.
This section describes how to manually create and apply shader objects. In addition to manually creating shaders, Vizard provides a high-level shader effect framework that simplifies the process of creating and combining shader effects. The effect framework also supports automatically generating effects from models created by the OpenSceneGraph 3ds Max exporter. Vizard also provides a library for applying post-process shader effects to the output image of rendered scene.
Note: This section of the documentation assumes you are already familiar with GLSL. There are many books and tutorials online that go into great detail about GLSL. You can visit the official OpenGL website for links to various GLSL resources.
To create a shader object in Vizard, simply use the <viz>.addShader command. You can specify the file or code for the vertex, geometry, and fragment shader when using this command. The following sample defines the code for a fragment shader directly in the script and creates a shader object using the code:
The above shader will simply set the color of the object to red. Once the shader is created you can apply it to any node3d object using the <node3d>.apply command. The code below will create a quad and apply our shader to it:
GLSL allows passing values to shaders through the use of uniforms. To create a uniform in Vizard you can use <viz>.addUniformBool, <viz>.addUniformFloat, or <viz>.addUniformInt depending on the type of data the uniform needs to hold. Once the uniform is created, use the <node3d>.apply command to apply the uniform to any node3d object. The following code shows how to create a uniform:
Notice how the name and initial value must be specified when creating the uniform. Let's modify the above shader so that the color is specified through a uniform, instead of being hard coded.
Once you have created a uniform, you can change the value associated with it using the <uniform>.set command. You must pass the same number of values to the <uniform>.set command as you used when creating the uniform. Try adding the following code to the previous example in order to change the color of the uniform when the spacebar is pressed:
The following list of uniforms are available to all shaders used with Vizard.
Uniform Name |
GLSL Type |
Description |
viz_ScreenSize |
ivec2 |
The width/height of the graphics window |
osg_FrameNumber |
int |
Incrementing frame number |
osg_FrameTime |
float |
Incrementing frame time (seconds) |
osg_DeltaFrameTime |
float |
Elapsed time since last frame (seconds) |
osg_ViewMatrix |
mat4 |
Viewpoint matrix for current frame |
osg_ViewMatrixInverse |
mat4 |
Inverse viewpoint matrix for current frame |
Here is an example shader that uses the built-in osg_FrameTime uniform to animate the color of a quad: