Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to embed Ogre3D in qt5 ?



  • how to embed Ogre3D in qt5 ?

    ogre I have 1.12.13 Astra linux
    qt 5.11 Astra linux

    gives me this error:

    /home/dima/dima_project/ogre_example2/MainWindow.cpp:11: ошибка: no matching function for call to ‘MainWindow::createWindowContainer(BasicTutorial1*&)’
         QWidget* ogreWidget = QWidget::createWindowContainer(ogreWindow);
                                                                        ^
    

    my attempt :

    ogre_example2.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2021-08-26T17:23:57
    #
    #-------------------------------------------------
    
    QT       += core gui widgets
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = ogre_example2
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
        BasicTutorial1.cpp \
    #    src/gestureDetector.cpp
        MainWindow.cpp \
        main.cpp
    
    
    HEADERS += \
    #    include/SampleBrowser.h \
    #    include/gestureDetector.h
        MainWindow.h \
        basictutorial1.h
    
    
    
    FORMS += \
        MainWindow.ui
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    INCLUDEPATH += /usr/local/include/OGRE/ \
                    /home/dima/ogre/ogre-1.12.13/Components/Bites/include \
                    /home/dima/ogre/ogre-1.12.13/build/include            \
                    /usr/local/include/OGRE/RTShaderSystem                \
                    /usr/local/include/OGRE/Overlay
    
    
    
    # -- из папки сборки все библы
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreBites
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreBitesQt
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreGLSupport
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreMain
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreMeshLodGenerator
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreOverlay
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgrePaging
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreProperty
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreRTShaderSystem
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreTerrain
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreVolume
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    ## - a
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/lib/libOgreGLSupport.a
    
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/OgreMain/ -lOgreMain_pch_dephelp
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/OgreMain
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/OgreMain
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/OgreMain/libOgreMain_pch_dephelp.a
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/ -lfreetype
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/libfreetype.a
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/ -lpugixml
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/libpugixml.a
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/ -lzzip
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/libzzip.a
    
    
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreBites
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreBitesQt
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreMain
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreMeshLodGenerator
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreOverlay
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgrePaging
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreProperty
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreRTShaderSystem
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreTerrain
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreVolume
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOpenThreads
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losg
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgAnimation
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgDB
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgFX
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgGA
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgManipulator
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgParticle
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgPresentation
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgShadow
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgSim
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgTerrain
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgText
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgUI
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgUtil
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgViewer
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgVolume
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgWidget
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    

    basictutorial1.h

    #ifndef BASICTUTORIAL1_H
    #define BASICTUTORIAL1_H
    
    #include "Ogre.h"
    #include "OgreApplicationContext.h"
    #include "OgreInput.h"
    #include "OgreRTShaderSystem.h"
    #include <iostream>
    
    using namespace Ogre;
    using namespace OgreBites;
    
    class BasicTutorial1
            : public ApplicationContext
            , public InputListener
    {
    public:
        BasicTutorial1();
        virtual ~BasicTutorial1() {}
    
        void setup();
        bool keyPressed(const KeyboardEvent& evt);
    };
    
    #endif // BASICTUTORIAL1_H
    

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "basictutorial1.h"
    #include "ui_MainWindow.h"
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    BasicTutorial1.cpp

    //! [fullsource]
    
    #include "basictutorial1.h"
    
    
    BasicTutorial1::BasicTutorial1()
        : ApplicationContext("OgreTutorialApp")
    {
    }
    
    
    void BasicTutorial1::setup()
    {
        // do not forget to call the base first
        ApplicationContext::setup();
        addInputListener(this);
    
        // get a pointer to the already created root
        Root* root = getRoot();
        SceneManager* scnMgr = root->createSceneManager();
    
        // register our scene with the RTSS
        RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
        shadergen->addSceneManager(scnMgr);
    
        // -- tutorial section start --
        //! [turnlights]
        scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
        //! [turnlights]
    
        //! [newlight]
        Light* light = scnMgr->createLight("MainLight");
        SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
        lightNode->attachObject(light);
        //! [newlight]
    
        //! [lightpos]
        lightNode->setPosition(20, 80, 50);
        //! [lightpos]
    
        //! [camera]
        SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
    
        // create the camera
        Camera* cam = scnMgr->createCamera("myCam");
        cam->setNearClipDistance(5); // specific to this sample
        cam->setAutoAspectRatio(true);
        camNode->attachObject(cam);
        camNode->setPosition(0, 0, 140);
    
        // and tell it to render into the main window
        getRenderWindow()->addViewport(cam);
        //! [camera]
    
        //! [entity1]
        Entity* ogreEntity = scnMgr->createEntity("ogrehead.mesh");
        //! [entity1]
    
        //! [entity1node]
        SceneNode* ogreNode = scnMgr->getRootSceneNode()->createChildSceneNode();
        //! [entity1node]
    
        //! [entity1nodeattach]
        ogreNode->attachObject(ogreEntity);
        //! [entity1nodeattach]
    
        //! [cameramove]
        camNode->setPosition(0, 47, 222);
        //! [cameramove]
    
        //! [entity2]
        Entity* ogreEntity2 = scnMgr->createEntity("ogrehead.mesh");
        SceneNode* ogreNode2 = scnMgr->getRootSceneNode()->createChildSceneNode(Vector3(84, 48, 0));
        ogreNode2->attachObject(ogreEntity2);
        //! [entity2]
    
        //! [entity3]
        Entity* ogreEntity3 = scnMgr->createEntity("ogrehead.mesh");
        SceneNode* ogreNode3 = scnMgr->getRootSceneNode()->createChildSceneNode();
        ogreNode3->setPosition(0, 104, 0);
        ogreNode3->setScale(2, 1.2, 1);
        ogreNode3->attachObject(ogreEntity3);
        //! [entity3]
    
        //! [entity4]
        Entity* ogreEntity4 = scnMgr->createEntity("ogrehead.mesh");
        SceneNode* ogreNode4 = scnMgr->getRootSceneNode()->createChildSceneNode();
        ogreNode4->setPosition(-84, 48, 0);
        ogreNode4->roll(Degree(-90));
        ogreNode4->attachObject(ogreEntity4);
        //! [entity4]
    
        // -- tutorial section end --
    }
    
    
    bool BasicTutorial1::keyPressed(const KeyboardEvent& evt)
    {
        if (evt.keysym.sym == SDLK_ESCAPE)
        {
            getRoot()->queueEndRendering();
        }
        return true;
    }
    
    //! [fullsource]
    

    main.cpp

    #include "MainWindow.h"
    #include <QApplication>
    #include "basictutorial1.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    MainWindow.cpp

    #include "MainWindow.h"
    #include <QCloseEvent>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        BasicTutorial1* ogreWindow = new BasicTutorial1();
        QWidget* ogreWidget = QWidget::createWindowContainer(ogreWindow);
        setCentralWidget(ogreWidget);
        setFocusPolicy(Qt::WheelFocus);
        setFocusProxy(ogreWidget);
    
        ogreWindow->initApp();
        ogreWindow->getRoot()->startRendering();
        ogreWindow->closeApp();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    MainWindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>649</width>
        <height>443</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <layout class="QHBoxLayout" name="horizontalLayout"/>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>649</width>
         <height>30</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
      <widget class="QDockWidget" name="dockWidget">
       <attribute name="dockWidgetArea">
        <number>2</number>
       </attribute>
       <widget class="QWidget" name="dockWidgetContents">
        <layout class="QVBoxLayout" name="verticalLayout_3">
         <item>
          <widget class="QLineEdit" name="lineEdit"/>
         </item>
        </layout>
       </widget>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    


  • main.cpp

    #include "widget.h"
    #include <QApplication>
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
    
        return a.exec();
    }
    

    QTOgreWindow.cpp

    #include "QTOgreWindow.h"
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
    #include <Compositor/OgreCompositorManager2.h>
    #endif
    
    /*
    Note that we pass any supplied QWindow parent to the base QWindow class. This is necessary should we
    need to use our class within a container.
    */
    QTOgreWindow::QTOgreWindow(QWindow *parent)
            : QWindow(parent)
            , m_update_pending(false)
            , m_animating(false)
            , m_ogreRoot(NULL)
            , m_ogreWindow(NULL)
            , m_ogreCamera(NULL)
            , m_cameraMan(NULL)
    {
            setAnimating(true);
            installEventFilter(this);
            m_ogreBackground = Ogre::ColourValue(0.0f, 0.5f, 1.0f);
    }
    
    /*
    Upon destruction of the QWindow object we destroy the Ogre3D scene.
    */
    QTOgreWindow::~QTOgreWindow()
    {
            if (m_cameraMan) delete m_cameraMan;
            delete m_ogreRoot;
    }
    
    /*
    In case any drawing surface backing stores (QRasterWindow or QOpenGLWindow) of Qt are supplied to this
    class in any way we inform Qt that they will be unused.
    */
    void QTOgreWindow::render(QPainter *painter)
    {
            Q_UNUSED(painter);
    }
    
    /*
    Our initialization function. Called by our renderNow() function once when the window is first exposed.
    */
    void QTOgreWindow::initialize()
    {
            /*
            As shown Ogre3D is initialized normally; just like in other documentation.
            */
    #ifdef _MSC_VER
            m_ogreRoot = new Ogre::Root(Ogre::String("plugins" OGRE_BUILD_SUFFIX ".cfg"));
    #else
            m_ogreRoot = new Ogre::Root(Ogre::String("plugins.cfg"));
    #endif
            Ogre::ConfigFile ogreConfig;
    
            /*
    
            Commended out for simplicity but should you need to initialize resources you can do so normally.
    
            ogreConfig.load("resources/resource_configs/resources.cfg");
    
            Ogre::ConfigFile::SectionIterator seci = ogreConfig.getSectionIterator();
            Ogre::String secName, typeName, archName;
            while (seci.hasMoreElements())
            {
                    secName = seci.peekNextKey();
                    Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
                    Ogre::ConfigFile::SettingsMultiMap::iterator i;
                    for (i = settings->begin(); i != settings->end(); ++i)
                    {
                            typeName = i->first;
                            archName = i->second;
                            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
                            archName, typeName, secName);
                    }
            }
    
            */
    
            const Ogre::RenderSystemList& rsList = m_ogreRoot->getAvailableRenderers();
            Ogre::RenderSystem* rs = rsList[0];
    
            /*
            This list setup the search order for used render system.
            */
            Ogre::StringVector renderOrder;
    #if defined(Q_OS_WIN)
            renderOrder.push_back("Direct3D9");
            renderOrder.push_back("Direct3D11");
    #endif
            renderOrder.push_back("OpenGL");
            renderOrder.push_back("OpenGL 3+");
            for (Ogre::StringVector::iterator iter = renderOrder.begin(); iter != renderOrder.end(); iter++)
            {
                    for (Ogre::RenderSystemList::const_iterator it = rsList.begin(); it != rsList.end(); it++)
                    {
                            if ((*it)->getName().find(*iter) != Ogre::String::npos)
                            {
                                    rs = *it;
                                    break;
                            }
                    }
                    if (rs != NULL) break;
            }
            if (rs == NULL)
            {
                    if (!m_ogreRoot->restoreConfig())
                    {
    //                        if (!m_ogreRoot->showConfigDialog())
    //                                OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS,
    //                                        "Abort render system configuration",
    //                                        "QTOgreWindow::initialize");
                    }
            }
    
            /*
            Setting size and VSync on windows will solve a lot of problems
            */
            QString dimensions = QString("%1 x %2").arg(this->width()).arg(this->height());
            rs->setConfigOption("Video Mode", dimensions.toStdString());
            rs->setConfigOption("Full Screen", "No");
            rs->setConfigOption("VSync", "Yes");
            m_ogreRoot->setRenderSystem(rs);
            m_ogreRoot->initialise(false);
    
            Ogre::NameValuePairList parameters;
            /*
            Flag within the parameters set so that Ogre3D initializes an OpenGL context on it's own.
            */
            if (rs->getName().find("GL") <= rs->getName().size())
                    parameters["currentGLContext"] = Ogre::String("false");
    
            /*
            We need to supply the low level OS window handle to this QWindow so that Ogre3D knows where to draw
            the scene. Below is a cross-platform method on how to do this.
            If you set both options (externalWindowHandle and parentWindowHandle) this code will work with OpenGL
            and DirectX.
            */
    #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
            parameters["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)(this->winId()));
            parameters["parentWindowHandle"] = Ogre::StringConverter::toString((size_t)(this->winId()));
    #else
            parameters["externalWindowHandle"] = Ogre::StringConverter::toString((unsigned long)(this->winId()));
            parameters["parentWindowHandle"] = Ogre::StringConverter::toString((unsigned long)(this->winId()));
    #endif
    
    #if defined(Q_OS_MAC)
            parameters["macAPI"] = "cocoa";
            parameters["macAPICocoaUseNSView"] = "true";
    #endif
    
            /*
            Note below that we supply the creation function for the Ogre3D window the width and height
            from the current QWindow object using the "this" pointer.
            */
            m_ogreWindow = m_ogreRoot->createRenderWindow("QT Window",
                    this->width(),
                    this->height(),
                    false,
                    &parameters);
            m_ogreWindow->setVisible(true);
    
            /*
            The rest of the code in the initialization function is standard Ogre3D scene code. Consult other
            tutorials for specifics.
            */
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            const size_t numThreads = std::max<int>(1, Ogre::PlatformInformation::getNumLogicalCores());
            Ogre::InstancingThreadedCullingMethod threadedCullingMethod = Ogre::INSTANCING_CULLING_SINGLETHREAD;
            if (numThreads > 1)threadedCullingMethod = Ogre::INSTANCING_CULLING_THREADED;
            m_ogreSceneMgr = m_ogreRoot->createSceneManager(Ogre::ST_GENERIC, numThreads, threadedCullingMethod);
    #else
            m_ogreSceneMgr = m_ogreRoot->createSceneManager(Ogre::ST_GENERIC);
    #endif
    
            m_ogreCamera = m_ogreSceneMgr->createCamera("MainCamera");
            m_ogreCamera->setPosition(Ogre::Vector3(0.0f, 0.0f, 10.0f));
            m_ogreCamera->lookAt(Ogre::Vector3(0.0f, 0.0f, -300.0f));
            m_ogreCamera->setNearClipDistance(0.1f);
            m_ogreCamera->setFarClipDistance(200.0f);
            m_cameraMan = new OgreQtBites::SdkQtCameraMan(m_ogreCamera);   // create a default camera controller
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            createCompositor();
    #else
            Ogre::Viewport* pViewPort = m_ogreWindow->addViewport(m_ogreCamera);
            pViewPort->setBackgroundColour(m_ogreBackground);
    #endif
    
            m_ogreCamera->setAspectRatio(
                    Ogre::Real(m_ogreWindow->getWidth()) / Ogre::Real(m_ogreWindow->getHeight()));
            m_ogreCamera->setAutoAspectRatio(true);
    
            Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    
            createScene();
    
            m_ogreRoot->addFrameListener(this);
    }
    
    void QTOgreWindow::createScene()
    {
            /*
            Example scene
            Derive this class for your own purpose and overwite this function to have a working Ogre widget with
            your own content.
            */
            m_ogreSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            Ogre::Entity* sphereMesh = m_ogreSceneMgr->createEntity(Ogre::SceneManager::PT_SPHERE);
    #else
            Ogre::Entity* sphereMesh = m_ogreSceneMgr->createEntity("mySphere", Ogre::SceneManager::PT_SPHERE);
    #endif
    
            Ogre::SceneNode* childSceneNode = m_ogreSceneMgr->getRootSceneNode()->createChildSceneNode();
    
            childSceneNode->attachObject(sphereMesh);
    
            Ogre::MaterialPtr sphereMaterial = Ogre::MaterialManager::getSingleton().create("SphereMaterial",
                    Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true);
    
            sphereMaterial->getTechnique(0)->getPass(0)->setAmbient(0.1f, 0.1f, 0.1f);
            sphereMaterial->getTechnique(0)->getPass(0)->setDiffuse(0.2f, 0.2f, 0.2f, 1.0f);
            sphereMaterial->getTechnique(0)->getPass(0)->setSpecular(0.9f, 0.9f, 0.9f, 1.0f);
            //sphereMaterial->setAmbient(0.2f, 0.2f, 0.5f);
            //sphereMaterial->setSelfIllumination(0.2f, 0.2f, 0.1f);
    
            sphereMesh->setMaterialName("SphereMaterial");
            childSceneNode->setPosition(Ogre::Vector3(0.0f, 0.0f, 0.0f));
            childSceneNode->setScale(Ogre::Vector3(0.01f, 0.01f, 0.01f)); // Radius, in theory.
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            Ogre::SceneNode* pLightNode = m_ogreSceneMgr->getRootSceneNode()->createChildSceneNode();
            Ogre::Light* light = m_ogreSceneMgr->createLight();
            pLightNode->attachObject(light);
            pLightNode->setPosition(20.0f, 80.0f, 50.0f);
    #else
            Ogre::Light* light = m_ogreSceneMgr->createLight("MainLight");
            light->setPosition(20.0f, 80.0f, 50.0f);
    #endif
    }
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
    void QTOgreWindow::createCompositor()
    {
            /*
            Example compositor
            Derive this class for your own purpose and overwite this function to have a working Ogre
            widget with your own compositor.
            */
            Ogre::CompositorManager2* compMan = m_ogreRoot->getCompositorManager2();
            const Ogre::String workspaceName = "default scene workspace";
            const Ogre::IdString workspaceNameHash = workspaceName;
            compMan->createBasicWorkspaceDef(workspaceName, m_ogreBackground);
            compMan->addWorkspace(m_ogreSceneMgr, m_ogreWindow, m_ogreCamera, workspaceNameHash, true);
    }
    #endif
    
    void QTOgreWindow::render()
    {
            /*
            How we tied in the render function for OGre3D with QWindow's render function. This is what gets call
            repeatedly. Note that we don't call this function directly; rather we use the renderNow() function
            to call this method as we don't want to render the Ogre3D scene unless everything is set up first.
            That is what renderNow() does.
    
            Theoretically you can have one function that does this check but from my experience it seems better
            to keep things separate and keep the render function as simple as possible.
            */
    //        Ogre::WindowEventUtilities::messagePump();
            m_ogreRoot->renderOneFrame();
    }
    
    void QTOgreWindow::renderLater()
    {
            /*
            This function forces QWindow to keep rendering. Omitting this causes the renderNow() function to
            only get called when the window is resized, moved, etc. as opposed to all of the time; which is
            generally what we need.
            */
            if (!m_update_pending)
            {
                    m_update_pending = true;
                    QApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
            }
    }
    
    bool QTOgreWindow::event(QEvent *event)
    {
            /*
            QWindow's "message pump". The base method that handles all QWindow events. As you will see there
            are other methods that actually process the keyboard/other events of Qt and the underlying OS.
    
            Note that we call the renderNow() function which checks to see if everything is initialized, etc.
            before calling the render() function.
            */
    
            switch (event->type())
            {
            case QEvent::UpdateRequest:
                    m_update_pending = false;
                    renderNow();
                    return true;
    
            default:
                    return QWindow::event(event);
            }
    }
    
    /*
    Called after the QWindow is reopened or when the QWindow is first opened.
    */
    void QTOgreWindow::exposeEvent(QExposeEvent *event)
    {
            Q_UNUSED(event);
    
            if (isExposed())
                    renderNow();
    }
    
    /*
    The renderNow() function calls the initialize() function when needed and if the QWindow is already
    initialized and prepped calls the render() method.
    */
    void QTOgreWindow::renderNow()
    {
            if (!isExposed())
                    return;
    
            if (m_ogreRoot == NULL)
            {
                    initialize();
            }
    
            render();
    
            if (m_animating)
            renderLater();
    }
    
    /*
    Our event filter; handles the resizing of the QWindow. When the size of the QWindow changes note the
    call to the Ogre3D window and camera. This keeps the Ogre3D scene looking correct.
    */
    bool QTOgreWindow::eventFilter(QObject *target, QEvent *event)
    {
            if (target == this)
            {
                    if (event->type() == QEvent::Resize)
                    {
                            if (isExposed() && m_ogreWindow != NULL)
                            {
                                    m_ogreWindow->resize(this->width(), this->height());
                            }
                    }
            }
    
            return false;
    }
    
    /*
    How we handle keyboard and mouse events.
    */
    void QTOgreWindow::keyPressEvent(QKeyEvent * ev)
    {
            if(m_cameraMan)
                    m_cameraMan->injectKeyDown(*ev);
    }
    
    void QTOgreWindow::keyReleaseEvent(QKeyEvent * ev)
    {
            if(m_cameraMan)
                    m_cameraMan->injectKeyUp(*ev);
    }
    
    void QTOgreWindow::mouseMoveEvent( QMouseEvent* e )
    {
            static int lastX = e->x();
            static int lastY = e->y();
            int relX = e->x() - lastX;
            int relY = e->y() - lastY;
            lastX = e->x();
            lastY = e->y();
    
            if(m_cameraMan && (e->buttons() & Qt::LeftButton))
                    m_cameraMan->injectMouseMove(relX, relY);
    }
    
    void QTOgreWindow::wheelEvent(QWheelEvent *e)
    {
            if(m_cameraMan)
                    m_cameraMan->injectWheelMove(*e);
    }
    
    void QTOgreWindow::mousePressEvent( QMouseEvent* e )
    {
            if(m_cameraMan)
                    m_cameraMan->injectMouseDown(*e);
    }
    
    void QTOgreWindow::mouseReleaseEvent( QMouseEvent* e )
    {
            if(m_cameraMan)
                    m_cameraMan->injectMouseUp(*e);
    
            QPoint pos = e->pos();
            Ogre::Ray mouseRay = m_ogreCamera->getCameraToViewportRay(
                    (Ogre::Real)pos.x() / m_ogreWindow->getWidth(),
                    (Ogre::Real)pos.y() / m_ogreWindow->getHeight());
            Ogre::RaySceneQuery* pSceneQuery = m_ogreSceneMgr->createRayQuery(mouseRay);
            pSceneQuery->setSortByDistance(true);
            Ogre::RaySceneQueryResult vResult = pSceneQuery->execute();
            for (size_t ui = 0; ui < vResult.size(); ui++)
            {
                    if (vResult[ui].movable)
                    {
                            if (vResult[ui].movable->getMovableType().compare("Entity") == 0)
                            {
                                    emit entitySelected((Ogre::Entity*)vResult[ui].movable);
                            }
                    }
            }
            m_ogreSceneMgr->destroyQuery(pSceneQuery);
    }
    
    /*
    Function to keep track of when we should and shouldn't redraw the window; we wouldn't want to do
    rendering when the QWindow is minimized. This takes care of those scenarios.
    */
    void QTOgreWindow::setAnimating(bool animating)
    {
            m_animating = animating;
    
            if (animating)
                    renderLater();
    }
    
    bool QTOgreWindow::frameRenderingQueued(const Ogre::FrameEvent& evt)
    {
            m_cameraMan->frameRenderingQueued(evt);
            return true;
    }
    
    void QTOgreWindow::log(Ogre::String msg)
    {
            if(Ogre::LogManager::getSingletonPtr() != NULL) Ogre::LogManager::getSingletonPtr()->logMessage(msg);
    }
    
    void QTOgreWindow::log(QString msg)
    {
            log(Ogre::String(msg.toStdString().c_str()));
    }
    

    widget.cpp

    #include "widget.h"
    
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        QTOgreWindow * ogreWindow = new QTOgreWindow ();
        ogreWindow->show ();
    }
    
    Widget::~Widget()
    {
    
    }
    

    plugin.cfg

    # Defines plugins to load
    
    # Define plugin folder
    PluginFolder=/usr/local/lib/OGRE
    
    # Define plugins
    # Plugin=RenderSystem_Direct3D9
    # Plugin=RenderSystem_Direct3D11
     Plugin=RenderSystem_GL
     Plugin=RenderSystem_GL3Plus
     Plugin=RenderSystem_GLES2
    # Plugin=RenderSystem_Metal
    # Plugin=RenderSystem_Tiny
     Plugin=Plugin_ParticleFX
     Plugin=Plugin_BSPSceneManager
    # Plugin=Plugin_CgProgramManager
     Plugin=Codec_EXR
     Plugin=Codec_STBI
    # Plugin=Codec_FreeImage
     Plugin=Plugin_PCZSceneManager
     Plugin=Plugin_OctreeZone
     Plugin=Plugin_OctreeSceneManager
     Plugin=Plugin_DotScene
    # Plugin=Codec_Assimp
    

    Screenshot_20211208_120343.png

    the first part of the answer at the top (failed to fit into one message)


  • Lifetime Qt Champion

    Hi,

    @timob256 said in how to embed Ogre3D in qt5 ?:

    QWidget::createWindowContainer(ogreWindow);

    This function requires a QWindow which your class is not.

    The official method is described here.



  • @SGaist said in how to embed Ogre3D in qt5 ?:

    Hi,
    @timob256 said in how to embed Ogre3D in qt5 ?:

    QWidget::createWindowContainer(ogreWindow);

    This function requires a QWindow which your class is not.
    The official method is described here.

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <OgreApplicationContextQt.h>
    #include "basictutorial1.h"
    #include "ui_MainWindow.h"
    
    class MainWindow : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "MainWindow.h"
    #include <QCloseEvent>
    
    MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MainWindow)
    {
    
        BasicTutorial1* ogreWindow = new BasicTutorial1();
        QWidget* ogreWidget = QWidget::createWindowContainer(ogreWindow);
         setCentralWidget(ogreWidget);
        setFocusPolicy(Qt::WheelFocus);
        setFocusProxy(ogreWidget);
    
        ogreWindow->initApp();
        ogreWindow->getRoot()->startRendering();
        ogreWindow->closeApp();
    
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    writes me 3 errors.

    /home/dima/dima_project/ogre_example2/MainWindow.cpp:10: ошибка: no matching function for call to ‘MainWindow::createWindowContainer(BasicTutorial1*&)’
         QWidget* ogreWidget = QWidget::createWindowContainer(ogreWindow);
                                                                        ^
    
    /home/dima/dima_project/ogre_example2/MainWindow.cpp:11: ошибка: ‘setCentralWidget’ was not declared in this scope
          setCentralWidget(ogreWidget);
                                     ^
    
    /home/dima/dima_project/ogre_example2/MainWindow.cpp:19: ошибка: no matching function for call to ‘Ui::MainWindow::setupUi(MainWindow*)’
         ui->setupUi(this);
                         ^
    

    I think it's a mistake createWindowContainer or rather, the fact that I cannot call it correctly


  • Lifetime Qt Champion

    @timob256 said in how to embed Ogre3D in qt5 ?:

    I think it's a mistake createWindowContainer or rather, the fact that I cannot call it correctly

    Why do you post your same code again even @SGaist already told you what's wrong and where a documentation about how to do it right can be found?



  • @Christian-Ehrlicher said in how to embed Ogre3D in qt5 ?:

    Why do you post your same code again even @SGaist already told you what's wrong and where a documentation about how to do it right can be found?

    I made a change

    i replaced class MainWindow : public QMainWindow on class MainWindow : public QWidget

    I do not understand without an example what I need to change. Very difficult . I can't read thick books, I'm a ballroom man, I have dyslexia


  • Lifetime Qt Champion

    @timob256 I'm pretty sure you will not find someone here who is using Ogre3D. You can try to ask at Ogre3D forums how to use it with Qt - we can't help since the problem is not on the Qt side.


  • Lifetime Qt Champion

    This Ogre3D article points to two projects that have been migrated to the new integration.



  • this example work on me :3 (ogre 1.12.13, qt5.11, astra linux smolensk )

    ogre_example3

    #-------------------------------------------------
    #
    # Project created by QtCreator 2021-12-07T15:27:56
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = ogre_example3
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
            main.cpp \
            widget.cpp \
        QTOgreWindow.cpp
    
    HEADERS += \
            widget.h \
        QTOgreWindow.h \
        SdkQtCameraMan.h
    
    DISTFILES += \
        plugins.cfg
    
    
    INCLUDEPATH += /usr/local/include/OGRE/ \
                    /home/dima/ogre/ogre-1.12.13/Components/Bites/include \
                    /home/dima/ogre/ogre-1.12.13/build/include            \
                    /usr/local/include/OGRE/RTShaderSystem                \
                    /usr/local/include/OGRE/Overlay                       \
                    /home/dima/ogre/ogre-1.12.13/build/inst/bin
    
    
    
    # -- из папки сборки все библы
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreBites
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreBitesQt
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreGLSupport
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreMain
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreMeshLodGenerator
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreOverlay
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgrePaging
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreProperty
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreRTShaderSystem
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreTerrain
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/lib/ -lOgreVolume
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    ## - a
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/lib/libOgreGLSupport.a
    
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/OgreMain/ -lOgreMain_pch_dephelp
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/OgreMain
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/OgreMain
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/OgreMain/libOgreMain_pch_dephelp.a
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/ -lfreetype
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/libfreetype.a
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/ -lpugixml
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/libpugixml.a
    
    #unix:!macx: LIBS += -L$$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/ -lzzip
    
    #INCLUDEPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    #DEPENDPATH += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/include
    
    #unix:!macx: PRE_TARGETDEPS += $$PWD/../../ogre/ogre-1.12.13/build/Dependencies/lib/libzzip.a
    
    
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreBites
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreBitesQt
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreMain
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreMeshLodGenerator
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreOverlay
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgrePaging
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreProperty
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreRTShaderSystem
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreTerrain
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOgreVolume
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lOpenThreads
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losg
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgAnimation
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgDB
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgFX
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgGA
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgManipulator
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgParticle
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgPresentation
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgShadow
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgSim
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgTerrain
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgText
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgUI
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgUtil
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgViewer
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgVolume
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    
    unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -losgWidget
    
    INCLUDEPATH += $$PWD/../../../../usr/local/include
    DEPENDPATH += $$PWD/../../../../usr/local/include
    

    QTOgreWindow.h

    #ifndef QTOGREWINDOW_H
    #define QTOGREWINDOW_H
    
    /*
    Qt headers
    */
    #include <QtWidgets/QApplication>
    #include <QtGui/QKeyEvent>
    #include <QtGui/QWindow>
    
    /*
    Ogre3D header
    */
    #include <Ogre.h>
    
    /*
    Changed SdkCameraMan implementation to work with QKeyEvent, QMouseEvent, QWheelEvent
    */
    #include "SdkQtCameraMan.h"
    
    /*
    With the headers included we now need to inherit from QWindow.
    */
    class QTOgreWindow : public QWindow, public Ogre::FrameListener
    {
            /*
            A QWindow still inherits from QObject and can have signals/slots; we need to add the appropriate
            Q_OBJECT keyword so that Qt's intermediate compiler can do the necessary wireup between our class
            and the rest of Qt.
            */
            Q_OBJECT
    
    public:
            explicit QTOgreWindow(QWindow *parent = NULL);
            ~QTOgreWindow();
    
            /*
            We declare these methods virtual to allow for further inheritance.
            */
            virtual void render(QPainter *painter);
            virtual void render();
            virtual void initialize();
            virtual void createScene();
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            virtual void createCompositor();
    #endif
    
            void setAnimating(bool animating);
    
    public slots:
    
            virtual void renderLater();
            virtual void renderNow();
    
            /*
            We use an event filter to be able to capture keyboard/mouse events. More on this later.
            */
            virtual bool eventFilter(QObject *target, QEvent *event);
    
    signals:
            /*
            Event for clicking on an entity.
            */
            void entitySelected(Ogre::Entity* entity);
    
    protected:
            /*
            Ogre3D pointers added here. Useful to have the pointers here for use by the window later.
            */
            Ogre::Root* m_ogreRoot;
            Ogre::RenderWindow* m_ogreWindow;
            Ogre::SceneManager* m_ogreSceneMgr;
            Ogre::Camera* m_ogreCamera;
            Ogre::ColourValue m_ogreBackground;
            OgreQtBites::SdkQtCameraMan* m_cameraMan;
    
            bool m_update_pending;
            bool m_animating;
    
            /*
            The below methods are what is actually fired when they keys on the keyboard are hit.
            Similar events are fired when the mouse is pressed or other events occur.
            */
            virtual void keyPressEvent(QKeyEvent * ev);
            virtual void keyReleaseEvent(QKeyEvent * ev);
            virtual void mouseMoveEvent(QMouseEvent* e);
            virtual void wheelEvent(QWheelEvent* e);
            virtual void mousePressEvent(QMouseEvent* e);
            virtual void mouseReleaseEvent(QMouseEvent* e);
            virtual void exposeEvent(QExposeEvent *event);
            virtual bool event(QEvent *event);
    
            /*
            FrameListener method
            */
            virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
    
            /*
            Write log messages to Ogre log
            */
            void log(Ogre::String msg);
            void log(QString msg);
    };
    
    #endif // QTOGREWINDOW_H
    

    SdkQtCameraMan.h

    // File modified to change OIS to Qt KeyEvents
    #ifndef __SdkQtCameraMan_H__
    #define __SdkQtCameraMan_H__
    
    #include "OgreCamera.h"
    #include "OgreSceneNode.h"
    #include "OgreFrameListener.h"
    #include <QKeyEvent>
    #include <QMouseEvent>
    
    // enum CameraStyle should be in other namespace than OgreBites::CameraStyle
    namespace OgreQtBites
    {
        enum CameraStyle   // enumerator values for different styles of camera movement
        {
            CS_FREELOOK,
            CS_ORBIT,
            CS_MANUAL
        };
    
        /*=============================================================================
        | Utility class for controlling the camera in samples.
        =============================================================================*/
        class SdkQtCameraMan
        {
        public:
            SdkQtCameraMan(Ogre::Camera* cam)
            : mCamera(0)
            , mTarget(0)
            , mOrbiting(false)
            , mZooming(false)
            , mTopSpeed(150)
            , mVelocity(Ogre::Vector3::ZERO)
            , mGoingForward(false)
            , mGoingBack(false)
            , mGoingLeft(false)
            , mGoingRight(false)
            , mGoingUp(false)
            , mGoingDown(false)
            , mFastMove(false)
            {
    
                setCamera(cam);
                setStyle(CS_FREELOOK);
            }
    
            virtual ~SdkQtCameraMan() {}
    
            /*-----------------------------------------------------------------------------
            | Swaps the camera on our camera man for another camera.
            -----------------------------------------------------------------------------*/
            virtual void setCamera(Ogre::Camera* cam)
            {
                mCamera = cam;
            }
    
            virtual Ogre::Camera* getCamera()
            {
                return mCamera;
            }
    
            /*-----------------------------------------------------------------------------
            | Sets the target we will revolve around. Only applies for orbit style.
            -----------------------------------------------------------------------------*/
            virtual void setTarget(Ogre::SceneNode* target)
            {
                if (target != mTarget)
                {
                    mTarget = target;
                    if(target)
                    {
                        setYawPitchDist(Ogre::Degree(0), Ogre::Degree(15), 150);
                        mCamera->setAutoTracking(true, mTarget);
                    }
                    else
                    {
                        mCamera->setAutoTracking(false);
                    }
    
                }
    
    
            }
    
            virtual Ogre::SceneNode* getTarget()
            {
                return mTarget;
            }
    
            /*-----------------------------------------------------------------------------
            | Sets the spatial offset from the target. Only applies for orbit style.
            -----------------------------------------------------------------------------*/
            virtual void setYawPitchDist(Ogre::Radian yaw, Ogre::Radian pitch, Ogre::Real dist)
            {
                mCamera->setPosition(mTarget->_getDerivedPosition());
                mCamera->setOrientation(mTarget->_getDerivedOrientation());
                mCamera->yaw(yaw);
                mCamera->pitch(-pitch);
                mCamera->moveRelative(Ogre::Vector3(0, 0, dist));
            }
    
            /*-----------------------------------------------------------------------------
            | Sets the camera's top speed. Only applies for free-look style.
            -----------------------------------------------------------------------------*/
            virtual void setTopSpeed(Ogre::Real topSpeed)
            {
                mTopSpeed = topSpeed;
            }
    
            virtual Ogre::Real getTopSpeed()
            {
                return mTopSpeed;
            }
    
            /*-----------------------------------------------------------------------------
            | Sets the movement style of our camera man.
            -----------------------------------------------------------------------------*/
            virtual void setStyle(CameraStyle style)
            {
                if (mStyle != CS_ORBIT && style == CS_ORBIT)
                {
                    setTarget(mTarget ? mTarget : mCamera->getSceneManager()->getRootSceneNode());
                    mCamera->setFixedYawAxis(true);
                    manualStop();
                    setYawPitchDist(Ogre::Degree(0), Ogre::Degree(15), 150);
                }
                else if (mStyle != CS_FREELOOK && style == CS_FREELOOK)
                {
                    mCamera->setAutoTracking(false);
                    mCamera->setFixedYawAxis(true);
                }
                else if (mStyle != CS_MANUAL && style == CS_MANUAL)
                {
                    mCamera->setAutoTracking(false);
                    manualStop();
                }
                mStyle = style;
    
            }
    
            virtual CameraStyle getStyle()
            {
                return mStyle;
            }
    
            /*-----------------------------------------------------------------------------
            | Manually stops the camera when in free-look mode.
            -----------------------------------------------------------------------------*/
            virtual void manualStop()
            {
                if (mStyle == CS_FREELOOK)
                {
                    mGoingForward = false;
                    mGoingBack = false;
                    mGoingLeft = false;
                    mGoingRight = false;
                    mGoingUp = false;
                    mGoingDown = false;
                    mVelocity = Ogre::Vector3::ZERO;
                }
            }
    
            virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt)
            {
                if (mStyle == CS_FREELOOK)
                {
                    // build our acceleration vector based on keyboard input composite
                    Ogre::Vector3 accel = Ogre::Vector3::ZERO;
                    if (mGoingForward) accel += mCamera->getDirection();
                    if (mGoingBack) accel -= mCamera->getDirection();
                    if (mGoingRight) accel += mCamera->getRight();
                    if (mGoingLeft) accel -= mCamera->getRight();
                    if (mGoingUp) accel += mCamera->getUp();
                    if (mGoingDown) accel -= mCamera->getUp();
    
                    // if accelerating, try to reach top speed in a certain time
                    Ogre::Real topSpeed = mFastMove ? mTopSpeed * 20 : mTopSpeed;
                    if (accel.squaredLength() != 0)
                    {
                        accel.normalise();
                        mVelocity += accel * topSpeed * evt.timeSinceLastFrame * 10;
                    }
                    // if not accelerating, try to stop in a certain time
                    else mVelocity -= mVelocity * evt.timeSinceLastFrame * 10;
    
                    Ogre::Real tooSmall = std::numeric_limits<Ogre::Real>::epsilon();
    
                    // keep camera velocity below top speed and above epsilon
                    if (mVelocity.squaredLength() > topSpeed * topSpeed)
                    {
                        mVelocity.normalise();
                        mVelocity *= topSpeed;
                    }
                    else if (mVelocity.squaredLength() < tooSmall * tooSmall)
                        mVelocity = Ogre::Vector3::ZERO;
    
                    if (mVelocity != Ogre::Vector3::ZERO) mCamera->move(mVelocity * evt.timeSinceLastFrame);
                }
    
                return true;
            }
    
            /*-----------------------------------------------------------------------------
            | Processes key presses for free-look style movement.
            -----------------------------------------------------------------------------*/
            virtual void injectKeyDown(const QKeyEvent& evt)
            {
                if (mStyle == CS_FREELOOK)
                {
                    if (evt.key() == Qt::Key_W || evt.key() == Qt::Key_Up) mGoingForward = true;
                    else if (evt.key() == Qt::Key_S || evt.key() == Qt::Key_Down) mGoingBack = true;
                    else if (evt.key() == Qt::Key_A || evt.key() == Qt::Key_Left) mGoingLeft = true;
                    else if (evt.key() == Qt::Key_D || evt.key() == Qt::Key_Right) mGoingRight = true;
                    else if (evt.key() == Qt::Key_PageUp) mGoingUp = true;
                    else if (evt.key() == Qt::Key_PageDown) mGoingDown = true;
                    else if (evt.key() == Qt::Key_Shift) mFastMove = true;
                }
            }
    
            /*-----------------------------------------------------------------------------
            | Processes key releases for free-look style movement.
            -----------------------------------------------------------------------------*/
            virtual void injectKeyUp(const QKeyEvent& evt)
            {
                if (mStyle == CS_FREELOOK)
                {
                    if (evt.key() == Qt::Key_W || evt.key() == Qt::Key_Up) mGoingForward = false;
                    else if (evt.key() == Qt::Key_S || evt.key() == Qt::Key_Down) mGoingBack = false;
                    else if (evt.key() == Qt::Key_A || evt.key() == Qt::Key_Left) mGoingLeft = false;
                    else if (evt.key() == Qt::Key_D || evt.key() == Qt::Key_Right) mGoingRight = false;
                    else if (evt.key() == Qt::Key_PageUp) mGoingUp = false;
                    else if (evt.key() == Qt::Key_PageDown) mGoingDown = false;
                    else if (evt.key() == Qt::Key_Shift) mFastMove = false;
                }
            }
    
            /*-----------------------------------------------------------------------------
            | Processes mouse movement differently for each style.
            -----------------------------------------------------------------------------*/
            virtual void injectMouseMove(int relX, int relY)
            {
    //            static int lastX = evt.x();
    //            static int lastY = evt.y();
    //            int relX = evt.x() - lastX;
    //            int relY = evt.y() - lastY;
    //            lastX = evt.x();
    //            lastY = evt.y();
                if (mStyle == CS_ORBIT)
                {
                    Ogre::Real dist = (mCamera->getPosition() - mTarget->_getDerivedPosition()).length();
    
                    if (mOrbiting)   // yaw around the target, and pitch locally
                    {
                        mCamera->setPosition(mTarget->_getDerivedPosition());
    
                        mCamera->yaw(Ogre::Degree(-relX * 0.025f));
                        mCamera->pitch(Ogre::Degree(-relY * 0.025f));
    
                        mCamera->moveRelative(Ogre::Vector3(0, 0, dist));
    
                        // don't let the camera go over the top or around the bottom of the target
                    }
                    else if (mZooming)  // move the camera toward or away from the target
                    {
                        // the further the camera is, the faster it moves
                        mCamera->moveRelative(Ogre::Vector3(0, 0, relY * 0.004f * dist));
                    }
                }
                else if (mStyle == CS_FREELOOK)
                {
                    mCamera->yaw(Ogre::Degree(-relX * 0.15f));
                    mCamera->pitch(Ogre::Degree(-relY * 0.15f));
                }
            }
    
            /*-----------------------------------------------------------------------------
            | Processes mouse movement differently for each style.
            -----------------------------------------------------------------------------*/
            virtual void injectWheelMove(const QWheelEvent& evt)
            {
                int relZ = evt.delta();
                if (mStyle == CS_ORBIT)
                {
                    Ogre::Real dist = (mCamera->getPosition() - mTarget->_getDerivedPosition()).length();
    
                    if (relZ != 0)  // move the camera toward or away from the target
                    {
                        // the further the camera is, the faster it moves
                        mCamera->moveRelative(Ogre::Vector3(0, 0, -relZ * 0.0008f * dist));
                    }
                }
            }
    
            /*-----------------------------------------------------------------------------
            | Processes mouse presses. Only applies for orbit style.
            | Left button is for orbiting, and right button is for zooming.
            -----------------------------------------------------------------------------*/
            virtual void injectMouseDown(const QMouseEvent& evt)
            {
                if (mStyle == CS_ORBIT)
                {
                    if (evt.buttons() & Qt::LeftButton) mOrbiting = true;
                    else if (evt.buttons() & Qt::RightButton) mZooming = true;
                }
            }
    
            /*-----------------------------------------------------------------------------
            | Processes mouse releases. Only applies for orbit style.
            | Left button is for orbiting, and right button is for zooming.
            -----------------------------------------------------------------------------*/
            virtual void injectMouseUp(const QMouseEvent& evt)
            {
                if (mStyle == CS_ORBIT)
                {
                    if (evt.buttons() & Qt::LeftButton) mOrbiting = false;
                    else if (evt.buttons() & Qt::RightButton) mZooming = false;
                }
            }
    
        protected:
    
            Ogre::Camera* mCamera;
            CameraStyle mStyle;
            Ogre::SceneNode* mTarget;
            bool mOrbiting;
            bool mZooming;
            Ogre::Real mTopSpeed;
            Ogre::Vector3 mVelocity;
            bool mGoingForward;
            bool mGoingBack;
            bool mGoingLeft;
            bool mGoingRight;
            bool mGoingUp;
            bool mGoingDown;
            bool mFastMove;
        };
    }
    
    #endif
    

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include "QTOgreWindow.h"
    #include "SdkQtCameraMan.h"
    
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    };
    
    #endif // WIDGET_H
    


  • main.cpp

    #include "widget.h"
    #include <QApplication>
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
    
        return a.exec();
    }
    

    QTOgreWindow.cpp

    #include "QTOgreWindow.h"
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
    #include <Compositor/OgreCompositorManager2.h>
    #endif
    
    /*
    Note that we pass any supplied QWindow parent to the base QWindow class. This is necessary should we
    need to use our class within a container.
    */
    QTOgreWindow::QTOgreWindow(QWindow *parent)
            : QWindow(parent)
            , m_update_pending(false)
            , m_animating(false)
            , m_ogreRoot(NULL)
            , m_ogreWindow(NULL)
            , m_ogreCamera(NULL)
            , m_cameraMan(NULL)
    {
            setAnimating(true);
            installEventFilter(this);
            m_ogreBackground = Ogre::ColourValue(0.0f, 0.5f, 1.0f);
    }
    
    /*
    Upon destruction of the QWindow object we destroy the Ogre3D scene.
    */
    QTOgreWindow::~QTOgreWindow()
    {
            if (m_cameraMan) delete m_cameraMan;
            delete m_ogreRoot;
    }
    
    /*
    In case any drawing surface backing stores (QRasterWindow or QOpenGLWindow) of Qt are supplied to this
    class in any way we inform Qt that they will be unused.
    */
    void QTOgreWindow::render(QPainter *painter)
    {
            Q_UNUSED(painter);
    }
    
    /*
    Our initialization function. Called by our renderNow() function once when the window is first exposed.
    */
    void QTOgreWindow::initialize()
    {
            /*
            As shown Ogre3D is initialized normally; just like in other documentation.
            */
    #ifdef _MSC_VER
            m_ogreRoot = new Ogre::Root(Ogre::String("plugins" OGRE_BUILD_SUFFIX ".cfg"));
    #else
            m_ogreRoot = new Ogre::Root(Ogre::String("plugins.cfg"));
    #endif
            Ogre::ConfigFile ogreConfig;
    
            /*
    
            Commended out for simplicity but should you need to initialize resources you can do so normally.
    
            ogreConfig.load("resources/resource_configs/resources.cfg");
    
            Ogre::ConfigFile::SectionIterator seci = ogreConfig.getSectionIterator();
            Ogre::String secName, typeName, archName;
            while (seci.hasMoreElements())
            {
                    secName = seci.peekNextKey();
                    Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
                    Ogre::ConfigFile::SettingsMultiMap::iterator i;
                    for (i = settings->begin(); i != settings->end(); ++i)
                    {
                            typeName = i->first;
                            archName = i->second;
                            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
                            archName, typeName, secName);
                    }
            }
    
            */
    
            const Ogre::RenderSystemList& rsList = m_ogreRoot->getAvailableRenderers();
            Ogre::RenderSystem* rs = rsList[0];
    
            /*
            This list setup the search order for used render system.
            */
            Ogre::StringVector renderOrder;
    #if defined(Q_OS_WIN)
            renderOrder.push_back("Direct3D9");
            renderOrder.push_back("Direct3D11");
    #endif
            renderOrder.push_back("OpenGL");
            renderOrder.push_back("OpenGL 3+");
            for (Ogre::StringVector::iterator iter = renderOrder.begin(); iter != renderOrder.end(); iter++)
            {
                    for (Ogre::RenderSystemList::const_iterator it = rsList.begin(); it != rsList.end(); it++)
                    {
                            if ((*it)->getName().find(*iter) != Ogre::String::npos)
                            {
                                    rs = *it;
                                    break;
                            }
                    }
                    if (rs != NULL) break;
            }
            if (rs == NULL)
            {
                    if (!m_ogreRoot->restoreConfig())
                    {
    //                        if (!m_ogreRoot->showConfigDialog())
    //                                OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS,
    //                                        "Abort render system configuration",
    //                                        "QTOgreWindow::initialize");
                    }
            }
    
            /*
            Setting size and VSync on windows will solve a lot of problems
            */
            QString dimensions = QString("%1 x %2").arg(this->width()).arg(this->height());
            rs->setConfigOption("Video Mode", dimensions.toStdString());
            rs->setConfigOption("Full Screen", "No");
            rs->setConfigOption("VSync", "Yes");
            m_ogreRoot->setRenderSystem(rs);
            m_ogreRoot->initialise(false);
    
            Ogre::NameValuePairList parameters;
            /*
            Flag within the parameters set so that Ogre3D initializes an OpenGL context on it's own.
            */
            if (rs->getName().find("GL") <= rs->getName().size())
                    parameters["currentGLContext"] = Ogre::String("false");
    
            /*
            We need to supply the low level OS window handle to this QWindow so that Ogre3D knows where to draw
            the scene. Below is a cross-platform method on how to do this.
            If you set both options (externalWindowHandle and parentWindowHandle) this code will work with OpenGL
            and DirectX.
            */
    #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
            parameters["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)(this->winId()));
            parameters["parentWindowHandle"] = Ogre::StringConverter::toString((size_t)(this->winId()));
    #else
            parameters["externalWindowHandle"] = Ogre::StringConverter::toString((unsigned long)(this->winId()));
            parameters["parentWindowHandle"] = Ogre::StringConverter::toString((unsigned long)(this->winId()));
    #endif
    
    #if defined(Q_OS_MAC)
            parameters["macAPI"] = "cocoa";
            parameters["macAPICocoaUseNSView"] = "true";
    #endif
    
            /*
            Note below that we supply the creation function for the Ogre3D window the width and height
            from the current QWindow object using the "this" pointer.
            */
            m_ogreWindow = m_ogreRoot->createRenderWindow("QT Window",
                    this->width(),
                    this->height(),
                    false,
                    &parameters);
            m_ogreWindow->setVisible(true);
    
            /*
            The rest of the code in the initialization function is standard Ogre3D scene code. Consult other
            tutorials for specifics.
            */
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            const size_t numThreads = std::max<int>(1, Ogre::PlatformInformation::getNumLogicalCores());
            Ogre::InstancingThreadedCullingMethod threadedCullingMethod = Ogre::INSTANCING_CULLING_SINGLETHREAD;
            if (numThreads > 1)threadedCullingMethod = Ogre::INSTANCING_CULLING_THREADED;
            m_ogreSceneMgr = m_ogreRoot->createSceneManager(Ogre::ST_GENERIC, numThreads, threadedCullingMethod);
    #else
            m_ogreSceneMgr = m_ogreRoot->createSceneManager(Ogre::ST_GENERIC);
    #endif
    
            m_ogreCamera = m_ogreSceneMgr->createCamera("MainCamera");
            m_ogreCamera->setPosition(Ogre::Vector3(0.0f, 0.0f, 10.0f));
            m_ogreCamera->lookAt(Ogre::Vector3(0.0f, 0.0f, -300.0f));
            m_ogreCamera->setNearClipDistance(0.1f);
            m_ogreCamera->setFarClipDistance(200.0f);
            m_cameraMan = new OgreQtBites::SdkQtCameraMan(m_ogreCamera);   // create a default camera controller
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            createCompositor();
    #else
            Ogre::Viewport* pViewPort = m_ogreWindow->addViewport(m_ogreCamera);
            pViewPort->setBackgroundColour(m_ogreBackground);
    #endif
    
            m_ogreCamera->setAspectRatio(
                    Ogre::Real(m_ogreWindow->getWidth()) / Ogre::Real(m_ogreWindow->getHeight()));
            m_ogreCamera->setAutoAspectRatio(true);
    
            Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    
            createScene();
    
            m_ogreRoot->addFrameListener(this);
    }
    
    void QTOgreWindow::createScene()
    {
            /*
            Example scene
            Derive this class for your own purpose and overwite this function to have a working Ogre widget with
            your own content.
            */
            m_ogreSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            Ogre::Entity* sphereMesh = m_ogreSceneMgr->createEntity(Ogre::SceneManager::PT_SPHERE);
    #else
            Ogre::Entity* sphereMesh = m_ogreSceneMgr->createEntity("mySphere", Ogre::SceneManager::PT_SPHERE);
    #endif
    
            Ogre::SceneNode* childSceneNode = m_ogreSceneMgr->getRootSceneNode()->createChildSceneNode();
    
            childSceneNode->attachObject(sphereMesh);
    
            Ogre::MaterialPtr sphereMaterial = Ogre::MaterialManager::getSingleton().create("SphereMaterial",
                    Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true);
    
            sphereMaterial->getTechnique(0)->getPass(0)->setAmbient(0.1f, 0.1f, 0.1f);
            sphereMaterial->getTechnique(0)->getPass(0)->setDiffuse(0.2f, 0.2f, 0.2f, 1.0f);
            sphereMaterial->getTechnique(0)->getPass(0)->setSpecular(0.9f, 0.9f, 0.9f, 1.0f);
            //sphereMaterial->setAmbient(0.2f, 0.2f, 0.5f);
            //sphereMaterial->setSelfIllumination(0.2f, 0.2f, 0.1f);
    
            sphereMesh->setMaterialName("SphereMaterial");
            childSceneNode->setPosition(Ogre::Vector3(0.0f, 0.0f, 0.0f));
            childSceneNode->setScale(Ogre::Vector3(0.01f, 0.01f, 0.01f)); // Radius, in theory.
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
            Ogre::SceneNode* pLightNode = m_ogreSceneMgr->getRootSceneNode()->createChildSceneNode();
            Ogre::Light* light = m_ogreSceneMgr->createLight();
            pLightNode->attachObject(light);
            pLightNode->setPosition(20.0f, 80.0f, 50.0f);
    #else
            Ogre::Light* light = m_ogreSceneMgr->createLight("MainLight");
            light->setPosition(20.0f, 80.0f, 50.0f);
    #endif
    }
    
    #if OGRE_VERSION >= ((2 << 16) | (0 << 8) | 0)
    void QTOgreWindow::createCompositor()
    {
            /*
            Example compositor
            Derive this class for your own purpose and overwite this function to have a working Ogre
            widget with your own compositor.
            */
            Ogre::CompositorManager2* compMan = m_ogreRoot->getCompositorManager2();
            const Ogre::String workspaceName = "default scene workspace";
            const Ogre::IdString workspaceNameHash = workspaceName;
            compMan->createBasicWorkspaceDef(workspaceName, m_ogreBackground);
            compMan->addWorkspace(m_ogreSceneMgr, m_ogreWindow, m_ogreCamera, workspaceNameHash, true);
    }
    #endif
    
    void QTOgreWindow::render()
    {
            /*
            How we tied in the render function for OGre3D with QWindow's render function. This is what gets call
            repeatedly. Note that we don't call this function directly; rather we use the renderNow() function
            to call this method as we don't want to render the Ogre3D scene unless everything is set up first.
            That is what renderNow() does.
    
            Theoretically you can have one function that does this check but from my experience it seems better
            to keep things separate and keep the render function as simple as possible.
            */
    //        Ogre::WindowEventUtilities::messagePump();
            m_ogreRoot->renderOneFrame();
    }
    
    void QTOgreWindow::renderLater()
    {
            /*
            This function forces QWindow to keep rendering. Omitting this causes the renderNow() function to
            only get called when the window is resized, moved, etc. as opposed to all of the time; which is
            generally what we need.
            */
            if (!m_update_pending)
            {
                    m_update_pending = true;
                    QApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
            }
    }
    
    bool QTOgreWindow::event(QEvent *event)
    {
            /*
            QWindow's "message pump". The base method that handles all QWindow events. As you will see there
            are other methods that actually process the keyboard/other events of Qt and the underlying OS.
    
            Note that we call the renderNow() function which checks to see if everything is initialized, etc.
            before calling the render() function.
            */
    
            switch (event->type())
            {
            case QEvent::UpdateRequest:
                    m_update_pending = false;
                    renderNow();
                    return true;
    
            default:
                    return QWindow::event(event);
            }
    }
    
    /*
    Called after the QWindow is reopened or when the QWindow is first opened.
    */
    void QTOgreWindow::exposeEvent(QExposeEvent *event)
    {
            Q_UNUSED(event);
    
            if (isExposed())
                    renderNow();
    }
    
    /*
    The renderNow() function calls the initialize() function when needed and if the QWindow is already
    initialized and prepped calls the render() method.
    */
    void QTOgreWindow::renderNow()
    {
            if (!isExposed())
                    return;
    
            if (m_ogreRoot == NULL)
            {
                    initialize();
            }
    
            render();
    
            if (m_animating)
            renderLater();
    }
    
    /*
    Our event filter; handles the resizing of the QWindow. When the size of the QWindow changes note the
    call to the Ogre3D window and camera. This keeps the Ogre3D scene looking correct.
    */
    bool QTOgreWindow::eventFilter(QObject *target, QEvent *event)
    {
            if (target == this)
            {
                    if (event->type() == QEvent::Resize)
                    {
                            if (isExposed() && m_ogreWindow != NULL)
                            {
                                    m_ogreWindow->resize(this->width(), this->height());
                            }
                    }
            }
    
            return false;
    }
    
    /*
    How we handle keyboard and mouse events.
    */
    void QTOgreWindow::keyPressEvent(QKeyEvent * ev)
    {
            if(m_cameraMan)
                    m_cameraMan->injectKeyDown(*ev);
    }
    
    void QTOgreWindow::keyReleaseEvent(QKeyEvent * ev)
    {
            if(m_cameraMan)
                    m_cameraMan->injectKeyUp(*ev);
    }
    
    void QTOgreWindow::mouseMoveEvent( QMouseEvent* e )
    {
            static int lastX = e->x();
            static int lastY = e->y();
            int relX = e->x() - lastX;
            int relY = e->y() - lastY;
            lastX = e->x();
            lastY = e->y();
    
            if(m_cameraMan && (e->buttons() & Qt::LeftButton))
                    m_cameraMan->injectMouseMove(relX, relY);
    }
    
    void QTOgreWindow::wheelEvent(QWheelEvent *e)
    {
            if(m_cameraMan)
                    m_cameraMan->injectWheelMove(*e);
    }
    
    void QTOgreWindow::mousePressEvent( QMouseEvent* e )
    {
            if(m_cameraMan)
                    m_cameraMan->injectMouseDown(*e);
    }
    
    void QTOgreWindow::mouseReleaseEvent( QMouseEvent* e )
    {
            if(m_cameraMan)
                    m_cameraMan->injectMouseUp(*e);
    
            QPoint pos = e->pos();
            Ogre::Ray mouseRay = m_ogreCamera->getCameraToViewportRay(
                    (Ogre::Real)pos.x() / m_ogreWindow->getWidth(),
                    (Ogre::Real)pos.y() / m_ogreWindow->getHeight());
            Ogre::RaySceneQuery* pSceneQuery = m_ogreSceneMgr->createRayQuery(mouseRay);
            pSceneQuery->setSortByDistance(true);
            Ogre::RaySceneQueryResult vResult = pSceneQuery->execute();
            for (size_t ui = 0; ui < vResult.size(); ui++)
            {
                    if (vResult[ui].movable)
                    {
                            if (vResult[ui].movable->getMovableType().compare("Entity") == 0)
                            {
                                    emit entitySelected((Ogre::Entity*)vResult[ui].movable);
                            }
                    }
            }
            m_ogreSceneMgr->destroyQuery(pSceneQuery);
    }
    
    /*
    Function to keep track of when we should and shouldn't redraw the window; we wouldn't want to do
    rendering when the QWindow is minimized. This takes care of those scenarios.
    */
    void QTOgreWindow::setAnimating(bool animating)
    {
            m_animating = animating;
    
            if (animating)
                    renderLater();
    }
    
    bool QTOgreWindow::frameRenderingQueued(const Ogre::FrameEvent& evt)
    {
            m_cameraMan->frameRenderingQueued(evt);
            return true;
    }
    
    void QTOgreWindow::log(Ogre::String msg)
    {
            if(Ogre::LogManager::getSingletonPtr() != NULL) Ogre::LogManager::getSingletonPtr()->logMessage(msg);
    }
    
    void QTOgreWindow::log(QString msg)
    {
            log(Ogre::String(msg.toStdString().c_str()));
    }
    

    widget.cpp

    #include "widget.h"
    
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        QTOgreWindow * ogreWindow = new QTOgreWindow ();
        ogreWindow->show ();
    }
    
    Widget::~Widget()
    {
    
    }
    

    plugin.cfg

    # Defines plugins to load
    
    # Define plugin folder
    PluginFolder=/usr/local/lib/OGRE
    
    # Define plugins
    # Plugin=RenderSystem_Direct3D9
    # Plugin=RenderSystem_Direct3D11
     Plugin=RenderSystem_GL
     Plugin=RenderSystem_GL3Plus
     Plugin=RenderSystem_GLES2
    # Plugin=RenderSystem_Metal
    # Plugin=RenderSystem_Tiny
     Plugin=Plugin_ParticleFX
     Plugin=Plugin_BSPSceneManager
    # Plugin=Plugin_CgProgramManager
     Plugin=Codec_EXR
     Plugin=Codec_STBI
    # Plugin=Codec_FreeImage
     Plugin=Plugin_PCZSceneManager
     Plugin=Plugin_OctreeZone
     Plugin=Plugin_OctreeSceneManager
     Plugin=Plugin_DotScene
    # Plugin=Codec_Assimp
    

    Screenshot_20211208_120343.png

    the first part of the answer at the top (failed to fit into one message)


Log in to reply