在VS-Qt-CMake基础上,打开mainwindow.ui,添加一个QWidget,然后在widget上右键-提升,选择SceneViewWidget
CMakeLists.txt中添加了OSG相关配置
cmake_minimum_required(VERSION 3.0) SET(PRODUCT_NAME ReadS3C) SET(PRODUCT_HEADER_FILES
mainwindow.h
SceneViewWidget.h
) SET(PRODUCT_UI_FILES
mainwindow.ui
) SET(PRODUCT_FILES
main.cpp
mainwindow.cpp
SceneViewWidget.cpp
${PRODUCT_HEADER_FILES}
) SET(PRODUCT_DATA_FILES
data/axes.osgt
data/arial.ttf
) FIND_PATH(OSG_INCLUDE_DIR osg/Referenced
PATHS
$ENV{OSG_ROOT}/include
$ENV{OSG_DIR}/include
/usr/include
/usr/local/include
)
FIND_PATH(OSG_LIB_DIR libosg.so osg.lib
PATHS
$ENV{OSG_ROOT}/lib
$ENV{OSG_DIR}/lib
/usr/lib
/usr/local/lib
)
SET(OSG_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on windows")
SET(OSG_RELEASE_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows")
SET(QT_FOUND FALSE)
IF(${CMAKE_VERSION} VERSION_LESS "3.2.0")
FIND_PACKAGE(Qt5Widgets)
IF(Qt5Widgets_FOUND)
ADD_DEFINITIONS(-DUSE_QT_VERSION=)
SET(QT_FOUND TRUE)
ELSE(Qt5Widgets_FOUND)
MESSAGE("*** Qt5 not found, which may be caused by missing/wrong CMake module files. "
"You should set Qt5Widgets_DIR to $(QT_DIR)/lib/cmake/Qt5Widgets/ to find variables.")
ENDIF(Qt5Widgets_FOUND)
ELSE()
FIND_PACKAGE(Qt5 COMPONENTS Widgets)
IF(Qt5_FOUND)
ADD_DEFINITIONS(-DUSE_QT_VERSION=)
SET(QT_FOUND TRUE)
ENDIF(Qt5_FOUND)
ENDIF() QT5_WRAP_UI(UI_FILES ${PRODUCT_UI_FILES})
QT5_WRAP_CPP(MOC_FILES ${PRODUCT_HEADER_FILES}) SET(PRODUCT_FILES ${PRODUCT_FILES} ${UI_FILES} ${MOC_FILES})
IF(WIN32)
#SET(PRODUCT_FILES ${PRODUCT_FILES} my.rc)
ENDIF(WIN32) IF(QT_FOUND)
#QT5_ADD_RESOURCES(RES_FILES skin/qdarkstyle/style.qrc)
ENDIF()
SET(PRODUCT_FILES ${PRODUCT_FILES} ${RES_FILES}) SOURCE_GROUP("auto generated files" FILES ${UI_FILES} ${MOC_FILES} ${RES_FILES})
SOURCE_GROUP("ui files" FILES ${PRODUCT_UI_FILES}) SET(EXTERNAL_LIBRARIES ${EXTERNAL_LIBRARIES}
debug osgQt${OSG_DEBUG_POSTFIX} optimized osgQt${OSG_RELEASE_POSTFIX}
) INCLUDE_DIRECTORIES(${OSG_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
LINK_DIRECTORIES(${OSG_LIB_DIR}) SET(WITH_CONSOLE )
IF(${WITH_CONSOLE})
ADD_EXECUTABLE(${PRODUCT_NAME} ${PRODUCT_FILES})
ELSE(${WITH_CONSOLE})
ADD_EXECUTABLE(${PRODUCT_NAME} WIN32 ${PRODUCT_FILES})
ENDIF(${WITH_CONSOLE}) SET_TARGET_PROPERTIES(${PRODUCT_NAME} PROPERTIES DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}") TARGET_LINK_LIBRARIES(${PRODUCT_NAME}
debug osg${OSG_DEBUG_POSTFIX} optimized osg${OSG_RELEASE_POSTFIX}
debug osgAnimation${OSG_DEBUG_POSTFIX} optimized osgAnimation${OSG_RELEASE_POSTFIX}
debug osgParticle${OSG_DEBUG_POSTFIX} optimized osgParticle${OSG_RELEASE_POSTFIX}
debug osgDB${OSG_DEBUG_POSTFIX} optimized osgDB${OSG_RELEASE_POSTFIX}
debug osgGA${OSG_DEBUG_POSTFIX} optimized osgGA${OSG_RELEASE_POSTFIX}
debug osgText${OSG_DEBUG_POSTFIX} optimized osgText${OSG_RELEASE_POSTFIX}
debug osgTerrain${OSG_DEBUG_POSTFIX} optimized osgTerrain${OSG_RELEASE_POSTFIX}
debug osgUtil${OSG_DEBUG_POSTFIX} optimized osgUtil${OSG_RELEASE_POSTFIX}
debug osgViewer${OSG_DEBUG_POSTFIX} optimized osgViewer${OSG_RELEASE_POSTFIX}
debug OpenThreads${OSG_DEBUG_POSTFIX} optimized OpenThreads${OSG_RELEASE_POSTFIX}
${OPENGL_LIBRARIES} ${THIRD_PARTY_LIBRARIES} ${EXTERNAL_LIBRARIES}
) SET_TARGET_PROPERTIES(${PRODUCT_NAME} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/bin"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/bin") INSTALL(TARGETS ${PRODUCT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
INSTALL(FILES ${PRODUCT_DATA_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/data) IF(QT_FOUND)
QT5_USE_MODULES(${PRODUCT_NAME} Widgets Network OpenGL)
ENDIF(QT_FOUND)
SceneViewWidget.h
#pragma once
#include <QWidget>
#include <QGridLayout>
#include <QTimer>
#include "osgViewer\CompositeViewer"
#include "osgDB\ReadFile"
#include "osgQt\GraphicsWindowQt" class SceneViewWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
SceneViewWidget(QWidget* parent = , Qt::WindowFlags f = , osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::CompositeViewer::SingleThreaded); QWidget* addViewWidget(osgQt::GraphicsWindowQt* gw, osg::Node* scene); osgQt::GraphicsWindowQt* createGraphicsWindow(int x, int y, int w, int h, const std::string& name = "", bool windowDecoration = false); virtual void paintEvent(QPaintEvent* event); protected:
virtual void initScene();
protected:
osg::ref_ptr<osg::Group> sceneRoot;
QTimer _timer;
};
SceneViewWidget.cpp
#include "SceneViewWidget.h"
#include "osgViewer\ViewerEventHandlers"
#include "osgGA\TrackballManipulator" SceneViewWidget::SceneViewWidget(QWidget* parent /*= 0*/, Qt::WindowFlags f /*= 0*/, osgViewer::ViewerBase::ThreadingModel threadingModel /*= osgViewer::CompositeViewer::SingleThreaded*/) : QWidget(parent, f)
{
setThreadingModel(threadingModel); // disable the default setting of viewer.done() by pressing Escape.
setKeyEventSetsDone(); initScene(); QWidget* widget1 = addViewWidget(createGraphicsWindow(, , , ), sceneRoot); QGridLayout* grid = new QGridLayout;
grid->addWidget(widget1, , );
setLayout(grid); connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
_timer.start();
} QWidget* SceneViewWidget::addViewWidget(osgQt::GraphicsWindowQt* gw, osg::Node* scene)
{
osgViewer::View* view = new osgViewer::View;
addView(view); osg::Camera* camera = view->getCamera();
camera->setGraphicsContext(gw); const osg::GraphicsContext::Traits* traits = gw->getTraits(); //camera->setClearColor(osg::Vec4(0.2, 0.2, 0.2, 1.0));
camera->setViewport(new osg::Viewport(, , traits->width, traits->height));
camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0f, 10000.0f); view->setSceneData(scene);
view->addEventHandler(new osgViewer::StatsHandler);
view->setCameraManipulator(new osgGA::TrackballManipulator);
gw->setTouchEventsEnabled(true);
return gw->getGLWidget();
} osgQt::GraphicsWindowQt* SceneViewWidget::createGraphicsWindow(int x, int y, int w, int h, const std::string& name /*= ""*/, bool windowDecoration /*= false*/)
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();//抗锯齿 return new osgQt::GraphicsWindowQt(traits.get());
} void SceneViewWidget::paintEvent(QPaintEvent* event)
{
frame();
}
void SceneViewWidget::initScene()
{
sceneRoot = new osg::Group(); osg::Node* axesModel = osgDB::readNodeFile("data/axes.osgt.(0.5,0.5,0.5).scale");
if(axesModel)
sceneRoot->addChild(axesModel); osg::ref_ptr<osgText::Font> font = osgText::readRefFontFile("data/arial.ttf");
osgText::Text* text = new osgText::Text();
text->setPosition(osg::Vec3(0.0, 0.0, 0.5));
text->setFont(font);
text->setAutoRotateToScreen(true);
text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));
text->setCharacterSize(0.1);
text->getOrCreateStateSet()->setRenderBinDetails(, "RenderBin");
text->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
text->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
text->getOrCreateStateSet()->setDataVariance(osg::Object::DYNAMIC);
text->setText("OSG");
sceneRoot->addChild(text);
}