Page 1 of 1

Using Qt Library in Ogre Project

Posted: Fri Feb 03, 2012 7:38 pm
by vounotras
Hello everybody,

I want to use Qt Library in my project to have some widgets. Everything work fine until the time I use Qt Library.

So here is my code:

Code: Select all

else if(button->getName() == "NewGame") {
        
        QApplication app(argc, argv);                           // Here I create Qt Application
        
        HelloForm form;                                              
        form.show();
        app.exec();

}
When I put button -> "New Game", I show my test Form in my screen but here it comes my problem. Mouse losts and system crashes.
I don' t know what to do. I need Qt Library because I want to have an Uploader widget.

Please help me!!

Thank you!

Re: Using Qt Library in Ogre Project

Posted: Sat Feb 04, 2012 10:00 pm
by vounotras
I found this code:

Code: Select all

#include <QtGui/QApplication>
#include <QtGui/QDialog>
#include <QtGui/QResizeEvent>
#include <QtGui/QGraphicsView>
#include <QtGui/QPaintEngine>
#include <QtGui/QPushButton>
#include <QtOpenGL/QGLWidget>
#include <QtCore/QTimer>

#include <OGRE/Ogre.h>

class OgreGraphicsView : public QGraphicsView
{
public:
    OgreGraphicsView()
		:QGraphicsView()
    {
		//Set the title
        setWindowTitle(tr("Ogre Graphics View"));	

		//Create the OpenGL widget and set it as the viewport for this graphics view.
		m_glWidget = new QGLWidget(QGLFormat());
		setViewport(m_glWidget);
		//This is just copied from an OpenGL example
		setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    }

	void initOgre(Ogre::Root* ogreRoot)
	{
		m_ogreRoot = ogreRoot;
		m_ogreRenderWindow = 0;

		//This is the crucial part. We give Ogre the window id of our QGLWidget which it will then
		//render into rather than creating its own external window. However, we still let Ogre create
		//its own OpenGL context as we need the QGLWidget's existing one for rendering the dialog
		Ogre::NameValuePairList params;
		Ogre::String externalWindowHandleParams;
		externalWindowHandleParams = Ogre::StringConverter::toString((unsigned int)(m_glWidget->winId()));
		params["externalWindowHandle"] = externalWindowHandleParams;
		params["externalGLControl"] = "true";
		m_ogreRenderWindow = m_ogreRoot->createRenderWindow("OgreWindow", width(), height(), false, &params);

		//Ogre has just created a new OpenGL context. We use these WIN32 functions to retrieve it
		//and store it for later use. We will need to switch between the ogre and qt contexts depending
		//on what we are rendering.
		ogreHDC = wglGetCurrentDC();
		ogreHGLRC = wglGetCurrentContext();
	}

	void drawBackground(QPainter *painter, const QRectF &rect)
	{
		//Test not really necessary, but it was in the sample...
		if (painter->paintEngine()->type() != QPaintEngine::OpenGL)
		{
			qWarning("OpenGLScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view");
			return;
		}

		//Make Ogre's OpenGL context current as we are about to do some ogre rendering
		makeOgreCurrent();

		//Render one ogre frame.
		m_ogreRoot->_fireFrameStarted();
		m_ogreRenderWindow->update(false);
		m_ogreRoot->_fireFrameEnded();

		//Set the Qt OpenGL context to be current ready to render the dialog.
		makeQtCurrent();
	}

	void makeOgreCurrent(void)
	{
		//Makes Ogre's context current using the handles we saved earlier
		wglMakeCurrent(ogreHDC, ogreHGLRC);
	}

	void makeQtCurrent(void)
	{
		//Make the Qt's context current;
		m_glWidget->makeCurrent();
	}

	Ogre::RenderWindow* getOgreRenderWindow(void)
	{
		//Just a basic accessor
		return m_ogreRenderWindow;
	}

protected:
    void resizeEvent(QResizeEvent *event)
	{
		//This was just taken from the sample. I guess we need it.
        if (scene())
		{
			qWarning("Resizing graphics view");
            scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
		}

		//We need to make sure Ogre's context is current before we resize the viewport
		makeOgreCurrent();
		m_ogreRenderWindow->resize(width(), height());

		//Switch back to Qt context
		makeQtCurrent();

		//Pass on the event
        QGraphicsView::resizeEvent(event);
    }

private:
	//These are WIN32 device context and OpenGL resource context types.
	//They are used to keep track of the Ogre OpenGL context.
	HGLRC ogreHGLRC;
	HDC ogreHDC;

	//Ogre stuff
	Ogre::Root *m_ogreRoot;
	Ogre::RenderWindow *m_ogreRenderWindow;

	//The OpenGL widget which is used for drawing both the Ogre scene and the test dialog
	QGLWidget* m_glWidget;
};

