Shaders in general
From SOFAWiki
Shaders
A complete set of tools about using shaders is implemented into SOFA. The three kinds of shaders (vertex and fragments (mandatory), geometry (optionally)) are available. Shader is used only for Visual Model as OglModel.
The effects of the shader is spread to the associated subtree. Finally, there is only one shader activated for each visual model : if two shaders are present in the same node, only the second will be effective.
To simply include a shader, add this into your node :
<OglShader vertFilename="test.vert'' fragFilename="test.frag'' />
vertFilename and fragFilename are the only mandatory parameters. Other optional parameters are about geometry shader : geoFilename, geometryInputType, geometryOutputType and geometryVerticesOut. A last parameter, turnOn, is for debugging purpose, when you want to disable shader without restarting the scene.
If you want to send values to uniform variables defined into the shader, a certain number of objects is available :
* OglIntVariable,OglInt{2,3,4}Variable : for int and ivec{2,3,4}
* OglFloatVariable,OglFloat{2,3,4}Variable : for float and vec{2,3,4}
* OglIntVectorVariable, OglIntVector{2,3,4}Variable : for arrays of int and ivec{2,3,4}
* OglFloatVectorVariable, OglFloatVector{2,3,4}Variable : for arrays of float and vec{2,3,4}
* OglMatrix{2,3,4}x{2,3,4} : for matrix n*m where n and m = {2,3,4}
Their parameters are id for their name into the shader and value(single type) or values (array type). Example :
<OglFloat3Variable id="fragmentColor" value="1.0 0.0 0.0" /> <OglFloatVariable id="fragmentOpacity" value="2.0"/> <OglFloatVector4Variable id="MappingTable" values="1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0" />
2D texture can be added with OglTexture2D object.Its parameters are id, texture2DFilename and textureUnit.
<OglTexture2D texture2DFilename="textures/lights4-small-noise.bmp" textureUnit="1" id="planeTexture" />
The last object about shaders is a partial support of macro processing in GLSL. It's possible to define macro variable if a part of code is enabled or not. For example, this can be very useful if there is a common code for two 3D objects, one with a texture, and the other with simple colors. You define the macro :
#define HAS_TEXTURE ...; //code about textured 3D object #else ...; //code about colored 3D object #endif
and put the following object into the scene file, in the same node as the OglShader used by the textured 3D object:
<OglShaderDefineMacro id="HAS_TEXTURE" />
