Class SoShaderObject

  • All Implemented Interfaces:
    Direct Known Subclasses:
    SoComputeShader, SoFragmentShader, SoGeometryShader, SoTessellationControlShader, SoTessellationEvaluationShader, SoVertexShader

    public abstract class SoShaderObject
    extends SoNode
    Abstract node class which defines a shader object. This abstract class is the parent of classes that define a vertex shader (SoVertexShader), a geometry shader (SoGeometryShader), a fragment shader (SoFragmentShader), a tessellation control shader (SoTessellationControlShader) or a tessellation evaluation shader (SoTessellationEvaluationShader) program. (Tessellation shaders are supported since Open Inventor 9.3)

    There are five types of shaders that may be executed (in this order) in the rendering pipeline. Any one or all of these stages may be replaced by an application defined shader program.

    • Vertex shader
      The vertex shader is executed once for each vertex (usually in parallel). The purpose is to transform each vertex's 3D position in virtual space to the 2D coordinate at which it appears on the screen (as well as a depth value for the Z-buffer). Vertex shaders can manipulate properties such as position, color and texture coordinate, but cannot create new vertices.
    • Tessellation control shader
      This shader accepts a list of vertices defining a patch from the vertex shader and controls the amount of tessellation applied to the patch. Following execution of this shader, a fixed tesselator computes a set of triangles in a parametric space.
    • Tessellation evaluation shader
      This shader is executed at least once for each vertex that was created by the tesselator in the parametric space. The TES takes the parametric coordinate and the patch data output by the TCS to generate a final position for the surface.
    • Geometry shader
      The geometry shader acts on a complete primitive (triangle or line): it can modify existing primitives, it can insert (create) new primitives, it can remove (destroy) existing primitives.
    • Fragment shader
      Fragment shaders compute color and other attributes of each fragment.

    Shader object nodes cannot be inserted directly in a scene graph. They must be added to the shaderObject field of an SoShaderProgram node.

    A shader object is defined by the following properties:

    • Source program, which is the shader's source code (see sourceProgram field),
    • Uniform parameters set by the application (see parameter field),
    • State (active or not) (see isActive field).

    The source program can be specified either by a string containing the program source code, or by a filename which contains the program source code. How the sourceProgram field is interpreted depends on the field sourceType. The shading languages accepted for the source program are OpenGL Shader Language (GLSL) , Cg from NVIDIA (see NOTE 1) and assembly language ( ARB_vertex_program, ARB_fragment_program). Generally GLSL is recommended because it works on any OpenGL hardware and is much higher level than the ARB commands.

    Uniform parameters can be set through the parameter field. Uniform means, in the case of a vertex or geometry program, a value which is the same for all vertices in a primitive, and, in the case of a fragment program, a value which is the same for all fragments created by a primitive. Each uniform parameter is represented by an instance of a specific subclass of SoUniformShaderParameter. For example, an SoShaderParameter1i holds a single integer value. A uniform parameter has no effect if it is not valid, that is, if there is no corresponding name (identifier) in the CG/GLSL (ARB) source program. See NOTE 2 for info on retrieving a texture sampler uniform parameter within a GLSL program, an NVIDIA Cg fragment program, or an ARB_vertex_program/ARB_fragment_program program.

    A vertex shader can also use vertex parameters, which are per-vertex data passed from the application to the vertex shader. Vertex parameters are represented by an instance of a specific subclass of SoVertexShaderParameter. For example, an SoVertexShaderParameter1f holds a set of floating point values and an SoVertexShaderParameter3f holds a set of SbVec3f values. Vertex parameter nodes are property nodes (similar to materials or normals) and should be added directly in the scene graph, not in the shader object.


    • When using GLSL shaders, set the environment variable OIV_GLSL_DEBUG to get the GLSL compile/link output in the console.
    • If you set the environment variable OIV_SHADER_CHECK_INTERVAL, the shader source file is checked for a change every n seconds, where n is the value specified by the variable. This allows you to edit a shader source file without needing to restart your application after each shader modification.

    NOTE 1: In case of Cg, the default profile used for the vertex shader is arbvp1, and arbfp1 for a fragment shader. However, we advise users to use the default profile because it enables you to retrieve the OpenGL state directly in your vertex/fragment program instead of passing the OpenGL state as uniform parameters, which could be inefficient in terms of performance.

    NOTE 2: With NVIDIA Cg and ARB_vertex_program/ARB_fragment_program, a texture sampler can be retrieved in your fragment program without specifying any SoShaderParameter parameter.

    • With ARB_vertex_program/ARB_fragment_program, texture[i] corresponds to the texture sampler of unit i.
    • With NVIDIA Cg, the first texture sampler parameter in your fragment program corresponds to the texture sampler of the texture unit 0, the second texture sampler parameter to the texture sampler of the texture unit 1, and so on.
       void  main(// Inputs
                  Input IN,
                  uniform sampler2D rampDiffuse,   // Texture sampler 2D of the texture unit 0
                  uniform sampler2D rampSpecular,  // Texture sampler 2D of the texture unit 1
                  uniform sampler2D rampEdge,      // Texture sampler 2D of the texture unit 2
    • With GLSL, an SoShaderParameter1i must used for each texture sampler in order to specify the texture unit and texture sampler uniform parameter name pair.

    NOTE 3: With Cg and ARB languages, at least the ARB_vertex_program and ARB_fragment_program, and with GLSL, at least GL_ARB_vertex_shader, GL_ARB_fragment_shader, and GL_ARB_shader_objects OpenGL extensions must be supported by your graphics board in order to be able to define a vertex shader and a fragment shader respectively. Otherwise no shader program will be executed.

    NOTE 4: You should keep in mind that vertex and fragment programs modify the standard OpenGL pipeline.

    • Vertex programs replace the following parts of the OpenGL graphics pipeline:
      • Vertex transformation,
      • Normal transformation normalization and rescaling,
      • Lighting,
      • Color material application,
      • Clamping of colors,
      • Texture coordinate generation,
      • Texture coordinate transformation.
    • But do not replace:
      • Perspective divide and viewport mapping,
      • Frustum and user clipping,
      • Backface culling,
      • Primitive assembly,
      • Two sided lighting selection,
      • Polygon offset,
      • Polygon mode.
    • Fragment programs replace the following parts of the OpenGL graphics pipeline:
      • Operations on interpolated values,
      • Pixel zoom,
      • Texture access,
      • Scale and bias,
      • Texture application,
      • Color table lookup,
      • Fog convolution,
      • Color sum,
      • Color matrix.
    • But do not replace:
      • Shading model,
      • Histogram,
      • Coverage,
      • Minmax,
      • Pixel ownership test,
      • Pixel packing and unpacking,
      • Scissor,
      • Stipple,
      • Alpha test,
      • Depth test,
      • Stencil test,
      • Alpha blending,
      • Logical ops,
      • Dithering,
      • Plane masking.

    File format/default:

    This is an abstract class. See the reference page of a derived class for the format and default values.


    Simple fragment shader with one uniform parameter:

     // Simple fragment shader with one uniform parameter
     // First load the fragment shader code
     SoFragmentShader fragmentShader = new SoFragmentShader();
     fragmentShader.sourceProgram.setValue( "filename.glsl" );
     // Set the shader parameter
     SoShaderParameter1i parameter = new SoShaderParameter1i(); "data1" );
     parameter.value.setValue( 1 );
     fragmentShader.parameter.set1Value( 0, parameter );
     // Associate fragment shader with a shader program node
     SoShaderProgram shaderProgram = new SoShaderProgram();
     shaderProgram.shaderObject.set1Value( 0, fragmentShader );

    See Also:
    SoVertexShader, SoGeometryShader, SoFragmentShader, SoShaderProgram, SoShaderParameter, SoUniformShaderParameter, SoVertexShaderParameter, SoTessellationControlShader, SoTessellationEvaluationShader