openscenegraph / OpenSceneGraph

OpenSceneGraph git repository
http://www.openscenegraph.org
Other
3.25k stars 1.42k forks source link

cirtical bug found in 3.6.5 or earlier #1130

Closed XDinEuro closed 2 years ago

XDinEuro commented 2 years ago

I was using openscenegraph 3.4 before, it was super smooth. Recently I am migrating my project to osg 3.6.5 and I found a weird bug about not being able to zoom in or out when wheeling mouse.

And I locate the issue to minimum example.

#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>

int main(int argc, char** argv)
{
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    viewer->setUpViewInWindow(0, 0, 640, 480);
    viewer->setCameraManipulator(new osgGA::TrackballManipulator());
    osg::Node* modelNode = osgDB::readNodeFile("cow.osg");
    osg::ref_ptr<osg::Group> root = new osg::Group();
    viewer->setSceneData( root );
    root->addChild(modelNode);
    viewer->realize();
    while(true){
        viewer->frame();
    }
    return 0;
}

This will give u a all black view, which is inside the cow. and you can't zoom out... But if you put root->addChild(modelNode) one line before, which means before viewer->setSceneData(root), everything works good.

If I remove viewer->setUpViewInWindow(0, 0, 640, 480); , this works again..

Bad View: Screenshot from 2022-04-12 12-37-24

Normal View; Screenshot from 2022-04-12 12-37-47

I didn't have time to try out in which version it turned out like this. I hope someone can give me some feedback if u know the solution.

nmielcarek commented 2 years ago

This is based on memory, but if you assign the scene data with an empty scene, it doesn't know where to place the camera based on the bounding boxes of the not-yet-loaded objects.

If you press spacebar, it re-calculates the scene and places the camera at a new default location. This can also be accomplished using the viewer->home() function.

XDinEuro commented 2 years ago

Thanks for the quick reply. It helps.

May I ask why zoom in/out doesn't work if this happens?