1 | /*========================================================================= |
---|
2 | |
---|
3 | Program: Visualization Toolkit |
---|
4 | Module: $RCSfile: Cone2.cxx,v $ |
---|
5 | |
---|
6 | Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen |
---|
7 | All rights reserved. |
---|
8 | See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
---|
9 | |
---|
10 | This software is distributed WITHOUT ANY WARRANTY; without even |
---|
11 | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
12 | PURPOSE. See the above copyright notice for more information. |
---|
13 | |
---|
14 | =========================================================================*/ |
---|
15 | // |
---|
16 | // This example shows how to add an observer to a C++ program. It extends |
---|
17 | // the Step1/Cxx/Cone.cxx C++ example (see that example for information on |
---|
18 | // the basic setup). |
---|
19 | // |
---|
20 | // VTK uses a command/observer design pattern. That is, observers watch for |
---|
21 | // particular events that any vtkObject (or subclass) may invoke on |
---|
22 | // itself. For example, the vtkRenderer invokes a "StartEvent" as it begins |
---|
23 | // to render. Here we add an observer that invokes a command when this event |
---|
24 | // is observed. |
---|
25 | // |
---|
26 | |
---|
27 | // first include the required header files for the vtk classes we are using |
---|
28 | #include "vtkConeSource.h" |
---|
29 | #include "vtkPolyDataMapper.h" |
---|
30 | #include "vtkRenderWindow.h" |
---|
31 | #include "vtkCommand.h" |
---|
32 | #include "vtkCamera.h" |
---|
33 | #include "vtkActor.h" |
---|
34 | #include "vtkRenderer.h" |
---|
35 | |
---|
36 | // Callback for the interaction |
---|
37 | class vtkMyCallback : public vtkCommand |
---|
38 | { |
---|
39 | public: |
---|
40 | static vtkMyCallback *New() |
---|
41 | { return new vtkMyCallback; } |
---|
42 | virtual void Execute(vtkObject *caller, unsigned long, void*) |
---|
43 | { |
---|
44 | vtkRenderer *renderer = reinterpret_cast<vtkRenderer*>(caller); |
---|
45 | cout << renderer->GetActiveCamera()->GetPosition()[0] << " " |
---|
46 | << renderer->GetActiveCamera()->GetPosition()[1] << " " |
---|
47 | << renderer->GetActiveCamera()->GetPosition()[2] << "\n"; |
---|
48 | } |
---|
49 | }; |
---|
50 | |
---|
51 | int main( int argc, char *argv[] ) |
---|
52 | { |
---|
53 | // |
---|
54 | // The pipeline creation is documented in Step1 |
---|
55 | // |
---|
56 | vtkConeSource *cone = vtkConeSource::New(); |
---|
57 | cone->SetHeight( 3.0 ); |
---|
58 | cone->SetRadius( 1.0 ); |
---|
59 | cone->SetResolution( 10 ); |
---|
60 | |
---|
61 | vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); |
---|
62 | coneMapper->SetInput( cone->GetOutput() ); |
---|
63 | vtkActor *coneActor = vtkActor::New(); |
---|
64 | coneActor->SetMapper( coneMapper ); |
---|
65 | |
---|
66 | vtkRenderer *ren1= vtkRenderer::New(); |
---|
67 | ren1->AddActor( coneActor ); |
---|
68 | ren1->SetBackground( 0.1, 0.2, 0.4 ); |
---|
69 | |
---|
70 | vtkRenderWindow *renWin = vtkRenderWindow::New(); |
---|
71 | renWin->AddRenderer( ren1 ); |
---|
72 | renWin->SetSize( 300, 300 ); |
---|
73 | |
---|
74 | // Here is where we setup the observer, we do a new and ren1 will |
---|
75 | // eventually free the observer |
---|
76 | vtkMyCallback *mo1 = vtkMyCallback::New(); |
---|
77 | ren1->AddObserver(vtkCommand::StartEvent,mo1); |
---|
78 | mo1->Delete(); |
---|
79 | |
---|
80 | // |
---|
81 | // now we loop over 360 degrees and render the cone each time |
---|
82 | // |
---|
83 | int i; |
---|
84 | for (i = 0; i < 360; ++i) |
---|
85 | { |
---|
86 | // render the image |
---|
87 | renWin->Render(); |
---|
88 | // rotate the active camera by one degree |
---|
89 | ren1->GetActiveCamera()->Azimuth( 1 ); |
---|
90 | } |
---|
91 | |
---|
92 | // |
---|
93 | // Free up any objects we created |
---|
94 | // |
---|
95 | cone->Delete(); |
---|
96 | coneMapper->Delete(); |
---|
97 | coneActor->Delete(); |
---|
98 | ren1->Delete(); |
---|
99 | renWin->Delete(); |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|