1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <vtkPolyDataMapper.h> #include <vtkObjectFactory.h> #include <vtkCommand.h> #include <vtkActor.h> #include <vtkSmartPointer.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyle.h> #include <vtkPolyData.h> #include <vtkSphereSource.h> #include <vtkInteractorStyleTrackballCamera.h>
class MyClass { public: void KeypressCallbackFunction(vtkObject*, long unsigned int vtkNotUsed(eventId), void* vtkNotUsed(callData)) { std::cout << "Caught event in MyClass" << std::endl; }
};
class MyInteractorStyle : public vtkInteractorStyleTrackballCamera { public: static MyInteractorStyle* New(); vtkTypeMacro(MyInteractorStyle, vtkInteractorStyleTrackballCamera);
void KeypressCallbackFunction(vtkObject*, long unsigned int vtkNotUsed(eventId), void* vtkNotUsed(callData) ) { std::cout << "Caught event in MyInteractorStyle" << std::endl; }
}; vtkStandardNewMacro(MyInteractorStyle);
int main(int, char *[]) { vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->SetCenter(0.0, 0.0, 0.0); sphereSource->SetRadius(5.0); sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow);
MyClass myClass; renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent, &myClass, &MyClass::KeypressCallbackFunction);
MyInteractorStyle* style = MyInteractorStyle::New(); renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent, style, &MyInteractorStyle::KeypressCallbackFunction);
vtkSmartPointer<MyInteractorStyle> style2 = vtkSmartPointer<MyInteractorStyle>::New(); renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent, style2, &MyInteractorStyle::KeypressCallbackFunction); renderer->AddActor(actor); renderer->SetBackground(1,1,1); renderWindow->Render(); renderWindowInteractor->Start();
style->Delete();
return EXIT_SUCCESS;
|