librtl

Polyhedrons

This example demonstrates rtl_polyhedron objects. A polyhedron is an object that is defined by a set of materials, vertices and triangles. Despite the name the mesh of triangles need not be closed. Polyhedrons also offer front and back materials. Intersection is accomplished through space subdivision of the object space for extremely efficient intersection with the mesh.

#include <rtl/rtl.h>

rtl_object* buildPolyhedron();

void main()
{
    rtl_initialize();
    
    rtl_world scene(45);
    jImage map(320,240);

    rtl_voxel::MaxDepth = 1;
    
    scene.lookat(jVec3(1,2,4),jVec3(0,0,0),jNorm3(0,1,0));
    
    scene.add(buildPolyhedron());
    scene.scale(5,1,5);
    scene.add(new rtl_floor());
    
    cerr.setf(ios::fixed);
    cerr.precision(3);

    scene.raytrace(map);

    jImage::writePPM(cout,map);

    rtl_terminate();
}

rtl_object* buildPolyhedron()
{
    rtl_material* Ma = new rtl_material(jColour::sea_green);
    rtl_material* Mb = new rtl_material(jColour::indian_red);

    rtl_polyhedron* P = new rtl_polyhedron(6,rtl_polyhedron::cw);
    
    P->add(Ma);
    P->add(Mb);

    P->addvertex(0,-1,0,0,0,0,0,0);
    P->addvertex(1,-1,1,0,0,0,0,0);
    P->addvertex(2,0,0,0,0,0,0,0);
    P->addvertex(3,0,1,0,0,0,1,0);
    P->addvertex(4,1,0,0,0,0,1,0);
    P->addvertex(5,1,1,0,0,0,1,0);
    
    P->setnormal(0,-1,0,1);
    P->setnormal(1,-1,0,1);
    P->setnormal(4,1,0,1);
    P->setnormal(5,1,0,1);

    P->add(new rtl_triangle(P,0,1,2));
    P->add(new rtl_triangle(P,2,1,3));
    P->add(new rtl_triangle(P,2,3,4));
    P->add(new rtl_triangle(P,3,5,4));
    P->add(new rtl_triangle(P,4,3,5));

    P->computenormals();

    return P;
}

Also provided is the output from the rendering of a model with over 9500 triangles. This image takes less than 20 seconds (Pentium 90) to generate.


JSP / librtl v0.2 mtigges@cpsc.ucalgary.ca