Abstract node class which defines a shader object. More...
#include <Inventor/nodes/SoShaderObject.h>
Public Types | |
enum | SourceType { ARB_PROGRAM, CG_PROGRAM, GLSL_PROGRAM, FILENAME } |
enum | ShaderType { VERTEX_SHADER, GEOMETRY_SHADER, FRAGMENT_SHADER, TESSELLATION_CONTROL_SHADER, TESSELLATION_EVALUATION_SHADER } |
Public Member Functions | |
virtual SoType | getTypeId () const |
template<typename UniformParamType , typename ParamValueType > | |
UniformParamType * | addShaderParameter (const SbString &name, ParamValueType val) |
virtual ShaderType | getShaderType () const =0 |
template<typename UniformParamType , typename ParamValueType > | |
void | setShaderParameter (const SbString &name, ParamValueType val) |
Static Public Member Functions | |
static SoType | getClassTypeId () |
Public Attributes | |
SoSFBool | isActive |
SoSFEnum | sourceType |
SoSFFilePathString | sourceProgram |
SoMFUniformShaderParameter | parameter |
Friends | |
class | SoShaderProgram |
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.
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:
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.
Tips:
NOTE 1: In case of Cg, the default profile used for the vertex shader is arbvp1, and arbfp1 for a fragment shader. The environment variables OIV_CG_VTX_PROFILE and OIV_CG_FRAG_PROFILE enable you to change the vertex and fragment profiles respectively. 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.
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 ...
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.
// First load the fragment shader code SoFragmentShader* fragmentShader = new SoFragmentShader(); fragmentShader->sourceProgram = "filename.glsl"; // Set a shader parameter // The addShaderParameter1i method is equivalent to: // SoShaderParameter1i *parameter = new SoShaderParameter1i; // parameter->name = "data1"; // parameter->value = 1; // fragmentShader->parameter.set1Value(0, parameter); fragmentShader->addShaderParameter1i( "data1", 1 ); // Associate fragment shader with a shader program node SoShaderProgram* shaderProgram = new SoShaderProgram(); shaderProgram->shaderObject.set1Value(0, fragmentShader); root->addChild( shaderProgram );
SoVertexShader, SoGeometryShader, SoFragmentShader, SoShaderProgram, SoShaderParameter, SoUniformShaderParameter, SoVertexShaderParameter, SoTessellationControlShader, SoTessellationEvaluationShader
ShadersBrowser, AnimatedFlag, GeometryShader, PixelLighting, shadowShader, TessellationShader, ToonShading
Type of the shader.
UniformParamType * SoShaderObject::addShaderParameter | ( | const SbString & | name, | |
ParamValueType | val | |||
) | [inline] |
Convenience method to create an SoShaderUniformParameter with the specified name and value and add it to the given shader object.
UniformParamType is the type of parameter to add. This function also generates the following helper methods not visible in the documentation:
These are template methods, so ParamValueType must have an operator= compatible with the field value of the corresponding SoShaderParameter (for example, addShaderParameter2f can take an SbVec2f as ParamValueType)
static SoType SoShaderObject::getClassTypeId | ( | ) | [static] |
Returns the type identifier for this class.
Reimplemented from SoNode.
Reimplemented in SoFragmentShader, SoGeometryShader, SoTessellationControlShader, SoTessellationEvaluationShader, and SoVertexShader.
virtual ShaderType SoShaderObject::getShaderType | ( | ) | const [pure virtual] |
Must be redefined by derived class.
Implemented in SoFragmentShader, SoGeometryShader, SoTessellationControlShader, SoTessellationEvaluationShader, and SoVertexShader.
virtual SoType SoShaderObject::getTypeId | ( | ) | const [virtual] |
Returns the type identifier for this specific instance.
Reimplemented from SoNode.
Reimplemented in SoFragmentShader, SoGeometryShader, SoTessellationControlShader, SoTessellationEvaluationShader, and SoVertexShader.
void SoShaderObject::setShaderParameter | ( | const SbString & | name, | |
ParamValueType | val | |||
) | [inline] |
UniformParamType is the type of parameter to set.
This function also generates the following helper methods not visible in the documentation:
These are template methods, so ParamValueType must have an operator= compatible with the field value of the corresponding SoShaderParameter (for example, setShaderParameter2f can take an SbVec2f as ParamValueType).
friend class SoShaderProgram [friend] |
Specifies if the shader object is active or not.
Contains the shader's uniform parameters.
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 ARB_PROGRAM, CG_PROGRAM, or 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.
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 (ARB_PROGRAM, CG_PROGRAM, or GLSL_PROGRAM). Use enum SourceType. Defaule is FILENAME.
NOTE: The source type must be specified before the source program (sourceProgram) is specified.