1coin_shaders_page(3) Coin coin_shaders_page(3)
2
3
4
6 coin_shaders_page - 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 • coin_two_sided_lighting - Set to 1 for two-sided, 0 for normal
48 Example scene graph that renders per fragment OpenGL Phong lighting for
49 one light source. The shaders assume the first light source is a
50 directional light. This is the case if you open the file in a standard
51 examiner viewer.
52 The iv-file:
53 Separator {
54 ShaderProgram {
55 shaderObject [
56 VertexShader {
57 sourceProgram "perpixel_vertex.glsl"
58 }
59 FragmentShader {
60 sourceProgram "perpixel_fragment.glsl"
61 }
62 ]
63 }
64 Complexity { value 1.0 }
65 Material { diffuseColor 1 0 0 specularColor 1 1 1 shininess 0.9 }
66 Sphere { }
67
68 Translation { translation 3 0 0 }
69 Material { diffuseColor 0 1 0 specularColor 1 1 1 shininess 0.9 }
70 Cone { }
71
72 Translation { translation 3 0 0 }
73 Material { diffuseColor 0.8 0.4 0.1 specularColor 1 1 1 shininess 0.9 }
74 Cylinder { }
75 }
76
77 The vertex shader (perpixel_vertex.glsl):
78 varying vec3 ecPosition3;
79 varying vec3 fragmentNormal;
80
81 void main(void)
82 {
83 vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
84 ecPosition3 = ecPosition.xyz / ecPosition.w;
85 fragmentNormal = normalize(gl_NormalMatrix * gl_Normal);
86
87 gl_Position = ftransform();
88 gl_FrontColor = gl_Color;
89 }
90
91 The fragment shader (perpixel_fragment.glsl):
92 varying vec3 ecPosition3;
93 varying vec3 fragmentNormal;
94
95 void DirectionalLight(in int i,
96 in vec3 normal,
97 inout vec4 ambient,
98 inout vec4 diffuse,
99 inout vec4 specular)
100 {
101 float nDotVP; // normal . light direction
102 float nDotHV; // normal . light half vector
103 float pf; // power factor
104
105 nDotVP = max(0.0, dot(normal, normalize(vec3(gl_LightSource[i].position))));
106 nDotHV = max(0.0, dot(normal, vec3(gl_LightSource[i].halfVector)));
107
108 if (nDotVP == 0.0)
109 pf = 0.0;
110 else
111 pf = pow(nDotHV, gl_FrontMaterial.shininess);
112
113 ambient += gl_LightSource[i].ambient;
114 diffuse += gl_LightSource[i].diffuse * nDotVP;
115 specular += gl_LightSource[i].specular * pf;
116 }
117
118 void main(void)
119 {
120 vec3 eye = -normalize(ecPosition3);
121 vec4 ambient = vec4(0.0);
122 vec4 diffuse = vec4(0.0);
123 vec4 specular = vec4(0.0);
124 vec3 color;
125
126 DirectionalLight(0, normalize(fragmentNormal), ambient, diffuse, specular);
127
128 color =
129 gl_FrontLightModelProduct.sceneColor.rgb +
130 ambient.rgb * gl_FrontMaterial.ambient.rgb +
131 diffuse.rgb * gl_Color.rgb +
132 specular.rgb * gl_FrontMaterial.specular.rgb;
133
134 gl_FragColor = vec4(color, gl_Color.a);
135 }
136
137Version 4.0.1 Mon Nov 20 2023 coin_shaders_page(3)