Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Qt and opengl


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 giugio   Members   -  Reputation: 211

Like
0Likes
Like

Posted 29 January 2011 - 07:55 AM

Hello.
I would like to find a way to view a opengl scene in a qt widget with a common OpenGL 3D engine that ogre or wildmagic.
The engine should still use CG shaders and VBO.
With DirectX 11 thanks to the tutorial gamedev I could visualize the scene using an hwnd and the paint event to invoke the rendering of the scene.
this can also work well in opengl?
link: link
I ask because in qt I saw many examples that do not use this method and they seem very limited, because these examples?

thanks.

Sponsor:

#2 GothSeiDank   Members   -  Reputation: 156

Like
0Likes
Like

Posted 29 January 2011 - 08:24 AM

With OpenGL, you just need to create an OpenGL Context. Then you can draw onto it via the draw function of the Widget.
I don't use QT, but wxWidgets (no offense), so I cannot help you with code.
But working with OpenGL is pretty straight forward:
Create the Canvas (Context), then you make it "current" and then you draw with "normal" OpenGL-Calls according to the Version of your context.
But there is a class called QGLContext in QT, maybe you wanna have a look at it's documentation.
HTH
rya.
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/

#3 e‍dd   Members   -  Reputation: 2109

Like
0Likes
Like

Posted 29 January 2011 - 08:44 AM

As NicoG said QGLWidget is the way to do OpenGL rendering in Qt-created window. Depending on how Ogre/wildmagic manage contexts, you might need to create a derived QGLContext too.

One problem I've run in to on Windows: QGLWidgets do not like being reparented, so I'd recommend putting your QGLwidget inside a wrapper widget (by adding the QGLWidget to layout of the parent).

#4 Seaßourne   Members   -  Reputation: 120

Like
0Likes
Like

Posted 29 January 2011 - 06:49 PM

As NicoG said QGLWidget is the way to do OpenGL rendering in Qt-created window. Depending on how Ogre/wildmagic manage contexts, you might need to create a derived QGLContext too.

One problem I've run in to on Windows: QGLWidgets do not like being reparented, so I'd recommend putting your QGLwidget inside a wrapper widget (by adding the QGLWidget to layout of the parent).


QGLWidget already has a context unless you want to make a custom context (Not really needed since Qt-4.7 supports OpenGL 3/4). All you need to do is override the functions it tells you on the documentation page. Then just go through it like you would with any other. You'll more than likely will have to call the context when needed. But just create a new widget that inherits QGLWidget and override the functions and you're good to go, nothing else.

#5 e‍dd   Members   -  Reputation: 2109

Like
0Likes
Like

Posted 30 January 2011 - 07:59 AM

QGLWidget already has a context unless you want to make a custom context (Not really needed since Qt-4.7 supports OpenGL 3/4). All you need to do is override the functions it tells you on the documentation page. Then just go through it like you would with any other. You'll more than likely will have to call the context when needed. But just create a new widget that inherits QGLWidget and override the functions and you're good to go, nothing else.

... unless Ogre (for the sake of argument) requires you to use its own OpenGL context management and you have to integrate the two frameworks, which is why I brought it up. If neither of the frameworks mentioned require this and give you explicit control of context acquisition, then agreed -- the default Qt context handling is probably just fine.

#6 Seaßourne   Members   -  Reputation: 120

Like
0Likes
Like

Posted 30 January 2011 - 03:59 PM


QGLWidget already has a context unless you want to make a custom context (Not really needed since Qt-4.7 supports OpenGL 3/4). All you need to do is override the functions it tells you on the documentation page. Then just go through it like you would with any other. You'll more than likely will have to call the context when needed. But just create a new widget that inherits QGLWidget and override the functions and you're good to go, nothing else.

... unless Ogre (for the sake of argument) requires you to use its own OpenGL context management and you have to integrate the two frameworks, which is why I brought it up. If neither of the frameworks mentioned require this and give you explicit control of context acquisition, then agreed -- the default Qt context handling is probably just fine.


Well for the sake of the argument. Doesn't Orge already have it's own widget to inherit and do all that for you?

#7 e‍dd   Members   -  Reputation: 2109

Like
0Likes
Like

Posted 30 January 2011 - 07:55 PM

