coin_shaders(3)

πŸ“¦ version 2.5.0, in fedora 43, fedora 44

✨ NAME ✨

coin_shaders β€” shaders in Coin, bestie. let's get into it πŸ’…


okay so Coin 2.5 added support for shaders, and the main characters of this whole situation are SoShaderProgram, SoVertexShader, SoFragmentShader, and SoGeometryShader. a typical scene graph with shaders looks something like this, no cap:

Separator {
  ShaderProgram {
    shaderObject [
      VertexShader {
        sourceProgram "myvertexshader.glsl"
        parameter [
          ShaderParameter1f { name "myvertexparam" value 1.0 }
        ]
      }
      FragmentShader {
        sourceProgram "myfragmentshader.glsl"
        parameter [
          ShaderParameter1f { name "myfragmentparam" value 2.0 }
        ]
      }
    ]
  }
  Cube { }
}

so this will render the Cube with the vertex and fragment shaders specified in myvertexshader.glsl and myfragmentshader.glsl. it's giving glow up, but for geometry ✨

Coin also supports ARB shaders and Cg shaders (if the Cg library is installed). however β€” and this is the part where i need you to listen β€” we recommend using GLSL, because we will focus mostly on supporting this shader language. periodt. πŸ‘‘


🌸 the named parameters 🌸

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. she does the work for you, we just vibe 🫢

  • 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.

πŸ”₯ the example (per-fragment OpenGL Phong lighting) πŸ”₯

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. iykyk 😌

πŸŽ€ The iv-file:

Separator {
  ShaderProgram {
    shaderObject [
      VertexShader {
        sourceProgram "perpixel_vertex.glsl"
      }
      FragmentShader {
        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 }
  Cylinder { }
}

πŸ¦‹ The vertex shader (perpixel_vertex.glsl):

varying vec3 ecPosition3;
varying vec3 fragmentNormal;

void main(void)
{
  vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
  ecPosition3 = ecPosition.xyz / ecPosition.w;
  fragmentNormal = normalize(gl_NormalMatrix * gl_Normal);

  gl_Position = ftransform();
  gl_FrontColor = gl_Color;
}

πŸ’œ The fragment shader (perpixel_vertex.glsl):

not me noticing the original literally says perpixel_vertex.glsl for the fragment shader too β€” i'm keeping it exactly as written, bestie, typos are canon πŸ’€
varying vec3 ecPosition3;
varying vec3 fragmentNormal;

void DirectionalLight(in int i,
                      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);
}

and that's the whole page, gorgeous 🌈 this was Coin, Version 2.5.0, page coin_shaders(3) β€” mwah, go render something iconic πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from