[vtkusers] Rendering VTK inside a QGLWidget

Clinton Stimpson clinton at elemtech.com
Wed Oct 12 17:37:10 EDT 2011


On Wednesday, October 12, 2011 03:14:31 pm Paul Rossi wrote:
> Hi Clint,  thank you really a lot, this way things work perfectly!
> In fact i had to change a little the flow, because i am using fbo's,
> therefore i had to perform the event sending in the display() callback,
> and in the mouse handlers i simply recorded the event and set a flag.
> 
> so now a snippet of my display callback looks like:
> 
> 			if (m_bInteractorUpdate) {
> 				m_bInteractorUpdate = false;
> 
> 				glPushAttrib(GL_ALL_ATTRIB_BITS);
> 				fbo.bind();
> 
> 				m_vtkRenWin->PushState();
> 				m_vtkRenWin->OpenGLInit();
> 
> 				m_QVTKIrenAdapter->ProcessEvent(&m_qMouseEvent,
> m_vtkRenWin->GetInteractor()); //m_vtkRenWin2->Render();
> 
> 				m_vtkRenWin->PopState();
> 
> 				glPopAttrib();
> 				fbo.release();
> 
> 				//GetPlugin().update();
> 			} else {
> 				fbo.bind();
> 				glPushAttrib(GL_ALL_ATTRIB_BITS);
> 
> 				m_vtkRenWin->PushState();

Note: PushState() does a glPushAttrib() for you.

> 				m_vtkRenWin->OpenGLInit();
> 
> 				m_vtkRenWin->Render();
> 
> 				m_vtkRenWin->PopState();
> 
> 				glPopAttrib();
> 				fbo.release();
> 			}
> 
> 
> And everything works perfectly :)

One more thing in case you might need it... When using an FBO for 
vtkGenericOpenGLRenderWindow, I've done this:
    mWin->SetDoubleBuffer(0);
    mWin->SetFrontBuffer(vtkgl::COLOR_ATTACHMENT0_EXT);
    mWin->SetFrontLeftBuffer(vtkgl::COLOR_ATTACHMENT0_EXT);
    mWin->SetBackBuffer(vtkgl::COLOR_ATTACHMENT0_EXT);
    mWin->SetBackLeftBuffer(vtkgl::COLOR_ATTACHMENT0_EXT);

I guess with SetDoubleBuffer(0), you won't need to call SwapBuffersOff(), and VTK 
will just do a glFlush() instead.