Well for the sake of the argument. Doesn't Orge already have it's own widget to inherit and do all that for you?


*shrug*. I know nothing about ogre or wildmagic, hence all the qualifications in my response.

#8 Red Ant   Members   -  Reputation: 463

Like
1Likes
Like

Posted 31 January 2011 - 04:24 PM

If you are indeed working with Ogre, do __NOT__ use QGLWidget!! Ogre supports both DX and OpenGL, so why would you want ditch that ability for no reason. I'll post the code for a custom Qt/Ogre-widget from one of my projects. I initially took this code from some dude on the Ogre forums (http://ogre3d.org/fo...php?f=2&t=61722).


Anywhere here it is:

The header file
#ifndef Flaps_QtOgreWidget_h
#define Flaps_QtOgreWidget_h

#include <QtGui/QWidget>
#include <Ogre/OgreCommon.h>
#include <boost/noncopyable.hpp>

#include "../../../../FlapsCore/String.h"
#include <iosfwd>
#include <cstddef>

// Ogre forward declarations
namespace Ogre
{
	class RenderWindow;
	class Root;
	class SceneManager;
};

namespace Flaps
{
	/// This class provides a custom Qt widget that can be used for Ogre rendering.
	/// Original code taken from user PolyVox (author of the QtOgre Application Framework).
	class QtOgreWidget : public QWidget, private boost::noncopyable
	{
		Q_OBJECT

	public:
		QtOgreWidget( QWidget& parentWidget, std::basic_ostream< FlapsCore::CharacterType >* logStream );

		void performUpdate();

		/// Initializes the Ogre part of the instance.
		void initialize();

	protected:
		virtual ~QtOgreWidget();

		/// Pointer to a log stream (only for use by the main thread).
		std::basic_ostream< FlapsCore::CharacterType >* const m_logStream;

		/// Returns a reference to the scene manager.
		inline Ogre::SceneManager& sceneMgr() { return *m_sceneManager; }

		inline Ogre::RenderWindow& renderWindow() { return *m_ogreRenderWindow; }

	private:
		Ogre::RenderWindow* m_ogreRenderWindow;

		/// Pointer to the scene manager.
		Ogre::SceneManager* m_sceneManager;

		Ogre::Root* m_root;

		/// The window name specified to Ogre::Root::createRenderWindow(). We might need this again later.
		Ogre::String m_windowName;

		/// Reimplemented to do our Ogre rendering.
		virtual void paintEvent( QPaintEvent* evt );

		/// Reimplemented to propagate the changed dimensions to Ogre.
		virtual void resizeEvent( QResizeEvent* evt );

		/// Returns true if the widget has already been properly set up.
		bool isInitialized() const;

		/// Sets up Ogre and returns a pointer to the Ogre::Root instance.
		Ogre::Root* setUpOgre() const;

		/// Renders the Ogre scene.
		void renderScene();

		/// Must be implemented to update the scene, meaning the positions and orientations of all objects in the scene
		/// must be updated.
		virtual void updateScene() = 0;

		/// Must be implemented in the derived class to actually set up the scene.
		virtual void createScene() = 0;

		/// Reimplemented to return a null pointer. Supposedly this is necessary to avoid flickering.
		virtual QPaintEngine* paintEngine() const;

		/// Reimplemented to handle event with type QEvent::WinIdChange (sent when the win id of the widget changes).
		virtual bool event( QEvent* evt );
	};
}

#endif

And the cpp

#include "QtOgreWidget.h"

// Ogre stuff
#include <Ogre/OgreRoot.h>
#include <Ogre/Ogre.h>

// Qt stuff
#include <QPaintEvent>

// utilities
#include <Utils/Assertions.h>
#include <Utils/StringFunctions.h>
#include <Utils/Logging.h>

// standard C++
#include <ostream>
#include <sstream>
#include <cstddef>

namespace usf = Utils::StringFunctions;

Flaps::QtOgreWidget::QtOgreWidget( QWidget& parentWidget, std::basic_ostream< FlapsCore::CharacterType >* logStream ) :
	QWidget( &parentWidget ),
	m_ogreRenderWindow( nullptr ),
	m_sceneManager( nullptr ),
	m_root( nullptr ),
	m_logStream( logStream )
{
	QPalette colourPalette = palette();
	colourPalette.setColor( QPalette::Active, QPalette::WindowText, Qt::black );
	colourPalette.setColor( QPalette::Active, QPalette::Window, Qt::black );
	setPalette( colourPalette );
}

