A node is an object that can be part of or entirely comprise a scene graph. Typically, a node is a collection of one or more fields and methods that together perform a specific function, for example, a csShape node encapsulates all information about the shape and appearance of a geometry.
Cosmo 3D nodes are divided into two types:
group—associate other nodes.
leaf— contain rendering information.
This chapter describes nodes and node types.
These are the sections in this chapter:
A node is a collection of one or more fields and methods. Each field is a C++ class with data members and methods that get and set those member values. The fields set a variety of parameters. For example, some of the fields in the csGeoSet are summarized in Table 4-1.
Is a csCoordSet containing vertex coordinates.
Is a csNormalSet containing normals for a geometry.
Is a csColorSet containing colors for a geometry.
Is a csTexCoord containing texture coordinates for a geometry.
Is a csIndexSet providing indices into a csCoordSet.
Is a csIndexSet providing indices into a csNormalSet.
Is a csIndexSet providing indices into a csColorSet.
Is a csIndexSet providing indices into a csTexCoordSet.
Specifies whether to cull back-facing polygons, front-facing polygons, or no polygons.
Each node supplies default values for each of its fields.
Leaf nodes are responsible for defining the visual and aural elements portrayed in a scene. Leaf nodes cannot have child nodes.
The following list shows all of the different types of Cosmo 3D leaf nodes; all are derivatives of csLeaf.
csShape—associates a csGeometry object with a csAppearance object.
The following section describes csShape. All of the other nodes are described in Chapter 8, “Lighting and Fog.”
To associate a csGeometry object with a csAppearance object, use the following methods:
void setAppearance(csAppearance* appearance); void setGeometry(csGeometry* geometry);
There is a corresponding set of get...() methods that return the current appearance and geometry objects in the csShape object.
Group nodes associate other nodes into a hierarchy known as a scene graph. Only group nodes can have children. A group node, for example, might associate two csShape nodes, as shown in Figure 4-1.
Actions, such as a draw action, applied to a group node may be applied in no particular order to some or all of its children. A group node, then, defines the scope of an action.
For more information about actions, see Chapter 7, “Traversing the Scene Graph.”
The following list shows all of the different types of Cosmo 3D group nodes; all are derivatives of csGroup:
csSwitch—selects none, one, or all of its children, depending on its value. See “Using csSwitch to Switch Between Nodes”.
csBillboard—rotates its children to face the viewer at the same time. See “Using csBillboard”.
csLOD—(level-of-detail) is a switch that selects one of its children based on the distance between the camera and the shape encapsulated by the csLOD; the closer the shape, the greater the detail used when rendering the shape, the farther away the shape, the less detailed the shape. For more information, see Chapter 14, “Optimizing Rendering.”
csTransform—positions and orients a shape in the coordinate system of the parent node to csTransform. For more information, see Chapter 6, “Placing Shapes in a Scene.”
csEnvironment—is a grouping node which defines the scope of influence for the effects provided by csLight. For more information, see Chapter 8, “Lighting and Fog.”
To specify whether none, one, or all of a csSwitch node's children are selected, use the following member function:
void setWhichChild(int which);
The possible values of which are
NO_CHILDREN—to select no nodes
an integer—to specify a child node
ALL_CHILDREN—to select all of the children of the csSwitch node
Each child of a switch node is assigned an index number when added to the group node: the first child added is index 0, the second child added is index 1, and so on.
You might use a csSwitch node to create an animation sequence. For example, if each of the five child nodes of a csSwitch node contained the image of a character in different stages of walking, your application could switch sequentially between the child nodes to create a simple animation sequence.
csBillboard is a subclass of csGroup. It is used to rotate its children to face the viewer at all times. csBillboard has the following fields:
csMFRef children (inherited from csGroup) csEnum mode csMFVec3f position csVec3f axis
The mode field specifies one of three billboard modes: AXIAL, POINT_SCREEN, and POINT_OBJECT. The mode defines how the billboard's children should be rotated to face the viewer. Specific descriptions of each mode follow:
AXIAL: In this mode, the local +z axis is rotated about the billboard axis to face the viewer (i.e. to match the negation of the view vector).
POINT_SCREEN: In this mode, the local +z axis is rotated to face the viewer. The local +y axis is aligned with the screen's +y axis.
POINT_OBJECT: In this mode, the local +z axis is rotated to face the viewer. The remaining degree of freedom is used to minimize the angle between the local +y axis and the billboard axis.
The position field specifies a position to which each child should be translated after it has been rotated to face the viewer. There should be as many entries in the position field as there are children.
The axis field is used in AXIAL and POINT_OBJECT modes.
Cosmo 3D allows you to set the values for nodes in two ways: either using the set() method in each of the node's fields, or by using tokens.
Setting the fields is different depending on whether or not the variable has a single or multiple value. If the variable has a single value, the variable can be set directly; if it has multiple values, the particular value in the set of values must be specified, as shown in Figure 4-2.
Nodes are composed of one or more fields, each of which is a class containing set(), get(), and, optionally, other methods. The csAppearance node, for example, contains many fields, some of which are Shininess, Material, and TranspEnable (enable transparency). To define one of these fields, you use the appropriate set() method, such as
csMaterial *mtl->setShininess(ShininessValue); ShininessValue = mtl->getShininess(); color = mtl->getDiffuseColor();
ShininessValue is a float. The first line of code sets the shininess value of the csMaterial, mtl. The following lines return an atomic, single value, ShininessValue, and a composite, single value, color. A composite, single value is a set of numbers that represent a single feature, for example, RGB values represent one feature: color
Get a handle to the field specified by the token.
Use the handle to set or get the field.
F = mtl->getField(SHININESS); F->set(ShininessValue); ShininessValue = F->get(); F->get(c);
ShininessValue is a float. The first line of code returns a handle, F, to the shininess field. The second line then sets the value of that field.
The third line returns an atomic, single value. Since there is only one value, it does not need to be specified in the argument. The last line returns a composite, single value, F.
sfFloat Shininess; sfVec DiffuseColor;
To get or set a value in a csMaterial field, you must specify which of the values in the field you are retrieving or setting, for example:
csGroup *g; g->addChild(child); child = g->getChild(1);
Child nodes of a group node are numbered, starting with zero. To retrieve the specific child in the csGroup, you must specify in the argument of getChild() which child you want returned; in this example, it is child number one.
To use tokens to set or get multiple-value fields, you
Get a handle to the field specified by the token.
Use the handle to set or get values from a specific variable in the field.
csMFRef *F; csGroup *g; F = g->getField(CHILDREN); F->append(child); child = F->get(1);
To retrieve the specific child in the csGroup, the last line of code shows that you must specify in the argument of get() which child you want returned; in this example, it is child number one.