Created
June 14, 2016 17:58
-
-
Save vicrucann/3e4ed8797311bc1f1d61864b7e9213ee to your computer and use it in GitHub Desktop.
OpenSceneGraph + QOpenGLWidget bug when using QTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
project(qtosg_tests) | |
cmake_minimum_required(VERSION 2.8.11) | |
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | |
set(CMAKE_INCLUDE_CURRENT_DIR ON) | |
set(CMAKE_AUTOMOC ON) | |
find_package(Qt5Core 5.4 REQUIRED) | |
find_package(Qt5Gui 5.4 REQUIRED) | |
find_package(Qt5OpenGL 5.4 REQUIRED) | |
find_package(Qt5Widgets 5.4 REQUIRED) | |
find_package(OpenSceneGraph REQUIRED COMPONENTS osgDB osgGA osgUtil osgViewer) | |
include_directories(${OPENSCENEGRAPH_INCLUDE_DIRS}) | |
set(QT_LIBRARIES | |
${Qt5Core_LIBRARIES} | |
${Qt5Gui_LIBRARIES} | |
${Qt5OpenGL_LIBRARIES} | |
${Qt5Widgets_LIBRARIES} | |
) | |
find_package(Qt5Test 5.4 REQUIRED) | |
enable_testing() | |
set(QT_QTTEST_LIBRARY | |
${Qt5Test_LIBRARIES} | |
) | |
add_subdirectory(qtosg) | |
add_subdirectory(tests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set(Application_SRCS | |
main.cpp | |
osgwidget.h | |
osgwidget.cpp | |
) | |
add_executable(${PROJECT_NAME} | |
${Application_SRCS} | |
) | |
target_link_libraries( ${PROJECT_NAME} | |
${QT_LIBRARIES} | |
${OPENSCENEGRAPH_LIBRARIES} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <QApplication> | |
#include <QMainWindow> | |
#include <QOpenGLWidget> | |
#include "osgwidget.h" | |
int main(int argc, char** argv) | |
{ | |
QApplication qapp(argc, argv); | |
QMainWindow window; | |
OSGWidget* widget = new OSGWidget(&window); | |
window.setCentralWidget(widget); | |
window.show(); | |
return qapp.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "osgwidget.h" | |
#include <osg/ShapeDrawable> | |
#include <osg/Geode> | |
#include <osgGA/TrackballManipulator> | |
#include <osg/StateSet> | |
#include <osg/Material> | |
OSGWidget::OSGWidget(QWidget *parent) | |
: QOpenGLWidget(parent) | |
, m_graphicsWindow(new osgViewer::GraphicsWindowEmbedded( this->x(), this->y(), | |
this->width(), this->height() ) ) | |
, m_viewer(new osgViewer::Viewer) | |
{ | |
osg::Cylinder* cylinder = new osg::Cylinder( osg::Vec3( 0.f, 0.f, 0.f ), 0.25f, 0.5f ); | |
osg::ShapeDrawable* sd = new osg::ShapeDrawable( cylinder ); | |
sd->setColor( osg::Vec4( 0.8f, 0.5f, 0.2f, 1.f ) ); | |
osg::Geode* geode = new osg::Geode; | |
geode->addDrawable(sd); | |
osg::Camera* camera = new osg::Camera; | |
camera->setViewport( 0, 0, this->width(), this->height() ); | |
camera->setClearColor( osg::Vec4( 0.9f, 0.9f, 1.f, 1.f ) ); | |
float aspectRatio = static_cast<float>( this->width()) / static_cast<float>( this->height() ); | |
camera->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f ); | |
camera->setGraphicsContext( m_graphicsWindow ); | |
m_viewer->setCamera(camera); | |
m_viewer->setSceneData(geode); | |
osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator; | |
manipulator->setAllowThrow( false ); | |
this->setMouseTracking(true); | |
m_viewer->setCameraManipulator(manipulator); | |
m_viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); | |
m_viewer->realize(); | |
} | |
void OSGWidget::paintGL() | |
{ | |
m_viewer->frame(); | |
} | |
void OSGWidget::resizeGL(int w, int h) | |
{ | |
this->getEventQueue()->windowResize(this->x(), this->y(), w, h); | |
m_graphicsWindow->resized(this->x(), this->y(), w, h); | |
osg::Camera* camera = m_viewer->getCamera(); | |
camera->setViewport(0, 0, this->width(), this->height()); | |
} | |
void OSGWidget::initializeGL() | |
{ | |
osg::Geode* geode = dynamic_cast<osg::Geode*>(m_viewer->getSceneData()); | |
osg::StateSet* stateSet = geode->getOrCreateStateSet(); | |
osg::Material* material = new osg::Material; | |
material->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE ); | |
stateSet->setAttributeAndModes( material, osg::StateAttribute::ON ); | |
stateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON ); | |
} | |
osgGA::EventQueue *OSGWidget::getEventQueue() const | |
{ | |
osgGA::EventQueue* eventQueue = m_graphicsWindow->getEventQueue(); | |
return eventQueue; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef OSGWIDGET_H | |
#define OSGWIDGET_H | |
#include <QOpenGLWidget> | |
#include <osg/ref_ptr> | |
#include <osgViewer/GraphicsWindow> | |
#include <osgViewer/Viewer> | |
class OSGWidget : public QOpenGLWidget | |
{ | |
public: | |
OSGWidget(QWidget* parent = 0); | |
protected: | |
virtual void paintGL(); | |
virtual void resizeGL(int w, int h); | |
virtual void initializeGL(); | |
osgGA::EventQueue* getEventQueue() const; | |
private: | |
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> m_graphicsWindow; | |
osg::ref_ptr<osgViewer::Viewer> m_viewer; | |
}; | |
#endif // OSGWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include_directories( | |
${CMAKE_SOURCE_DIR}/qtosg | |
) | |
SET( TEST_LIBRARIES | |
${QT_QTTEST_LIBRARY} | |
${QT_LIBRARIES} | |
${OPENSCENEGRAPH_LIBRARIES} | |
) | |
set(FOO_SRC | |
fooTest.cpp | |
${CMAKE_SOURCE_DIR}/qtosg/osgwidget.h | |
${CMAKE_SOURCE_DIR}/qtosg/osgwidget.cpp | |
) | |
set(FOO_NAME foo_test) | |
add_executable(${FOO_NAME} ${FOO_SRC}) | |
target_link_libraries(${FOO_NAME} ${TEST_LIBRARIES}) | |
add_test(${FOO_NAME} ${FOO_SRC}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QTest> | |
#include <QDebug> | |
#include <QScopedPointer> | |
#include "osgwidget.h" | |
class Foo: public QObject{ | |
Q_OBJECT | |
public: | |
explicit Foo(QObject* parent = 0) : QObject(parent) | |
{} | |
private slots: | |
void trial() { | |
qDebug() << "Trial test to see if the crash will happen"; | |
OSGWidget widget; // crash, no leak | |
// OSGWidget* widget = new OSGWidget; // no crash, leaks | |
QVERIFY(true); | |
} | |
}; | |
QTEST_MAIN(Foo) | |
#include "fooTest.moc" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment