Chapter 4. Scene Graph Nodes

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:

This chapter describes nodes and node types.

These are the sections in this chapter:

What Is a Node

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.

Table 4-1. Examples of Fields in Nodes

Field Type

Fields

Description

SFRef

COORDS

Is a csCoordSet containing vertex coordinates.

SFRef

NORMALS

Is a csNormalSet containing normals for a geometry.

SFRef

COLORS

Is a csColorSet containing colors for a geometry.

SFRef

TEX_COORDS

Is a csTexCoord containing texture coordinates for a geometry.

SFRef

COORD_INDICES

Is a csIndexSet providing indices into a csCoordSet.

SFRef

NORMAL_INDICES

Is a csIndexSet providing indices into a csNormalSet.

SFRef

COLOR_INDICES

Is a csIndexSet providing indices into a csColorSet.

SFRef

TEX_COORD_INDICES

Is a csIndexSet providing indices into a csTexCoordSet.

SFEnum

CULL_FACE

Specifies whether to cull back-facing polygons, front-facing polygons, or no polygons.

Each node supplies default values for each of its fields.

Node Types

There are two types of nodes:

  • Group—associates nodes into hierarchies.

  • Leaf—sets the visual and audio values for a scene.

The following sections describe these node types.

Leaf Nodes

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.

  • csLight—is an abstract base class for light sources.

  • csDirectionalLight—is a directional light source whose origin is at infinity.

  • csPointLight—is a point source of light that radiates equally in all directions.

  • csSpotLight—is a conical spot light.

The following section describes csShape. All of the other nodes are described in Chapter 8, “Lighting and Fog.”

csShape

A csShape node associates a csGeometry object with a csAppearance object. Together, the csGeometry and csAppearance objects create a complete description of a shape.

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

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.

Figure 4-1. A Simple Grouping

Figure 4-1 A Simple Grouping

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.”

Group Node Types

The following list shows all of the different types of Cosmo 3D group nodes; all are derivatives of csGroup:

Using csSwitch to Switch Between Nodes

The csSwitch node selects none, one, or all of its children. The constructor has the following prototype:

csSwitch();

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.

Using csBillboard

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.

Setting the Values in Scene Graph Nodes

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.

Figure 4-2. Setting Single and Multiple-Value Variables

Figure 4-2 Setting Single and Multiple-Value Variables

Using set() and get() Methods to Set and Get Single-Value Fields

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

Using Tokens to Set and Get Single-Value Fields

To use tokens to set or get single-value fields, you

  1. Get a handle to the field specified by the token.

  2. Use the handle to set or get the field.

For example:

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.

Using set() and get() Methods to Set and Get Multiple-Value Fields

Multiple-value fields are arrays of variables. For example, the csMaterial field has a number of values, including

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.

Using Tokens to Set and Get Multiple-Value Fields

To use tokens to set or get multiple-value fields, you

  1. Get a handle to the field specified by the token.

  2. Use the handle to set or get values from a specific variable in the field.

For example:

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.