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