int main(int argc, char *argv[])
{
	//Main Qt Application object
	QApplication app(argc, argv);

	//Initialize the Ogre root and the OpenGL Render system.
	Ogre::Root* root = new Ogre::Root();
#if defined(_DEBUG)
	root->loadPlugin("RenderSystem_GL_d");
#else
	root->loadPlugin("RenderSystem_GL");
#endif
	Ogre::RenderSystemList *list = root->getAvailableRenderers();
	Ogre::RenderSystemList::iterator i = list->begin();
	root->setRenderSystem(*i);
	root->initialise(false);
	
	//Create the Ogre graphics view. 
	OgreGraphicsView ogreGraphicsView; 
	ogreGraphicsView.resize(640, 480);
	ogreGraphicsView.initOgre(root);

	//Create the graphics scene object and attach it to the graphics view
	QGraphicsScene* graphicsScene = new QGraphicsScene;
	ogreGraphicsView.setScene(graphicsScene);

	//Now we are on to initializing Ogre
	Ogre::SceneManager* sceneManager = root->createSceneManager(Ogre::ST_GENERIC);

	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/models", "FileSystem");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/textures", "FileSystem");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/materials", "FileSystem");
	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

	Ogre::Camera* cam = sceneManager->createCamera("Cam");

	ogreGraphicsView.getOgreRenderWindow()->addViewport(cam)->setBackgroundColour(Ogre::ColourValue::Green);

	cam->setPosition(5, 5, 5);
	cam->lookAt(0, 0, 0);
	cam->setNearClipDistance(1.0);
	cam->setFarClipDistance(1000.0);

	sceneManager->setAmbientLight( Ogre::ColourValue( 1, 1, 1 ) );
	Ogre::Entity *ent1 = sceneManager->createEntity( "Box", "Bulletbox.mesh" );
	Ogre::SceneNode *node1 = sceneManager->getRootSceneNode()->createChildSceneNode( "RobotNode" );
	node1->attachObject( ent1 );

	//Ogre is ready to go. We hook up a timer to generate update events for our graphics view
	QTimer *timer = new QTimer(0);
	app.connect(timer, SIGNAL(timeout()), &ogreGraphicsView, SLOT(update()));
	timer->start(20);

	//We create a test dialog and add it to the scene
	QDialog *dialog = new QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    dialog->setWindowOpacity(0.8);
    dialog->setWindowTitle("My test window");
	dialog->resize(320,240);
	dialog->move(160,120);
	graphicsScene->addWidget(dialog);

	//And go!
	ogreGraphicsView.show();
	app.exec();
}
but I am on ubuntu and I get wrong message for this:
HGLRC ogreHGLRC;
HDC ogreHDC;

Any idea to fix it...?
Thank you!

Re: Using Qt Library in Ogre Project

Posted: Sat Feb 04, 2012 11:49 pm
by duststorm
You might want to rethink your architecture. The way you're doing it, just creating a new application and starting the Qt application loop within your Ogre loop is not a good idea (as the crash shows).

If you want to use Qt windows for the GUI, build your app like a Qt app and embed Ogre in a Qt widget, like is already explained in this forum and on the wiki (and as the post above shows). Or look at ogitor, they do it.

If you want to use other Qt libraries, not the GUI, you could do like I do: start those libraries in a different thread (or inline them in your render loop) and call the Qt event loop manually (http://developer.qt.nokia.com/doc/qt-4. ... cessEvents). This means that you don't create a QApplication and that you don't run exec(), but instead process the events manually at certain times.

Re: Using Qt Library in Ogre Project

Posted: Sun Feb 05, 2012 2:55 am
by vounotras
Thanks for your answer. I am not good in English and I can't explain you exactly what I want... but I will try.

I have an application with 3d graphics for my diploma in the University. Everything are going well but its my first time I use Ogre Library and its first time I use Qt Library.
I understand some things for every library but now I have to use them together. There will be some widgets which I can find from Ogre Library and it is simple with Qt.
I would like to have a very simple example to understand how to see this two libraries together.

The link that you send me looks very good but really I would be grateful if you give me a simple example with main and resources to see how they are work.

Thank you very much for your answer.

* I use linux (ubuntu 11.10)...

Re: Using Qt Library in Ogre Project

Posted: Sun Feb 05, 2012 10:29 am
by duststorm
vounotras wrote:The link that you send me looks very good but really I would be grateful if you give me a simple example with main and resources two see how they are work.
Since you want widgets (so, a GUI) you will want to create a Qt application and create an Ogre widget for it.

The code in the first reply contains a main(), I don't see the problem. It's a perfectly working, fully runnable code example. Of course you need to get the dependencies first and configure them. You will probably want to setup an IDE, like QtCreator.
All the info you need to get started developing with Ogre can be found here:
http://www.ogre3d.org/tikiwiki/Setting+ ... pplication
eg this one: http://www.ogre3d.org/tikiwiki/Setting+ ... +QtCreator

If you are qoing to use QtCreator and want a project file to get you started, you can download this (kindly donated by Jacmoe): http://www.ogre3d.org/forums/download/file.php?id=1058
It's from this topic: http://www.ogre3d.org/forums/viewtopic. ... 81#p366381

As for other examples of embedding Ogre in Qt, this wiki article is very good and explains it very well:
http://www.ogre3d.org/tikiwiki/QtOgre

There was also a forum topic about it:
http://www.ogre3d.org/forums/viewtopic.php?t=45709

And one more topic:
http://www.ogre3d.org/forums/viewtopic.php?f=2&t=54646

So, as you can see, examples and material enough on these forums and the wiki. Just use the search function.
Hope this helps.