Chapter 6. Placing Shapes in a Scene

When you create a geometry, it has a specified size, location, and orientation, as defined in its own space. You place such a geometry

This chapter describes how to perform each of those tasks.

The final transformation that affects the view of the user is that created with the camera. Rotating the camera has an obvious affect on the view of the scene. To read more about the camera transformation, see “Using a Camera to View a Scene”.

This chapter has the following sections:

Creating a Sense of Depth

Geometries are layered by Cosmo 3D according to the order in which they are rendered. For example, the first geometry rendered is usually covered by the second geometry if they overlap.

Overriding the Default Order of Layering Shapes

To override this layering effect, you can use the csContext::setcsDepthFunc() method; it determines the layering order of geometries in a scene according to values in the Z dimension. To specify a layering method, use one of the tokens in csContext::DepthFuncEnum.









Here are the effects of some of the arguments:


the incoming pixel is never displayed on top of the current, corresponding pixel in the buffer. This function has the effect of reversing the normal order of layering: pixels are rendered behind the pixels currently in the buffer.


A pixel is displayed only if its Z component is less than the Z value of the corresponding pixel currently in the buffer. This function presents the intuitive representation of close objects appearing in front of distant objects.


The incoming pixel is always displayed on top of what is currently displayed regardless of the Z component.

The default is LEQUAL_DFUNC.

Transforming Shapes to New Locations, Sizes, and Orientations

The csTransform node

  • Allows you to specify vertex coordinates of a shape in local space, using (0, 0, 0) as the origin.

  • Translates the local coordinates into the coordinates of its parent node.

If there is more than one csTransform node in a hierarchy of nodes, each csTransform node translates the child node coordinates into coordinates of its parents all the way up the hierarchy until a final csTransform node translates the coordinates of the shape into those of the root node. The coordinate system of the root node is called world space because all shapes in all parts of the scene graph are translated into that coordinate system.

Placing Transform Nodes

Typically, csTransform nodes are placed between csShape or csGroup-type nodes and the rest of the scene graph, as shown in Figure 6-1.

Figure 6-1. Placement of csTransform Nodes

Figure 6-1 Placement of csTransform Nodes

Any node, however, can be the child of a transformation node.

Setting the Transformation

The csTransform node allows you to set the location (translation), rotation, and scale of its children using the following methods:

void setTranslation(const csVec3f& translation);
void setTranslation(csFloat v0, csFloat v1, csFloat v2);

void setRotation(const csRotation& rotation);
void setRotation(csFloat v0, csFloat v1, csFloat v2, csFloat v3);

void setScale(const csVec3f& scale);
void setScale(csFloat v0, csFloat v1, csFloat v2);

void setScaleOrientation(const csRotation& scaleOrientation);
void setScaleOrientation(csFloat v0, csFloat v1, csFloat v2, 
    csFloat v3);

void setCenter(const csVec3f& center);
void setCenter(csFloat v0, csFloat v1, csFloat v2);
void setMatrix(Matrix4f mat)

There is a corresponding set of get...() methods for each of these methods.

Each method is overridden so that you can specify the arguments to the methods either as objects or individual coordinates.

setTranslation() positions the children of the transform in the space of the node that is the parent to csTransform.

setCenter() specifies the point around which an object rotates.

setRotation() rotates the children of the transform around the point specified in setCenter().

setScale() specify the scale factor of the children of the transform along the X, Y, and Z axes.

setScaleOrientation() specifies the orientation in which the scaling takes effect. Figure 6-2 shows a shape scaled by a factor of 2 in two different orientations, 0 and 45 degrees, respectively.

Figure 6-2. Scaling in Different Orientations

Figure 6-2 Scaling in Different Orientations

All of these methods invisibly set a transformation matrix to carry out their actions. If you want to set the matrix directly, you can use the setMatrix() method.

Ordering Transformations

The order in which you perform transformations can effect the final result. Take, for example, translating and rotating a model. If you perform the transformations in this order, you end up with a rotated model translated, for example, down the X axis, as shown in Figure 6-3.

Figure 6-3. Order of Transformations

Figure 6-3 Order of Transformations

When you reverse the order of the transformations, the end result is different. Since the center of rotation is the origin, the rotation transformation lifts the object above the X axis.

Placing Geometries in World Space

Multiple transformation nodes can orient and size all shapes in a scene graph into the space of the root node. The space of the root node is called world space.

World space is the coordinate system of the root node in which all shapes in a scene graph can reside. Local space is a coordinate system in a subsection of a scene graph.

Cosmo 3D Matrices

Many geometry variables are defined in local space. To translate those values into the world space you use a transformation matrix. The transformation matrix is a 4 × 4 matrix of type csMatrix4f that contains scaling, translation, and rotation information. You can set the matrix explicitly or, more easily, you can use class methods to generate a transformation matrix.

Cosmo 3D matrices are column major, which means their members are ordered in the following way:

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

You can set a transform matrix directly, or you can use csContext methods to scale and orient a shape in world space.