1coin_shaders(3Version) coin_shaders(3Version)
2
3
4
6 coin_shaders - Shaders in Coin
7
8 Coin 2.5 added support for shaders. The main nodes used are
9 SoShaderProgram, SoVertexShader, SoFragmentShader, and
10 SoGeometryShader. A typical scene graph with shaders will look
11 something like this:
12
13 Separator {
14 ShaderProgram {
15 shaderObject [
16 VertexShader {
17 sourceProgram "myvertexshader.glsl"
18 parameter [
19 ShaderParameter1f { name "myvertexparam" value 1.0 }
20 ]
21 }
22 FragmentShader {
23 sourceProgram "myfragmentshader.glsl"
24 parameter [
25 ShaderParameter1f { name "myfragmentparam" value 2.0 }
26 ]
27 }
28 ]
29 }
30 Cube { }
31 }
32
33 This will render the Cube with the vertex and fragment shaders
34 specified in myvertexshader.glsl and myfragmentshader.glsl. Coin also
35 supports ARB shaders and Cg shaders (if the Cg library is installed).
36 However, we recommend using GLSL since we will focus mostly on support
37 this shader language.
38
39 Coin defines some named parameters that can be added by the application
40 programmer, and which will be automatically updated by Coin while
41 traversing the scene graph.
42
43 • coin_texunit[n]_model - Set to 0 when texturing is disabled, and to
44 SoTextureImageElement::Model if there's a current texture on the
45 state for unit n.
46 • coin_light_model - Set to 1 for PHONG, 0 for BASE_COLOR lighting.
47 Example scene graph that renders per-fragment OpenGL Phong lighting for
48 one light source. The shaders assume the first light source is a
49 directional light. This is the case if you open the file in a standard
50 examiner viewer.
51 The iv-file:
52 Separator {
53 ShaderProgram {
54 shaderObject [
55 VertexShader {
56 sourceProgram "perpixel_vertex.glsl"
57 }
58 FragmentShader {
59 sourceProgram "perpixel_fragment.glsl"
60 }
61 ]
62 }
63 Complexity { value 1.0 }
64 Material { diffuseColor 1 0 0 specularColor 1 1 1 shininess 0.9 }
65 Sphere { }
66
67 Translation { translation 3 0 0 }
68 Material { diffuseColor 0 1 0 specularColor 1 1 1 shininess 0.9 }
69 Cone { }
70
71 Translation { translation 3 0 0 }
72 Material { diffuseColor 0.8 0.4 0.1 specularColor 1 1 1 shininess 0.9 }
73 Cylinder { }
74 }
75
76 The vertex shader (perpixel_vertex.glsl):
77 varying vec3 ecPosition3;
78 varying vec3 fragmentNormal;
79
80 void main(void)
81 {
82 vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
83 ecPosition3 = ecPosition.xyz / ecPosition.w;
84 fragmentNormal = normalize(gl_NormalMatrix * gl_Normal);
85
86 gl_Position = ftransform();
87 gl_FrontColor = gl_Color;
88 }
89
90 The fragment shader (perpixel_fragment.glsl):
91 varying vec3 ecPosition3;
92 varying vec3 fragmentNormal;
93
94 void DirectionalLight(in int i,
95 in vec3 normal,
96 inout vec4 ambient,
97 inout vec4 diffuse,
98 inout vec4 specular)
99 {
100 float nDotVP; // normal . light direction
101 float nDotHV; // normal . light half vector
102 float pf; // power factor
103
104 nDotVP = max(0.0, dot(normal, normalize(vec3(gl_LightSource[i].position))));
105 nDotHV = max(0.0, dot(normal, vec3(gl_LightSource[i].halfVector)));
106
107 if (nDotVP == 0.0)
108 pf = 0.0;
109 else
110 pf = pow(nDotHV, gl_FrontMaterial.shininess);
111
112 ambient += gl_LightSource[i].ambient;
113 diffuse += gl_LightSource[i].diffuse * nDotVP;
114 specular += gl_LightSource[i].specular * pf;
115 }
116
117 void main(void)
118 {
119 vec3 eye = -normalize(ecPosition3);
120 vec4 ambient = vec4(0.0);
121 vec4 diffuse = vec4(0.0);
122 vec4 specular = vec4(0.0);
123 vec3 color;
124
125 DirectionalLight(0, normalize(fragmentNormal), ambient, diffuse, specular);
126
127 color =
128 gl_FrontLightModelProduct.sceneColor.rgb +
129 ambient.rgb * gl_FrontMaterial.ambient.rgb +
130 diffuse.rgb * gl_Color.rgb +
131 specular.rgb * gl_FrontMaterial.specular.rgb;
132
133 gl_FragColor = vec4(color, gl_Color.a);
134 }
135
136Coin 3.1.3" coin_shaders(3Version)