将Ogre嵌入到Windows平台上的qt应用程序中

发布于 2024-10-17 22:29:32 字数 4464 浏览 3 评论 0原文

将 Ogre 嵌入到 qt 应用程序中 链接:QtOgre

但它是mac和linux平台,我尝试迁移到windows平台,但是失败了。 我的应用程序运行如下: 在此处输入图像描述

enter code here
#ifndef OGREWIDGET_H
#define OGREWIDGET_H
#include "XVQCUGLLIB_Global.h"
#include <QGLWidget>
#include <Ogre/Ogre.h>


class XVQCUGLFXLIB_API OgreWidget : public QGLWidget
{
    //Q_OBJECT;

public:
    OgreWidget( QWidget *parent=0 );
    virtual ~OgreWidget();

protected:
     virtual void initializeGL();
    virtual void resizeGL( int, int );
    virtual void paintGL();

    void init();

    virtual Ogre::RenderSystem* chooseRenderer( Ogre::RenderSystemList* );

    Ogre::Root *mOgreRoot;
    Ogre::RenderWindow *mOgreWindow;
    Ogre::Camera *mCamera;
    Ogre::Viewport *mViewport;
    Ogre::SceneManager *mSceneMgr;
};
#endif // OGREWIDGET_H

//////////////////////// //////////////////////////////////

  #include "XVQCUGLLIB_Internal.h"

 #include "OgreWidget.h"
 #define THIS OgreWidget

 OgreWidget::OgreWidget(QWidget *parent)
     : QGLWidget( parent ),
     mOgreWindow(NULL)
 {
     init();
 }

 OgreWidget::~OgreWidget()
 {
     mOgreRoot->shutdown();
     delete mOgreRoot;
     destroy();
 }

 /**
  * @brief init the object
  * @author kito berg-taylor
  */
      void THIS::init()
      {
   // create the main ogre object
   mOgreRoot = new Ogre::Root;

   mOgreRoot->loadPlugin("RenderSystem_GL_d");
   Ogre::String rName("OpenGL Rendering Subsystem");
   Ogre::RenderSystemList rList = mOgreRoot->getAvailableRenderers();
   Ogre::RenderSystemList::iterator it = rList.begin();
   Ogre::RenderSystem *rSys = 0;
   while(it != rList.end())
   {
       rSys = * (it++);
       Ogre::String strx=rSys->getName();
       if(strx == rName)
       {
           mOgreRoot->setRenderSystem(rSys);
           break;
       }
   }
   mOgreRoot->initialise(false); // don't create a window
 }

 /**
  * @brief setup the rendering context
  * @author Kito Berg-Taylor
  */
 void THIS::initializeGL()
 {

   //== Creating and Acquiring Ogre Window ==//

   // Get the parameters of the window QT created

   Ogre::String winHandle=Ogre::StringConverter::toString((size_t)(HWND)winId());

   Ogre::NameValuePairList params;
   params["parentWindowHandle"] = winHandle;

   mOgreWindow = mOgreRoot->createRenderWindow( "QOgreWidget_RenderWindow",
                            this->width(),
                            this->height(),
                            false,
                            &params );

   //mOgreWindow->setActive(true);
   //WId ogreWinId = 0x0;
   //mOgreWindow->getCustomAttribute( "WINDOW", &ogreWinId );

   //assert( ogreWinId );

   //this->create( ogreWinId );
   setAttribute( Qt::WA_PaintOnScreen, true );
   setAttribute( Qt::WA_NoBackground );

   //== Ogre Initialization ==//
   Ogre::SceneType scene_manager_type = Ogre::ST_EXTERIOR_CLOSE;

   mSceneMgr = mOgreRoot->createSceneManager( scene_manager_type );
   mSceneMgr->setAmbientLight( Ogre::ColourValue(1,1,1) );

   mCamera = mSceneMgr->createCamera( "QOgreWidget_Cam" );
   mCamera->setPosition( Ogre::Vector3(0,1,0) );
   mCamera->lookAt( Ogre::Vector3(0,0,0) );
   mCamera->setNearClipDistance( 1.0 );

   Ogre::Viewport *mViewport = mOgreWindow->addViewport( mCamera );
   mViewport->setBackgroundColour( Ogre::ColourValue( 1.0,1.0,1.0 ) );
 }

 /**
  * @brief render a frame
  * @author Kito Berg-Taylor
  */
 void THIS::paintGL()
 {
     //QGLWidget::paintGL();
   assert( mOgreWindow );


   mOgreRoot->_fireFrameStarted();

  mOgreWindow->update();

  // mOgreRoot->renderOneFrame();

   mOgreRoot->_fireFrameEnded();
 }

 /**
  * @brief resize the GL window
  * @author Kito Berg-Taylor
  */
 void THIS::resizeGL( int width, int height )
 {
//QGLWidget::resizeGL(width,height);
   assert( mOgreWindow );
   mOgreWindow->windowMovedOrResized();
   mCamera->setAspectRatio(Ogre::Real(width) / Ogre::Real(height));
 }

 /**
  * @brief choose the right renderer
  * @author Kito Berg-Taylor
  */
 Ogre::RenderSystem* THIS::chooseRenderer( Ogre::RenderSystemList *renderers )
 {
   // It would probably be wise to do something more friendly 
   // that just use the first available renderer
   return *renderers->begin();
 }

Embedding Ogre into a qt application
Link:QtOgre

But it is mac and linux platform,I try to migrate to windows platform, but failed.
My app run like this:
enter image description here

