Jump to content
  • Advertisement
Sign in to follow this  
blank_oda

Qt Ogre integration

This topic is 2716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to integrate Ogre in a Qt application with this french tutorial http://irmatden.developpez.com/tutor...ation-ogre-qt/

I only made the first part (a simple window with a menu allowing to change the background color) I already have to cope with a few difficulties icon_confused.gif

actually after several modifications, I get still the same result : the whole screen becomes black with just the Qt window of which I can't change the background color

this is my code :

.pro :

TEMPLATE = app
TARGET = Qt_Ogre
QT += core gui
INCLUDEPATH = /usr/include/OGRE;
HEADERS += ogrewidget.h
SOURCES += ogrewidget.cpp \
main.cpp
FORMS +=
RESOURCES +=
unix:LIBS += -lOgreMain


main.cpp :

#include <QtGui>

#include "ogrewidget.h"

class MainWindow : public QMainWindow

#include "main.moc"

int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}


ogrewidget.h : (I put apple and MS sections in comments since I don't use them)

#ifndef OGREWIDGET_H
#define OGREWIDGET_H

#include <Ogre.h>
#include <QtGui>

#ifdef Q_WS_X11
#include <QtGui/QX11Info>
#endif



class OgreWidget : public QWidget
{
Q_OBJECT

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

public slots:
void setBackgroundColor(QColor c);

protected:
virtual void moveEvent(QMoveEvent *e);
virtual QPaintEngine* paintEngine() const;
virtual void paintEvent(QPaintEvent *e);
virtual void resizeEvent(QResizeEvent *e);
virtual void showEvent(QShowEvent *e);

private:
void initOgreSystem();

private:
Ogre::Root *ogreRoot;
Ogre::SceneManager *ogreSceneManager;
Ogre::RenderWindow *ogreRenderWindow;
Ogre::Viewport *ogreViewport;
Ogre::Camera *ogreCamera;

};

#endif // OGREWIDGET_H


ogrewidget.cpp

#include "ogrewidget.h"

/*OgreWidget::OgreWidget(QWidget *parent)
: QWidget(parent)
{
//ui.setupUi(this);
}*/

OgreWidget::OgreWidget(QWidget *parent)
:QWidget(parent),
ogreRoot(0), ogreSceneManager(0), ogreRenderWindow(0), ogreViewport(0),
ogreCamera(0)
{
//setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_PaintOnScreen);
setMinimumSize(240,240);
}

OgreWidget::~OgreWidget()
{
if(ogreRenderWindow)
{
ogreRenderWindow->removeAllViewports();
}

if(ogreRoot)
{
ogreRoot->detachRenderTarget(ogreRenderWindow);

if(ogreSceneManager)
{
ogreRoot->destroySceneManager(ogreSceneManager);
}
}

delete ogreRoot;
}

void OgreWidget::setBackgroundColor(QColor c)
{
if(ogreViewport)
{
Ogre::ColourValue ogreColour;
ogreColour.setAsARGB(c.rgba());
ogreViewport->setBackgroundColour(ogreColour);
}
}

void OgreWidget::moveEvent(QMoveEvent *e)
{
QWidget::moveEvent(e);

if(e->isAccepted() && ogreRenderWindow)
{
ogreRenderWindow->windowMovedOrResized();
update();
}
}

QPaintEngine *OgreWidget:: paintEngine() const
{
return 0;
}

void OgreWidget::paintEvent(QPaintEvent *e)
{
// ogreRoot->_fireFrameStarted();
// ogreRenderWindow->update();
// ogreRoot->_fireFrameEnded();

// ogreRoot->renderOneFrame();
// ogreRenderWindow->update();

// e->accept();

ogreRoot->_fireFrameStarted();
ogreRenderWindow->update();
ogreRoot->_fireFrameRenderingQueued();
ogreRoot->_fireFrameEnded();
}

void OgreWidget::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);

if(e->isAccepted())
{
const QSize &newSize = e->size();
if(ogreRenderWindow)
{
ogreRenderWindow->resize(newSize.width(), newSize.height());
ogreRenderWindow->windowMovedOrResized();
}
if(ogreCamera)
{
Ogre::Real aspectRatio = Ogre::Real(newSize.width()) / Ogre::Real(newSize.height());
ogreCamera->setAspectRatio(aspectRatio);
}
}
}

void OgreWidget::showEvent(QShowEvent *e)
{
if(!ogreRoot)
{
initOgreSystem();
}

QWidget::showEvent(e);
}

void OgreWidget::initOgreSystem()
{
ogreRoot = new Ogre::Root();

Ogre::RenderSystem *renderSystem = ogreRoot->getRenderSystemByName("OpenGL Rendering Subsystem");
ogreRoot->setRenderSystem(renderSystem);
ogreRoot->initialise(true);

ogreSceneManager = ogreRoot->createSceneManager(Ogre::ST_GENERIC);

Ogre::NameValuePairList viewConfig;
Ogre::String widgethandle;

//#ifdef Q_WS_WIN
// widgetHandle = Ogre::StringConverter::toString((size_t)((HWND)winId()));
//#else
// QWidget *q_parent = dynamic_cast <QWidget *> (parent());
// QX11Info xInfo = x11Info();
//
// widgetHandle = Ogre::StringConverter::toString ((unsigned long)xInfo.display()) +
// ":" + Ogre::StringConverter::toString ((unsigned int)xInfo.screen()) +
// ":" + Ogre::StringConverter::toString ((unsigned long)q_parent->winId());
//#endif
// viewConfig["externalWindowHandle"] = widgetHandle;

//#if defined(Q_WS_WIN)
// //positive integer for W32 (HWND handle) - According to Ogre Docs
// widgethandle = Ogre::StringConverter::toString((unsigned int)(winId()));
// viewConfig["externalWindowHandle"] = widgethandle;
//
//#endif

#if defined(Q_WS_X11)
//poslong:posint:poslong:poslong (display*:screen:windowHandle:XVisualInfo*) for GLX - According to Ogre Docs

QWidget *q_parent = dynamic_cast <QWidget *> (parent());
QX11Info info = x11Info();
widgethandle = Ogre::StringConverter::toString((unsigned long)info.display());
widgethandle += ":";
widgethandle += Ogre::StringConverter::toString((unsigned int)info.screen());
widgethandle += ":";
widgethandle += Ogre::StringConverter::toString((unsigned long)q_parent->winId());
//widgethandle += ":";
//widgethandle += Ogre::StringConverter::toString((unsigned long)(info.visual()));

viewConfig["parentWindowHandle"] = widgethandle;
#endif


//#if defined(Q_WS_MAC)
//
// widgethandle = Ogre::StringConverter::toString((unsigned int)(winId()));
// viewConfig["macAPI"] = "cocoa";
// viewConfig["macAPICocoaUseNSView"] = "true";
//
//#endif

ogreRenderWindow = ogreRoot->createRenderWindow("Ogre rendering window",
width(), height(), false, &viewConfig);

ogreCamera = ogreSceneManager->createCamera("myCamera");

ogreViewport = ogreRenderWindow->addViewport(ogreCamera);
ogreViewport->setBackgroundColour(Ogre::ColourValue(0,0,0));
ogreCamera->setAspectRatio(Ogre::Real(width()) / Ogre::Real(height()));
}


thanks for your answers :)

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!