[vtkusers] vtk animation and interactor problem!!!!

Jens Fisseler fisseler at rob.uni-luebeck.de
Fri Feb 4 03:53:33 EST 2005


Hi Kostas!

The first problem with your program is that you create a new
vtkRenderWindowInteractor with every iteration of your infinite
"while"-loop. Fix this first.

Your animation will probably also run as fast as it can, because you
don't consider any timing information. One approach I have used recently
is creating a timer which calls an "animate"-method every x msecs. For
example if you want a framerate of 25 frames per second, this method
must be called every 40 msecs. You then update your scene inside this
method and render it. This way, the animation continues even while
you're interacting with the scene.

Creating a timer is quite easy if your use wxWidgets. Just use "wxTimer"
and you're done. I think it should also be possible if you use another
GUI toolkit, but I have only done it using wxWidgets.

I hope this helps,

	Jens

--

> #include "vtkSphereSource.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkRenderWindow.h"
> #include "vtkCamera.h"
> #include "vtkActor.h"
> #include "vtkRenderer.h"
> #include "vtkStructuredPointsReader.h"
> #include "vtkGlyph3D.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkPoints.h"
> #include "vtkPolyData.h"
> #include "vtkProperty.h"
> 
> int main( int argc, char *argv[] )
> {
> 	vtkPoints *points = vtkPoints::New();
> 	vtkSphereSource *sphere = vtkSphereSource::New();
> 	vtkPolyData *data = vtkPolyData::New();
> 	vtkGlyph3D *verts = vtkGlyph3D::New();
> 	vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
> 	vtkActor *sphereActor = vtkActor::New();
> 	vtkRenderer *ren1= vtkRenderer::New();
> 	vtkRenderWindow *renWin = vtkRenderWindow::New();
> 
> 	const int ni = 20;
> 
> 	points->SetNumberOfPoints(ni);
> 	for (int i=0; i <ni; i++) {
> 		points->SetPoint(i,
> 				(double)(i%30)*0.1,
> 				(double)0,
> 				(double)(i/20));
> 	}
> 
> 	sphere->SetRadius(0.04 );
> 	sphere->SetPhiResolution( 4 );
> 	sphere->SetThetaResolution( 4 );
> 
> 	data->SetPoints(points);
> 
> 	verts->SetSource(sphere->GetOutput());
> 	verts->SetInput(data);
> 	verts->ScalingOff();
> 
> 	sphereMapper->SetInput( verts->GetOutput() );
> 
> 	sphereActor->SetMapper( sphereMapper );
> 	sphereActor->GetProperty()->SetOpacity(0.5);
> 	sphereActor->RotateY(-45.0);
> 
> 	ren1->AddActor( sphereActor );
> 	ren1->SetBackground( 1.0, 1.0, 1.0 );
> 
> 	renWin->AddRenderer( ren1 );
> 	renWin->SetSize( 300, 300 );
> 
> 	double u=0.2;
> 	int uu=0;
> 	int n=0;
> 	while (1) {
> 		for (int i=0; i <ni; i++) {
> 			points->SetPoint(i,
> 					 (double)(i%20)*0.1,
> 					 (double)0.2*sin(u*i/10.0*u),
> 					 (double)(i/20));
> 		}
> 		u+=0.01;
> 		points->Modified();
> 		renWin->Render();
> 
> 	vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
> 	iren->SetRenderWindow(renWin);
> 	iren->Initialize();
> 	iren->Start();
> 	}
> 	return 0;
> }




More information about the vtkusers mailing list