[osg-users] Camera rotation by Mouse

126 views
Skip to first unread message

Andrew Nelson

unread,
Sep 28, 2013, 12:47:28 AM9/28/13
to osg-...@lists.openscenegraph.org
Hi Everyone,

I'm new to using OSG so please forgive me if some of my terms or ideas are incorrect. My overall goal currently is to simply rotate the camera(x-axis, y-axis, and/or z-axis), based on the users mouse position(i.e. when the user moves the mouse in any direction, camera rotates - rather than click and drag). For now, I've just been trying to rotate(or even translate) the camera(in any way) of the BasicGeometry.cpp program just so I can start to get the idea. What I have so far:

.
.
.
//viewer defined as osgViewer::Viewer viewer;
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
viewer.realize();
while( !viewer.done() )
{
osg::Matrixd cameraRotation;
osg::Matrixd cameraTrans;
cameraRotation.makeRotate(osg::DegreesToRadians(-20.0), osg::Vec3(0,1,0), // roll
osg::DegreesToRadians(-15.0), osg::Vec3(1,0,0) , // pitch
osg::DegreesToRadians( 10.0), osg::Vec3(0,0,1) ); // heading
cameraTrans.makeTranslate( 10,-50,15 );
viewer.getCamera()->setViewMatrix(cameraTrans * cameraRotation);
viewer.frame();
}


My thinking is that by setting up a rotation matrix and a translation matrix, the product of these matrices would provided by the desired position. Then, with this we can set the viewer's camera to this desired matrix.

The code compiles and runs, but this doesn't operate as planned. Could someone explain why this process doesn't work and how I should approach this issue?

Any help is much appreciated.
Thank you!

Cheers,
Drew

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=56531#56531





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Mattias Helsing

unread,
Feb 27, 2014, 7:05:35 AM2/27/14
to OpenSceneGraph Users
Hi Drew
The TrackballManipulator is overriding your setting of viewmatrix! The
call to frame will (in the updatetraversal) update the ViewMatrix
local to the TrackbalManipulator, then your viewer goes and collects a
currect ViewMatrix from an attached CameraManipulator (which is the
TrackballManipulator) before rendering.
If you don't want a TrackballManipulator you should call
setCameraManipulator(0), probably after viewer.realize() since that
call installs a TrackballManipulator if none was found. Only then will
your setViewMatrix take effect.
Given your task description what you should probably do is code your
own CameraManipulator. There are some (more or less complex) examples
in the osgGA source, but basically a CameraManipulator is responsible
for responding to events of your choice (mouse, keyboard etc.) and
then produce a ViewMatrix (actually the view retreives the
InverseViewMatrix from a connected CameraManipulator)

Also - see the osgmanipulator example
cheers
/Mattias
Reply all
Reply to author
Forward
0 new messages