[vtkusers] Rendering VTK inside a QGLWidget

Paul Rossi paulrossi at mail.ru
Tue Oct 11 09:58:30 EDT 2011


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?
by the way, is the page http://qt.nokia.com/products/3rdparty/vtksupport.html still available somewhere else?
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();

		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();
	}

/* 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();
	}

	void mouseMoveEvent(QMouseEvent *event) 
	{
		QPointF pf = event->pos();
		QPoint pi = pf.toPoint();

		event->accept();
		QMouseEvent e2(QEvent::MouseMove, pi, Qt::NoButton,
			event->buttons(), event->modifiers());
		m_QVTKIrenAdapter->ProcessEvent(&e2, m_vtkIren);

		updateGL();
	}

	void mouseReleaseEvent(QMouseEvent *event) 
	{
		QPointF pf = event->pos();
		QPoint pi = pf.toPoint();

		event->accept();
		QMouseEvent e2(QEvent::MouseButtonRelease, pi, event->button(),
			event->buttons(), event->modifiers());
		m_QVTKIrenAdapter->ProcessEvent(&e2, m_vtkIren);
		updateGL();
	}

private:
	vtkGenericOpenGLRenderWindow *m_vtkRenWin;
	vtkRenderer *m_vtkRen;
	vtkSphereSource *m_vtkSphereSource;
	vtkPolyDataMapper *m_vtkSphereMapper;
	vtkActor *m_vtkSphereActor;
	QVTKInteractorAdapter *m_QVTKIrenAdapter;
	vtkRenderWindowInteractor *m_vtkIren; 

	int m_uWidth,m_uHeight;
};

#endif



//CLASS ENDS



06 октября 2011, 21:05 от Clinton Stimpson <clinton at elemtech.com>:
> 
> Do you get anything from glCheckFramebufferStatus() after the
> glBindFramebufferEXT()?
> Have you considered using QGLFramebufferObject, if that is simpler?
> The GraphicsView example in VTK uses QGLFramebufferObject with
> vtkGenericOpenGLRenderWindow.
> 
> If you want to feed events into VTK, you should have a
> vtkRenderWindowInteractor.  But you don't have to call Start() on it.
> Have a look at the QVTKInteractorAdapter class.  You may be able to pass your
> QEvent's through there and it'll translate them for you and pass them to VTK.
> 
> Clint
> 
> On Thursday, October 06, 2011 10:42:18 am Paul Rossi wrote:
> > Thank you so much, Clinton
> >
> > Just by following your tips i managed to render a sphere inside a qglwidget
> > by using the vtkGenericOpenGLRenderWindow
> >
> > However, i have a question at this point:
> > I kind of do offscreen rendering, and before and after the rendering i
> > always call:
> >
> > 		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,m_uFramebuffer);
> > 		GLenum glError = glGetError();
> >
> > 		if (glError != GL_NO_ERROR)
> > 			throw Exception("Error binding framebuffer
> > (%s).",gluErrorString(glError));
> >
> >
> > and
> >
> > 		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
> > 		const GLenum glError = glGetError();
> >
> > 		if (glError != GL_NO_ERROR)
> > 			throw Exception("Error releasing framebuffer
> > (%s).",gluErrorString(glError));
> >
> > to attach and detach the fbo.
> > my problem is that this code renders well:
> >
> > 			renWin->PushState();
> > 			renWin->OpenGLInit();
> > 			renWin->Render();
> > 			renWin->PopState();
> >
> > but after that, if i try to detach the framebuffer i get an error "Invalid
> > operation" (even if it renders properly) I will now try to dig a little
> > into the vtkGenericOpenGLRenderWindow, but do you know what it could be
> > and how it could be fixed?
> >
> > I also have a last question: is it possible to skip using the
> > RenderWindowInteractor, and somehow feed events to vtk from qt events?
> > Otherwise, how should i proceed to get interaction from a qglwidget into a
> > vtk interactor?
> >
> > thank you so much already
> > Paul
> >
> > 06 октября 2011, 03:18 от Clinton Stimpson <clinton at elemtech.com>:
> > > On Wednesday, October 05, 2011 04:54:40 pm Paul Rossi wrote:
> > > > Hi all,
> > > >
> > > > I have a problem and i havent been able to fix it.
> > > > i want to use VTK from Qt, and i know that there is the
> > > > nice QVTK module for that.
> > > > My problem is that i  have to do my rendering from within a QGLWidget
> > > > (i am using a sort of framework where my views are qglwidgets, and i
> > > > cannot use any other viewer class)
> > > >
> > > > I know it is possible to render  vtk from within a QGraphicsView, but i
> > > > still cannot find if or how to render a  vtkrenderer from within a
> > > > qglwidget
> > > >
> > > > any suggestion?
> > >
> > > Try using the vtkGenericOpenGLRenderWindow in VTK 5.8.
> > > You can also see QVTKWidget2 as an example of using it, but depending on
> > > what you are doing, it may be simpler.  Or simply use QVTKWidget2 which
> > > is derived from QGLWidget, if that'll fit in your framework.
> > >
> > > If you do it yourself, you should at least call in the construction,
> > > depending on how you are going to use it:
> > > renWin->SwapBuffersOff()
> > >
> > > in the resize
> > > renWin->SetSize()
> > >
> > > in the paintGL()
> > > renWin->PushState()
> > > renWin->OpenGLInit()
> > > renWin->Render()
> > > renWin->PopState()
> > >
> > > If you are also mixing calls to QPainter, make sure to call
> > > QPainter::beginNativePainting()/endNativePainting().
> > >
> > > There's more to do if you want to use vtkRenderWindowInteractor as well.
> > >
> > > --
> > > 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
> _______________________________________________
> 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
> 


More information about the vtkusers mailing list