From 3df0401007758a5105effd4bcd1d15d1e807e885 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 14 Apr 2003 13:22:21 +0000 Subject: [PATCH] Addd new computeLocalToWorld(NodePath) etc methods. --- include/osg/Transform | 21 +++++++++++ src/osg/Transform.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/include/osg/Transform b/include/osg/Transform index 5c5cff45b..1ed82c7ab 100644 --- a/include/osg/Transform +++ b/include/osg/Transform @@ -19,6 +19,27 @@ namespace osg { + + +/** compute the matrix which transforms objects in local coords to world coords, + * by accumulating the Transform local to world matrices along the specified node path.*/ +extern SG_EXPORT Matrix computeLocalToWorld(NodePath& nodePath); + +/** compute the matrix which transforms objects in world coords to local coords, + * by accumulating the Transform world to local matrices along the specified node path.*/ +extern SG_EXPORT Matrix computeWorldToLocal(NodePath& nodePath); + +/** compute the matrix which transforms objects in local coords to world coords, + * by accumulating the Transform local to world matrices along the specified node path + * the supplied initialial camera modelview .*/ +extern SG_EXPORT Matrix computeLocalToEye(const Matrix& modelview, NodePath& nodePath); + +/** compute the matrix which transforms objects in world coords to local coords, + * by accumulating the Transform world to local matrices along the specified node path + * the inverse of the supplied initialial camera modelview.*/ +extern SG_EXPORT Matrix computeEyeToLocal(const Matrix& modelview, NodePath& nodePath); + + /** A Transform is a group node for which all children are transformed by * a 4x4 matrix. It is often used for positioning objects within a scene, * producing trackball functionality or for animation. diff --git a/src/osg/Transform.cpp b/src/osg/Transform.cpp index 35994fbce..2dbb47d82 100644 --- a/src/osg/Transform.cpp +++ b/src/osg/Transform.cpp @@ -14,6 +14,93 @@ using namespace osg; +class TransformVisitor : public NodeVisitor +{ + public: + + enum CoordMode + { + WORLD_TO_LOCAL, + LOCAL_TO_WORLD + }; + + + CoordMode _coordMode; + Matrix& _matrix; + + TransformVisitor(Matrix& matrix,CoordMode coordMode): + NodeVisitor(), + _coordMode(coordMode), + _matrix(matrix) + {} + + virtual void apply(Transform& transform) + { + if (_coordMode==LOCAL_TO_WORLD) + { + transform.getLocalToWorldMatrix(_matrix,this); + } + else // worldToLocal + { + transform.getWorldToLocalMatrix(_matrix,this); + } + } + + void accumulate(NodePath& nodePath) + { + for(NodePath::iterator itr=nodePath.begin(); + itr!=nodePath.end(); + ++itr) + { + (*itr)->accept(*this); + } + } + +}; + +Matrix osg::computeLocalToWorld(NodePath& nodePath) +{ + Matrix matrix; + TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD); + tv.accumulate(nodePath); + return matrix; +} + +Matrix osg::computeWorldToLocal(NodePath& nodePath) +{ + osg::Matrix matrix; + TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL); + tv.accumulate(nodePath); + return matrix; +} + +Matrix osg::computeLocalToEye(const Matrix& modelview,NodePath& nodePath) +{ + Matrix matrix(modelview); + TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD); + tv.accumulate(nodePath); + return matrix; +} + +Matrix osg::computeEyeToLocal(const Matrix& modelview,NodePath& nodePath) +{ + Matrix matrix; + matrix.invert(modelview); + TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL); + tv.accumulate(nodePath); + return matrix; +} + + + + + + + + + + + Transform::Transform() { _referenceFrame = RELATIVE_TO_PARENTS;