"Tutorial: Understanding the IndexedFaceSet (IFS) -
Putting it All Together"
>Page 1, 2
The geometry node which consists of an IndexFaceSet is the field of a Shape node. The entire shape node so far consists of the set of points, the coord node and the indecies into that set of points, the coordIndex. So the entire Shape node looks like:
Shape { geometry IndexedFaceSet { coord Coordinate { point [ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,2,0, 1,2,0 ] } coordIndex [ 0,1,2,3,-1 ] } }
All we actually need to add to create a valid VRML file is the correct header which is the line:
#VRML V2.0 utf8
This may all seem like a lot of trouble, and of course if all we wanted to do was create a single square, then yes it is silly. However with a few more additions we can start to create more complex shapes and structures. One key aspects is to be able to reuse all those coordinates. It is not unrealistic to see VRML files with thousands of points. The ability to reuse them is important and is accomplished via the DEF USE statements. DEF is a way of defining a Node, and USE is a way of using that node by creating a new instance of the data. We can easily create a new shape which uses different indecies in another Shape node. First a repeat of the original Shape node with the Coordinates DEFed.
Shape { geometry IndexedFaceSet { coord DEF MYPOINTS Coordinate { point [ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,2,0, 1,2,0 ] } coordIndex [ 0,1,2,3,-1 ] } }
Notice how the Coordinate node itself has been assigned the name "MYPOINTS".
Next we can use those same coordinates in another shape:
Shape { geometry IndexedFaceSet { coord USE MYPOINTS coordIndex [ 3,2,5,-1 ] }
This defines a triangle since we only specified the indecies for 3 points. We replce the Coordinate node definition with the "USE MYPOINTS" construct.
The entire file looks like:
#VRML V2.0 utf8 Shape { geometry IndexedFaceSet { coord DEF MYPOINTS Coordinate { point [ 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,2,0, 1,2,0 ] } coordIndex [ 0,1,2,3,-1 ] } } Shape { geometry IndexedFaceSet { coord USE MYPOINTS coordIndex [ 3,2,5,-1 ] } }
You can simply cut and paste the text from this article into a new text file and if you simply save it with a .wrl extension you'll be all set to display the VRML. Not all that exciting of a file! But we have hopefully illustrated how to create arbitrary shapes with VRML. In subsequent tutorials we'll extend these concepts illustrating more of the complexities of the IFS and how to make it look better.
It is also possible you might not see anything! Since we have not set any of the lighting, if the "headlight" of your browser is off, the polygon might not be visible. An easy way to fix this is to add the following as the first node of the file:
Background { skyColor [0, 0, 0.5] }
This will be specify a blue backgroud to the scene, making sure the polygon is visible. We will expand this example on occasion in the following few weeks until you are all set to tackle the world of IFS.
Previous page ISB
>Page 1, 2