Hello,
Thanks to everyone's help, I managed to get a bare-bones Ogre3D window working with just a QWindow versus a QWidget.
The difference is that a QWindow uses less resources than a QWidget in memory; perfect for getting Qt/Ogre3D instance with the smallest memory footprint.
Would love if anyone can offer comments or improvements.
Anyway, here goes:
plugins.cfg:
Code: Select all
# Defines plugins to load
# Define plugin folder
PluginFolder=Plugins
# Define plugins
#
#Commented out Direct3D for preference to use OpenGL but you can decide on your own.
#
# Plugin=RenderSystem_Direct3D9
# Plugin=RenderSystem_Direct3D11
Plugin=RenderSystem_GL
# Plugin=RenderSystem_GL3Plus
# Plugin=RenderSystem_GLES
# Plugin=RenderSystem_GLES2
Plugin=Plugin_ParticleFX
Plugin=Plugin_BSPSceneManager
Plugin=Plugin_CgProgramManager
Plugin=Plugin_PCZSceneManager
Plugin=Plugin_OctreeZone
Plugin=Plugin_OctreeSceneManager
QTOgreWindow.h (Header)
Code: Select all
#ifndef QTOGREWINDOW_H_
#define QTOGREWINDOW_H_
#ifndef QT_INCLUDES_
#define QT_INCLUDES_
#include <QtCore/QCoreApplication>
#include <QtCore/QString>
#include <QtCore/QVector>
#include <QtCore/QtAlgorithms>
#include <QtCore/QFlags>
#include <QtCore/qlist.h>
#include <QtCore/qurl.h>
#include <QtCore/qtimer.h>
#include <QtCore/qfile.h>
#include <QtCore/qdir.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qmath.h>
#include <QtCore/QByteArray>
#include <QtCore/QtDebug>
#include <QtCore/QtGlobal>
#include <QtCore/QDateTime>
#include <QtCore/QCryptographicHash>
#include <QtCore/qsysinfo.h>
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qstandardpaths.h>
#include <QtCore/qprocess.h>
#include <QtCore/qmimedata.h>
#include <QtGui/QGuiApplication>
#include <QtGui/QMatrix3x3>
#include <QtGui/QMatrix4x4>
#include <QtGui/QVector2D>
#include <QtGui/QVector3D>
#include <QtGui/QVector4D>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QScreen>
#include <QtGui/QWindow>
#include <QtGui/QOpenGLFunctions>
#include <QtGui/QOpenGLFunctions_3_1>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLPaintDevice>
#include <QtGui/QPainter>
#include <QtGui/qdesktopservices.h>
#include <QtGui/qclipboard.h>
#include <QtWidgets/qopenglwidget.h>
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qboxlayout.h>
#include <QtWidgets/qgroupbox.h>
#include <QtWidgets/qmessagebox.h>
#include <QtWidgets/qdesktopwidget.h>
#include <QtWidgets/qdockwidget.h>
#include <QtWidgets/qmainwindow.h>
#include <QtWidgets/qfiledialog.h>
#include <QtWidgets/qstatusbar.h>
#include <QtWidgets/qtablewidget.h>
#include <QtWidgets/qdialogbuttonbox.h>
#include <QtWidgets/QAction>
#include <QtWidgets/QMenu>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qlineedit.h>
#include <QtWidgets/qtextedit.h>
#include <QtWidgets/qcheckbox.h>
#include <QtWidgets/qcombobox.h>
#include <QtWidgets/qpushbutton.h>
#include <QtWidgets/qscrollarea.h>
#include <QtWidgets/QMenubar>
#include <QtWidgets/QProgressBar>
#include <QtWidgets/qslider.h>
#include <QtWidgets/QToolBar>
#include <QtGui/QKeyEvent>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkcookiejar.h>
#include <QtNetwork/qnetworkinterface.h>
#include <QtNetwork/qnetworkrequest.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtNetwork/qsslerror.h>
#endif
#ifndef OGRE_INCLUDES_
#define OGRE_INCLUDES_
#include "OGRE/Ogre.h"
#endif
class QTOgreWindow : public QWindow {
Q_OBJECT
public:
explicit QTOgreWindow(QWindow *parent = NULL);
~QTOgreWindow();
virtual void render(QPainter *painter);
virtual void render();
virtual void initialize();
void setAnimating(bool animating);
public slots:
virtual void renderLater();
virtual void renderNow();
virtual bool eventFilter(QObject *target, QEvent *event);
protected:
Ogre::Root* ogreRoot;
Ogre::RenderWindow* ogreWindow;
Ogre::SceneManager* ogreSceneMgr;
Ogre::Camera* ogreCamera;
Ogre::Viewport* ogreViewPort;
bool m_update_pending;
bool m_animating;
virtual void keyPressEvent(QKeyEvent * ev);
virtual void keyReleaseEvent(QKeyEvent * ev);
virtual void exposeEvent(QExposeEvent *event);
virtual bool event(QEvent *event);
private:
private slots:
};
#endif
QtOgreWindow.cpp (Implementation)
Code: Select all
#include "QTOgreWindow.h"
QTOgreWindow::QTOgreWindow(QWindow *parent) : QWindow(parent)
, m_update_pending(false)
, m_animating(false)
{
setAnimating(true);
installEventFilter(this);
}
QTOgreWindow::~QTOgreWindow()
{
delete ogreRoot;
}
void QTOgreWindow::render(QPainter *painter)
{
Q_UNUSED(painter);
}
void QTOgreWindow::initialize()
{
ogreRoot = new Ogre::Root(Ogre::String("plugins.cfg"));
Ogre::ConfigFile ogreConfig;
/*
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);
}
}
*/
ogreRoot->setRenderSystem(ogreRoot->getAvailableRenderers()[0]);
ogreRoot->initialise(false);
Ogre::NameValuePairList parameters;
parameters["currentGLContext"] = Ogre::String("false");
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
parameters["externalWindowHandle"] = Ogre::StringConverter::toString((size_t) (this->winId()));
#else
parameters["externalWindowHandle"] = Ogre::StringConverter::toString((unsigned long)(this->winId()));
#endif
#if defined(Q_OS_MAC)
parameters["macAPI"] = "cocoa";
parameters["macAPICocoaUseNSView"] = "true";
#endif
ogreWindow = ogreRoot->createRenderWindow("QT Window", this->width(), this->height(), false, ¶meters);
ogreWindow->setVisible(true);
ogreSceneMgr = ogreRoot->createSceneManager(Ogre::ST_GENERIC);
ogreCamera = ogreSceneMgr->createCamera("MainCamera");
ogreCamera->setPosition(Ogre::Vector3(0.0f, 0.0f, 10.0f));
ogreCamera->lookAt(Ogre::Vector3(0.0f, 0.0f, -300.0f));
ogreCamera->setNearClipDistance(5.0f);
ogreViewPort = ogreWindow->addViewport(ogreCamera);
ogreViewPort->setBackgroundColour(Ogre::ColourValue(0.4f, 0.3f, 0.1f));
ogreCamera->setAspectRatio(Ogre::Real(ogreViewPort->getActualWidth()) / Ogre::Real(ogreViewPort->getActualHeight()));
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
ogreSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
Ogre::Light* light = ogreSceneMgr->createLight("MainLight");
light->setPosition(20.0f, 80.0f, 50.0f);
}
void QTOgreWindow::render()
{
Ogre::WindowEventUtilities::messagePump();
ogreRoot->renderOneFrame();
}
void QTOgreWindow::renderLater()
{
if (!m_update_pending) {
m_update_pending = true;
QApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
}
bool QTOgreWindow::event(QEvent *event)
{
switch (event->type()) {
case QEvent::UpdateRequest:
m_update_pending = false;
renderNow();
return true;
default:
return QWindow::event(event);
}
}
void QTOgreWindow::exposeEvent(QExposeEvent *event)
{
Q_UNUSED(event);
if (isExposed())
renderNow();
}
void QTOgreWindow::renderNow()
{
if (!isExposed())
return;
bool needsInitialize = false;
if (!ogreRoot) {
needsInitialize = true;
}
if (needsInitialize) {
initialize();
}
render();
if (m_animating)
renderLater();
}
bool QTOgreWindow::eventFilter(QObject *target, QEvent *event) {
if (target == this) {
if (event->type() == QEvent::Resize) {
if (isExposed()) {
ogreWindow->resize(this->width(), this->height());
ogreCamera->setAspectRatio(Ogre::Real(ogreViewPort->getActualWidth()) / Ogre::Real(ogreViewPort->getActualHeight()));
}
}
}
return false;
}
void QTOgreWindow::keyPressEvent(QKeyEvent * ev) {
}
void QTOgreWindow::keyReleaseEvent(QKeyEvent * ev) {
}
void QTOgreWindow::setAnimating(bool animating)
{
m_animating = animating;
if (animating)
renderLater();
}