> 
> However, i have yet another question: i embedded the example of the
> TubesWithVaryingRadiusAndColors, and it is great, but when, for instance i
> tried to embed the hello world example, i got stuck, as it makes use of a
> view, instead of a renderwindow.
> 
> As far as i can understand, a view is essentially a group of a bit of
> everything (render, renderwindow, interactor, actors, etc etc).
> What i did was something like this (without success):
> 
> 	void initVtk3() {
> 		vtkRandomGraphSource* source = vtkRandomGraphSource::New();
> 
> 		vtkGraphLayoutView* view = vtkGraphLayoutView::New();
> 		view->SetRepresentationFromInputConnection(
> 			source->GetOutputPort());
> 
> 		//view->ResetCamera();
> 
> 		m_vtkRenWin = vtkGenericOpenGLRenderWindow::New();
> 		m_vtkRenWin->SetSize(m_uWidth, m_uHeight);
> 
> 		m_vtkIren = QVTKInteractor::New();
> 		m_vtkIren->SetRenderWindow(m_vtkRenWin);
> 		m_vtkIren->Initialize();
> 		m_vtkIren->SetInteractorStyle(view->GetInteractorStyle());
> 		m_vtkRenWin->AddRenderer(view->GetRenderer());
> 		view->SetInteractor(m_vtkIren);
> 		view->SetRenderWindow(m_vtkRenWin);

The view classes might be a bit picky.  Try this:

 		m_vtkIren = QVTKInteractor::New();
 		m_vtkIren->SetRenderWindow(m_vtkRenWin);
 		m_vtkIren->Initialize();
 		view->SetInteractor(m_vtkIren);
 		view->SetRenderWindow(m_vtkRenWin);

Or have a look at VTK/Examples/GUI/Qt/GraphicsView/GraphLayoutViewItem.cpp.

Clint

> 
> 
> 		m_vtkRenWin->SwapBuffersOff();
> 		//source->Delete();
> 	}
> 
> is there something particular i should do in order to get views to work
> with QVTK_QTOPENGL? thanks,
> Paul
> 
> 11 октября 2011, 18:14 от Clinton Stimpson <clinton at elemtech.com>:
> > On Tuesday, October 11, 2011 07:58:30 am Paul Rossi wrote:
> > > Hi Clint,
> > > so in fact the status seems to be complete, and in general
> > > bindFramebufferEXT() called with 0 should never return invalid
> > > operation. But oh well.. it seems to work, and so i dont care too much
> > > about it.
> > > 
> > > Rather the interaction: i followed your suggestion, but unfortunately
> > > there is not much documentation about QVTKInteractorAdapter. i found
> > > only this page:
> > > 
> > > https://www.kitware.com/InfovisWiki/index.php/IP2/qgraphicsview-events
> > > 
> > > and i created the sample code below, that unfortunately crashes when i
> > > try to interact. I am not sure what i am doing wrong here, perhaps you
> > > have an idea?
> > 
> > Have you tried using the debugger?  Maybe you need a debug build of VTK
> > to get useful information.  Some suggestions below in the code...
> > 
> > > by the way, is the page
> > > http://qt.nokia.com/products/3rdparty/vtksupport.html still available
> > > somewhere else?
> > 
> > I don't know.
> > 
> > > best,
> > > Paul
> > > 
> > > //CLASS STARTS
> > > 
> > > 
> > > #ifndef GLWIDGETCUBE_H
> > > #define GLWIDGETCUBE_H
> > > 
> > > 
> > > #include <QtGui>
> > > #include <QtOpenGL>
> > > #include <QGLWidget>
> > > 
> > > #include <math.h>
> > > #include <stdlib.h>
> > > #include <stdio.h>
> > > #include "vtkRenderWindowInteractor.h"
> > > #include "vtkRenderWindow.h"
> > > #include "vtkRenderer.h"
> > > #include "vtkGenericOpenGLRenderWindow.h"
> > > #include "vtkSphereSource.h"
> > > #include "vtkCubeSource.h"
> > > #include "vtkPolyDataMapper.h"
> > > #include "vtkActor.h"
> > > #include "vtkPolyDataNormals.h"
> > > #include "QVTKInteractorAdapter.h"
> > > #include "QPoint"
> > > #include "QPointF"
> > > #include "QMouseEvent"
> > > #include "QFrame"
> > > #include "QGLWidget"
> > > #include "QListWidget"
> > > 
> > > class GLWidgetCube : public QGLWidget
> > > {
> > > 
> > > 	Q_OBJECT
> > > 
> > > public:
> > > 	GLWidgetCube(QWidget *parent=0) : QGLWidget(parent)
> > > 	{
> > > 	
> > > 	};
> > > 	~GLWidgetCube()
> > > 	{
> > > 	};
> > > 
> > > signals:
> > > 		void sliceNumberChanged(int slices);
> > > 
> > > protected:
> > > 	void initializeGL() {
> > > 	
> > > 		glClearColor(0, 0, 0, 1);
> > > 		glEnable(GL_DEPTH_TEST);
> > > 		initVtk();
> > > 	
> > > 	}
> > > 	
> > > 	void initVtk() {
> > > 	
> > > 		m_vtkRenWin = vtkGenericOpenGLRenderWindow::New();
> > > 		m_vtkRen = vtkRenderer::New();
> > > 		vtkRenderWindowInteractor *m_vtkIren =
> > 
> > vtkRenderWindowInteractor::New();
> > 
> > If you want timer events to work, this should be a QVTKInteractor, not
> > one created by vtkRenderWindowInteractor::New();
> > 
> > > 		m_QVTKIrenAdapter = new QVTKInteractorAdapter(this);
> > > 		
> > > 		if (!m_QVTKIrenAdapter)
> > > 		
> > > 			printf("Adapter null\n");
> > > 		
> > > 		m_vtkIren->SetRenderWindow(m_vtkRenWin);
> > > 		m_vtkRenWin->AddRenderer(m_vtkRen);
> > > 		m_vtkSphereSource = vtkSphereSource::New();
> > > 		m_vtkSphereSource->SetRadius(1);
> > > 		m_vtkSphereSource->SetThetaResolution(8);
> > > 		m_vtkSphereSource->SetPhiResolution(8);
> > > 		m_vtkSphereMapper = vtkPolyDataMapper::New();
> > > 		m_vtkSphereMapper->SetInput(m_vtkSphereSource->GetOutput());
> > > 		m_vtkSphereActor = vtkActor::New();
> > > 		m_vtkSphereActor->SetMapper(m_vtkSphereMapper);
> > > 		m_vtkRen->AddActor(m_vtkSphereActor);
> > > 		m_vtkRenWin->SwapBuffersOff();
> > > 	
> > > 	}
> > > 	
> > > 	void paintGL() {
> > > 	
> > > 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
> > > 		glMatrixMode(GL_MODELVIEW);
> > > 		glLoadIdentity();
> > > 		
> > > 		drawVtk();
> > > 	
> > > 	}
> > > 	
> > > 	void drawVtk() {
> > > 	
> > > 		m_vtkRenWin->PushState();
> > > 		m_vtkRenWin->OpenGLInit();
> > > 		m_vtkRenWin->Render();
> > > 		m_vtkRenWin->PopState();
> > > 	
> > > 	}
> > > 	
> > > 	void resizeGL(int width, int height) {
> > > 	
> > > 		m_uWidth = width;
> > > 		m_uHeight =  height;
> > > 		m_vtkRenWin->SetSize(m_uWidth,m_uHeight);
> > > 		updateGL();
> > > 	
> > > 	}
> > 
> > For resize event, have a look at QVTKWidget2::resizeGL().  No need for
> > updateGL() is there?
> > 
> > > /* Handlers */
> > > 
> > > 	void mousePressEvent(QMouseEvent *event)
> > > 	{
> > > 	
> > > 		QPointF pf = event->pos();
> > > 		QPoint pi = pf.toPoint();
> > > 		
> > > 		event->accept();
> > > 		QMouseEvent e2(QEvent::MouseButtonPress, pi, event->button(),
> > > 		
> > > 			event->buttons(), event->modifiers());
> > > 		
> > > 		m_QVTKIrenAdapter->ProcessEvent(&e2, m_vtkIren);
> > > 		updateGL();
> > > 	
> > > 	}
> > 
> > I suggest looking at QVTKWidget2::mousePressEvent() for an example. 
> > Ditto for other handlers in your example.
> > 
> > Hope that helps.
> > 
> > --
> > Clinton Stimpson
> > Elemental Technologies, Inc
> > Computational Simulation Software, LLC
> > www.csimsoft.com
> > _______________________________________________
> > Powered by www.kitware.com
> > 
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> > 
> > Please keep messages on-topic and check the VTK FAQ at:
> > http://www.vtk.org/Wiki/VTK_FAQ
> > 
> > Follow this link to subscribe/unsubscribe:
> > http://www.vtk.org/mailman/listinfo/vtkusers
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

-- 
Clinton Stimpson
Elemental Technologies, Inc
Computational Simulation Software, LLC
www.csimsoft.com



More information about the vtkusers mailing list