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 programmable graphics pipeline stage that composes a shader program.

    There are five types of shader objects that can be added to a shader program. Any of these stages can be user defined.

    • Vertex shader
      The vertex shader is executed once for each vertex (usually in parallel). The main purpose is to transform each vertex's 3D position from model space to projection space. 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 defined as a patch to control the amount of tessellation applied to the patch. Following the execution of this shader, a tessellator 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.

    Note: Compute shaders are not part of the rendering pipeline. They represent a shader stage used entirely for computing arbitrary information.

    Shader object nodes cannot be inserted directly in a scene graph. They must be added to the field shaderObject 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 supported shading language of the program source is OpenGL Shader Language (GLSL) . Furthermore, the Open Inventor shader API or VolumeViz shader API must be used to write any GLSL shader program. See ShaderAPI for detail.

    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 GLSL source program. An SoShaderParameter1i must be used for each texture sampler in order to specify the texture unit and texture sampler uniform parameter name pair.

    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.


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

    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 );

    ShaderAPI, SoVertexShader, SoGeometryShader, SoFragmentShader, SoShaderProgram, SoShaderParameter, SoUniformShaderParameter, SoVertexShaderParameter, SoTessellationControlShader, SoTessellationEvaluationShader

    • Field Detail

      • isActive

        public final SoSFBool isActive
        Specifies if the shader object is active or not.
      • sourceType

        public final SoSFEnum<SoShaderObject.SourceTypes> sourceType
        Specifies the shader object's source type. The type of source can be either a filename containing the program (FILENAME), or a string containing the source program (GLSL_PROGRAM). Use enum SourceType. Default is FILENAME.

        NOTE: The source type must be specified before the source program (sourceProgram) is specified.

      • sourceProgram

        public final SoSFFilePathString sourceProgram
        Contains the shader object's source program, specified by a filename (sourceType set to FILENAME) or by the string containing the program (sourceType set to GLSL_PROGRAM). If the filename is not an absolute path name, the list of directories maintained by SoInput is searched. If the source program is not found in any of those directories, then the file is searched for relative to the directory from which the SoShaderObject node was read.

        NOTE: The source type (sourceType) must be specified before the source program is specified.