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