Keywords: main

Next: 3.19 Geometry Shaders Up: 3. Using and Writing Previous: 3.17 Output Shaders

# 3.18 Displacement Shaders

Displacement shaders are called during geometry tessellation if the material of a polygonal or free-form surface object specifies a `displace` shader. Whenever the tessellator introduces or copies a vertex, the displacement shader is called and expected to return a scalar value that tells the tessellator to move the vertex by this distance along its normal vector. If 0 is returned, the vertex remains unchanged. If the object specifies displacement approximations, curvature introduced by displacement can lead to further subdivision. Here is an example:

         #include <stdio.h>
         #include <mi/shader.h>

         struct mydisplace {
            miTag             tex;
            miScalar          factor;
         };

         int mydisplace_version(void) {return(1);}

         miBoolean mydisplace(
            miScalar          *result,
            miState           *state,
            struct mydisplace *paras)
         {
            miColor           color;

            mi_lookup_color_texture(&color, state,
                                    *mi_eval_tag(&paras->tex),
                                    &state->tex_list[0]);

            *result += (color.r + color.g + color.b) / 3 *
                                *mi_eval_scalar(&paras->factor);
            return(miTRUE);
         }

Note that the shader adds its displacement to the result instead of storing it. This allows chaining displacement shaders in shader lists. Shaders in shader lists get called in sequence, each adding its contribution to the result of the previous. mental ray calls the first displacement shader of the list with a result that is initialized to 0.

Displacement shaders may also change the vector state - > normal along which the displacement will take place, or change state - > point, which is the original vertex position that the displacement is added to. However, note that mental ray does not check that surfaces resulting from such arbitrary displacements are free of self-intersections.