Keywords: main

Next: 3.20 Contours Up: 3. Using and Writing Previous: 3.18 Displacement Shaders

# 3.19 Geometry Shaders

Geometry shaders are functions that procedurally create geometric objects. They are different from most of the shaders described here because they do not return a color, but a `miTag` reference to the object or instance group created by the shader. The geometry shader is called with a `miState` containing valid fields for camera, options, current shader, and version. The shader parameters are passed as the third argument. The geometry shader is expected to return the tag of the object or instance group it creates.

Here is a simple example for a geometry shader that creates a single triangle. A more robust shader would check the return values of all function calls made here. Geometry shaders use a special set of shader interface functions to create geometry. All these functions begin with mi_api_.

         #include <mi/shader.h>

         typedef struct {
            miTag      mtl;
         } GeoTriangle;

         static void
         add_vector(miScalar x, miScalar y, miScalar z)
         {
            miVector v;
            v.x = x; v.y = y; v.z = z;
            mi_api_vector_xyz_add(&v);
         }

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

         miBoolean geotriangle(
            miTag              *result,
            miState            *state,
            GeoTriangle        *paras)
         {
            miObject        *obj;

            obj = mi_api_object_begin(NULL);
            obj->visible = obj->shadow = obj->trace = miTRUE;
            mi_api_basis_list_clear();
            mi_api_object_group_begin(0.0);

            add_vector(-1., 0., 0.);
            add_vector( 1., 0., 0.);
            add_vector( 0., 0., 1.);

            mi_api_vertex_add(0);
            mi_api_vertex_add(1);
            mi_api_vertex_add(2);

            mi_api_poly_begin_tag(1, *mi_eval_tag(&paras->mtl));
            mi_api_poly_index_add(0);
            mi_api_poly_index_add(1);
            mi_api_poly_index_add(2);
            mi_api_poly_end();

            mi_api_object_group_end();
            return(mi_geoshader_add_result(result,
                                          mi_api_object_end()));
         }

In this example an unnamed object is created with the flags visible, shadow and trace set to `miTRUE`, a caustic mode set to 0, and a label 0. In the mandatory object group a triangle is constructed with API calls. The material of the triangle is assigned from the shader parameters. The object group definition is finalized with the polygonal information argument set to `miTRUE`.

Next: 3.20 Contours Up: 3. Using and Writing Previous: 3.18 Displacement Shaders