• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
giugio

Qt and opengl

11 posts in this topic

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: [url="http://www.gamedev.net/page/resources/_//feature/fprogramming/hosting-a-c-d3d-engine-in-c-winforms-r2526"]link[/url]
I ask because in qt I saw many examples that do not use this method and they seem very limited, because these examples?

thanks.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1296312244' post='4766631']
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).
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='Seaßourne' timestamp='1296348596' post='4766857']
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.
[/quote]
... 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.
0

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1296395940' post='4767017']
[quote name='Seaßourne' timestamp='1296348596' post='4766857']
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.
[/quote]
... 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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='Seaßourne' timestamp='1296424783' post='4767190']
Well for the sake of the argument. Doesn't Orge already have it's own widget to inherit and do all that for you?
[/quote]

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

Share this post


Link to post
Share on other sites
[b]If[/b] 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 ([url="http://ogre3d.org/forums/viewtopic.php?f=2&t=61722"]http://ogre3d.org/fo...php?f=2&t=61722[/url]).


Anywhere here it is:

The header file
[code]
#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
[/code]

And the cpp

[code]
#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 );
}
[/code]

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.
1

Share this post


Link to post
Share on other sites
[quote name='Red Ant' timestamp='1296512693' post='4767690']
[b]If[/b] 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 ([url="http://ogre3d.org/forums/viewtopic.php?f=2&t=61722"]http://ogre3d.org/fo...php?f=2&t=61722[/url]).

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.
[/quote]

I see a lot of things wrong with your code.
0

Share this post


Link to post
Share on other sites
[quote name='Seaßourne' timestamp='1296635649' post='4768376']
I see a lot of things wrong with your code.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='Red Ant' timestamp='1296653954' post='4768484']
[quote name='Seaßourne' timestamp='1296635649' post='4768376']
I see a lot of things wrong with your code.
[/quote]

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.
[/quote]

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

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)
[url="http://doc.qt.nokia.com/latest/qtglobal.html#Q_ASSERT"]http://doc.qt.nokia....l.html#Q_ASSERT[/url]
[url="http://doc.qt.nokia.com/latest/qtglobal.html#Q_ASSERT_X"]http://doc.qt.nokia....html#Q_ASSERT_X[/url]

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

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.
0

Share this post


Link to post
Share on other sites
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 [i]fundamentally[/i] unsound with Red Ant's contribution. IMHO, it's the most helpful and direct piece of advice given in the thread so far.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0