2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-22 05:27:40 +08:00
|
|
|
#include <osg/Transform>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2003-04-14 21:22:21 +08:00
|
|
|
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)
|
|
|
|
{
|
2003-12-09 22:07:44 +08:00
|
|
|
transform.computeLocalToWorldMatrix(_matrix,this);
|
2003-04-14 21:22:21 +08:00
|
|
|
}
|
|
|
|
else // worldToLocal
|
|
|
|
{
|
2003-12-09 22:07:44 +08:00
|
|
|
transform.computeWorldToLocalMatrix(_matrix,this);
|
2003-04-14 21:22:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-09 23:14:33 +08:00
|
|
|
void accumulate(const NodePath& nodePath)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
2004-09-09 23:14:33 +08:00
|
|
|
NodePath& non_const_nodePath = const_cast<NodePath&>(nodePath);
|
|
|
|
for(NodePath::iterator itr=non_const_nodePath.begin();
|
|
|
|
itr!=non_const_nodePath.end();
|
2003-04-14 21:22:21 +08:00
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->accept(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2004-09-09 23:14:33 +08:00
|
|
|
Matrix osg::computeLocalToWorld(const NodePath& nodePath)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
Matrix matrix;
|
|
|
|
TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD);
|
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2004-09-09 23:14:33 +08:00
|
|
|
Matrix osg::computeWorldToLocal(const NodePath& nodePath)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
osg::Matrix matrix;
|
|
|
|
TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL);
|
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2004-09-09 23:14:33 +08:00
|
|
|
Matrix osg::computeLocalToEye(const Matrix& modelview,const NodePath& nodePath)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
Matrix matrix(modelview);
|
|
|
|
TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD);
|
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2004-09-09 23:14:33 +08:00
|
|
|
Matrix osg::computeEyeToLocal(const Matrix& modelview,const NodePath& nodePath)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
Matrix matrix;
|
|
|
|
matrix.invert(modelview);
|
|
|
|
TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL);
|
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
Transform::Transform()
|
|
|
|
{
|
2004-10-25 04:04:00 +08:00
|
|
|
_referenceFrame = RELATIVE_RF;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
2002-01-29 22:04:06 +08:00
|
|
|
Transform::Transform(const Transform& transform,const CopyOp& copyop):
|
|
|
|
Group(transform,copyop),
|
2002-07-12 22:25:10 +08:00
|
|
|
_referenceFrame(transform._referenceFrame)
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
{
|
|
|
|
}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
Transform::~Transform()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
void Transform::setReferenceFrame(ReferenceFrame rf)
|
|
|
|
{
|
|
|
|
if (_referenceFrame == rf) return;
|
|
|
|
|
|
|
|
_referenceFrame = rf;
|
|
|
|
|
|
|
|
// switch off culling if transform is absolute.
|
2004-11-06 18:18:55 +08:00
|
|
|
setCullingActive(_referenceFrame==RELATIVE_RF);
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere Transform::computeBound() const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere bsphere = Group::computeBound();
|
|
|
|
if (!bsphere.valid()) return bsphere;
|
2002-02-06 05:54:46 +08:00
|
|
|
|
|
|
|
// note, NULL pointer for NodeVisitor, so compute's need
|
|
|
|
// to handle this case gracefully, normally this should not be a problem.
|
|
|
|
Matrix l2w;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2003-12-09 22:07:44 +08:00
|
|
|
computeLocalToWorldMatrix(l2w,NULL);
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
Vec3 xdash = bsphere._center;
|
|
|
|
xdash.x() += bsphere._radius;
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
xdash = xdash*l2w;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
Vec3 ydash = bsphere._center;
|
|
|
|
ydash.y() += bsphere._radius;
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
ydash = ydash*l2w;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
Vec3 zdash = bsphere._center;
|
|
|
|
zdash.z() += bsphere._radius;
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
zdash = zdash*l2w;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
bsphere._center = bsphere._center*l2w;
|
|
|
|
|
|
|
|
xdash -= bsphere._center;
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
float len_xdash = xdash.length();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
ydash -= bsphere._center;
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
float len_ydash = ydash.length();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
zdash -= bsphere._center;
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
float len_zdash = zdash.length();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
bsphere._radius = len_xdash;
|
|
|
|
if (bsphere._radius<len_ydash) bsphere._radius = len_ydash;
|
|
|
|
if (bsphere._radius<len_zdash) bsphere._radius = len_zdash;
|
2002-11-12 21:36:56 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
return bsphere;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-01-24 06:15:39 +08:00
|
|
|
}
|