[osg-users] Positioning the viewers camera

718 views
Skip to first unread message

Kaiser, Hagen (CT)

unread,
Jun 6, 2008, 10:10:25 AM6/6/08
to osg-...@lists.openscenegraph.org



Hello everyone,
I am desparately trying to position a viewers camera manually and correctly.
My give data give me a Position Vector a RotationAxis and a Rotation Angle.
And a float that is named fieldofView.

Here is how I implement it:
osgGA::TrackballManipulator* man = (osgGA::TrackballManipulator*)this->viewer->getCameraManipulator();
osg::Matrix trans;
trans.makeTranslate(osg::Vec3(position.x(),position.y(),position.z()));
osg::Matrix rot;
rot.makeRotate(Angle,osg::Vec3(Axis.x,Axis.y,Axis.z)));
osg::Matrix cam = rot*trans;
this->viewer->setByMatrix(cam)
}

But I never get the right position or rotation.
After testing around, I noticed the following:

Changing
trans.makeTranslate(…
to trans.makeTranslate(osg::Vec3(position.x, -position.z, position.y));

I somehow get the right position.
But the right rotation??

I found a tutorial somewhere, where it says Everything under osgGa:: Namespace has another AxisOrientation.
And in the tutorial there is something said about producer::rotate.
What is Producer. This namespace doesnt even exist in OpenSceneGraph;

Please help!!
Thx in advance
Greetings to everyone

Alejandro Segovia

unread,
Jun 6, 2008, 10:23:32 AM6/6/08
to OpenSceneGraph Users
Hello,

On Fri, Jun 6, 2008 at 11:10 AM, Kaiser, Hagen (CT) <hagen....@siemens.com> wrote:

Hello everyone,
I am desparately trying to position a viewers camera manually and correctly.
My give data give me a Position Vector a RotationAxis and a Rotation Angle.
And a float that is named fieldofView.

Here is how I implement it:
osgGA::TrackballManipulator* man = (osgGA::TrackballManipulator*)this->viewer->getCameraManipulator();
osg::Matrix trans;
trans.makeTranslate(osg::Vec3(position.x(),position.y(),position.z()));
osg::Matrix rot;
rot.makeRotate(Angle,osg::Vec3(Axis.x,Axis.y,Axis.z)));
osg::Matrix cam = rot*trans;
this->viewer->setByMatrix(cam)
}

But I never get the right position or rotation.
After testing around, I noticed the following:

