Setting Camera View Matrix Not Taking Effect

32 views
Skip to first unread message

Erik Hensens

unread,
Apr 21, 2020, 2:43:45 AM4/21/20
to OpenSceneGraph Users
Hello everyone,

I am trying to change the camera's perspective by calling setViewMatrixAsLookAt, but it is not making any change in the orientation of my scene. Here is the full code for the simplest application that reproduces my problem. There must be something I'm fundamentally misunderstanding here. I appreciate any and all help! Thanks in advance!

#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osgViewer/Viewer>


// Get a new sphere geode
osg
::ref_ptr<osg::Geode> CreateSphereGeode(
 osg
::Vec3 tCenter, // The location of the center of the sphere
 osg
::Vec4 tColor, // The color of the sphere
 
float fRadius // The sphere radius
 
)
{
 
// Create the geode to return
 osg
::ref_ptr<osg::Geode> pRetv = new osg::Geode;


 
// Create the sphere
 osg
::ref_ptr<osg::Sphere> pSphere = new osg::Sphere(tCenter, fRadius);


 
// Create the drawable
 osg
::ref_ptr<osg::ShapeDrawable> pSphereDrawable = new osg::ShapeDrawable(pSphere);


 
// Set the color
 pSphereDrawable
->setColor(tColor);


 
// Add the drawable
 pRetv
->addDrawable(pSphereDrawable);


 
// Return this geode
 
return pRetv;
}


// The main application entry point
int main(int argc, char *argv[])
{
 
// Create the viewer
 osg
::ref_ptr<osgViewer::Viewer> pViewer = new osgViewer::Viewer;
 
 
// Create the top group parent
 osg
::ref_ptr<osg::Group> pTopGroup = new osg::Group;


 
// Create some spheres
 osg
::ref_ptr<osg::Geode> pSphere1 = CreateSphereGeode(osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f), 2.0f);
 osg
::ref_ptr<osg::Geode> pSphere2 = CreateSphereGeode(osg::Vec3(10.0f, 0.0f, 5.0f), osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f), 1.0f);
 osg
::ref_ptr<osg::Geode> pSphere3 = CreateSphereGeode(osg::Vec3(-15.0f, 6.0f, 0.0f), osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f), 4.0f);


 
// Add these spheres to the top group node
 pTopGroup
->addChild(pSphere1);
 pTopGroup
->addChild(pSphere2);
 pTopGroup
->addChild(pSphere3);


 
// Here I am attempting to change the initial perspective, but I can make these numbers anything and it doesn't seem to change anything
 pViewer
->getCamera()->setViewMatrixAsLookAt(
 osg
::Vec3(-0.9983f, 0.0252f, -0.0530f),
 osg
::Vec3(0.8639f, -2.5189f, -70.0939f),
 osg
::Vec3(0.8880f, -1.5194f, -70.0726f)
 
);
 
 
// Add a viewport to the viewer and attach the scene graph
 pViewer
->setSceneData(pTopGroup);


 
// Run the viewer
 
return pViewer->run();
}

OpenSceneGraph Users

unread,
Apr 21, 2020, 4:33:54 AM4/21/20
to osg-...@lists.openscenegraph.org
Hi, camera settings are updated frame by frame by the camera
manipulator, so although you are changing the settings, the manipulator
resets them to whatever it wants. It is created by default when you
just call "run()" on a viewer (it can be seen in the source code).

You should modify the camera manipulator to do what you want.

Regards,

--
Alberto

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

OpenSceneGraph Users

unread,
Apr 21, 2020, 8:05:16 PM4/21/20
to osg-...@lists.openscenegraph.org
Worth pointing out that if you don’t want any camera manipulator, you can just call setCameraManipulator(NULL) on each of the Viewer’s views:

Views views;
    pViewer->getViews(views);
    for (Views::iterator it = views.begin(); it != views.end(); it++) {
        (*it)->setCameraManipulator(NULL);

OpenSceneGraph Users

unread,
Apr 22, 2020, 6:49:31 AM4/22/20
to OpenSceneGraph Users
On Wed, 22 Apr 2020 at 11:10, OpenSceneGraph Users <osg-...@lists.openscenegraph.org> wrote:
Worth pointing out that if you don’t want any camera manipulator, you can just call setCameraManipulator(NULL) on each of the Viewer’s views:

Views views;
    pViewer->getViews(views);
    for (Views::iterator it = views.begin(); it != views.end(); it++) {
        (*it)->setCameraManipulator(NULL);
    }

The default TrackballManipulator is only added by the Viewer::run() method when there are no other means for setting the view matrix, so if you don't use the Viewer::run() convenience method and instead roll hour own frame loop then you won't get the fallback TrackballManipulator being added.

To replace the viewer::run() usage all you have to do is:

  viewer.realize();
  while (!viewer.done())
  {
       viewer.frame();
  }

You can also break the viewer.frame() down into it's constituent parts.  Have a look at the Viewer::run(), frame() methods to demystify what's happen in these convenience methods.

Cheers,
Robert


 

 

Erik Hensens

unread,
Apr 22, 2020, 9:20:44 AM4/22/20
to OpenSceneGraph Users
Thank you all so much for your help on this. Generally speaking, what is the proper approach if I want to set the initial view to a specific look-at but after that I want the default manipulator to behave as normal?

Trajce Nikolov NICK

unread,
Apr 22, 2020, 11:24:08 AM4/22/20
to osg-...@googlegroups.com
Hi Eric,

have a look at CameraManipulator->setHomePosition()

--
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/5a697087-a62b-4a5a-b8ab-15ab1c25cfdc%40googlegroups.com.


--
trajce nikolov nick

Erik Hensens

unread,
Apr 22, 2020, 1:22:30 PM4/22/20
to OpenSceneGraph Users
Beautiful, that did the trick. Thank you all for all of your help. I've learned something.


On Tuesday, April 21, 2020 at 1:43:45 AM UTC-5, Erik Hensens wrote:
Reply all
Reply to author
Forward
0 new messages