A simple scene
This is a simple scene to illustrate the use of the more common nodes in X3D.
- using the H3D doxygen documentation
- grouping nodes
- shapes and geometry
- appearance, material, texture, surface
- DEF and USE keywords
In an X3D file encoded in XML format, nodes are represented with tags enclosed in angle brackets, and fields are written as elements within a tag. You could see this is the example code - nodes are highlighted bold and black, fields are blue and field values are red.
<Scene> <Transform translation="0.2 0 0"> <Shape> <Appearance> <Material diffuseColor="0.5 0.5 0.8" /> <FrictionalSurface /> </Appearance> <Sphere radius="0.01"/> </Shape> <Shape DEF="S"> <Appearance> <Material diffuseColor="0.7 0.2 0.2" /> <FrictionalSurface /> </Appearance> <IndexedFaceSet coordIndex="3 4 5 -1 0 1 2 3"> <Coordinate point="-0.1 0 0, 0 0 0, 0 0.1 0, -0.1 0.1 0, -0.1 0.15 0, -0.15 0.1 0" /> </IndexedFaceSet> </Shape> </Transform> <Shape USE="S" /> </Scene>
The example above begins with the Scene node, which is the topmost node that takes care of haptics and graphics rendering of the scene graph. A strict X3D encoding would have required proper header tags in the file but H3D des not impose this. However, an H3D scene must only have one root node, in this case, it is the Scene node. Any grouping node (which we will get to in a while) may also be used as the root node.
The Scene node above is followed by the Transform node. The Transform node defines a coordinate system for its children that is relative to the coordinate systems of its ancestors. Here, we determine the value of its tanslation field as the vector <0.2 0 0>, which translates all its children nodes (i.e. all the nodes enclosed by the Transform tag) to the position (0, 0, 0) relative to the world coordinate.
If we refer to the H3D::Transform class reference page, we will see a list of Transform fields and the field types (image below). As expected, the translation field is of type SFVec3f.
Transform is also a grouping node, which means that it inherits from the abstract node X3DGroupingNode. All grouping nodes share the common function of aggregating all its children node under it. The inheritance diagram at the H3D::X3DGroupingNode class reference page shows grouping nodes other than Transform e.g. Collision, Group, Switch, DynamicTransform, FitToBoxTransform.
In our example, the Transform node groups two Shape nodes and translates them 0.2m to the right.
Shape, geometry, appearance and surface nodes
There are two shapes in our scene. One is a blue sphere and the other a red "fish" looking geometry. The Shape node is used to create objects that are rendered in our scene. A shape is specified with two other nodes: Appearance and an X3DGeometryNode.
The first Shape in our example uses Sphere as a geometry to the shape. An Appearance node is then used to specify the visual and surface haptic properties of this Sphere. It uses the Material node and colours the Sphere by setting the value of the diffuseColor field. We add haptics to the Sphere geometry by simply adding an H3DSurfaceNode. We use the FrictionalSurface in this case. If you have a haptics device connected you should be able to feel the friction on the surface of both shapes.
The only main difference between the first and second Shape node is the use of a different geometry node. The second shape uses the IndexedFaceSet. Both Sphere and IndexedFaceSet inherit from the X3DGeometryNode. The H3D::X3DGeometry class reference lists all other geometry nodes available in H3D.
DEF and USE
Note that the second shape is defined "S" with the expression DEF="S". This allows us to refer to the same object elsewhere by using the USE keyword. A node that USEs a DEF-ed node does not create another object. It merely copies the DEF-ed object. The last node in the example uses S, which means that the fish-like shape is copied once more to the scene.