![]() |
Coin
4.0.10
Coin3D core library
|
Coin 2.5 added support for shaders. The main nodes used are SoShaderProgram, SoVertexShader, SoFragmentShader, and SoGeometryShader. A typical scene graph with shaders will look something like this:
This will render the cube with the vertex and fragment shaders specified in myvertexshader.glsl and myfragmentshader.glsl. Coin also supports ARB shaders and Cg shaders (if the Cg library is installed). However, we recommend using GLSL since we will focus mostly on support this shader language.
Constructing shaders at run-time
The scene graph above can just as well be built up programmatically instead of read from an .iv file – there is no separate "runtime
loading" API for shaders, since they are just ordinary nodes:
sourceProgram defaults to being interpreted as a filename (SoShaderObject::sourceType defaults to SoShaderObject::FILENAME). If the shader source itself is only available at run-time – generated on the fly, downloaded, extracted from an archive, or otherwise not sitting in a plain file Coin can open by path – set sourceType to SoShaderObject::GLSL_PROGRAM and pass the actual GLSL source text to sourceProgram instead of a filename:
Since the shader program is just a node like any other, this works identically regardless of which action ends up traversing the scene graph – rendering straight to screen with SoGLRenderAction, or rendering to an SoOffscreenRenderer – there is nothing render-target-specific about setting up shaders.
Coin defines some named parameters that can be added by the application programmer, and which will be automatically updated by Coin while traversing the scene graph.
Example scene graph that renders per fragment OpenGL Phong lighting for one light source. The shaders assume the first light source is a directional light. This is the case if you open the file in a standard examiner viewer.
The iv-file:
The vertex shader (perpixel_vertex.glsl):
The fragment shader (perpixel_fragment.glsl):