Commit 91b7a5c2 authored by Christian Heine's avatar Christian Heine
Browse files

[FIX] OSG/Qt4 bindings for improved portability (i.e. OSX)

parent 5d80e095
......@@ -28,24 +28,12 @@
#include "exceptions/WGEInitFailed.h"
WGEGraphicsWindow::WGEGraphicsWindow( osg::ref_ptr<osg::Referenced> wdata,
int x,
WGEGraphicsWindow::WGEGraphicsWindow( int x,
int y,
int width,
int height )
: m_GraphicsWindow( new osgViewer::GraphicsWindowEmbedded( x, y, width, height ) )
{
m_WindowData = wdata;
// initialize context
try
{
createContext( x, y, width, height );
}
catch( ... )
{
// use our own exceptions
throw WGEInitFailed( "Initialization of OpenGL graphics context failed." );
}
}
WGEGraphicsWindow::~WGEGraphicsWindow()
......@@ -53,72 +41,6 @@ WGEGraphicsWindow::~WGEGraphicsWindow()
// cleanup
}
osg::ref_ptr<osgViewer::GraphicsWindow> WGEGraphicsWindow::getGraphicsWindow()
{
return m_GraphicsWindow;
}
void WGEGraphicsWindow::createContext( int x, int y, int width, int height )
{
// Create traits for graphics contest request
osg::ref_ptr<osg::DisplaySettings> ds = osg::DisplaySettings::instance();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
// ensure correct $DISPLAY variable
traits->readDISPLAY();
if ( traits->displayNum < 0 )
{
traits->displayNum = 0;
}
// set a lot of values
traits->windowName = "OpenWalnut";
traits->screenNum = 0; // XXX is this a good idea?
traits->x = x;
traits->y = y;
traits->width = width;
traits->height = height;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
// traits->windowDecoration = false;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
traits->inheritedWindowData = m_WindowData;
// Stereo
// TODO(ebaum): Stereo Mode: test whether it works
if ( ds->getStereo() )
{
switch( ds->getStereoMode() )
{
case( osg::DisplaySettings::QUAD_BUFFER ):
traits->quadBufferStereo = true;
break;
case( osg::DisplaySettings::VERTICAL_INTERLACE ):
case( osg::DisplaySettings::CHECKERBOARD ):
case( osg::DisplaySettings::HORIZONTAL_INTERLACE ):
traits->stencil = 8;
break;
default:
break;
}
}
// finally create graphics context and window
m_GraphicsContext = osg::GraphicsContext::createGraphicsContext( traits.get() );
m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
static_cast<osgViewer::GraphicsWindow*>( m_GraphicsContext.get() ) );
// get around dearranged traits on X11 (MTCompositeViewer only)
traits->x = x;
traits->y = x;
traits->width = width;
traits->height = height;
}
void WGEGraphicsWindow::resize( int width, int height )
{
m_GraphicsWindow->getEventQueue()->windowResize( 0, 0, width, height );
......
......@@ -39,27 +39,19 @@ public:
/**
* Default constructor.
*
* \param wdata the WindowData instance for the widget to use as render widget
* \param x X coordinate of widget where to create the context.
* \param y Y coordinate of widget where to create the context.
* \param width Width of the widget.
* \param height Height of the Widget.
* \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
*/
WGEGraphicsWindow( osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height );
WGEGraphicsWindow( int x, int y, int width, int height );
/**
* Destructor.
*/
virtual ~WGEGraphicsWindow();
/**
* Getter for m_GraphicsWindow.
*
* \return the OSG GraphicsWindow instance.
*/
osg::ref_ptr<osgViewer::GraphicsWindow> getGraphicsWindow();
/**
* Event types for the keyEvent() handler.
*/
......@@ -110,30 +102,10 @@ public:
protected:
/**
* Creates a new OpenGL context in the calling thread. Needs to be called before any other OpenGL operation.
*
* \param x X coordinate of widget where to create the context.
* \param y Y coordinate of widget where to create the context.
* \param width Width of the widget.
* \param height Height of the Widget.
*/
void createContext( int x, int y, int width, int height );
/**
* OpenSceneGraph render window.
*/
osg::ref_ptr<osgViewer::GraphicsWindow> m_GraphicsWindow;
/**
* OpenSceneGraph render context.
*/
osg::ref_ptr<osg::GraphicsContext> m_GraphicsContext;
/**
* Widget window data.
*/
osg::ref_ptr<osg::Referenced> m_WindowData;
private:
};
......
......@@ -47,18 +47,17 @@
#include "WGEViewer.h"
WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
WGEViewer::WGEViewer( std::string name, int x, int y,
int width, int height, WGECamera::ProjectionMode projectionMode )
: WGEGraphicsWindow( wdata, x, y, width, height ),
: WGEGraphicsWindow( x, y, width, height ),
boost::enable_shared_from_this< WGEViewer >(),
m_name( name )
{
try
{
m_View = osg::ref_ptr<osgViewer::View>( new osgViewer::View );
m_View = new osgViewer::Viewer();
m_View->setCamera( new WGECamera( width, height, projectionMode ) );
m_View->getCamera()->setGraphicsContext( m_GraphicsContext );
m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
switch( projectionMode )
{
......@@ -139,6 +138,11 @@ void WGEViewer::setBgColor( WColor bgColor )
m_View->getCamera()->setClearColor( osg::Vec4( bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue(), 1. ) );
}
void WGEViewer::paint()
{
m_View->frame();
}
void WGEViewer::resize( int width, int height )
{
m_View->getEventQueue()->windowResize( 0, 0, width, height );
......
......@@ -62,7 +62,6 @@ public:
* Default constructor.
*
* \param name the name of the viewer
* \param wdata the WindowData instance for the widget to use as render widget
* \param x X coordinate of widget where to create the context.
* \param y Y coordinate of widget where to create the context.
* \param width Width of the widget.
......@@ -70,7 +69,7 @@ public:
* \param projectionMode Projection mode of the viewer.
* \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
*/
WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y, int width, int height,
WGEViewer( std::string name, int x, int y, int width, int height,
WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC );
/**
......@@ -78,6 +77,11 @@ public:
*/
virtual ~WGEViewer();
/**
* Repaints the contents.
*/
virtual void paint();
/**
* Updates size information. Also updates camera.
*
......@@ -169,7 +173,7 @@ protected:
/**
* The OpenSceneGraph view used in this (Composite)Viewer.
*/
osg::ref_ptr< osgViewer::View > m_View;
osg::ref_ptr< osgViewer::Viewer > m_View;
/**
* The name of the viewer.
......
......@@ -61,28 +61,6 @@ WGraphicsEngine::WGraphicsEngine():
m_shaderPath = "";
m_fontPath = "";
// initialize OSG render window
m_viewer = osg::ref_ptr<osgViewer::CompositeViewer>( new osgViewer::CompositeViewer() );
// ThreadingModel: enum with the following possibilities
//
// SingleThreaded
// CullDrawThreadPerContext
// ThreadPerContext
// DrawThreadPerContext
// CullThreadPerCameraDrawThreadPerContext
// ThreadPerCamera
// AutomaticSelection
bool multiThreadedViewers = true;
if( WPreferences::getPreference( "ge.multiThreadedViewers", &multiThreadedViewers ) && !multiThreadedViewers )
{
m_viewer->setThreadingModel( osgViewer::Viewer::SingleThreaded );
}
else
{
m_viewer->setThreadingModel( osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext );
}
// init resource manager ( it is a singleton and gets created during first "getResourceManager" request.
WGEResourceManager::getResourceManager();
}
......@@ -132,23 +110,15 @@ void WGraphicsEngine::setFontPath( std::string path )
WGEResourceManager::getResourceManager()->setFontPath( path );
}
boost::shared_ptr<WGEViewer> WGraphicsEngine::createViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
boost::shared_ptr<WGEViewer> WGraphicsEngine::createViewer( std::string name, int x, int y,
int width, int height, WGECamera::ProjectionMode projectionMode,
WColor bgColor )
{
// init the composite viewer if not already done
if ( m_viewer == osg::ref_ptr< osgViewer::CompositeViewer >() )
{
}
boost::shared_ptr<WGEViewer> viewer = boost::shared_ptr<WGEViewer>(
new WGEViewer( name, wdata, x, y, width, height, projectionMode ) );
new WGEViewer( name, x, y, width, height, projectionMode ) );
viewer->setBgColor( bgColor );
viewer->setScene( getScene() );
// finally add view
m_viewer->addView( viewer->getView() );
// store it in viewer list
boost::mutex::scoped_lock lock( m_viewersLock );
bool insertSucceeded = m_viewers.insert( make_pair( name, viewer ) ).second;
......@@ -186,17 +156,11 @@ boost::shared_ptr< WGEViewer > WGraphicsEngine::getViewer()
void WGraphicsEngine::threadMain()
{
WLogger::getLogger()->addLogMessage( "Starting Graphics Engine", "GE", LL_INFO );
m_viewer->startThreading();
m_viewer->run();
m_viewer->stopThreading();
}
void WGraphicsEngine::notifyStop()
{
WLogger::getLogger()->addLogMessage( "Stopping Graphics Engine", "GE", LL_INFO );
m_viewer->setDone( true );
}
void WGraphicsEngine::requestShaderReload()
......
......@@ -110,7 +110,6 @@ public:
* Creates a new viewer. Does basic initialization and sets the default scene.
*
* \param name the name of the viewer
* \param wdata the WindowData instance for the widget to use as render widget
* \param x X coordinate of widget where to create the context.
* \param y Y coordinate of widget where to create the context.
* \param width Width of the widget.
......@@ -120,7 +119,7 @@ public:
* \return the new instance, ready to be used.
* \exception WGEInitFailed thrown if initialization of graphics context or graphics window has failed.
*/
boost::shared_ptr< WGEViewer > createViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int x, int y,
boost::shared_ptr< WGEViewer > createViewer( std::string name, int x, int y,
int width, int height, WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC,
WColor bgColor = WColor( .9, .9, .9 ) );
......
......@@ -38,44 +38,8 @@
#include "../../kernel/WKernel.h"
#if defined( WIN32 ) && !defined( __CYGWIN__ )
#include <osgViewer/api/Win32/GraphicsWindowWin32>
typedef osgViewer::GraphicsWindowWin32::WindowData WindowData;
#elif defined( __APPLE__ )
# if defined( __LP64__ ) // assume Cocoa on 64bit
# include <osgViewer/api/Cocoa/GraphicsWindowCocoa>
struct WindowData
: public osgViewer::GraphicsWindowCocoa::WindowData
{
explicit WindowData( ::WId view )
: osgViewer::GraphicsWindowCocoa::WindowData( 0 )
{
}
};
# else // assume Carbon on 32bit
# include <osgViewer/api/Carbon/GraphicsWindowCarbon>
struct WindowData
: public osgViewer::GraphicsWindowCarbon::WindowData
{
explicit WindowData( ::WId view )
: osgViewer::GraphicsWindowCarbon::WindowData(
HIViewGetWindow( static_cast<HIViewRef>( view ) ) )
{
}
};
# endif
#else // all other unix
#include <osgViewer/api/X11/GraphicsWindowX11>
typedef osgViewer::GraphicsWindowX11::WindowData WindowData;
#endif
WQtGLWidget::WQtGLWidget( std::string nameOfViewer, QWidget* parent, WGECamera::ProjectionMode projectionMode )
#ifdef _WIN32
: QWidget( parent ),
#else
: QGLWidget( parent ),
#endif
m_nameOfViewer( nameOfViewer ),
m_recommendedSize(),
m_isInitialized( new WConditionOneShot(), false ),
......@@ -90,6 +54,9 @@ WQtGLWidget::WQtGLWidget( std::string nameOfViewer, QWidget* parent, WGECamera::
setAttribute( Qt::WA_PaintOnScreen );
setAttribute( Qt::WA_NoSystemBackground );
setFocusPolicy( Qt::ClickFocus );
connect( &m_Timer, SIGNAL( timeout() ), this, SLOT( updateGL() ) );
m_Timer.start( 10 );
}
WQtGLWidget::~WQtGLWidget()
......@@ -108,12 +75,9 @@ void WQtGLWidget::initialize()
return;
}
// initialize OpenGL context and OpenSceneGraph
osg::ref_ptr<osg::Referenced> wdata = new WindowData( winId() );
// create viewer
m_Viewer = WKernel::getRunningKernel()->getGraphicsEngine()->createViewer(
m_nameOfViewer, wdata, x(), y(), width(), height(), m_initialProjectionMode );
m_nameOfViewer, x(), y(), width(), height(), m_initialProjectionMode );
m_isInitialized( true );
}
......@@ -195,28 +159,6 @@ boost::shared_ptr< WGEViewer > WQtGLWidget::getViewer() const
return m_Viewer;
}
void WQtGLWidget::paintEvent( QPaintEvent* /*event*/ )
{
// maybe this helps finding the startup segfaults. This will be removed after the problem has been found.
if ( !m_firstPaint )
{
WLogger::getLogger()->addLogMessage( "Painted the first time.",
"WQtGLWidget(" + m_Viewer->getName() + ")",
LL_DEBUG );
m_firstPaint = true;
}
// m_Viewer->paint();
}
#ifndef WIN32
void WQtGLWidget::destroyEvent( bool /*destroyWindow*/, bool /*destroySubWindows*/ )
{
// forward events
//m_Viewer->close();
}
void WQtGLWidget::closeEvent( QCloseEvent* event )
{
// forward events
......@@ -226,9 +168,14 @@ void WQtGLWidget::closeEvent( QCloseEvent* event )
}
void WQtGLWidget::resizeEvent( QResizeEvent* event )
void WQtGLWidget::paintGL()
{
m_Viewer->paint();
}
void WQtGLWidget::resizeGL( int width, int height )
{
m_Viewer->resize( event->size().width(), event->size().height() );
m_Viewer->resize( width, height );
}
int WQtGLWidget::translateButton( QMouseEvent* event )
......@@ -241,8 +188,6 @@ int WQtGLWidget::translateButton( QMouseEvent* event )
return 2;
case( Qt::RightButton ):
return 3;
case( Qt::NoButton ):
return 0;
default:
return 0;
}
......@@ -346,4 +291,3 @@ void WQtGLWidget::wheelEvent( QWheelEvent* event )
}
m_Viewer->mouseEvent( WGEViewer::MOUSESCROLL, x, y, 0 );
}
#endif
......@@ -27,8 +27,9 @@
#include <string>
#include <QtOpenGL/QGLWidget>
#include <QtCore/QTimer>
#include <QtGui/QWidget>
#include <QtOpenGL/QGLWidget>
#include <boost/shared_ptr.hpp>
#include <boost/signals2/signal.hpp>
......@@ -45,11 +46,7 @@ class WColor;
* \ingroup gui
*/
class WQtGLWidget
#ifdef _WIN32
: public QWidget
#else
: public QGLWidget
#endif
{
public:
/**
......@@ -134,10 +131,6 @@ protected:
std::string m_nameOfViewer;
// The GraphincsWindowWin32 implementation already takes care of message handling.
// We don't want to relay these on Windows, it will just cause duplicate messages
// with further problems downstream (i.e. not being able to throw the trackball
#ifndef WIN32
/**
* Event handler for double clicks.
*
......@@ -153,19 +146,17 @@ protected:
virtual void closeEvent( QCloseEvent* event );
/**
* Event handler for destroy events (called after close).
*
* \param destroyWindow flag indicatinng whether to destroy window
* \param destroySubWindows flag indicatinng whether to destroy sub windows
* QT Callback for handling repaints.
*/
virtual void destroyEvent( bool destroyWindow = true, bool destroySubWindows = true );
virtual void paintGL();
/**
* Event handler for resize events.
*
* \param event the event description.
* \param width the new width.
* \param height the new height.
*/
virtual void resizeEvent( QResizeEvent* event );
virtual void resizeGL( int width, int height );
/**
* Event handler for key press.
......@@ -208,14 +199,6 @@ protected:
* \param event the event description.
*/
virtual void wheelEvent( QWheelEvent* event );
#endif
/**
* QT Callback for handling repaints.
*
* \param event event descriptor.
*/
virtual void paintEvent( QPaintEvent* event );
/**
* Simply translate the mouse button from an event to an int.
......@@ -237,6 +220,11 @@ protected:
WGECamera::ProjectionMode m_initialProjectionMode;
private:
/**
* Timer for periodic repaints.
*/
QTimer m_Timer;
/**
* Holds the recommended size for the widget
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment