Class SoShaderProgram

  • All Implemented Interfaces:
    SafeDisposable
    Direct Known Subclasses:
    SoVolumeShader

    public class SoShaderProgram
    extends SoNode
    Shader program property node. This property node defines the complete shader program for all subsequent shapes. A complete shader program may contain vertex shaders (SoVertexShader), geometry shaders (SoGeometryShader) and/or fragment shaders (SoFragmentShaders). See SoShaderObject for information common to all these classes.

    With some languages such as NVIDIA Cg or assembly language (ARB_vertex_program/ARB_fragment_program), only one vertex and one fragment program can be active at the same time. The field shaderObject contains the vertex, geometry and fragment shader objects which form the shader program. Each shader object should be of type SoShaderObject. Modifications that occur in shader objects are dynamically detected and the state is updated.

    Uniform parameters may be set for each shader object. 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.

    Vertex parameters may be set for a vertex shader object. Vertex parameters 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.

    SoShaderProgram has specialized subclasses for volume visualization. If you are using the VolumeViz extension, see SoVolumeShader and SoVolumeRenderingQuality nodes.

    Notes:

    • For geometry shaders: the EXT_geometry_shader4 OpenGL extension must be supported by your graphics board.
    • For tesselation shaders: the GL_ARB_tessellation_shader OpenGL extensions must be supported by your graphics board.
    • Since Open Inventor 10.0, when writing geometry shaders, the input and output primitive types must be specified in the shader code. See https://developer.openinventor.com/index.php/general-documentation/how-to-write-shaders-with-oiv-10/#geomshader for more information.
    • Transparency and fast editing are compatible with shader modified geometry, but most actions use the vertices of the basic shape (the vertices stored in the scene graph). This includes, for example, SoGetBoundingBoxAction, SoGetPrimitiveCountAction and SoRayPickAction.

    Tips:

    • To see the output from the GLSL compiler/linker, set the environment variable OIV_GLSL_DEBUG.
    • 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 the shader source code without needing to restart your application after each shader modification.
    • If your shader makes geometry transparent (sets alpha values < 1), you should set the generateTransparency field to true. This ensures that Open Inventor knows to apply the correct handling for the current transparency mode.
    • Since the GLSL specification doesn't currently provide a "#include" directive, Open Inventor provides this service through a comment directive. This provides greater flexibility in implementing complex GLSL shaders. Included files are loaded using SoInput and respect the same search path order. For example:

       //!oiv_include <MyShaderDirectory/common.h>

      File format/default:

    SoShaderProgram {

    }

    EXAMPLE

    Shader program with one 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();
     parameter.name.setValue( "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 );
     root.addChild(shaderProgram);

    Action behavior:

    SoGLRenderAction
    Sets the Open Inventor state with the active shader program. Sets: SoGLShaderProgramElement

    See Also:
    SoFragmentShader, SoGeometryShader, SoShaderObject, SoTessellationControlShader, SoTessellationEvaluationShader, SoUniformShaderParameter, SoVertexShader, SoVertexShaderParameter
    • Field Detail

      • shaderObject

        public final SoMFNode shaderObject
        Specifies the list of shader objects (i.e., vertex shaders, geometry and fragment shaders) which form the shader program. Be careful, with some languages (CG_PROGRAM or ARB_PROGRAM), only one vertex program and one fragment program can be active at the same time. In this case, only the first vertex shader and the first fragment shader are used.
      • vertexProgramTwoSide

        public final SoSFBool vertexProgramTwoSide
        If set to true, vertex shaders will operate in two-sided color mode. Default is false.

        Since:
        Open Inventor 7.2

      • shadowShader

        public final SoSFBool shadowShader
        Only used when an SoShadowGroup is active. Default is false. If set to false, a default shader will be used during the shadowmap generation pass. If true, the shader will be used as is and must handle the shadowmap pass correctly:
        • If the uniform OivShadowPass is true, call OivGenerateShadowMap() and output nothing into gl_FragColor, but discard fragments if needed.
        • If the uniform OivShadowPass is false, follow the normal render path If the shader doesn't modify the depth (with a discard or an alpha test), Open Inventor will handle the shadowmap generation pass automatically. Default is false
          See also: SoShadowGroup

        Since:
        Open Inventor 8.1

    • maxGeometryOutputVertices

      public final SoSFInt32 maxGeometryOutputVertices
      Set the maximum number of vertices the geometry shader will emit in one invocation. Default is -1 which means it is set to the hardware limit.

      Since:
      Open Inventor 8.1

    • generateTransparency

      public final SoSFBool generateTransparency
      If set to true, then shapes affected by this shader will be considered transparent. Otherwise, the shape transparency is deducted from the state.

      This allows Open Inventor to apply the correct handling for the current transparency mode.

      Default is false.

      Since:
      Open Inventor 9.0

    • patchLength

      public final SoSFInt32 patchLength
      Set the length of the fixed-size collection of vertices used by tessellation shaders. Default is 0.

      Since:
      Open Inventor 9.3

    • images

      public final SoMFNode images
      Specifies a list of SoShaderParameterImage nodes to use with this shader. Default is empty.

      Since:
      Open Inventor 10.10

  • Constructor Detail

    • SoShaderProgram

      public SoShaderProgram()
      Constructor.
  • Method Detail

    • setFragmentShader

      public SoFragmentShader setFragmentShader​(int pos,
                                                java.lang.String filenameOrSource)
      Calls setFragmentShader(pos, filenameOrSource, SoShaderObject.SourceTypes.valueOf( SoShaderObject.SourceTypes.FILENAME.getValue() )).
    • setVertexShader

      public SoVertexShader setVertexShader​(int pos,
                                            java.lang.String filenameOrSource)
      Calls setVertexShader(pos, filenameOrSource, SoShaderObject.SourceTypes.valueOf( SoShaderObject.SourceTypes.FILENAME.getValue() )).
    • setTessellationControlShader

      public SoTessellationControlShader setTessellationControlShader​(int pos,
                                                                      java.lang.String filenameOrSource)
      Calls setTessellationControlShader(pos, filenameOrSource, SoShaderObject.SourceTypes.valueOf( SoShaderObject.SourceTypes.FILENAME.getValue() )).
    • setTessellationEvaluationShader

      public SoTessellationEvaluationShader setTessellationEvaluationShader​(int pos,
                                                                            java.lang.String filenameOrSource)
      Calls setTessellationEvaluationShader(pos, filenameOrSource, SoShaderObject.SourceTypes.valueOf( SoShaderObject.SourceTypes.FILENAME.getValue() )).
    • setGeometryShader

      public SoGeometryShader setGeometryShader​(int pos,
                                                java.lang.String filenameOrSource)
      Calls setGeometryShader(pos, filenameOrSource, SoShaderObject.SourceTypes.valueOf( SoShaderObject.SourceTypes.FILENAME.getValue() )).
    • setComputeShader

      public SoComputeShader setComputeShader​(int pos,
                                              java.lang.String filenameOrSource)
      Calls setComputeShader(pos, filenameOrSource, SoShaderObject.SourceTypes.valueOf( SoShaderObject.SourceTypes.FILENAME.getValue() )).
    • getNumReservedTextures

      public static int getNumReservedTextures()
      Returns the number of reserved texture units.
    • getTessellationEvaluationShader

      public SoTessellationEvaluationShader getTessellationEvaluationShader​(int pos)
      Returns the tessellation evaluation shader at the specified position.
    • getTessellationControlShader

      public SoTessellationControlShader getTessellationControlShader​(int pos)
      Returns the tessellation control shader at the specified position.
    • setComputeShader

      public SoComputeShader setComputeShader​(int pos,
                                              java.lang.String filenameOrSource,
                                              SoShaderObject.SourceTypes sourceType)
      Convenience method to create a compute shader with the specified filename and add it at the specified position. Return value is the new compute shader.
    • setTessellationControlShader

      public SoTessellationControlShader setTessellationControlShader​(int pos,
                                                                      java.lang.String filenameOrSource,
                                                                      SoShaderObject.SourceTypes sourceType)
      Convenience method to create a tessellation control shader with the specified filename and add it at the specified position. Return value is the new tessellation control shader.
    • setTessellationEvaluationShader

      public SoTessellationEvaluationShader setTessellationEvaluationShader​(int pos,
                                                                            java.lang.String filenameOrSource,
                                                                            SoShaderObject.SourceTypes sourceType)
      Convenience method to create a tessellation evaluation shader with the specified filename and add it at the specified position. Return value is the new tessellation evaluation shader.
    • setGeometryShader

      public SoGeometryShader setGeometryShader​(int pos,
                                                java.lang.String filenameOrSource,
                                                SoShaderObject.SourceTypes sourceType)
      Convenience method to create a geometry shader with the specified filename and add it at the specified position. Return value is the new geometry shader.
    • getVertexShader

      public SoVertexShader getVertexShader​(int pos)
      Returns the vertex shader at the specified position.
    • getFragmentShader

      public SoFragmentShader getFragmentShader​(int pos)
      Returns the fragment shader at the specified position.
    • getGeometryShader

      public SoGeometryShader getGeometryShader​(int pos)
      Returns the geometry shader at the specified position.
    • setVertexShader

      public SoVertexShader setVertexShader​(int pos,
                                            java.lang.String filenameOrSource,
                                            SoShaderObject.SourceTypes sourceType)
      Convenience method to create a vertex shader with the specified filename and add it at the specified position. Return value is the new vertex shader.
    • setFragmentShader

      public SoFragmentShader setFragmentShader​(int pos,
                                                java.lang.String filenameOrSource,
                                                SoShaderObject.SourceTypes sourceType)
      Convenience method to create a fragment shader with the specified filename and add it at the specified position. Return value is the new fragment shader.