[vtkusers] Animation problem
Gib Bogle
g.bogle at auckland.ac.nz
Fri Jul 25 01:58:20 EDT 2014
Hi,
I'm trying to implement an animation program, basing it closely (I think) on this example:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Utilities/Animation
Apparently I'm missing something because my program renders my actor correctly in the main program, but the code in the timer callback does not change what is being rendered. The main program and the callback function are shown below. The relevant part of the callback code that does not lead to a modified actor being rendered is this:
strips->Initialize();
MakeTube(nd, nstrips, dtheta, points, strips);
polydata->Reset();
polydata->SetPoints(points);
polydata->SetStrips(strips);
mapper->RemoveAllInputs();
mapper->SetInput(polydata);
actor->SetMapper(mapper);
mapper->Update();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::SafeDownCast(caller);
iren->GetRenderWindow()->Render();
As you can see, I have been flailing around, trying various likely possibilities, revealing my obvious lack of understanding. Initially I thought it would be sufficient to just change 'points' and 'strips', then I thought I should repopulate polydata, then...
class vtkTimerCallback2 : public vtkCommand
{
public:
static vtkTimerCallback2 *New()
{
vtkTimerCallback2 *cb = new vtkTimerCallback2;
cb->TimerCount = 0;
return cb;
}
virtual void Execute(vtkObject *caller, unsigned long eventId, void * vtkNotUsed(callData))
{
if (vtkCommand::TimerEvent == eventId)
{
++this->TimerCount;
}
std::cout << this->TimerCount << std::endl;
// actor->SetPosition(this->TimerCount/10., this->TimerCount/10.,0.);
dtheta = new double[nstrips];
for (int i=0; i<nstrips; i++) {
dtheta[i] = 0.0;
}
strips->Initialize();
MakeTube(nd, nstrips, dtheta, points, strips);
polydata->Reset();
polydata->SetPoints(points);
polydata->SetStrips(strips);
mapper->RemoveAllInputs();
mapper->SetInput(polydata);
actor->SetMapper(mapper);
mapper->Update();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::SafeDownCast(caller);
iren->GetRenderWindow()->Render();
}
private:
int TimerCount;
double *dtheta;
public:
int nd;
int nstrips;
vtkPolyData *polydata;
vtkCellArray *strips;
vtkPoints *points;
vtkDataSetMapper *mapper;
vtkActor* actor;
};
int main(int argc, char *argv[])
{
int nd = 20;
int nstrips = 4;
double *dtheta;
PI = 4*atan(1.0);
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkCellArray> strips = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); // we want just one big array of points
points->SetNumberOfPoints(nd*(nstrips+1));
strips->SetNumberOfCells(nstrips);
dtheta = new double[nstrips];
dthetafinal = new double[nstrips];
for (int i=0; i<nstrips; i++) {
dtheta[i] = 0.1;
dthetafinal[i] = 0.1;
}
MakeTube(nd, nstrips, dtheta, points, strips);
polydata->SetPoints(points);
polydata->SetStrips(strips);
// Create an actor and mapper
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper->SetInput(polydata);
#else
mapper->SetInputData(polydata);
#endif
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderWindow->Render();
// Initialize must be called prior to creating timer events.
renderWindowInteractor->Initialize();
// Sign up to receive TimerEvent
vtkSmartPointer<vtkTimerCallback2> cb = vtkSmartPointer<vtkTimerCallback2>::New();
cb->actor = actor;
cb->nd = nd;
cb->nstrips = nstrips;
cb->points = points;
cb->strips = strips;
cb->polydata = polydata;
cb->mapper = mapper;
renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
int timerId = renderWindowInteractor->CreateRepeatingTimer(1000);
std::cout << "timerId: " << timerId << std::endl;
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140725/ec3306a5/attachment.html>
More information about the vtkusers
mailing list