Coin  4.0.10
Coin3D core library
Loading...
Searching...
No Matches
Shaders in Coin

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:

shaderObject [
sourceProgram "myvertexshader.glsl"
parameter [
ShaderParameter1f { name "myvertexparam" value 1.0 }
]
}
sourceProgram "myfragmentshader.glsl"
parameter [
ShaderParameter1f { name "myfragmentparam" value 2.0 }
]
}
]
}
Cube { }
}
The SbList class is a template container class for lists.
Definition SbList.h:72

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:

SoSeparator * root = new SoSeparator;
vshader->sourceProgram.setValue("myvertexshader.glsl");
fshader->sourceProgram.setValue("myfragmentshader.glsl");
program->shaderObject.set1Value(0, vshader);
program->shaderObject.set1Value(1, fshader);
root->addChild(new SoCube);
The SoCube class is for rendering cubes.
Definition SoCube.h:42
The SoFragmentShader class is used for setting up fragment shader programs.
Definition SoFragmentShader.h:40
virtual void addChild(SoNode *node)
Definition SoGroup.cpp:454
The SoSeparator class is a state-preserving group node.
Definition SoSeparator.h:44
The SoShaderProgram class is used to specify a set of vertex/geometry/fragment objects.
Definition SoShaderProgram.h:50
The SoVertexShader class is used for setting up vertex shader programs.
Definition SoVertexShader.h:38

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:

SbString glslsource = "..."; // built up or loaded however you like
vshader->sourceProgram.setValue(glslsource);
The SbString class is a string class with convenience functions for string operations.
Definition SbString.h:52
@ GLSL_PROGRAM
Definition SoShaderObject.h:58

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.

  • coin_texunit[n]_model - Set to 0 when texturing is disabled, and to SoTextureImageElement::Model if there's a current texture on the state for unit n.
  • coin_light_model - Set to 1 for PHONG, 0 for BASE_COLOR lighting.
  • coin_two_sided_lighting - Set to 1 for two-sided, 0 for normal

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:

shaderObject [
sourceProgram "perpixel_vertex.glsl"
}
sourceProgram "perpixel_fragment.glsl"
}
]
}
Complexity { value 1.0 }
Material { diffuseColor 1 0 0 specularColor 1 1 1 shininess 0.9 }
Sphere { }
Translation { translation 3 0 0 }
Material { diffuseColor 0 1 0 specularColor 1 1 1 shininess 0.9 }
Cone { }
Translation { translation 3 0 0 }
Material { diffuseColor 0.8 0.4 0.1 specularColor 1 1 1 shininess 0.9 }
}

The vertex shader (perpixel_vertex.glsl):

The fragment shader (perpixel_fragment.glsl):

in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
inout vec4 specular)
{
float nDotVP; // normal . light direction
float nDotHV; // normal . light half vector
float pf; // power factor
nDotVP = max(0.0, dot(normal, normalize(vec3(gl_LightSource[i].position))));
nDotHV = max(0.0, dot(normal, vec3(gl_LightSource[i].halfVector)));
if (nDotVP == 0.0)
pf = 0.0;
else
pf = pow(nDotHV, gl_FrontMaterial.shininess);
ambient += gl_LightSource[i].ambient;
diffuse += gl_LightSource[i].diffuse * nDotVP;
specular += gl_LightSource[i].specular * pf;
}
void main(void)
{
vec3 eye = -normalize(ecPosition3);
vec4 ambient = vec4(0.0);
vec4 diffuse = vec4(0.0);
vec4 specular = vec4(0.0);
vec3 color;
DirectionalLight(0, normalize(fragmentNormal), ambient, diffuse, specular);
color =
gl_FrontLightModelProduct.sceneColor.rgb +
ambient.rgb * gl_FrontMaterial.ambient.rgb +
diffuse.rgb * gl_Color.rgb +
specular.rgb * gl_FrontMaterial.specular.rgb;
gl_FragColor = vec4(color, gl_Color.a);
}