Added support for osg::AutoTransform::setAutoRotateMode(AutoRotateMode) deprecating

the previous setAutoRotateToScreen().  Added support for ROTATE_TO_CAMERA mode.

Cleaned up the autotransform demo to use a sensible number of labels
This commit is contained in:
Robert Osfield
2004-08-06 08:22:58 +00:00
parent ed10594139
commit 3579506fa8
4 changed files with 280 additions and 221 deletions

View File

@@ -1,173 +1,182 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
*
* 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.
* (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.
*/
#include <osg/AutoTransform>
#include <osg/CullStack>
* 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.
*/
#include <osg/AutoTransform>
#include <osg/CullStack>
using namespace osg;
using namespace osg;
AutoTransform::AutoTransform():
_autoUpdateEyeMovementTolerance(0.0f),
_autoRotateToScreen(false),
_autoScaleToScreen(false),
_scale(1.0f,1.0f,1.0f),
_firstTimeToInitEyePoint(true),
_matrixDirty(true)
{
// setNumChildrenRequiringUpdateTraversal(1);
}
AutoTransform::AutoTransform():
_autoUpdateEyeMovementTolerance(0.0f),
_autoRotateMode(NO_ROTATION),
_autoScaleToScreen(false),
_scale(1.0f,1.0f,1.0f),
_firstTimeToInitEyePoint(true),
_matrixDirty(true)
{
// setNumChildrenRequiringUpdateTraversal(1);
}
AutoTransform::AutoTransform(const AutoTransform& pat,const CopyOp& copyop):
Transform(pat,copyop),
AutoTransform::AutoTransform(const AutoTransform& pat,const CopyOp& copyop):
Transform(pat,copyop),
_position(pat._position),
_pivotPoint(pat._pivotPoint),
_autoUpdateEyeMovementTolerance(pat._autoUpdateEyeMovementTolerance),
_autoRotateToScreen(pat._autoRotateToScreen),
_autoRotateMode(pat._autoRotateMode),
_autoScaleToScreen(pat._autoScaleToScreen),
_rotation(pat._rotation),
_scale(pat._scale),
_firstTimeToInitEyePoint(true),
_matrixDirty(true)
{
// setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
}
{
// setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
}
bool AutoTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_matrixDirty) computeMatrix();
if (_referenceFrame==RELATIVE_TO_PARENTS)
{
matrix.preMult(_cachedMatrix);
}
else // absolute
{
matrix = _cachedMatrix;
}
return true;
}
bool AutoTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_matrixDirty) computeMatrix();
if (_referenceFrame==RELATIVE_TO_PARENTS)
{
matrix.preMult(_cachedMatrix);
}
else // absolute
{
matrix = _cachedMatrix;
}
return true;
}
bool AutoTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_referenceFrame==RELATIVE_TO_PARENTS)
{
matrix.postMult(osg::Matrix::translate(-_position)*
osg::Matrix::rotate(_rotation.inverse())*
osg::Matrix::scale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.z())*
osg::Matrix::translate(_pivotPoint));
}
else // absolute
{
matrix = osg::Matrix::translate(-_position)*
osg::Matrix::rotate(_rotation.inverse())*
osg::Matrix::scale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.z())*
osg::Matrix::translate(_pivotPoint);
}
return true;
}
bool AutoTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_referenceFrame==RELATIVE_TO_PARENTS)
{
matrix.postMult(osg::Matrix::translate(-_position)*
osg::Matrix::rotate(_rotation.inverse())*
osg::Matrix::scale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.z())*
osg::Matrix::translate(_pivotPoint));
}
else // absolute
{
matrix = osg::Matrix::translate(-_position)*
osg::Matrix::rotate(_rotation.inverse())*
osg::Matrix::scale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.z())*
osg::Matrix::translate(_pivotPoint);
}
return true;
}
void AutoTransform::computeMatrix() const
{
if (!_matrixDirty) return;
_cachedMatrix.set(osg::Matrix::translate(-_pivotPoint)*
osg::Matrix::scale(_scale)*
osg::Matrix::rotate(_rotation)*
osg::Matrix::translate(_position));
_matrixDirty = false;
}
void AutoTransform::computeMatrix() const
{
if (!_matrixDirty) return;
_cachedMatrix.set(osg::Matrix::translate(-_pivotPoint)*
osg::Matrix::scale(_scale)*
osg::Matrix::rotate(_rotation)*
osg::Matrix::translate(_position));
_matrixDirty = false;
}
void AutoTransform::accept(NodeVisitor& nv)
{
// if app traversal update the frame count.
if (nv.getVisitorType()==NodeVisitor::UPDATE_VISITOR)
{
}
else
if (nv.getVisitorType()==NodeVisitor::CULL_VISITOR)
{
void AutoTransform::accept(NodeVisitor& nv)
{
// if app traversal update the frame count.
if (nv.getVisitorType()==NodeVisitor::UPDATE_VISITOR)
{
}
else
if (nv.getVisitorType()==NodeVisitor::CULL_VISITOR)
{
CullStack* cs = dynamic_cast<CullStack*>(&nv);
if (cs)
{
CullStack* cs = dynamic_cast<CullStack*>(&nv);
if (cs)
{
int width = _previousWidth;
int height = _previousHeight;
int width = _previousWidth;
int height = _previousHeight;
osg::Viewport* viewport = cs->getViewport();
if (viewport)
{
width = viewport->width();
height = viewport->height();
}
osg::Viewport* viewport = cs->getViewport();
if (viewport)
{
width = viewport->width();
height = viewport->height();
}
osg::Vec3 eyePoint = cs->getEyeLocal();
osg::Vec3 position = getPosition();
osg::Vec3 position = getPosition();
const osg::Matrix& projection = cs->getProjectionMatrix();
bool doUpdate = _firstTimeToInitEyePoint;
if (!_firstTimeToInitEyePoint)
{
osg::Vec3 dv = _previousEyePoint-eyePoint;
if (dv.length2()>getAutoUpdateEyeMovementTolerance()*(eyePoint-getPosition()).length2())
{
doUpdate = true;
}
else if (width!=_previousWidth || height!=_previousHeight)
{
doUpdate = true;
}
bool doUpdate = _firstTimeToInitEyePoint;
if (!_firstTimeToInitEyePoint)
{
osg::Vec3 dv = _previousEyePoint-eyePoint;
if (dv.length2()>getAutoUpdateEyeMovementTolerance()*(eyePoint-getPosition()).length2())
{
doUpdate = true;
}
else if (width!=_previousWidth || height!=_previousHeight)
{
doUpdate = true;
}
else if (projection != _previousProjection)
{
doUpdate = true;
}
{
doUpdate = true;
}
else if (position != _previousPosition)
{
doUpdate = true;
}
}
_firstTimeToInitEyePoint = false;
}
_firstTimeToInitEyePoint = false;
if (doUpdate)
{
if (doUpdate)
{
if (getAutoScaleToScreen())
{
float size = 1.0f/cs->pixelSize(getPosition(),1.0f);
setScale(size);
}
if (getAutoScaleToScreen())
{
float size = 1.0f/cs->pixelSize(getPosition(),1.0f);
setScale(size);
}
if (getAutoRotateToScreen())
{
osg::Quat rotation;
cs->getModelViewMatrix().get(rotation);
setRotation(rotation.inverse());
}
if (_autoRotateMode==ROTATE_TO_SCREEN)
{
osg::Quat rotation;
cs->getModelViewMatrix().get(rotation);
setRotation(rotation.inverse());
}
else if (_autoRotateMode==ROTATE_TO_CAMERA)
{
osg::Vec3 PosToEye = _position - eyePoint;
osg::Matrix lookto = osg::Matrix::lookAt(
osg::Vec3(0,0,0), PosToEye, cs->getUpLocal());
Quat q;
q.set(osg::Matrix::inverse(lookto));
setRotation(q);
}
_previousEyePoint = eyePoint;
_previousWidth = width;
_previousHeight = height;
_previousProjection = projection;
_previousPosition = position;
_previousEyePoint = eyePoint;
_previousWidth = width;
_previousHeight = height;
_previousProjection = projection;
_previousPosition = position;
_matrixDirty = true;
}
_matrixDirty = true;
}
}
}
// now do the proper accept
Transform::accept(nv);
}
}
}
// now do the proper accept
Transform::accept(nv);
}

