[osg-users] Executing code before gl context is closed when viewer is shutdown

Chris Denham c.m.denham at gmail.com
Thu Jan 7 09:41:58 PST 2016


Hi Bjorn & Robert,

I've been pondering a related problem with shutting down openvr in an integration I've been working on (Bjorn: which is partly based on some of your great work on Oculus integration!)

I think I came to same conclusion as Robert suggests, which was to perform cleanup immediately after the viewer run loop is terminated. The only "gotcha" I found in doing that was if I terminate the application by closing the viewer window rather than pressing <Esc>. If I quit the app by clicking on the window close button, OSG disposes of the contexts BEFORE the termination of the run loop. My fix for this problem was to intercept the CLOSE_WINDOW event an replace it with a quit application event. It's a bit of an ugly thing to have to do, so I wondered if there was a better way than this:

Code:

class GraphicsWindowViewer : public osgViewer::Viewer
{
public:
	GraphicsWindowViewer(osg::ArgumentParser& arguments, osgViewer::GraphicsWindow* graphicsWindow)
		: osgViewer::Viewer(arguments), _graphicsWindow(graphicsWindow) { }

	virtual void eventTraversal()
	{
		if (_graphicsWindow.valid() && _graphicsWindow->checkEvents())
		{
			osgGA::EventQueue::Events events;
			_graphicsWindow->getEventQueue()->copyEvents(events);
			osgGA::EventQueue::Events::iterator itr;
			for (itr = events.begin(); itr != events.end(); ++itr)
			{
				osgGA::GUIEventAdapter* event = itr->get();
				if (event->getEventType() == osgGA::GUIEventAdapter::CLOSE_WINDOW)
				{
					// We have "peeked" at the event queue for the GraphicsWindow and 
					// found a CLOSE_WINDOW event. This would normally mean that OSG 
					// is about to release OpenGL contexts attached to the window.
					// For some applications it is better to make the application
					// terminate in a more "normal" way e.g. as it does when <Esc>
					// key had been pressed.
					// In this way we can safely perform any cleanup required after
					// termination of the Viewer::run() loop, i.e. cleanup that would 
					// otherwise malfunction if the earlier processing of the CLOSE_WINDOW
					// event had already released required OpenGL contexts.
					// So, here we "get in early" and remove the close event and append
					// a quit application event.
					events.erase(itr);
					_graphicsWindow->getEventQueue()->setEvents(events);
					_graphicsWindow->getEventQueue()->quitApplication();
					break;
				}
			}
		}
		osgViewer::Viewer::eventTraversal();
	}
private:
	osg::ref_ptr<osgViewer::GraphicsWindow> _graphicsWindow;
};






Chris[/code]

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







More information about the osg-users mailing list