Hi everyone,
I've recently started using Ogre and I'm really enjoying it. I'm currently working on embedding the render window into my Qt application. I found OgreApplicationContextQt and migrated the BasicTutorial code into it, then added some Qt widgets. The program worked fine, except that the Ogre window was located outside the Qt window.
I tried adding the following line of code:
Code: Select all
container->createWindowContainer(window);
However, after adding this line, the Ogre window disappeared.
I also tried calling startTimer() as suggested in the documentation, but it didn't work. Additionally, I attempted to write some code in timerEvent after finding it in the source code, but that didn't work either.
Could anyone please help me figure out what I did wrong?
Ogre Version: 14.2
Operating System: Windows10 22H2 19045
Render System: OpenGL
Code: Select all
#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QLabel>
#include "Ogre.h"
#include "OgreApplicationContextQt.h"
#include "OgreInput.h"
#include "OgreRTShaderSystem.h"
#include <iostream>
using namespace Ogre;
using namespace OgreBites;
class BasicTutorial1
: public ApplicationContextQt
, public InputListener
{
public:
BasicTutorial1();
virtual ~BasicTutorial1() {}
void createRoot() override;
void setup();
bool keyPressed(const KeyboardEvent& evt);
// protected:
// void timerEvent(QTimerEvent* event) override
// {
// mRoot->renderOneFrame();
// }
private:
std::string mPluginsCfg;
std::string mResourcesCfg;
std::string mOgreCfg;
};
BasicTutorial1::BasicTutorial1()
: ApplicationContextQt("OgreTutorialApp")
{
}
void BasicTutorial1::createRoot() {
// Define the paths to your configuration files
mPluginsCfg = "../../config/plugins.cfg";
mResourcesCfg = "../../config/resources.cfg";
mOgreCfg = "../../log/ogre.cfg";
// Create the Root object with custom config file paths
mRoot = new Ogre::Root(mPluginsCfg, mOgreCfg, "../../log/custom-ogre-log.log");
}
void BasicTutorial1::setup()
{
// Load resource paths from config file
Ogre::ConfigFile cf;
cf.load(mResourcesCfg);
Ogre::ConfigFile::SectionIterator seci = cf.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);
}
}
// do not forget to call the base first
ApplicationContextQt::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;
}
int main(int argc, char **argv)
{
QApplication qapp(argc, argv);
auto* mainwindow = new QMainWindow();
auto* label = new QLabel("some text");
auto* window = new QWindow();
auto* container = new QWidget();
BasicTutorial1 app;
// container->createWindowContainer(window);
auto* placeholder = new QWidget();
placeholder->setFixedWidth(600);
placeholder->setFixedHeight(600);
auto* layout = new QHBoxLayout(placeholder);
layout->addWidget(label);
layout->addWidget(container);
mainwindow->setCentralWidget(placeholder);
mainwindow->show();
window->show();
app.injectMainWindow(window);
app.initApp();
app.startTimer(1000);
qapp.exec();
return 0;
}
//! [fullsource]