Hi again,
This is the fix for the rest of the virtual inheritance warnings. Same as for
issues as for the Operation class.
Regards
Björn
>-----Original Message-----
>From: osg-users [mailto:[email protected]] On
>Behalf Of Björn Blissing
>Sent: Friday, June 3, 2016 2:39 PM
>To: OpenSceneGraph Users <[email protected]>
>Subject: Re: [osg-users] Preparing to make 3.5.3 dev release, please test
>
>Hi Robert,
>
>The warnings relating the Operation class comes from the two protected
>constructors. Since the class uses virtual inheritance it is initialized by
>the most
>derived class. So the initializer for the virtual base class is ignored.
>
>So just removing the base class initializer for Operation will remove this
>warning. I do not think this will have any negative side effects, since base
>class
>initializer should be ignored by all compliant compilers anyway.
>
>I have attached the changed file.
>
>Regards
>Björn
>
>>-----Original Message-----
>>From: osg-users [mailto:[email protected]] On
>>Behalf Of Robert Osfield
>>Sent: Friday, June 3, 2016 1:46 PM
>>To: OpenSceneGraph Users <[email protected]>
>>Subject: Re: [osg-users] Preparing to make 3.5.3 dev release, please test
>>
>>Hi Bjorn,
>>
>>On 3 June 2016 at 09:48, Björn Blissing <[email protected]> wrote:
>>>
>>> I compiled the latest master with Visual Studio 2015.
>>>
>>> I got a couple of warnings. First of all I got a ton of these, which all
>originates
>>from the same origin:
>>>
>>>
>>> Code:
>>>
>>> ...
>>> OpenSceneGraph\include\osg/OperationThread(80): warning C4589:
>>Constructor of abstract class 'osg::Operation' ignores initializer for virtual
>base
>>class 'osg::Referenced' (compiling source file
>>D:\github\OpenSceneGraph\src\osg\AnimationPath.cpp)
>>> OpenSceneGraph\include\osg/OperationThread(80): note: virtual base
>>classes are only initialized by the most-derived type (compiling source file
>>D:\github\OpenSceneGraph\src\osg\AnimationPath.cpp)
>>> ...
>>>
>>>
>>>
>>>
>>> And the same warnings for the following classes:
>>>
>>>
>>> Code:
>>>
>>> OpenSceneGraph\src\osgGA\CameraManipulator.cpp(24): warning C4589:
>>Constructor of abstract class 'osgGA::CameraManipulator' ignores initializer
>for
>>virtual base class 'osg::Callback'
>>> OpenSceneGraph\src\osgGA\CameraManipulator.cpp(24): note: virtual
>>base classes are only initialized by the most-derived type
>>> OpenSceneGraph\src\osgGA\StandardManipulator.cpp(51): warning
>C4589:
>>Constructor of abstract class 'osgGA::StandardManipulator' ignores initializer
>>for virtual base class 'osg::Callback'
>>> OpenSceneGraph\src\osgGA\StandardManipulator.cpp(51): note: virtual
>>base classes are only initialized by the most-derived type
>>> OpenSceneGraph\src\osgViewer\ViewerBase.cpp(44): warning C4589:
>>Constructor of abstract class 'osgViewer::ViewerBase' ignores initializer for
>>virtual base class 'osg::Object'
>>> OpenSceneGraph\src\osgViewer\ViewerBase.cpp(44): note: virtual base
>>classes are only initialized by the most-derived type
>>> OpenSceneGraph\src\osgViewer\ViewerBase.cpp(50): warning C4589:
>>Constructor of abstract class 'osgViewer::ViewerBase' ignores initializer for
>>virtual base class 'osg::Object'
>>> OpenSceneGraph\src\osgViewer\ViewerBase.cpp(50): note: virtual base
>>classes are only initialized by the most-derived type
>>> OpenSceneGraph\src\osgAnimation\AnimationManagerBase.cpp(65):
>>warning C4589: Constructor of abstract class
>>'osgAnimation::AnimationManagerBase' ignores initializer for virtual base
>class
>>'osg::Callback'
>>> OpenSceneGraph\src\osgAnimation\AnimationManagerBase.cpp(65):
>>note: virtual base classes are only initialized by the most-derived type
>>
>>Unfortunately I don't see these warnings with the compiler I have
>>available with the settings that are currently available. Do you know
>>of what to enable these warnings in gcc or Clang?
>>
>>The nature of these warnings are such that I really need to be able to
>>see the warning first hand and attempt a local fix to see the affect.
>>
>>Cheers,
>>Robert.
>>_______________________________________________
>>osg-users mailing list
>>[email protected]
>>http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
#include <osg/GL>
#include <osg/Matrix>
#include <osg/io_utils>
#include <osg/ComputeBoundsVisitor>
#include <osgGA/CameraManipulator>
#include <string.h>
using namespace osg;
using namespace osgGA;
CameraManipulator::CameraManipulator()
{
_intersectTraversalMask = 0xffffffff;
_autoComputeHomePosition = true;
_homeEye.set(0.0,-1.0,0.0);
_homeCenter.set(0.0,0.0,0.0);
_homeUp.set(0.0,0.0,1.0);
}
CameraManipulator::CameraManipulator(const CameraManipulator& mm, const CopyOp&
copyOp)
: inherited(mm, copyOp),
_intersectTraversalMask(mm._intersectTraversalMask),
_autoComputeHomePosition(mm._autoComputeHomePosition),
_homeEye(mm._homeEye),
_homeCenter(mm._homeCenter),
_homeUp(mm._homeUp),
_coordinateFrameCallback(dynamic_cast<CoordinateFrameCallback*>(copyOp(mm._coordinateFrameCallback.get())))
{
}
CameraManipulator::~CameraManipulator()
{
}
std::string CameraManipulator::getManipulatorName() const
{
const char* className = this->className();
const char* manipString = strstr(className, "Manipulator");
if (!manipString)
return std::string(className);
else
return std::string(className, manipString-className);
}
bool CameraManipulator::handle(const GUIEventAdapter&,GUIActionAdapter&)
{
return false;
}
/** Compute the home position.
*
* The computation considers camera's fov (field of view) and model size and
* positions camera far enough to fit the model to the screen.
*
* camera parameter enables computations of camera's fov. If camera is NULL,
* scene to camera distance can not be computed and default value is used,
* based on model size only.
*
* useBoundingBox parameter enables to use bounding box instead of bounding
sphere
* for scene bounds. Bounding box provide more precise scene center that may be
* important for many applications.*/
void CameraManipulator::computeHomePosition(const osg::Camera *camera, bool
useBoundingBox)
{
if (getNode())
{
osg::BoundingSphere boundingSphere;
OSG_INFO<<" CameraManipulator::computeHomePosition("<<camera<<",
"<<useBoundingBox<<")"<<std::endl;
if (useBoundingBox)
{
// compute bounding box
// (bounding box computes model center more precisely than bounding
sphere)
osg::ComputeBoundsVisitor cbVisitor;
getNode()->accept(cbVisitor);
osg::BoundingBox &bb = cbVisitor.getBoundingBox();
if (bb.valid()) boundingSphere.expandBy(bb);
else boundingSphere = getNode()->getBound();
}
else
{
// compute bounding sphere
boundingSphere = getNode()->getBound();
}
OSG_INFO<<" boundingSphere.center() =
("<<boundingSphere.center()<<")"<<std::endl;
OSG_INFO<<" boundingSphere.radius() =
"<<boundingSphere.radius()<<std::endl;
// set dist to default
double dist = 3.5f * boundingSphere.radius();
if (camera)
{
// try to compute dist from frustum
double left,right,bottom,top,zNear,zFar;
if
(camera->getProjectionMatrixAsFrustum(left,right,bottom,top,zNear,zFar))
{
double vertical2 = fabs(right - left) / zNear / 2.;
double horizontal2 = fabs(top - bottom) / zNear / 2.;
double dim = horizontal2 < vertical2 ? horizontal2 : vertical2;
double viewAngle = atan2(dim,1.);
dist = boundingSphere.radius() / sin(viewAngle);
}
else
{
// try to compute dist from ortho
if
(camera->getProjectionMatrixAsOrtho(left,right,bottom,top,zNear,zFar))
{
dist = fabs(zFar - zNear) / 2.;
}
}
}
// set home position
setHomePosition(boundingSphere.center() + osg::Vec3d(0.0,-dist,0.0f),
boundingSphere.center(),
osg::Vec3d(0.0f,0.0f,1.0f),
_autoComputeHomePosition);
}
}
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
*
* 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.
*
* StandardManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
* while some pieces of code were taken from OSG.
* Thanks to company Cadwork (www.cadwork.ch) and
* Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
*/
#include <osgGA/StandardManipulator>
#include <osgViewer/View>
using namespace osg;
using namespace osgGA;
using namespace osgUtil;
int StandardManipulator::numRelativeFlagsAllocated = 0;
int StandardManipulator::allocateRelativeFlag()
{
return numRelativeFlagsAllocated++;
}
/// Constructor.
StandardManipulator::StandardManipulator( int flags )
: inherited(),
_thrown( false ),
_allowThrow( true ),
_mouseCenterX(0.0f), _mouseCenterY(0.0f),
_delta_frame_time(0.01), _last_frame_time(0.0),
_modelSize( 0. ),
_verticalAxisFixed( true ),
_flags( flags ),
_relativeFlags( 0 )
{
}
/// Constructor.
StandardManipulator::StandardManipulator( const StandardManipulator& uim, const
CopyOp& copyOp )
: inherited( uim, copyOp ),
_thrown( uim._thrown ),
_allowThrow( uim._allowThrow ),
_mouseCenterX(0.0f), _mouseCenterY(0.0f),
_ga_t1( dynamic_cast< GUIEventAdapter* >( copyOp( uim._ga_t1.get() ) ) ),
_ga_t0( dynamic_cast< GUIEventAdapter* >( copyOp( uim._ga_t0.get() ) ) ),
_delta_frame_time(0.01), _last_frame_time(0.0),
_modelSize( uim._modelSize ),
_verticalAxisFixed( uim._verticalAxisFixed ),
_flags( uim._flags ),
_relativeFlags( uim._relativeFlags )
{
}
/** Attach a node to the manipulator.
Automatically detaches previously attached node.
setNode(NULL) detaches previously attached nodes.
Is ignored by manipulators which do not require a reference model.*/
void StandardManipulator::setNode( Node* node )
{
_node = node;
// update model size
if( _node.get() )
{
const BoundingSphere& boundingSphere = _node->getBound();
_modelSize = boundingSphere.radius();
}
else
{
_modelSize = 0.;
}
// compute home position
if( getAutoComputeHomePosition() )
computeHomePosition( NULL, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 );
}
/** Return node if attached.*/
const Node* StandardManipulator::getNode() const
{
return _node.get();
}
/** Return node if attached.*/
Node* StandardManipulator::getNode()
{
return _node.get();
}
/** Makes manipulator to keep camera's "UP" vector.
*
* In general, fixed up vector makes camera control more user friendly.
*
* To change up vector, use CameraManipulator::setCoordinateFrameCallback.*/
void StandardManipulator::setVerticalAxisFixed( bool value )
{
_verticalAxisFixed = value;
}
/// Sets manipulator animation time when centering on mouse wheel up is enabled.
void StandardManipulator::setAnimationTime( const double t )
{
if( t <= 0. )
{
finishAnimation();
_animationData = NULL;
return;
}
if( !_animationData )
allocAnimationData();
_animationData->_animationTime = t;
}
/// Returns manipulator animation time when centering on mouse wheel up is
enabled.
double StandardManipulator::getAnimationTime() const
{
if( _animationData )
return _animationData->_animationTime;
else
return 0.;
}
/// Returns whether manipulator is performing animation at the moment.
bool StandardManipulator::isAnimating() const
{
if( _animationData )
return _animationData->_isAnimating;
else
return false;
}
/// Finishes the animation by performing a step that moves it to its final
position.
void StandardManipulator::finishAnimation()
{
_thrown = false;
if( !isAnimating() )
return;
applyAnimationStep( 1., _animationData->_phase );
}
/** Move the camera to the default position.
The user should probably want to use home(GUIEventAdapter&,
GUIActionAdapter&)
instead to set manipulator to the home position. This method does not
trigger
any redraw processing or updates continuous update processing.
StandardManipulator implementation only updates its internal structures and
recomputes its home position if autoComputeHomePosition is set.
Descendant classes are expected to update camera position.*/
void StandardManipulator::home( double /*currentTime*/ )
{
if( getAutoComputeHomePosition() )
computeHomePosition( NULL, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 );
_thrown = false;
setTransformation( _homeEye, _homeCenter, _homeUp );
flushMouseEventStack();
}
/** Move the camera to the default position.
If autoComputeHomePosition is on, home position is computed.
The computation considers camera fov and model size and
positions camera far enough to fit the model to the screen.
StandardManipulator implementation only updates its internal data.
If home position is expected to be supported by the descendant manipulator,
it has to reimplement the method to update manipulator transformation.*/
void StandardManipulator::home( const GUIEventAdapter& /*ea*/,
GUIActionAdapter& us )
{
if( getAutoComputeHomePosition() )
{
const Camera *camera = us.asView() ? us.asView()->getCamera() : NULL;
computeHomePosition( camera, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0
);
}
_thrown = false;
setTransformation( _homeEye, _homeCenter, _homeUp );
us.requestRedraw();
us.requestContinuousUpdate( false );
flushMouseEventStack();
}
/** Start/restart the manipulator.*/
void StandardManipulator::init( const GUIEventAdapter& /*ea*/,
GUIActionAdapter& us )
{
flushMouseEventStack();
// stop animation
_thrown = false;
us.requestContinuousUpdate(false);
}
/** Handles events. Returns true if handled, false otherwise.*/
bool StandardManipulator::handle( const GUIEventAdapter& ea, GUIActionAdapter&
us )
{
switch( ea.getEventType() )
{
case GUIEventAdapter::FRAME:
return handleFrame( ea, us );
case GUIEventAdapter::RESIZE:
return handleResize( ea, us );
default:
break;
}
if( ea.getHandled() )
return false;
switch( ea.getEventType() )
{
case GUIEventAdapter::MOVE:
return handleMouseMove( ea, us );
case GUIEventAdapter::DRAG:
return handleMouseDrag( ea, us );
case GUIEventAdapter::PUSH:
return handleMousePush( ea, us );
case GUIEventAdapter::RELEASE:
return handleMouseRelease( ea, us );
case GUIEventAdapter::KEYDOWN:
return handleKeyDown( ea, us );
case GUIEventAdapter::KEYUP:
return handleKeyUp( ea, us );
case GUIEventAdapter::SCROLL:
if( _flags & PROCESS_MOUSE_WHEEL )
return handleMouseWheel( ea, us );
else
return false;
default:
return false;
}
}
/// Handles GUIEventAdapter::FRAME event.
bool StandardManipulator::handleFrame( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
double current_frame_time = ea.getTime();
_delta_frame_time = current_frame_time - _last_frame_time;
_last_frame_time = current_frame_time;
if( _thrown && performMovement() )
{
us.requestRedraw();
}
if( _animationData && _animationData->_isAnimating )
{
performAnimationMovement( ea, us );
}
return false;
}
/// Handles GUIEventAdapter::RESIZE event.
bool StandardManipulator::handleResize( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
init( ea, us );
us.requestRedraw();
return true;
}
/// Handles GUIEventAdapter::MOVE event.
bool StandardManipulator::handleMouseMove( const GUIEventAdapter& /*ea*/,
GUIActionAdapter& /*us*/ )
{
return false;
}
/// Handles GUIEventAdapter::DRAG event.
bool StandardManipulator::handleMouseDrag( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
addMouseEvent( ea );
if( performMovement() )
us.requestRedraw();
us.requestContinuousUpdate( false );
_thrown = false;
return true;
}
/// Handles GUIEventAdapter::PUSH event.
bool StandardManipulator::handleMousePush( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
flushMouseEventStack();
addMouseEvent( ea );
if( performMovement() )
us.requestRedraw();
us.requestContinuousUpdate( false );
_thrown = false;
return true;
}
/// Handles GUIEventAdapter::RELEASE event.
bool StandardManipulator::handleMouseRelease( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
if( ea.getButtonMask() == 0 )
{
double timeSinceLastRecordEvent = _ga_t0.valid() ? (ea.getTime() -
_ga_t0->getTime()) : DBL_MAX;
if( timeSinceLastRecordEvent > 0.02 )
flushMouseEventStack();
if( isMouseMoving() )
{
if( performMovement() && _allowThrow )
{
us.requestRedraw();
us.requestContinuousUpdate( true );
_thrown = true;
}
return true;
}
}
flushMouseEventStack();
addMouseEvent( ea );
if( performMovement() )
us.requestRedraw();
us.requestContinuousUpdate( false );
_thrown = false;
return true;
}
/// Handles GUIEventAdapter::KEYDOWN event.
bool StandardManipulator::handleKeyDown( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
if( ea.getKey() == GUIEventAdapter::KEY_Space )
{
flushMouseEventStack();
_thrown = false;
home(ea,us);
return true;
}
return false;
}
/// Handles GUIEventAdapter::KEYUP event.
bool StandardManipulator::handleKeyUp( const GUIEventAdapter& /*ea*/,
GUIActionAdapter& /*us*/ )
{
return false;
}
/// Handles GUIEventAdapter::SCROLL event.
bool StandardManipulator::handleMouseWheel( const GUIEventAdapter& /*ea*/,
GUIActionAdapter& /*us*/ )
{
return false;
}
/** Get the keyboard and mouse usage of the manipulator.*/
void StandardManipulator::getUsage( ApplicationUsage& usage ) const
{
usage.addKeyboardMouseBinding( getManipulatorName() + ": Space", "Reset the
viewing position to home" );
}
/// Make movement step of manipulator. Returns true if any movement was made.
bool StandardManipulator::performMovement()
{
// return if less then two events have been added
if( _ga_t0.get() == NULL || _ga_t1.get() == NULL )
return false;
// get delta time
double eventTimeDelta = _ga_t0->getTime() - _ga_t1->getTime();
if( eventTimeDelta < 0. )
{
OSG_WARN << "Manipulator warning: eventTimeDelta = " << eventTimeDelta
<< std::endl;
eventTimeDelta = 0.;
}
// get deltaX and deltaY
float dx = _ga_t0->getXnormalized() - _ga_t1->getXnormalized();
float dy = _ga_t0->getYnormalized() - _ga_t1->getYnormalized();
// return if there is no movement.
if( dx == 0. && dy == 0. )
return false;
// call appropriate methods
unsigned int buttonMask = _ga_t1->getButtonMask();
if( buttonMask == GUIEventAdapter::LEFT_MOUSE_BUTTON )
{
return performMovementLeftMouseButton( eventTimeDelta, dx, dy );
}
else if( buttonMask == GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
buttonMask == (GUIEventAdapter::LEFT_MOUSE_BUTTON |
GUIEventAdapter::RIGHT_MOUSE_BUTTON) )
{
return performMovementMiddleMouseButton( eventTimeDelta, dx, dy );
}
else if( buttonMask == GUIEventAdapter::RIGHT_MOUSE_BUTTON )
{
return performMovementRightMouseButton( eventTimeDelta, dx, dy );
}
return false;
}
/** Make movement step of manipulator.
This method implements movement for left mouse button.*/
bool StandardManipulator::performMovementLeftMouseButton( const double
/*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
{
return false;
}
/** Make movement step of manipulator.
This method implements movement for middle mouse button
or combination of left and right mouse button pressed together.*/
bool StandardManipulator::performMovementMiddleMouseButton( const double
/*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
{
return false;
}
/** Make movement step of manipulator.
This method implements movement for right mouse button.*/
bool StandardManipulator::performMovementRightMouseButton( const double
/*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
{
return false;
}
/// The method processes events for manipulation based on relative mouse
movement (mouse delta).
bool StandardManipulator::handleMouseDeltaMovement( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
float dx = ea.getX() - _mouseCenterX;
float dy = ea.getY() - _mouseCenterY;
if( dx == 0.f && dy == 0.f )
return false;
addMouseEvent( ea );
centerMousePointer( ea, us );
return performMouseDeltaMovement( dx, dy );
}
/// The method performs manipulator update based on relative mouse movement
(mouse delta).
bool StandardManipulator::performMouseDeltaMovement( const float /*dx*/, const
float /*dy*/ )
{
return false;
}
/// Makes the manipulator progress in its current animation.
bool StandardManipulator::performAnimationMovement( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
double f = (ea.getTime() - _animationData->_startTime) /
_animationData->_animationTime;
if( f >= 1. )
{
f = 1.;
_animationData->_isAnimating = false;
if( !_thrown )
us.requestContinuousUpdate( false );
}
applyAnimationStep( f, _animationData->_phase );
_animationData->_phase = f;
us.requestRedraw();
return _animationData->_isAnimating;
}
/// Updates manipulator by a single animation step
void StandardManipulator::applyAnimationStep( const double /*currentProgress*/,
const double /*prevProgress*/ )
{
}
/// Centers mouse pointer
void StandardManipulator::centerMousePointer( const GUIEventAdapter& ea,
GUIActionAdapter& us )
{
_mouseCenterX = (ea.getXmin() + ea.getXmax()) / 2.0f;
_mouseCenterY = (ea.getYmin() + ea.getYmax()) / 2.0f;
us.requestWarpPointer( _mouseCenterX, _mouseCenterY );
}
/** Add the current mouse GUIEvent to internal stack.*/
void StandardManipulator::addMouseEvent( const GUIEventAdapter& ea )
{
_ga_t1 = _ga_t0;
_ga_t0 = &ea;
}
/** Reset the internal GUIEvent stack.*/
void StandardManipulator::flushMouseEventStack()
{
_ga_t1 = NULL;
_ga_t0 = NULL;
}
/** Check the speed at which the mouse is moving.
If speed is below a threshold then return false, otherwise return true.*/
bool StandardManipulator::isMouseMoving() const
{
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
static const float velocity = 0.1f;
float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized();
float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized();
float len = sqrtf(dx*dx+dy*dy);
float dt = _ga_t0->getTime()-_ga_t1->getTime();
return (len>dt*velocity);
}
/** Set the 'allow throw' flag. If it is set to true (default), releasing the
mouse button
while moving the mouse results in a throw. If manipulator was thrown, it
continues spinning
although no mouse button is down at the moment.*/
void StandardManipulator::setAllowThrow( bool allowThrow )
{
_allowThrow = allowThrow;
}
/** Returns the scale that should be applied on animation of "thrown"
manipulator state
to avoid its dependency on varying frame rate.
eventTimeDelta parameter gives the time difference between last two
events that started the animation.*/
float StandardManipulator::getThrowScale( const double eventTimeDelta ) const
{
if( _thrown )
{
if (eventTimeDelta == 0.f)
return 0.f;
return float( _delta_frame_time / eventTimeDelta );
}
else return 1.f;
}
/** Update rotation by yaw and pitch.
*
* localUp parameter defines either camera's "UP" vector
* that will be preserved during rotation, or it can be zero (0,0,0) to specify
* that camera's "UP" vector will be not preserved and free rotation will be
made.*/
void StandardManipulator::rotateYawPitch( Quat& rotation, const double yaw,
const double pitch,
const Vec3d& localUp )
{
bool verticalAxisFixed = (localUp != Vec3d( 0.,0.,0. ));
// fix current rotation
if( verticalAxisFixed )
fixVerticalAxis( rotation, localUp, true );
// rotations
Quat rotateYaw( -yaw, verticalAxisFixed ? localUp : rotation * Vec3d(
0.,1.,0. ) );
Quat rotatePitch;
Quat newRotation;
Vec3d cameraRight( rotation * Vec3d( 1.,0.,0. ) );
double my_dy = pitch;
int i = 0;
do {
// rotations
rotatePitch.makeRotate( my_dy, cameraRight );
newRotation = rotation * rotateYaw * rotatePitch;
// update vertical axis
if( verticalAxisFixed )
fixVerticalAxis( newRotation, localUp, false );
// check for viewer's up vector to be more than 90 degrees from "up"
axis
Vec3d newCameraUp = newRotation * Vec3d( 0.,1.,0. );
if( newCameraUp * localUp > 0. )
{
// apply new rotation
rotation = newRotation;
return;
}
my_dy /= 2.;
if( ++i == 20 )
{
rotation = rotation * rotateYaw;
return;
}
} while( true );
}
/** The method corrects the rotation to make impression of fixed up direction.
* Technically said, it makes the roll component of the rotation equal to zero.
*
* Up vector is given by CoordinateFrame and it is +z by default.
* It can be changed by osgGA::CameraManipulator::setCoordinateFrameCallback().
*
* Eye parameter is user position, rotation is the rotation to be fixed, and
* disallowFlipOver, when set on true, avoids pitch rotation component to grow
* over +/- 90 degrees. If this happens and disallowFlipOver is true,
* manipulator is rotated by 180 degrees. More precisely, roll rotation
component is changed by 180 degrees,
* making pitch once again between -90..+90 degrees limits.*/
void StandardManipulator::fixVerticalAxis( Vec3d& eye, Quat& rotation, bool
disallowFlipOver )
{
CoordinateFrame coordinateFrame = getCoordinateFrame( eye );
Vec3d localUp = getUpVector( coordinateFrame );
fixVerticalAxis( rotation, localUp, disallowFlipOver );
}
/** The method corrects the rotation to make impression of fixed up direction.
* Technically said, it makes the roll component of the rotation equal to zero.
*
* rotation parameter is the rotation to be fixed.
* localUp is UP vector and must not be zero length.
* disallowFlipOver, when set on true, avoids pitch rotation component to grow
* over +/- 90 degrees. If this happens and disallowFlipOver is true,
* manipulator is rotated by 180 degrees. More precisely, roll rotation
component is changed by 180 degrees,
* making pitch once again between -90..+90 degrees limits.*/
void StandardManipulator::fixVerticalAxis( Quat& rotation, const Vec3d&
localUp, bool disallowFlipOver )
{
// camera direction vectors
Vec3d cameraUp = rotation * Vec3d( 0.,1.,0. );
Vec3d cameraRight = rotation * Vec3d( 1.,0.,0. );
Vec3d cameraForward = rotation * Vec3d( 0.,0.,-1. );
// computed directions
Vec3d newCameraRight1 = cameraForward ^ localUp;
Vec3d newCameraRight2 = cameraUp ^ localUp;
Vec3d newCameraRight = (newCameraRight1.length2() >
newCameraRight2.length2()) ?
newCameraRight1 : newCameraRight2;
if( newCameraRight * cameraRight < 0. )
newCameraRight = -newCameraRight;
// vertical axis correction
Quat rotationVerticalAxisCorrection;
rotationVerticalAxisCorrection.makeRotate( cameraRight, newCameraRight );
// rotate camera
rotation *= rotationVerticalAxisCorrection;
if( disallowFlipOver )
{
// make viewer's up vector to be always less than 90 degrees from "up"
axis
Vec3d newCameraUp = newCameraRight ^ cameraForward;
if( newCameraUp * localUp < 0. )
rotation = Quat( PI, Vec3d( 0.,0.,1. ) ) * rotation;
}
}
/** The method corrects the rotation to make impression of fixed up direction.
* Technically said, it makes the roll component of the rotation equal to zero.
*
* forward and up parameters are the forward and up vectors of the manipulator.
* newUp will receive corrected UP manipulator vector. localUp is UP vector
* that is used for vertical correction.
* disallowFlipOver when set on true avoids pitch rotation component to grow
* over +/- 90 degrees. If this happens and disallowFlipOver is true,
* right and up camera vectors are negated (changing roll by 180 degrees),
* making pitch once again between -90..+90 degrees limits.*/
void StandardManipulator::fixVerticalAxis( const osg::Vec3d& forward, const
osg::Vec3d& up, osg::Vec3d& newUp,
const osg::Vec3d& localUp, bool
/*disallowFlipOver*/ )
{
// right direction
osg::Vec3d right1 = forward ^ localUp;
osg::Vec3d right2 = up ^ localUp;
osg::Vec3d right = (right1.length2() > right2.length2()) ? right1 : right2;
// updatedUp
osg::Vec3d updatedUp = right ^ forward;
if( updatedUp.normalize() >= 0. )
// return updatedUp
newUp = updatedUp;
else {
// return original up
OSG_WARN << "StandardManipulator::fixVerticalAxis warning: Can not
update vertical axis." << std::endl;
newUp = up;
}
}
/** The method sends a ray into the scene and the point of the closest
intersection
is used to set a new center for the manipulator. For Orbit-style
manipulators,
the orbiting center is set. For FirstPerson-style manipulators, view is
pointed
towards the center.*/
bool StandardManipulator::setCenterByMousePointerIntersection( const
GUIEventAdapter& ea, GUIActionAdapter& us )
{
osg::View* view = us.asView();
if( !view )
return false;
Camera *camera = view->getCamera();
if( !camera )
return false;
// prepare variables
float x = ( ea.getX() - ea.getXmin() ) / ( ea.getXmax() - ea.getXmin() );
float y = ( ea.getY() - ea.getYmin() ) / ( ea.getYmax() - ea.getYmin() );
LineSegmentIntersector::CoordinateFrame cf;
Viewport *vp = camera->getViewport();
if( vp ) {
cf = Intersector::WINDOW;
x *= vp->width();
y *= vp->height();
} else
cf = Intersector::PROJECTION;
// perform intersection computation
ref_ptr< LineSegmentIntersector > picker = new LineSegmentIntersector( cf,
x, y );
IntersectionVisitor iv( picker.get() );
camera->accept( iv );
// return on no intersections
if( !picker->containsIntersections() )
return false;
// get all intersections
LineSegmentIntersector::Intersections& intersections =
picker->getIntersections();
// get current transformation
osg::Vec3d eye, oldCenter, up;
getTransformation( eye, oldCenter, up );
// new center
osg::Vec3d newCenter = (*intersections.begin()).getWorldIntersectPoint();
// make vertical axis correction
if( getVerticalAxisFixed() )
{
CoordinateFrame coordinateFrame = getCoordinateFrame( newCenter );
Vec3d localUp = getUpVector( coordinateFrame );
fixVerticalAxis( newCenter - eye, up, up, localUp, true );
}
// set the new center
setTransformation( eye, newCenter, up );
// warp pointer
// note: this works for me on standard camera on GraphicsWindowEmbedded and
Qt,
// while it was necessary to implement requestWarpPointer like
follows:
//
// void QOSGWidget::requestWarpPointer( float x, float y )
// {
// osgViewer::Viewer::requestWarpPointer( x, y );
// QCursor::setPos( this->mapToGlobal( QPoint( int( x+.5f ), int( y+.5f
) ) ) );
// }
//
// Additions of .5f are just for the purpose of rounding.
centerMousePointer( ea, us );
return true;
}
/** Makes mouse pointer intersection test with the geometry bellow the pointer
and starts animation to center camera to look at the closest hit bellow the
mouse pointer.
If there is a hit, animation is started and true is returned.
Otherwise, the method returns false.*/
bool StandardManipulator::startAnimationByMousePointerIntersection(
const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& /*us*/ )
{
return false;
}
StandardManipulator::AnimationData::AnimationData()
:_isAnimating( false )
{
}
void StandardManipulator::AnimationData::start( const double startTime )
{
_isAnimating = true;
_startTime = startTime;
_phase = 0.;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org