Flaps::QtOgreWidget::~QtOgreWidget()
{
	if ( m_ogreRenderWindow )
	{
		m_ogreRenderWindow->destroy();
	}
}

void Flaps::QtOgreWidget::initialize()
{
	setAttribute( Qt::WA_PaintOnScreen );
	setAttribute( Qt::WA_OpaquePaintEvent );
	setAttribute( Qt::WA_NoSystemBackground );
	setAutoFillBackground( false );

	setFocusPolicy( Qt::StrongFocus );

	m_root = Ogre::Root::getSingletonPtr();
	
	if ( ! m_root )
	{
		m_root = setUpOgre();
		HARD_ASSERT( m_root );
	}

	{
		std::ostringstream out;
		out << "QtOgreWidget_at_0x" << std::hex << static_cast < const void* > ( this );
		m_windowName = out.str();
	}

	// The external windows handle parameters are platform-specific
	const Ogre::String externalWindowHandleParams = Ogre::StringConverter::toString( reinterpret_cast < std::size_t > ( winId() ) );
	Ogre::NameValuePairList params;
	params[ "externalWindowHandle" ] = externalWindowHandleParams;

	m_ogreRenderWindow = m_root->createRenderWindow( m_windowName, width(), height(), false, &params );
	HARD_ASSERT( m_ogreRenderWindow );

	if ( m_logStream )
	{
		NEW_LOG_ENTRY_W( *m_logStream ) << TXT_( "Created render window with name \"" ) << usf::encode< FlapsCore::CharacterType > (  m_windowName ) << TXT_( "\".\n" );
	}
 
	m_sceneManager = m_root->createSceneManager( Ogre::ST_EXTERIOR_CLOSE );
	HARD_ASSERT( m_sceneManager );

	createScene();
}

Ogre::Root* Flaps::QtOgreWidget::setUpOgre() const
{
	/// @todo Do not hard-code these path names! Also allow user to specify a file name to which the Ogre library can spew debug output.
	Ogre::Root* const ogreRoot = new Ogre::Root( "OgrePlugins.cfg", Ogre::String(), Ogre::String() );

	const Ogre::RenderSystemList& renderers = ogreRoot->getAvailableRenderers();
	HARD_ASSERT( ! renderers.empty() );

	if ( m_logStream )
	{
		NEW_LOG_ENTRY_W( *m_logStream ) << TXT_( "List of available render systems contains " ) << renderers.size() << TXT_( " entries.\n" );

		for ( Ogre::RenderSystemList::const_iterator it = renderers.begin(); it != renderers.end(); ++it )
		{
			NEW_LOG_ENTRY_W( *m_logStream ) << TXT_( "Render system \"" ) << usf::encode< FlapsCore::CharacterType > (  ( *it )->getName() ) << TXT_( "\" found in list of available render systems.\n" );
		}
	}
 
	/// @todo Implement something to allow the user to pick a rendering system ... and don't do it all directly in this class.
	Ogre::RenderSystem* const renderSystem = renderers.front();
	HARD_ASSERT( renderSystem );
 
	ogreRoot->setRenderSystem( renderSystem );

	{
		std::ostringstream out;
		out << width() << "x" << height();
		renderSystem->setConfigOption( "Video Mode", out.str() );
	}
 
	// initialize without creating window
	ogreRoot->getRenderSystem()->setConfigOption( "Full Screen", "No" );
	ogreRoot->saveConfig();
	ogreRoot->initialise( false );

	return ogreRoot;
}

bool Flaps::QtOgreWidget::isInitialized() const
{
	return m_ogreRenderWindow;
}

void Flaps::QtOgreWidget::paintEvent( QPaintEvent* evt )
{
	if ( ! isInitialized() )
	{
		return;
	}

	renderScene();

	evt->accept();
}

void Flaps::QtOgreWidget::renderScene()
{
	m_root->_fireFrameStarted();
	m_ogreRenderWindow->update();
	m_root->_fireFrameRenderingQueued();
	m_root->_fireFrameEnded();
}

