Using Polyhedra to Teach OOP
|
|
In Chapter 4, we will implement scaling and translation, as additional methods in the Polyhedron and MatrixOps classes (see Chapter 1). |
The VRML viewer expects a text file, just like Povray does, but wrl format is significantly different from the pov format. For this demo, we use the IndexedLineSet feature to implement a simple wrl file with a structure very similar to the Points and Edges tables internal to each polyhedron object. First, all n points associated with a shape get listed in xyz format. Next, a list of edges refering to these points by number, using 0 (first point) through n-1 (last point), defines the wireframe mesh.
|
|
The WriteVRML class uses its startdata() method to dump the Points table in record number order. By the time startdata() is finished, our wrl file looks like this: #VRML V1.0 ascii DEF BackgroundColor Info { string "0.0 0.0 1.0" } Material { diffuseColor 1 0 0} #end Material set to blue Coordinate3 { point [ 0.7071068 0.7071068 0.7071068, -0.7071068 -0.7071068 0.7071068, -0.7071068 0.7071068 -0.7071068, 0.7071068 -0.7071068 -0.7071068, ] } IndexedLineSet { coordIndex [ Then the rest of writeoutput(), a method inherited from the WritePoly superclass, invokes writeEdge() repeatedly to output edges. The writeEdge() method gets Point table record numbers passed as r1 and r2 to synchronize its node references with the above point set: procedure writeEdge(a,b,c,d,e,f,r1,r2) =fputs(this.hnd,space(10)+str(r1-1,3)+", "+str(r2-1,3)+ ", -1,") return endproc Since the WriteVRML subclass has its own version of thewriteEdge() and writePoint() methods, it ignores the superclass versions. This is how inheritance works: if a subclass has its own version of a method, execute it here and now -- otherwise pass the buck up the chain of command, until some superclass method by the right name is found. The same goes for properties: if a property isn't mentioned by name in a subclass definition, browse the family tree to discover where it appears in an object's lineage -- as defined by the class hierarchy. By the time the writeoutput() method is finished, we have the balance of our wrl file, ready for loading in a VRML viewer, such as CosmoPlayer, a free plug-in for the Netscape web browser: 0, 1, -1, 0, 2, -1, 0, 3, -1, 1, 2, -1, 1, 3, -1, 2, 3, -1, ] } Note: The -1s occuring after each edge (or node-pair) function as "line breaks", telling the viewer to consider the next node the start of a new edge, and not a continuation of a "connect the dots" series. |
An alternative and more elaborate VRML view of our polyhedra would depend on cylinders for edges, more like Povray. However, VRML cylinders always show up at the origin, centered along the X axis, and would need to be rotated and translated into place to serve as a shape's edges -- more operations than Povray requires. Also, if we wanted to get into the facial aspects of shapes, we could use the IndexedFaceSet feature, with applied textures and colors. The most straightforward implementation of a facial view would probably involve supplementing the internal Points and Edges tables with a Faces table, containing sets of edges around each opening grouped by FaceID. |
click for VRML view (if plug-in installed) |
Rick Bono provides this face-based view as the VRML default for his DOME program (freeware), which likewise outputs both Povray and VRML scripts (among others). DOME also generates buckminsterfullerene cages as VRML wireframes. On-line Resources:
Return to Symbols and Operators
|