Changing
trans.makeTranslate(…
to trans.makeTranslate(osg::Vec3(position.x, -position.z, position.y));

Have you tried making a translation of (-x, -y, -z) instead? Remember that placing the camera at some point P is actually applying a translation of -P to the whole world.

cam = rot*trans seems fine.

Let me know how it went.

Alejandro.-

--
Varrojo@Linux
http://varrojo.linuxuruguay.org

Kaiser, Hagen (CT)

unread,
Jun 6, 2008, 10:33:17 AM6/6/08
to OpenSceneGraph Users
Thanks for the idea,
But that doesnt seem to bring me any furhter.
Coordinates are somthing like 500, -5,50 so making this all minus
brings me so far away, that i cant see anything anymore
 
This is what I found in atutorial about that problem:

To position a camera manually, we can use the viewer class setViewByMatrix() method. This call should be placed between the viewer.update and viewer.frame calls within the simulation loop. The following guidelines are useful:

  • Producer and all classes below osgGA::MatrixManipulator (in terms of abstraction) use 'Y' up coordinates. All others -including the Viewer class matrix manipulators - use 'Z' up.
  • The inverse of a position/orientation matrix can be used to orient a camera.
 
This correseponds to my makeTranslate call, I think.
 

Von: osg-user...@lists.openscenegraph.org [mailto:osg-user...@lists.openscenegraph.org] Im Auftrag von Alejandro Segovia
Gesendet: Freitag, 6. Juni 2008 16:24
An: OpenSceneGraph Users
Betreff: Re: [osg-users] Positioning the viewers camera

Paul Melis

unread,
Jun 6, 2008, 10:35:59 AM6/6/08
to OpenSceneGraph Users
Kaiser, Hagen (CT) wrote:

> Thanks for the idea,
> But that doesnt seem to bring me any furhter.
> Coordinates are somthing like 500, -5,50 so making this all minus
> brings me so far away, that i cant see anything anymore
> This is what I found in atutorial about that problem:
>
> To position a camera manually, we can use the viewer class
> setViewByMatrix() method. This call should be placed between the
> viewer.update and viewer.frame calls within the simulation loop. The
> following guidelines are useful:
>

> * Producer and all classes below osgGA::MatrixManipulator (in


> terms of abstraction) use 'Y' up coordinates. All others
> -including the Viewer class matrix manipulators - use 'Z' up.

> * The inverse of a position/orientation matrix can be used to


> orient a camera.
>
> This correseponds to my makeTranslate call, I think.

Just a check: is the rotation value you apply in degrees or radians?
makeRotate() expects a value in radians...

Paul

> ------------------------------------------------------------------------
> *Von:* osg-user...@lists.openscenegraph.org
> [mailto:osg-user...@lists.openscenegraph.org] *Im Auftrag von
> *Alejandro Segovia
> *Gesendet:* Freitag, 6. Juni 2008 16:24
> *An:* OpenSceneGraph Users
> *Betreff:* Re: [osg-users] Positioning the viewers camera


>
> Hello,
>
> On Fri, Jun 6, 2008 at 11:10 AM, Kaiser, Hagen (CT)
> <hagen....@siemens.com <mailto:hagen....@siemens.com>> wrote:
>
>
> Hello everyone,
> I am desparately trying to position a viewers camera manually and
> correctly.
> My give data give me a Position Vector a RotationAxis and a
> Rotation Angle.
> And a float that is named fieldofView.
> Here is how I implement it:
> osgGA::TrackballManipulator* man =
> (osgGA::TrackballManipulator*)this->viewer->getCameraManipulator();
> osg::Matrix trans;
> trans.makeTranslate(osg::Vec3(position.x(),position.y(),position.z()));
> osg::Matrix rot;
> rot.makeRotate(Angle,osg::Vec3(Axis.x,Axis.y,Axis.z)));
> osg::Matrix cam = rot*trans;
> this->viewer->setByMatrix(cam)
> }
> But I never get the right position or rotation.
> After testing around, I noticed the following:
> Changing
> trans.makeTranslate(…

> to trans.makeTranslate(osg::Vec3(position.x,* -position.z,
> position.y*));


>
> Have you tried making a translation of (-x, -y, -z) instead? Remember
> that placing the camera at some point P is actually applying a
> translation of -P to the whole world.
>
> cam = rot*trans seems fine.
>
> Let me know how it went.
>
> Alejandro.-
>
> --
> Varrojo@Linux
> http://varrojo.linuxuruguay.org
>

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

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

Kaiser, Hagen (CT)

unread,
Jun 6, 2008, 10:45:07 AM6/6/08
to OpenSceneGraph Users
It is in radians.
But do you know something about those diffent Up-Vectors?

-----Ursprüngliche Nachricht-----
Von: osg-user...@lists.openscenegraph.org [mailto:osg-user...@lists.openscenegraph.org] Im Auftrag von Paul Melis
Gesendet: Freitag, 6. Juni 2008 16:36
An: OpenSceneGraph Users
Betreff: Re: [osg-users] Positioning the viewers camera

Kaiser, Hagen (CT) wrote:

Paul

> trans.makeTranslate(...

Alejandro Segovia

unread,
Jun 6, 2008, 10:59:30 AM6/6/08
to OpenSceneGraph Users
On Fri, Jun 6, 2008 at 11:45 AM, Kaiser, Hagen (CT) <hagen....@siemens.com> wrote:
It is in radians.
But do you know something about those diffent Up-Vectors?

I didn't notice those vectors were different since I user SceneView directly, instead of osgViewer::Viewer.

You can change the coordinate system for all these objects by creating your own CoordinateFrameCallback class whose getCoordinateFrame method returns a matrix corresponding to whatever you want the Axes to be. The coordinate frame callback can be passed to the MatrixManipulators to change what they "think" the axes are like.

Zoltán

unread,
Jun 6, 2008, 11:15:59 AM6/6/08
to OpenSceneGraph Users
Kaiser, Hagen (CT) wrote on Friday 06 June 2008:
> It is in radians.
> But do you know something about those diffent Up-Vectors?

This is a basic convenion:
- the World (incl OSG) uses X-Y horizontal plane, and Z is
vertical (UP)
- opticians use X-Y as the plane you look at (like the
screen) and Z is the depth (ever heard about a Z-buffer ?)

So "UP" is in the world the Z axis (0,0,1) and Y axis
(0,1,0) in optics, like a camera


Zoltán

> >----------------- *Von:*


> > osg-user...@lists.openscenegraph.org
> > [mailto:osg-user...@lists.openscenegraph.org] *Im
> > Auftrag von *Alejandro Segovia
> > *Gesendet:* Freitag, 6. Juni 2008 16:24
> > *An:* OpenSceneGraph Users
> > *Betreff:* Re: [osg-users] Positioning the viewers
> > camera
> >
> > Hello,
> >
> > On Fri, Jun 6, 2008 at 11:10 AM, Kaiser, Hagen (CT)
> > <hagen....@siemens.com
> > <mailto:hagen....@siemens.com>> wrote:
> >
> >
> > Hello everyone,
> > I am desparately trying to position a viewers
> > camera manually and correctly.
> > My give data give me a Position Vector a
> > RotationAxis and a Rotation Angle.
> > And a float that is named fieldofView.
> > Here is how I implement it:
> > osgGA::TrackballManipulator* man =
> >
> > (osgGA::TrackballManipulator*)this->viewer->getCameraMa
> >nipulator(); osg::Matrix trans;
> >
> > trans.makeTranslate(osg::Vec3(position.x(),position.y()

> >,position.z())); osg::Matrix rot;

--

____________________________________

Zoltan

http://sourceforge.net/projects/zsim
____________________________________

Paul Melis

unread,
Jun 6, 2008, 11:20:40 AM6/6/08
to OpenSceneGraph Users
Kaiser, Hagen (CT) wrote:

>It is in radians.
>But do you know something about those diffent Up-Vectors?
>
>

Well, the default "camera" orientation for OpenGL (i.e. when the
model-view matrix is the identity matrix) is to have the camera at the
origin, with Y being up, and the camera looking down the negative
Z-axis. This also applies to the MatrixManipulator classes. So if a
manipulator returns the identity matrix the view will have Y up.
However, the manipulator classes in OSG seem to default (i.e. home
position when pressing the space bar) to return a matrix in which Z is
up. So the matrix they return includes as a first step a rotation around
the X around by 90 degrees to turn Y up in Z up. More confusing is the
fact that the _inverse matrix_ returned by a matrix manipulator is
actually used as the model-view matrix.

Hmm, I hope I got this all correct, it's so easy to make a mistake with
this stuff...

BTW, you don't have to use a matrix manipulator, as it seems you want
full control over the view orientation (and assuming mouse interaction
is unwanted). In that case you can do

viewer->getCamera()->setViewMatrix(...)

within the frame loop and set your desired model-view matrix there.

Paul

Paul Melis

unread,
Jun 6, 2008, 11:47:08 AM6/6/08
to OpenSceneGraph Users
Paul Melis wrote:

> Kaiser, Hagen (CT) wrote:
>
>> It is in radians.
>> But do you know something about those diffent Up-Vectors?
>>
>>
> Well, the default "camera" orientation for OpenGL (i.e. when the
> model-view matrix is the identity matrix) is to have the camera at the
> origin, with Y being up, and the camera looking down the negative
> Z-axis. This also applies to the MatrixManipulator classes. So if a
> manipulator returns the identity matrix the view will have Y up.
> However, the manipulator classes in OSG seem to default (i.e. home
> position when pressing the space bar) to return a matrix in which Z is
> up. So the matrix they return includes as a first step a rotation
> around the X around by 90 degrees to turn Y up in Z up. More confusing
> is the fact that the _inverse matrix_ returned by a matrix manipulator
> is actually used as the model-view matrix.

To clearify: what is returned by a manipulator with getInverseMatrix()
is used as the model-view matrix

Paul

Reply all
Reply to author
Forward
0 new messages