[osg-users] Trackball and view question

Poirier, Guillaume Guillaume.Poirier at nrc-cnrc.gc.ca
Tue Oct 2 12:18:49 PDT 2007


I am still stuck on this problem... :( Here is some concrete code so that maybe someone
can help me figure it out and for those who understand code better than english :) 

First we are given a camera position (in world coordinate
system) and an orientation. The orientation is the yaw / pitch / roll to transform the
vector (0,0,1) to the desired camera view direction.

  osg::Vec3 camTrans(0.1443978698344	,
        0.0204530392050	,
        0.0124436690688	);
    osg::Vec3 camRot(5.4461109291	, 
        -4.7768289550	,
        90.9259008041	);

To set the view in the viewer I do:

setView() method

Set view in viewer given camera
translation and rotation.
osg::Vec4 vsCtr(0.0, 0.0, 1.0, 1.0);
osg::Vec4 vsUp(0.0, 1.0, 0.0, 0.0);
osg::Vec4 vsEye(0.0, 0.0, 0.0, 1.0);
osg::Vec4 newCtr, newUp, newEye;
osg::Matrix rotX, rotY, rotZ, transXYZ;
rotX.makeRotate(Deg2Rad(camRot.x()), osg::Vec3(1,0,0));
rotY.makeRotate(Deg2Rad(camRot.y()), osg::Vec3(0,1,0));
rotZ.makeRotate(Deg2Rad(camRot.z()), osg::Vec3(0,0,1));
newCtr  = vsCtr * rotZ * rotY * rotX * transXYZ;
newEye  = vsEye * rotZ * rotY * rotX * transXYZ;
newUp   = vsUp * rotZ * rotY * rotX * transXYZ;
osg::Vec3 v3Eye(newEye.x(), newEye.y(), newEye.z()); 
osg::Vec3 v3Ctr(newCtr.x(), newCtr.y(), newCtr.z()); 
osg::Vec3 v3Up(newUp.x(), newUp.y(), newUp.z());
pMainCam->setViewMatrixAsLookAt(v3Eye, v3Ctr, v3Up);  

This works well. Now I want to be able to have 
a function that gives the translation / orientation
from the current view matrix:

getView() method

Find position and orientation parameters from view matrix

osg::Matrix viewMat = pMainCam->getViewMatrix();
osg::Matrix invViewMat;
osg::Vec3 translate = invViewMat.getTrans();

With this code I can recuperate the initial translation 
(0.1443978698344, 0.0204530392050, 0.0124436690688).

Question 1:

How can I recuperate the initial rotation ?
(5.4461109291, -4.7768289550, 90.9259008041);

Question 2:

I'd like to add a trackball manipulator to my viewer.
I want to then position the view manually similar to what I 
do in my setView() method. When I read back the translation,
the value is very different from (0.1443978698344, 0.0204530392050, 0.0124436690688).
How can I adapt my getView() to handle the trackball and give me
the value that I want ?

Any help / suggestion would be highly appreciated as I am struggling
with this.

best regards,


-----Original Message-----
From: osg-users-bounces at lists.openscenegraph.org on behalf of J.P. Delport
Sent: Fri 9/28/2007 2:36 AM
To: OpenSceneGraph Users
Subject: Re: [osg-users] Trackball and view question

I am attaching an old email below that I think addressed some of the 
same issues you are having.

Also attached is a code snippet of a modified manipulator base class I use.


/** get by distance, rotation, center. */
virtual void getDRC(double& distance, osg::Quat& rotation, osg::Vec3d& 
center) {
    distance = _distance;
    rotation = _rotation;
    center = _center;

/** set by distance, rotation, center. This keeps the focus point 
correct. */
virtual void setByDRC(double distance, osg::Quat rotation, osg::Vec3d 
center) {
    _distance = distance;
    _rotation = rotation;
    _center = center;
    _thrown = false;

Old email:
Hi Morne,

I've done something like this, though not exactly the same. One gotcha I
found was that getting the matrix from the Trackball was not enough to
reset it later. Internally the trackball works with a distance, rotation
and center, (thats a scalar, quat and vec3) and all this info is not
encoded in the final matrix used for drawing. I added getDRC and
setByDRC to the cameras I have.

You can prob find the code for these in the list archives. If you don't,
let me know.


ps. are you in South Africa by any chance?

Morné Pistorius wrote:
 > > Hi,
 > >
 > > I have a scene with various models loaded.  I want to be able to double
 > > click on a model and have the camera move and position itself in front
 > > of the model, and afterwards return to the default trackball 
 > >
 > > I looked at the osgcamera and osganimation examples and I'm trying to
 > > come up with a way of doing this.  These are my thoughts:
 > >
 > > 1. Somehow get the current camera position and attitude from the
 > > trackball manipulator.
 > > 2. Compute an animation path from this point to a point in front of the
 > > model.
 > > 3. Replace the trackball manipulator with an AnimationPathManipulator
 > > and set the animation
 > > 4. Once it is done, replace it with with the original trackball
 > > manipulator and set its home position to the current camera position.
 > >
 > > Does this sound like the right way to go forward?  I would appreciate
 > > any feedback on this, pointers or gothcas from people with more
 > > experience than I have.  Also, if there are any examples that 
better fit
 > > what I am trying to do, I would be much obliged.
 > >
 > > Thank you kindly,
 > > Morne
 > >


Poirier, Guillaume wrote:
> Well this is what I was trying, using viewer.getCamera()->getViewMatrixAsLookAt(osg::Vec3 &eye, osg::Vec3 
> &center, osg::Vec3 &up, float lookDistance=1.0f). In step 2 (with trackball manipulator) I read back the 
> eye, center, and up values in the camera post draw callback. When the pose is almost identical as in the 
> first step, I read the values and they look quite different than what I was setting in the first step. I am still 
> unclear whether this is "normal" or not. So my question is, if it's "normal", is there a transformation 
> I can do to recuperate the step 1 values ? Or do I need to implement a custom view manipulator to achieve
> what I am looking for ?
> regards,
> bill
> -----Original Message-----
> From: osg-users-bounces at lists.openscenegraph.org on behalf of Alberto Luaces
> Sent: Thu 9/27/2007 4:38 AM
> To: OpenSceneGraph Users
> Subject: Re: [osg-users] Trackball and view question
> Hi Robert et al.
> Let me explain what I want to do accomplish an example:
> I load a model with no camera manipulator. I know the camera parameters
> (eye, ctr, up)
> that yield a desired pose. I set the viewer camera with those parameters and
> obtain the
> desired pose.
> Now I want to assume that I don't know those parameters. A user loads a
> model with a trackball
> manipulator. He / she can manipulate the model until the desired pose is
> obtained and
> recuperate the camera parameters (eye, ctr, up), which should be what I used
> in the previous
> step...
> Is that more clear ? How would I achieve this ?
> regards,
> Bill
> What about osg::Camera::getViewMatrixAsLookAt (osg::Vec3 &eye, osg::Vec3 
> &center, osg::Vec3 &up, float lookDistance=1.0f) ?
> Alberto

This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. 
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner, 
and is believed to be clean.  MailScanner thanks Transtec Computers for their support.

osg-users mailing list
osg-users at lists.openscenegraph.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 6672 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20071002/f26dc084/attachment-0002.bin>

More information about the osg-users mailing list