View File

@@ -93,7 +93,18 @@ bool AutoTransform_readLocalData(Object& obj, Input& fr)
if (fr.matchSequence("autoRotateToScreen %w"))
{
std::string w(fr[1].getStr());
transform.setAutoRotateToScreen(w == "TRUE");
transform.setAutoRotateMode((w == "TRUE") ? osg::AutoTransform::ROTATE_TO_SCREEN : osg::AutoTransform::NO_ROTATION);
fr += 2;
iteratorAdvanced = true;
}
if (fr.matchSequence("autoRotateMode %w"))
{
std::string w(fr[1].getStr());
if (w=="ROTATE_TO_SCREEN") transform.setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
else if (w=="ROTATE_TO_CAMERA") transform.setAutoRotateMode(osg::AutoTransform::ROTATE_TO_CAMERA);
else if (w=="NO_ROTATION") transform.setAutoRotateMode(osg::AutoTransform::NO_ROTATION);
fr += 2;
iteratorAdvanced = true;
}
@@ -118,9 +129,18 @@ bool AutoTransform_writeLocalData(const Object& obj, Output& fw)
fw.indent()<<"rotation "<<transform.getRotation()<<std::endl;
fw.indent()<<"scale "<<transform.getScale()<<std::endl;
fw.indent()<<"pivotPoint "<<transform.getPivotPoint()<<std::endl;
fw.indent()<<"autoUpdateEyeMovementTolerance "<<transform.getAutoUpdateEyeMovementTolerance()<<std::endl;
fw.indent()<<"autoRotateToScreen "<<(transform.getAutoRotateToScreen()?"TRUE":"FALSE")<<std::endl;
fw.indent()<<"autoScaleToScreen "<<(transform.getAutoScaleToScreen()?"TRUE":"FALSE")<<std::endl;
fw.indent()<<"autoUpdateEyeMovementTolerance "<<transform.getAutoUpdateEyeMovementTolerance()<<std::endl;
fw.indent()<<"autoRotateMode ";
switch(transform.getAutoRotateMode())
{
case(osg::AutoTransform::ROTATE_TO_SCREEN): fw<<"ROTATE_TO_SCREEN"<<std::endl; break;
case(osg::AutoTransform::ROTATE_TO_CAMERA): fw<<"ROTATE_TO_CAMERA"<<std::endl; break;
case(osg::AutoTransform::NO_ROTATION):
default: fw<<"NO_ROTATION"<<std::endl; break;
}
fw.indent()<<"autoScaleToScreen "<<(transform.getAutoScaleToScreen()?"TRUE":"FALSE")<<std::endl;
return true;
}