2007年9月30日星期日

OGRE中如何将SceneNode挂接在Entity的Bone上

OGRE本身支持将一个MovableObject挂接到Entity的Bone上,但是不支持将一个SceneNode直接挂接上去。很不方便,但是我也没有看到合适的已封装好的解决方案。

我们看一下目前实现:

TagPoint* Entity::attachObjectToBone(const String &boneName, MovableObject *pMovable, const Quaternion &offsetOrientation, const Vector3 &offsetPosition)
{
    ...
    Bone* bone = mSkeletonInstance->getBone(boneName);
    ...

    TagPoint *tp = mSkeletonInstance->createTagPointOnBone(
    bone, offsetOrientation, offsetPosition);
    tp->setParentEntity(this);
    tp->setChildObject(pMovable);

    attachObjectImpl(pMovable, tp);

    ...

    return tp;
}

挂接的步骤为:
  1. 取得Bone
  2. 在Bone上创建一个TagPoint
  3. 将MovableObject挂接到TagPoint上
参考了TagPoint:

class _OgreExport TagPoint : public Bone
{
    ...


而Bone:

class _OgreExport Bone : public Node
{
    ...


TagPoint来自Bone,间接的继承自Node。当Skeleton动画更新时,将通知到这个节点进行更新时,TagPoint在虚函数
updateFromParentImpl
中进行变换矩阵的处理。

为了达到直接挂接SceneNode的目的,我采用了一个并不优雅但是相对简易的方案:
  1. 增加一个MovableObjectSceneNode类,它继承自MovableObject
  2. 将MovableObjectSceneNode类实例挂接到TagPoint上
  3. MovableObjectSceneNode类创建一个影子SceneNode,挂接在SceneManager的根节点下
  4. 将需要挂接的SceneNode挂接到影子SceneNode上
  5. 当MovableObjectSceneNode被通知刷新时,刷新影子SceneNode的变换矩阵

没有评论:

发表评论