21.5. Shader API

21.5 Shader API

It is recommended in the fragment shader to use depth-peeling helper functions to test fragment coordinates and output fragment color. These functions are avaible whatever the type of transparency is. It is mandatory to use these functions if you want to benefit from advanced Open Inventor features such as order independant transparency.

You must add the appropriated header file at the beginning of the fragment shader file to use these functions such as:

//!oiv_include < Inventor/oivDepthPeeling_frag.h >

When using multi-pass order independant transparency, a fragment can be unsignificant for a given pass and significant for the next one. In order to know if the current fragment is relevant for the current pass, the following function should be used such as:

bool OivDepthPeel(vec3 fragmentCoords);

with fragmentCoords the fragment coordinates in screen space (e.g. gl_FragCoord.xyz should be used most of the time). This method returns true if the fragment is significant, false otherwise.

To output the fragment color, the following function must be used:

void OivDepthPeelingOutputColor(vec4 color);

with color, the color of the fragment.

When using instancing capabilities in Open Inventor (see SoMultipleInstance( C++ | Java | .NET )), functions are defined to query instance parameters. You must add the appropriated header file at the beginning of the vertex shader file to use these functions such as:

//!oiv_include < Inventor/oivShapeInstanceMatrix.h >

Instancing helper functions are:

int OivGetShapeInstanceID();

returns the current instanceID.

vec3 OivGetShapeInstanceTranslation();

returns the translation vector of the current geometry instance.

vec3 OivGetShapeInstanceScale();

returns the scale vector of the current geometry instance.

vec4 OivGetShapeInstanceRotation();

returns the rotation (i.e. a quaternion) of the current geometry instance.

mat4 OivGetShapeInstanceMatrix();

returns the model matrix to apply to the current geometry instance (i.e. the composition of rotation, translation and scale transformations).

mat4 OivGetModelViewMatrix();

returns the model matrix to apply to the current geometry vertex. This matrix contains instance and local transformations.

mat3 OivGetNormalMatrix();

returns the normal matrix to apply to the current geometry normal. This matrix takes into account instance and local transformations.

mat3 OivGetNormalMatrix( in mat4 modelViewMatrix );

returns the normal matrix to apply to the current geometry normal, with precomputed model view matrix as parameter.

Algebraic shapes (see SoAlgebraicShape( C++ | Java | .NET ) and derivated classes) provide a shader API to define types and helper functions. You must add the appropriated header file at the beginning of the shader file such as:

//!oiv_include < Inventor/oivAlgebraicShape.h >

Types used by helper functions are:

struct OivASRay
{
  vec3 rs; // ray start
  vec3 re; // ray end
  vec3 rd; // ray direction
};

and,

struct OivASPoint
{
  vec3 position;
  vec3 normal;
  vec4 color;
};

Helper functions are defined to compute intersection points such as:

bool OivASSolveQuadric ( in vec3 abc, inout vec2 roots );

to solve quadric of type axx + bx + c = 0* with coefficients a, b and c packed in the 3D vector abc. It returns true if there are roots, stored in the roots vector. Note that it can be used in fragment shader only.

bool OivASRayPlaneIntersection ( in OivASRay ray, in vec4 p, inout float t );

to test if a ray (i.e. parameter ray) intersects a plane defined by vector p such as xyz represents the orientation and w the distance from origin (i.e. the point p.wp.xyz lies on the plane). It returns true* is there is an intersection at t (i.e. the intersection point is ray.rs + t*ray.rd).

vec4 OivASGetColor ();

to get the color (i.e. from material or vertex color) of the shape.

float OivASGetOpacity ();

to get the opacity (i.e. alpha component) of the shape.

bool OivASIsOpaque ();

to ask if the shape is opaque (i.e. OivASGetOpacity() == 1.0).