bjornblissing / osgoculusviewer

An OsgViewer with support for the Oculus Rift
Other
106 stars 67 forks source link

Viewer destructor incomplete, restart not possible #49

Closed Thantalos closed 6 years ago

Thantalos commented 9 years ago

Just tested the viewer in my setup. Works really well, thank you. Just one bug I came across: When the viewer is closed, it can't be restarted. I will look into it myself too.

Here just a simple modification of the viewerexample.cpp, demonstrating the issue:

#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include "oculusviewer.h"
#include "oculuseventhandler.h"
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgUtil/GLObjectsVisitor>

void view()
{
    // read the scene from the list of file specified command line arguments.
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osgt");

    // Create Trackball manipulator
    osg::ref_ptr<osgGA::CameraManipulator> cameraManipulator = new osgGA::TrackballManipulator;
    const osg::BoundingSphere& bs = loadedModel->getBound();

    if (bs.valid()) {
        // Adjust view to object view
        osg::Vec3 modelCenter = bs.center();
        osg::Vec3 eyePos = bs.center() + osg::Vec3(0, bs.radius(), 0);
        cameraManipulator->setHomePosition(eyePos, modelCenter, osg::Vec3(0, 0, 1));
    }

    // Open the HMD
    float nearClip = 0.01f;
    float farClip = 10000.0f;
    float pixelsPerDisplayPixel = 1.0;
    float worldUnitsPerMetre = 1.0f;
    osg::ref_ptr<OculusDevice> oculusDevice = new OculusDevice(nearClip, farClip, pixelsPerDisplayPixel, worldUnitsPerMetre);

    // Get the suggested context traits
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = oculusDevice->graphicsContextTraits();
    traits->windowName = "OsgOculusViewerExample";

    // Create a graphic context based on our desired traits
    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits);
    if (!gc) {
        osg::notify(osg::NOTICE) << "Error, GraphicsWindow has not been created successfully" << std::endl;
    }

    if (gc.valid()) {
        gc->setClearColor(osg::Vec4(0.2f, 0.2f, 0.4f, 1.0f));
        gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    osgViewer::Viewer viewer;
    // Force single threaded to make sure that no other thread can use the GL context
    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
    viewer.getCamera()->setGraphicsContext(gc);
    viewer.getCamera()->setViewport(0, 0, traits->width, traits->height);

    // Disable automatic computation of near and far plane
    viewer.getCamera()->setComputeNearFarMode( osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR );
    viewer.setCameraManipulator(cameraManipulator);

    // Things to do when viewer is realized
    osg::ref_ptr<OculusRealizeOperation> oculusRealizeOperation = new OculusRealizeOperation(oculusDevice); 
    viewer.setRealizeOperation(oculusRealizeOperation.get());

    osg::ref_ptr<OculusViewer> oculusViewer = new OculusViewer(&viewer, oculusDevice, oculusRealizeOperation);
    oculusViewer->addChild(loadedModel);
    viewer.setSceneData(oculusViewer);
    // Add statistics handler
    viewer.addEventHandler(new osgViewer::StatsHandler);

    viewer.addEventHandler(new OculusEventHandler(oculusDevice));

    // Realize viewer
    viewer.realize();

    // Start Viewer
    while (!viewer.done()) {
        viewer.frame();
    }
}

int main( int argc, char** argv )
{
    view();

    view();

    return 0;
}
bjornblissing commented 9 years ago

I guess this is SDK v0.5.0.1 you are using?

My guess would be some resource which is not destructed correctly inside the library.

Thantalos commented 9 years ago

No, this is with v0.6.0.1.0 I would also assume, that there is something missing in the destructor, or destructed in the wrong order.

bjornblissing commented 9 years ago

Well, the v0.6 branch of the osgoculusviewer is still unstable. So I would expect these types of problems. But if you are able to track down the problem we could hopefully fix it.

bjornblissing commented 9 years ago

I have fixed some of these problems in: 6885e52ba13bddb8e3eff18a4a0673e3d2acd41a

But there is still the open issue with the destruction of the mirrorFBO. To handle this we need to be able to detect when the graphic context is about to close and run the destruction of the MirrorTexture.

bjornblissing commented 8 years ago

@Thantalos I just tried your example with v1.5 of the Oculus runtime. And it do not crash anymore, so Oculus must have plugged this bug. But I am not entirely happy with not closing the resources correctly when destroying the mirror texture.

I have submitted a pull request to the OpenSceneGraph project which includes a cleanUp operator for the graphic contexts (openscenegraph/OpenSceneGraph@ed7d49c5d2169dc422dad37bdbe334250fc7362e). This will probably be included in the upcoming OSG v3.6 release. And as soon as that has been released I will updated the code to support that functionality.