enter code here
#ifndef OGREWIDGET_H
#define OGREWIDGET_H
#include "XVQCUGLLIB_Global.h"
#include <QGLWidget>
#include <Ogre/Ogre.h>


class XVQCUGLFXLIB_API OgreWidget : public QGLWidget
{
    //Q_OBJECT;

public:
    OgreWidget( QWidget *parent=0 );
    virtual ~OgreWidget();

protected:
     virtual void initializeGL();
    virtual void resizeGL( int, int );
    virtual void paintGL();

    void init();

    virtual Ogre::RenderSystem* chooseRenderer( Ogre::RenderSystemList* );

    Ogre::Root *mOgreRoot;
    Ogre::RenderWindow *mOgreWindow;
    Ogre::Camera *mCamera;
    Ogre::Viewport *mViewport;
    Ogre::SceneManager *mSceneMgr;
};
#endif // OGREWIDGET_H

///////////////////////////////////////

  #include "XVQCUGLLIB_Internal.h"

 #include "OgreWidget.h"
 #define THIS OgreWidget

 OgreWidget::OgreWidget(QWidget *parent)
     : QGLWidget( parent ),
     mOgreWindow(NULL)
 {
     init();
 }

 OgreWidget::~OgreWidget()
 {
     mOgreRoot->shutdown();
     delete mOgreRoot;
     destroy();
 }

 /**
  * @brief init the object
  * @author kito berg-taylor
  */
      void THIS::init()
      {
   // create the main ogre object
   mOgreRoot = new Ogre::Root;

   mOgreRoot->loadPlugin("RenderSystem_GL_d");
   Ogre::String rName("OpenGL Rendering Subsystem");
   Ogre::RenderSystemList rList = mOgreRoot->getAvailableRenderers();
   Ogre::RenderSystemList::iterator it = rList.begin();
   Ogre::RenderSystem *rSys = 0;
   while(it != rList.end())
   {
       rSys = * (it++);
       Ogre::String strx=rSys->getName();
       if(strx == rName)
       {
           mOgreRoot->setRenderSystem(rSys);
           break;
       }
   }
   mOgreRoot->initialise(false); // don't create a window
 }

 /**
  * @brief setup the rendering context
  * @author Kito Berg-Taylor
  */
 void THIS::initializeGL()
 {

   //== Creating and Acquiring Ogre Window ==//

   // Get the parameters of the window QT created

   Ogre::String winHandle=Ogre::StringConverter::toString((size_t)(HWND)winId());

   Ogre::NameValuePairList params;
   params["parentWindowHandle"] = winHandle;

   mOgreWindow = mOgreRoot->createRenderWindow( "QOgreWidget_RenderWindow",
                            this->width(),
                            this->height(),
                            false,
                            ¶ms );

   //mOgreWindow->setActive(true);
   //WId ogreWinId = 0x0;
   //mOgreWindow->getCustomAttribute( "WINDOW", &ogreWinId );

   //assert( ogreWinId );

   //this->create( ogreWinId );
   setAttribute( Qt::WA_PaintOnScreen, true );
   setAttribute( Qt::WA_NoBackground );

   //== Ogre Initialization ==//
   Ogre::SceneType scene_manager_type = Ogre::ST_EXTERIOR_CLOSE;

   mSceneMgr = mOgreRoot->createSceneManager( scene_manager_type );
   mSceneMgr->setAmbientLight( Ogre::ColourValue(1,1,1) );

   mCamera = mSceneMgr->createCamera( "QOgreWidget_Cam" );
   mCamera->setPosition( Ogre::Vector3(0,1,0) );
   mCamera->lookAt( Ogre::Vector3(0,0,0) );
   mCamera->setNearClipDistance( 1.0 );

   Ogre::Viewport *mViewport = mOgreWindow->addViewport( mCamera );
   mViewport->setBackgroundColour( Ogre::ColourValue( 1.0,1.0,1.0 ) );
 }

 /**
  * @brief render a frame
  * @author Kito Berg-Taylor
  */
 void THIS::paintGL()
 {
     //QGLWidget::paintGL();
   assert( mOgreWindow );


   mOgreRoot->_fireFrameStarted();

  mOgreWindow->update();

  // mOgreRoot->renderOneFrame();

   mOgreRoot->_fireFrameEnded();
 }

 /**
  * @brief resize the GL window
  * @author Kito Berg-Taylor
  */
 void THIS::resizeGL( int width, int height )
 {
//QGLWidget::resizeGL(width,height);
   assert( mOgreWindow );
   mOgreWindow->windowMovedOrResized();
   mCamera->setAspectRatio(Ogre::Real(width) / Ogre::Real(height));
 }

 /**
  * @brief choose the right renderer
  * @author Kito Berg-Taylor
  */
 Ogre::RenderSystem* THIS::chooseRenderer( Ogre::RenderSystemList *renderers )
 {
   // It would probably be wise to do something more friendly 
   // that just use the first available renderer
   return *renderers->begin();
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-10-24 22:29:32

您是否在任何地方调用了initializeGL()?

IIRC 你必须明确地调用它 - 它不会自动发生

Are you calling initializeGL() anywhere?

IIRC you have to explicitly call it - it doesn't happen automatically

勿忘心安 2024-10-24 22:29:32

重写 QWidget::resizeEvent() 方法。在该方法中,您会收到窗口大小更改的通知。调整食人魔窗口的大小。

Over ride the QWidget::resizeEvent() method. In that method you receive notifications of changes in the size of the window. Resize the ogre window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文