[osg-users] setViewMatrixAsLookAt not working to set the initial camera position

30 views
Skip to first unread message

Rodrigo Dias

unread,
Jan 7, 2019, 9:48:14 PM1/7/19
to osg-...@lists.openscenegraph.org
Hi,

I'm able to change the camera's position with TrackballManipulator. However, I'd like to change the camera's initial position, but all the examples I've found just won't work. My source code is:


Code:
#include <iostream>
#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgEarth/ImageLayer>
#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarthDrivers/tms/TMSOptions>
#include <osgEarthDrivers/gdal/GDALOptions>

using namespace std;
using namespace osg;
using namespace osgEarth;
using namespace osgEarth::Drivers;

int main (int argc, char** argv) {
MapOptions mapOpt;
mapOpt.coordSysType() = MapOptions::CSTYPE_PROJECTED;
mapOpt.profile() = ProfileOptions("plate-carre");
osg::ref_ptr<Map> map = new Map(mapOpt);
{
GDALOptions gdal;
gdal.url() = "br_modified.tif";
osg::ref_ptr<ImageLayer> layer = new ImageLayer( "BR", gdal );
map->addLayer( layer );
}
{
GDALOptions gdal;
gdal.url() = "BRalt.tif";
osg::ref_ptr<ElevationLayer> layer = new ElevationLayer( "SRTM", gdal );
map->addLayer( layer );
}
osg::ref_ptr<MapNode> mapNode = new MapNode( map );
osgViewer::Viewer viewer;
viewer.setSceneData( mapNode.get() );
viewer.setCameraManipulator( new osgGA::TrackballManipulator );

Vec3 eye( 10000.0, 50000.0, 100000.0 ); // The position of your camera
Vec3 center( 0.0, 0.0, 0.0 ); // The point your camera is looking at
Vec3 up( 0.0, 1.0, 0.0 ); // The up-vector of your camera - this controls how your viewport will be rotated about its center and should be equal to [0, 1, 0] in a conventional graphics coordinate system
viewer.getCamera()->setViewMatrixAsLookAt( eye, center, up );
while ( !viewer.done() ) {
viewer.frame();
}
return 0;
}




No matter which values I use for "eye", the camera always begins in the same position. Why is that?

Thank you!

Cheers,
Rodrigo

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





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

sam

unread,
Jan 7, 2019, 10:01:34 PM1/7/19
to OpenSceneGraph Users
Hey Rodrigo,

You have to set the home position of your camera manipulator and make a call to home(1). You can force the delta time with a 1.

Sam

Rodrigo Dias

unread,
Jan 7, 2019, 10:16:53 PM1/7/19
to osg-...@lists.openscenegraph.org
Hi Sam,

By "home" I think you mean


Code:
viewer.getCameraManipulator()->setHomePosition( eye, center, up );




This really changed camera's position, though it's totally freaking out now (I'm still fiddling with the values in eye, center and up).

But I have no idea what you meant by "delta time", something to go inside the loop?

Thank you!

Cheers,
Rodrigo

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

sam

unread,
Jan 7, 2019, 10:39:53 PM1/7/19
to OpenSceneGraph Users
After you set the home position try: viewer.getCameraManipulator()->home(1);

Sorry I'm not right in front of my computer right now.

Rodrigo Dias

unread,
Jan 7, 2019, 10:54:36 PM1/7/19
to osg-...@lists.openscenegraph.org
I just tried it, but still the camera is messed up. No matter which numbers I choose to eye, center and up, it never behaves as default. I have not even a clue about the difference between 10, 100 and 1000, which one should I use? (I'm using osgEarth's Map and MapNode, btw, which are displaying a whole flat white Earth, with only the desired country drawn with a texture) I tried using getViewMatrixAsLookAt with the default values, but it's still a mess. I've looked at many questions here, and in some of the source code, but nothing clears this up.

Thank you!

Cheers,
Rodrigo

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

Rodrigo Dias

unread,
May 12, 2020, 5:04:54 AM5/12/20
to OpenSceneGraph Users
Back at it again...

I'm using a Brazil georeferenced map, so I know what coordinates are good enough to be above the country, high enough to see it all, looking down:

(...)
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    osg::ref_ptr<OVNIController> controller = new OVNIController(&viewer); // my own WASD flying controller
    viewer.setCameraManipulator(controller);
    viewer.getCamera()->setViewMatrixAsLookAt( osg::Vec3(-55.0f,-15.0f,50.0f), osg::Vec3(), osg::Y_AXIS ); // eye (lon, lat, alt), center, up
    viewer.getCameraManipulator()->home(1);
    viewer.realize();

    while ( !viewer.done() ) { viewer.frame(); }
    return 0;

I don't know if setViewMatrixAsLookAt() and home() should go before or after realize(). Played with the axis as 3rd parameter for setViewMatrixAsLookAt, but the camera just won't move! Any help, please?
Reply all
Reply to author
Forward
0 new messages