vtlib: Custom (Dynamic) Geometry

There are several reasons why you might want to draw your own geometry directly, without using the vtlib or OSG classes.

The vtDynGeom Class

vtDynGeom is a subclass of vtGeom which allows you to draw the geometry yourself.  All that is needed is to override three virtual methods.  The following example shows how this works.

class MyGeom : public vtDynGeom
{
public:
    // these are overrides for virtual methods
    void DoRender();
    void DoCalcBoundBox(FBox3 &box);
    void DoCull(FPoint3 &eyepos_ogl, IPoint2 window_size, float fov) {}
};

void MyGeom::DoRender()
{
    // an example: draw a red-green axis in X and Y
    glColor3f(1, 0, 0);
    glBegin(GL_LINES);
    glVertex3f(-1000,0,0);
    glVertex3f( 1000,0,0);
    glEnd();

    glColor3f(0, 1, 0);
    glBegin(GL_LINES);
    glVertex3f(0,-1000,0);
    glVertex3f(0, 1000,0);
    glEnd();
}

void MyGeom::DoCalcBoundBox(FBox3 &box)
{
    // provide the bounding box of your geometry
    box.min.Set(-1000, -1000, 0);
    box.max.Set(1000, 1000, 0);
}

Note that the Cull() method is a place to do culling within your geometry, so it is optional.  Your DoRender() method should be polite and return the OpenGL state as it was, which can be accomplished by appropriate use of OpenGL's Push and Pop methods.

The above code defines how the node will draw itself each frame.  All that remains is to create a node of this type and insert it in the scene graph, with code like this:

MyGeom *geom = new MyGeom();
vtGetScene()->GetRoot()->AddChild(geom);

That's it!