<div dir="ltr">Hi Sebastian,<div><br></div><div>I am not using Qt but faced the same problem. So here is a 'hack' for Windows if it helps:</div><div><br></div><div><div> osgViewer::CompositeViewer::Windows wins;</div><div> viewer->getWindows(wins);</div><div><br></div><div> while (!viewer->done())</div><div> {</div><div><br></div><div>#if defined(_WIN32)</div><div> MSG msg;</div><div> if (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))</div><div> {</div><div> ::GetMessage(&msg, NULL, 0, 0);</div><div><br></div><div> if (wins.size())</div><div> {</div><div> osgViewer::GraphicsHandleWin32 *hdl = dynamic_cast<osgViewer::GraphicsHandleWin32*>(<a href="http://wins.at">wins.at</a>(0));</div><div> if(hdl)</div><div> {</div><div> WNDPROC fWndProc = (WNDPROC)::GetWindowLongPtr(hdl->getHWND(), GWLP_WNDPROC);</div><div> if (fWndProc && hdl->getHWND())</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>::CallWindowProc(fWndProc,hdl->getHWND(),msg.message, msg.wParam, msg.lParam);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div> }</div><div> }</div><div> }</div><div>#endif</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 3, 2015 at 3:43 PM, Sebastian Messerschmidt <span dir="ltr"><<a href="mailto:sebastian.messerschmidt@gmx.de" target="_blank">sebastian.messerschmidt@gmx.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Can,<br>
<br>
Have you created a full code example yet?<br>
My problem right now is the lack of keyboard events being passed through to OSG. Any hints on this?<br>
<br>
Cheers<span class="HOEnZb"><font color="#888888"><br>
Sebastian</font></span><div class="HOEnZb"><div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I have been working Qt5 integration for my current rendering application implementing deferred rendering and came up with couple of solutions. I want to share it with the people struggling Qt5 integration while waiting official stable release :)<br>
<br>
Since the current stable release is OSG 3.2.1, this will be based on that version.<br>
<br>
For Qt5 version, I recommend using >= 5.4, because in earlier versions you have to do a lot by yourself. In 5.4, at least you have QOpenGLWidget.<br>
<br>
Even though I will give solution for widget, this can be applied to QWindow solution as well. The codes will be bits and pieces, unfortunately cannot share full working code.<br>
<br>
Firstly, create a new widget rendering class subclassing QOpenGLWidget. This one is almost same as the QGLWidget version of it.<br>
<br>
<br>
Code:<br>
<br>
class RenderWidget : public [b]QOpenGLWidget[/b]<br>
{<br>
Q_OBJECT<br>
public:<br>
RenderWidget(QWidget* parent = 0, Qt::WindowFlags f = 0);<br>
~RenderWidget();<br>
<br>
protected:<br>
virtual void initializeGL();<br>
virtual void paintGL();<br>
virtual void resizeGL(int width, int height);<br>
<br>
osg::ref_ptr<osgViewer::GraphicsWindow> gw;<br>
osg::ref_ptr<osgViewer::Viewer> viewer;<br>
<br>
private:<br>
QTimer heartbeat;<br>
};<br>
<br>
RenderWidget::RenderWidget(QWidget* parent, Qt::WindowFlags f)<br>
{<br>
// instead of osgViewer::setUpViewerAsEmbeddedInWindow, we are going to<br>
// inject our osg::State subclass<br>
gw = new GraphicsWindowEx(0, 0, width(), height());<br>
viewer = new osgViewer::Viewer();<br>
viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);<br>
// setup viewer's camera etc.<br>
// In my case, I don't want the base camera to clear anything<br>
// I have a lot of other cameras queued as FBO rendering<br>
viewer->getCamera()->setViewport(0, 0, width(), height())<br>
viewer->getCamera()->setGraphicsContext(gw);<br>
viewer->getCamera()->setClearMask(0);<br>
//...<br>
connect(&heartbeat, SIGNAL(timeout()), this, SLOT(update()), Qt::QueuedConnection);<br>
hearbeat.start(10);<br>
}<br>
<br>
void RenderWidget::initializeGL()<br>
{<br>
viewer->realize();<br>
}<br>
<br>
void RenderWidget::paintGL()<br>
{<br>
static_cast<StateEx *>(&state)->setDefaultFbo(defaultFramebufferObject());<br>
<br>
viewer->frame();<br>
// OR if you want to mix OSG with Qt 2D API<br>
QPainter painter(this);<br>
painter.beginNativePainting();<br>
viewer->frame();<br>
painter.endNativePainting();<br>
// calculate fps...<br>
painter.setPen(Qt::white);<br>
painter.drawText(width() - 100, 10, 50, 25, Qt::AlignLeft, QString::number(fps));<br>
painter.end();<br>
}<br>
<br>
void RenderWidget::resizeGL(int width, int height)<br>
{<br>
gw->getEventQueue()->windowResize(0, 0, width, height);<br>
gw->resized(0, 0, width, height);<br>
//...<br>
}<br>
<br>
<br>
<br>
<br>
The difference between old QGLWidget and QOpenGLWidget is how they handle the rendering in the background. QOpenGLWidget is using QOffscreenSurface and QFrameBufferObject to render its content. The main problem of the current OSG integration is that it does not expect a superior FBO as main framebuffer. Like in my case, if you are using a lot of FBOs, some point OSG unbinds them and returns to direct drawing or leaves the last FBO bound after drawing. However, it should return(bind) to our superior FBO used by QOpenGLWidget.<br>
<br>
Let me explain it with the source code of OSG.<br>
<br>
<br>
Code:<br>
<br>
void RenderStage::drawInner(osg::RenderInfo& renderInfo, osgUtil::RenderLeaf*& previous, bool& doCopyTexture)<br>
{<br>
//...<br>
osg::State& state = *renderInfo.getState();<br>
<br>
osg::FBOExtensions* fbo_ext = _fbo.valid() ? osg::FBOExtensions::instance(state.getContextID(),true) : 0;<br>
bool fbo_supported = fbo_ext && fbo_ext->isSupported();<br>
<br>
bool using_multiple_render_targets = fbo_supported && _fbo->hasMultipleRenderingTargets();<br>
<br>
if (!using_multiple_render_targets)<br>
{<br>
#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)<br>
<br>
if( getDrawBufferApplyMask() )<br>
glDrawBuffer(_drawBuffer);<br>
<br>
if( getReadBufferApplyMask() )<br>
glReadBuffer(_readBuffer);<br>
<br>
#endif<br>
}<br>
<br>
if (fbo_supported)<br>
{<br>
_fbo->apply(state);<br>
}<br>
RenderBin::draw(renderInfo,previous);<br>
//...<br>
}<br>
<br>
<br>
<br>
<br>
As you can see, _fbo->apply(state); is the only point where FBO of the camera (which comes from our osg::Camera and RenderStage::runCameraSetUp) is bound before drawing our geometry etc. However, there is no line to handle returning back to FBO of QOpenGLWidget. Even we put a empty FBO as a last camera, it will executes following line:<br>
<br>
<br>
Code:<br>
<br>
void FrameBufferObject::apply(State &state, BindTarget target) const<br>
{<br>
//...<br>
if (_attachments.empty())<br>
{<br>
ext->glBindFramebuffer(target, 0);<br>
return;<br>
}<br>
//...<br>
}<br>
<br>
<br>
<br>
<br>
So basicly, it switches back to direct rendering because of 0 argument. Therefore, we have to inject default FBO of QOpenGLWidget somehow. You might already notice two special lines in paintGL() and constructor methods of RenderWidget implementation. We are going to subclass osg::State and osgViewer::GraphicsWindow. Here are the classes:<br>
<br>
<br>
Code:<br>
<br>
<br>
class StateEx : public osg::State<br>
{<br>
public:<br>
StateEx();<br>
<br>
inline void setDefaultFbo(GLuint fbo);<br>
inline GLuint getDefaultFbo() const;<br>
<br>
protected:<br>
GLuint defaultFbo;<br>
};<br>
<br>
StateEx::StateEx() :<br>
defaultFbo(0)<br>
{<br>
}<br>
<br>
inline void StateEx::setDefaultFbo(GLuint fbo)<br>
{<br>
defaultFbo = fbo;<br>
}<br>
<br>
inline GLuint getDefaultFbo() const<br>
{<br>
return defaultFbo;<br>
}<br>
<br>
<br>
<br>
<br>
Code:<br>
<br>
class GraphicsWindowEx : public osgViewer::GraphicsWindow<br>
{<br>
public:<br>
GraphicsWindowEx(osg::GraphicsContext::Traits* traits);<br>
GraphicsWindowEx(int x, int y, int width, int height);<br>
<br>
void init();<br>
<br>
virtual bool isSameKindAs(const osg::Object* object) const { return dynamic_cast<const GraphicsWindowEx *>(object) != 0; }<br>
virtual const char* libraryName() const { return ""; }<br>
virtual const char* className() const { return "GraphicsWindowEx"; }<br>
<br>
// dummy implementations, assume that graphics context is *always* current and valid.<br>
virtual bool valid() const { return true; }<br>
virtual bool realizeImplementation() { return true; }<br>
virtual bool isRealizedImplementation() const { return true; }<br>
virtual void closeImplementation() {}<br>
virtual bool makeCurrentImplementation() { return true; }<br>
virtual bool releaseContextImplementation() { return true; }<br>
virtual void swapBuffersImplementation() {}<br>
virtual void grabFocus() {}<br>
virtual void grabFocusIfPointerInWindow() {}<br>
virtual void raiseWindow() {}<br>
};<br>
<br>
GraphicsWindowEx::GraphicsWindowEx(osg::GraphicsContext::Traits* traits)<br>
{<br>
_traits = traits;<br>
init();<br>
}<br>
<br>
GraphicsWindowEx::GraphicsWindowEx(int x, int y, int width, int height);<br>
{<br>
_traits = new osg::GraphicsContext::Traits();<br>
_traits->x = x;<br>
_traits->x = y;<br>
_traits->width = width;<br>
_traits->height = height;<br>
<br>
init();<br>
}<br>
<br>
void GraphicsWindowEx::init()<br>
{<br>
if(valid())<br>
{<br>
// inject our "extended" state<br>
setState(new StateEx());<br>
getState()->setGraphicsContext(this);<br>
if (_traits.valid() && _traits->sharedContext.valid())<br>
{<br>
getState()->setContextID(_traits->sharedContext->getState()->getContextID() );<br>
incrementContextIDUsageCount(getState()->getContextID());<br>
}<br>
else<br>
{<br>
getState()->setContextID(osg::GraphicsContext::createNewContextID());<br>
}<br>
}<br>
}<br>
<br>
<br>
<br>
<br>
Now, our rendering knows the default FBO of the QOpenGLWidget so with custom RenderStage we can use this information. Only method we have to override is drawInner.<br>
<br>
<br>
Code:<br>
<br>
void RenderStageEx::drawInner(osg::RenderInfo& renderInfo, osgUtil::RenderLeaf*& previous, bool& doCopyTexture)<br>
{<br>
//...<br>
osg::State& state = *renderInfo.getState();<br>
osg::FBOExtensions* fbo_ext = osg::FBOExtensions::instance(state.getContextID(), true);<br>
bool fbo_supported = fbo_ext && fbo_ext->isSupported();<br>
if (fbo_supported)<br>
{<br>
if(_fbo.valid())<br>
{<br>
if (!_fbo->hasMultipleRenderingTargets())<br>
{<br>
#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)<br>
<br>
if( getDrawBufferApplyMask() )<br>
glDrawBuffer(_drawBuffer);<br>
<br>
if( getReadBufferApplyMask() )<br>
glReadBuffer(_readBuffer);<br>
<br>
#endif<br>
}<br>
<br>
_fbo->apply(state);<br>
}<br>
else<br>
fbo_ext->glBindFramebuffer(osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER, static_cast<StateEx *>(&state)->getDefaultFbo());<br>
}<br>
RenderBin::draw(renderInfo,previous);<br>
//...<br>
}<br>
<br>
<br>
<br>
<br>
After all this hussle, the most strange part of this solutions is extending osgUtil::CullVisitor. Unfortunately, there is no easy way to inject our RenderStageEx into the rendering pipeline. To solve it, I have overriden the apply(osg::Camera& camera) method. Actually almost all the code come from the original source code but two special care should be given. Firstly, here is what I did:<br>
<br>
<br>
Code:<br>
<br>
<br>
void CullVisitorEx::apply(osg::Camera &camera)<br>
{<br>
//...<br>
if(camera.getRenderOrder() == osg::Camera::NESTED_RENDER)<br>
{<br>
handle_cull_callbacks_and_traverse(camera);<br>
}<br>
else<br>
{<br>
osgUtil::RenderStage *prevRenderStage = getCurrentRenderBin()->getStage();<br>
osg::ref_ptr<RenderStageCacheEx> rsCache = dynamic_cast<RenderStageCacheEx *>(camera.getRenderingCache());<br>
if(!rsCache)<br>
{<br>
rsCache = new RenderStageCacheEx();<br>
camera.setRenderingCache(rsCache);<br>
}<br>
<br>
osg::ref_ptr<osgUtil::RenderStage> rtts = rsCache->getRenderStage(this);<br>
if(!rtts)<br>
{<br>
rtts = new RenderStageEx();<br>
<br>
//...<br>
}<br>
else<br>
rtts->reset();<br>
<br>
//...<br>
}<br>
<br>
//...<br>
}<br>
<br>
<br>
<br>
<br>
I kept only the parts that changed. Instead of new osgUtil::RenderStage(), we should call new RenderStageEx(). Unfortunately, renderstage caching implementation is hidden (implemented inside CPP) so you have to create your own RenderStageCache from scratch. Just copy the source code of it into the beginning of your CullVisitorEx.cpp and rename it.<br>
<br>
If you can manage to properly inject all these classes into your project, you can use any combination of FBO rendering with Qt5 and OSG 3.2.1. I have struggled a lot to make it work in current stable release. Hope this helps.<br>
<br>
P.S. I am preparing full source code and post it later.<br>
<br>
Thank you!<br>
<br>
Happy coding,<br>
Can[/code]<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=64403#64403" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=64403#64403</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">trajce nikolov nick<br></div>
</div>