PDA

View Full Version : problem with combine Qt 5.4 QOpenGLContext to native opengl context



jennal
3rd December 2014, 12:00
I am new in qt, and I am trying to make a toolkit for faster game development. My target platform is MacOSX and Windows. I am working on a game engine called cocos2d-x, which used glfw as desktop opengl window. glfw uses NSGL as opengl context in MacOSX and WGL in windows.

Qt 5.4 (http://qt-project.org/wiki/New-Features-in-Qt-5.4) introduced a new class called QOpenGLContext (http://doc-snapshot.qt-project.org/qt5-5.4/qopenglcontext.html) to take place of QGLContext. And QOpenGLContext has a new method setNativeHandle (http://doc-snapshot.qt-project.org/qt5-5.4/qopenglcontext.html#setNativeHandle) which official sai QOpenGLContext is now able to adopt existing native contexts (EGL, GLX, …). This allows interop between Qt and other frameworks, like game engines.

But I tried some ways and all failed.


I try to create a new QOpenGLContext and call setNativeHandle, and set it to setShareContext (http://doc-snapshot.qt-project.org/qt5-5.4/qopenglcontext.html#setShareContext). Then I create a new QOpenGLWidget (http://doc-snapshot.qt-project.org/qt5-5.4/qopenglwidget.html) and hoping it will use the shareContext, but it is not.
I start with a new QOpenGLWidget, then get its context, and call context's setNativeHandle, but it occur an error.


Now I have 2 ideas, but don't know how to approach

create a QOpenGLContext then adopt glfw's gl context to it, and show it to screen.
create a QOpenGLWidget and change its gl context to glfw's gl context.


Someone knows how to approach this? Or my ideas are totally wrong? Please tell me.
Thanks.

wysota
3rd December 2014, 12:53
I try to create a new QOpenGLContext and call setNativeHandle, and set it to setShareContext (http://doc-snapshot.qt-project.org/qt5-5.4/qopenglcontext.html#setShareContext). Then I create a new QOpenGLWidget (http://doc-snapshot.qt-project.org/qt5-5.4/qopenglwidget.html) and hoping it will use the shareContext, but it is not.
What sense would it make? If you already have a surface to draw on which context you want to adopt, what would creating a new widget (hence another surface to draw on) do? Where would the rendering occur?


I start with a new QOpenGLWidget, then get its context, and call context's setNativeHandle, but it occur an error.
Again, in my opinion this doesn't make sense.

Am I right that you would like your alien game engine render within Qt's window? In that case it is the game engine that should adopt Qt's context, not the other way round. You could probably use a QOpenGLFramebufferObject and bind it with the adopted context, then make your game engine render itself to the FBO and then use that FBO as a texture in Qt.

jennal
3rd December 2014, 13:04
Yes, I want to render game engine in Qt's window. And the game engine used glfw as its window. I want to adopt glfw's gl context to QOpenGLContext, or there is some other way to do that?

Game Engine Team tried to adopt to Qt context, but they failed and give up Qt. And I found new feature from Qt 5.4 may solve this problem with new QOpenGLContext which has a new method: setNativeHandle.

So I am trying to make this happen. Does this make sense to you?

wysota
3rd December 2014, 13:35
The addition in Qt 5.4 is if you want Qt to render within the context created by someone else, not the other way round. As I said you could try binding an FBO to the adopted context and see if the engine is able to render there. Then you can use the result as a texture in Qt part of the program (e.g. through a shared context).

jennal
3rd December 2014, 14:44
If Qt could render in native GL context, it is OK for me. As I know, Qt could render GUI widgets in GraphicView. It seems that Qt could render widgets in GL context. Can you show me some code sample of this?

Or the FBO solution could fit my need, I also need some code sample, because I have no idea how to begin with.

Could you please show me some code sample or link to the documents?

wysota
3rd December 2014, 15:03
As I know, Qt could render GUI widgets in GraphicView.
GraphicsView is a widget. And the "widgets" it renders are not really widgets.


It seems that Qt could render widgets in GL context.
It probably could (at least since 5.4) but I don't think that would be a good approach.

Can you show me some code sample of this?
No :)


Or the FBO solution could fit my need, I also need some code sample, because I have no idea how to begin with.
Use the docs Luke -- QOpenGLFramebufferObject.