Using Objects

There are two basic ways of using Objects to render some particular type of geometry:

Derived classes

QuadricObject is an example of a derived class that implements a particular type of geometry. In this case, it draws GLU quadrics.

The basic code that does this is in QuadricObject's draw() function, which, in an abbreviated form, looks like:

void QuadricObject::draw(void)
    {
    if (quadricType_ == DMS_GLU_SPHERE)
        gluSphere(quadric_, radius_, slices_, stacks_);
    else if (quadricType_ == DMS_GLU_CYLINDER)
        gluCylinder(quadric_, radius_, topRadius_, height_,
                    slices_, stacks_);
    else if (quadricType_ == DMS_GLU_DISK)
        gluDisk(quadric_, innerRadius_, radius_, slices_,
                rings_);
    else if (quadricType_ == DMS_GLU_PARTIAL_DISK)
        gluPartialDisk(quadric_, innerRadius_, radius_, slices_,
                       rings_, startAngle_, sweep_);
    }

The class also includes several functions to set the variables used in the draw function (and some additional features that were left out of the above copy).

Draw callbacks

The alternative to creating a whole new class for a new type of geometry is to define a function that renders it, and then assign that function to a generic Object.

For example, we could write a simple function to render a teapot:

void drawTeapot(dms::Object&,void *)
    {
    glutSolidTeapot(2.0);
    }

And then create an object, and make drawTeapot() its draw-callback function:

Object teapot;
teapot.setDrawCallback(drawTeapot);

Then, whenever teapot.draw() is called, the drawTeapot() will be called. Calling teapot.drawAll() will bind any material, texture, transparency, and transformation that has been assigned to the object, before calling drawTeapot().



next