2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2006-02-28 03:48:34 +08:00
|
|
|
|
|
|
|
#include <osg/Notify>
|
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;
|
2006-11-27 22:52:07 +08:00
|
|
|
bool _ignoreCameras;
|
2003-04-14 21:22:21 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
TransformVisitor(Matrix& matrix,CoordMode coordMode, bool ignoreCameras):
|
2003-04-14 21:22:21 +08:00
|
|
|
NodeVisitor(),
|
|
|
|
_coordMode(coordMode),
|
2006-02-28 03:48:34 +08:00
|
|
|
_matrix(matrix),
|
2006-11-27 22:52:07 +08:00
|
|
|
_ignoreCameras(ignoreCameras)
|
2003-04-14 21:22:21 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2006-02-28 03:48:34 +08:00
|
|
|
if (nodePath.empty()) return;
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
2006-11-27 22:52:07 +08:00
|
|
|
if (_ignoreCameras)
|
2006-02-28 03:48:34 +08:00
|
|
|
{
|
2006-11-27 22:52:07 +08:00
|
|
|
// we need to found out the last absolute Camera in NodePath and
|
2006-02-28 03:48:34 +08:00
|
|
|
// set the i index to after it so the final accumulation set ignores it.
|
|
|
|
i = nodePath.size();
|
|
|
|
NodePath::const_reverse_iterator ritr;
|
|
|
|
for(ritr = nodePath.rbegin();
|
|
|
|
ritr != nodePath.rend();
|
|
|
|
++ritr, --i)
|
|
|
|
{
|
2006-11-27 22:52:07 +08:00
|
|
|
const osg::Camera* camera = dynamic_cast<const osg::Camera*>(*ritr);
|
2006-02-28 03:48:34 +08:00
|
|
|
if (camera &&
|
2007-02-08 00:32:14 +08:00
|
|
|
(camera->getReferenceFrame()!=osg::Transform::RELATIVE_RF || camera->getParents().empty()))
|
2006-02-28 03:48:34 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do the accumulation of the active part of nodepath.
|
|
|
|
for(;
|
|
|
|
i<nodePath.size();
|
|
|
|
++i)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
2006-02-28 03:48:34 +08:00
|
|
|
const_cast<Node*>(nodePath[i])->accept(*this);
|
2003-04-14 21:22:21 +08:00
|
|
|
}
|
|
|
|
}
|
2009-02-03 04:35:19 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
TransformVisitor& operator = (const TransformVisitor&) { return *this; }
|
2003-04-14 21:22:21 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
Matrix osg::computeLocalToWorld(const NodePath& nodePath, bool ignoreCameras)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
Matrix matrix;
|
2006-11-27 22:52:07 +08:00
|
|
|
TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD,ignoreCameras);
|
2003-04-14 21:22:21 +08:00
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
Matrix osg::computeWorldToLocal(const NodePath& nodePath, bool ignoreCameras)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
osg::Matrix matrix;
|
2006-11-27 22:52:07 +08:00
|
|
|
TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL,ignoreCameras);
|
2003-04-14 21:22:21 +08:00
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
Matrix osg::computeLocalToEye(const Matrix& modelview,const NodePath& nodePath, bool ignoreCameras)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
Matrix matrix(modelview);
|
2006-11-27 22:52:07 +08:00
|
|
|
TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD,ignoreCameras);
|
2003-04-14 21:22:21 +08:00
|
|
|
tv.accumulate(nodePath);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
Matrix osg::computeEyeToLocal(const Matrix& modelview,const NodePath& nodePath, bool ignoreCameras)
|
2003-04-14 21:22:21 +08:00
|
|
|
{
|
|
|
|
Matrix matrix;
|
|
|
|
matrix.invert(modelview);
|
2006-11-27 22:52:07 +08:00
|
|
|
TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL,ignoreCameras);
|
2003-04-14 21:22:21 +08:00
|
|
|
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
|
|
|
|
2009-06-01 19:08:34 +08:00
|
|
|
osg::BoundingSphere::vec_type xdash = bsphere._center;
|
2005-05-12 22:03:22 +08:00
|
|
|
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
|
|
|
|
2009-06-01 19:08:34 +08:00
|
|
|
osg::BoundingSphere::vec_type ydash = bsphere._center;
|
2005-05-12 22:03:22 +08:00
|
|
|
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
|
|
|
|
2009-06-01 19:08:34 +08:00
|
|
|
osg::BoundingSphere::vec_type zdash = bsphere._center;
|
2005-05-12 22:03:22 +08:00
|
|
|
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;
|
2009-06-01 19:08:34 +08:00
|
|
|
osg::BoundingSphere::value_type len_xdash = xdash.length();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
ydash -= bsphere._center;
|
2009-06-01 19:08:34 +08:00
|
|
|
osg::BoundingSphere::value_type len_ydash = ydash.length();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
zdash -= bsphere._center;
|
2009-06-01 19:08:34 +08:00
|
|
|
osg::BoundingSphere::value_type 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
|
|
|
}
|