void Flaps::QtOgreWidget::performUpdate()
{
	updateScene();

	/// @todo Is it really necessary to render directly or should we be causing a QPaintEvent to be sent to this widget?
	renderScene();
}

void Flaps::QtOgreWidget::resizeEvent( QResizeEvent* )
{
	m_ogreRenderWindow->windowMovedOrResized();
}

QPaintEngine* Flaps::QtOgreWidget::paintEngine() const
{
	return nullptr;
}

 bool Flaps::QtOgreWidget::event( QEvent* evt )
 {
	 /// @todo How do we react to this PROPERLY!?
	 // Hmm, apparently this event is sent to the widget on creation.
	 //HARD_ASSERT( evt->type() != QEvent::WinIdChange ); 
	 return QObject::event( evt );
 }

This code is directly from my project .... I didn't strip out all the project-specific things, so just throw away the stuff you don't need.

#9 Seaßourne   Members   -  Reputation: 120

Like
0Likes
Like

Posted 02 February 2011 - 02:34 AM

If you are indeed working with Ogre, do __NOT__ use QGLWidget!! Ogre supports both DX and OpenGL, so why would you want ditch that ability for no reason. I'll post the code for a custom Qt/Ogre-widget from one of my projects. I initially took this code from some dude on the Ogre forums (http://ogre3d.org/fo...php?f=2&t=61722).

Anywhere here it is:


.....

This code is directly from my project .... I didn't strip out all the project-specific things, so just throw away the stuff you don't need.


I see a lot of things wrong with your code.

#10 Red Ant   Members   -  Reputation: 463

Like
0Likes
Like

Posted 02 February 2011 - 07:39 AM

I see a lot of things wrong with your code.


I'd appreciate it if you could point out my errors to me. :) I in no way claim to be an Ogre / Qt expert, and it is entirely possible that I'm doing things completely wrong.

#11 Seaßourne   Members   -  Reputation: 120

Like
0Likes
Like

Posted 02 February 2011 - 01:19 PM


I see a lot of things wrong with your code.


I'd appreciate it if you could point out my errors to me. :) I in no way claim to be an Ogre / Qt expert, and it is entirely possible that I'm doing things completely wrong.


1. You don't need boost::noncopyable as you can use: http://doc.qt.nokia.com/4.7/qobject.html#Q_DISABLE_COPY
class OrgeWidget {
private:
    Q_DISABLE_COPY(OrgeWidget)
};

2. Your constructor should be QtOgreWidget(QWidget* parent = 0); not with reference and it should be the last parameter with exactly QWidget* parent = 0 (Qt uses 0 instead of NULL)

3. Qt already has macros for assertion Q_ASSERT (plain assert) and Q_ASSERT_X (with a message)
http://doc.qt.nokia....l.html#Q_ASSERT
http://doc.qt.nokia....html#Q_ASSERT_X

4. You should be using QString instead of std::ostringstream to convert data like numbers, appending, etc.
QString::number(20) == "20" etc.
QString("Foo %1 and %2").arg("foo", "bar"); // "Foo foo and bar"
http://doc.qt.nokia....st/qstring.html

5. Qt already has it's own containers, QList, QVector, QHash, QMap, QMultiMap, QMultiHash, QLinkedList, etc

6. Qt already has stream classes QTextStream and QDataStream with great unicode support. Maybe should convert to it.

7. Not sure what TXT_() is and if thats for translating like L"" etc are. Then there is tr() for it which you can use Linguist with to translate.

8. You shouldn't prefix classes with Qt as it's reserved naming convention same with QFoo.

#12 e‍dd   Members   -  Reputation: 2109

Like
1Likes
Like

Posted 02 February 2011 - 06:16 PM

Many of those complaints are really about style.

Some people like to use Qt as an implementation detail of the GUI rather than spreading Qt code all throughout the application. There are often good reasons to choose non-Qt stuff over Qt stuff and vice-versa, but that's an engineering decision to be made based on the merits of each. I wouldn't dare to call those things "wrong" without knowing the complete picture.

I don't see anything fundamentally unsound with Red Ant's contribution. IMHO, it's the most helpful and direct piece of advice given in the thread so far.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS