Game Development Reference
In-Depth Information
of the scene graph will be pretty flat, but some objects, such as articulated vehicles
and characters, have a deep hierarchy of connected parts.
You might wonder why I chose an STL
vector<>
instead of a
list<>
.It
s an easy
choice because all scene nodes tend to keep a similar number of children. Even if the
number of children changes, say when a car loses a wheel in a crash, it
'
s easy enough
to make the node invisible. Lists are much better for structures that need fast inser-
tion and deletion, and vectors are fastest for iteration and random access, which
makes them a better candidate to store child nodes. There
'
'
s nothing stopping you,
of course, from creating a special scene node that uses STL
list<>
to store its
children.
Here
'
s how the
SceneNode
class implements the
VSetTransform
method:
void SceneNode::VSetTransform(const Mat4x4 *toWorld, const Mat4x4 *fromWorld)
{
m_Props.m_ToWorld = *toWorld;
if (!fromWorld)
m_Props.m_FromWorld = m_Props.m_ToWorld.Inverse();
else
m_Props.m_FromWorld = *fromWorld;
}
If the calling routine already has the
fromWorld
transform, it doesn
'
t have to be cal-
culated with a call to the expensive
D3DXMatrixInverse
function. The
fromWorld
transformation is extremely useful for things like picking, which is finding the exact
intersection of a ray with a polygon on a scene node. You might decide that some
objects in your scene don
'
t need this, but in this
“
training wheels
”
scene graph, it is
convenient for every node to have it.
This kind of picking is similar to the ray cast provided by most physics systems, but
this one is for visible geometry, not physical geometry. Most games actually consoli-
date the calls to both, giving the caller the opportunity to grab the right target based
on what it looks like or how it is physically represented in the game world. These are
usually very different, since the visible geometry is often finely detailed, and the phys-
ical geometry is a simplified version of that.
The
VOnRestore()
and
VOnUpdate()
implementations iterate through
m_Children
and call the same method; child classes will usually do something useful, such as cre-
ate geometry, load textures, or handle animations and call these methods of
Scene-
Node
to make sure the entire scene graph is handled:
HRESULT SceneNode::VOnRestore(Scene *pScene)
{