1 | /*========================================================================= |
---|
2 | |
---|
3 | Program: Visualization Toolkit |
---|
4 | Module: $RCSfile: Cone5.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 introduces the concepts of interaction into the |
---|
17 | // C++ environment. A different interaction style (than |
---|
18 | // the default) is defined. |
---|
19 | // |
---|
20 | |
---|
21 | // First include the required header files for the VTK classes we are using. |
---|
22 | #include "vtkConeSource.h" |
---|
23 | #include "vtkPolyDataMapper.h" |
---|
24 | #include "vtkRenderWindow.h" |
---|
25 | #include "vtkRenderWindowInteractor.h" |
---|
26 | #include "vtkCamera.h" |
---|
27 | #include "vtkActor.h" |
---|
28 | #include "vtkRenderer.h" |
---|
29 | #include "vtkInteractorStyleTrackballCamera.h" |
---|
30 | |
---|
31 | int main( int argc, char *argv[] ) |
---|
32 | { |
---|
33 | // |
---|
34 | // Next we create an instance of vtkConeSource and set some of its |
---|
35 | // properties. The instance of vtkConeSource "cone" is part of a |
---|
36 | // visualization pipeline (it is a source process object); it produces data |
---|
37 | // (output type is vtkPolyData) which other filters may process. |
---|
38 | // |
---|
39 | vtkConeSource *cone = vtkConeSource::New(); |
---|
40 | cone->SetHeight( 3.0 ); |
---|
41 | cone->SetRadius( 1.0 ); |
---|
42 | cone->SetResolution( 10 ); |
---|
43 | |
---|
44 | // |
---|
45 | // In this example we terminate the pipeline with a mapper process object. |
---|
46 | // (Intermediate filters such as vtkShrinkPolyData could be inserted in |
---|
47 | // between the source and the mapper.) We create an instance of |
---|
48 | // vtkPolyDataMapper to map the polygonal data into graphics primitives. We |
---|
49 | // connect the output of the cone souece to the input of this mapper. |
---|
50 | // |
---|
51 | vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); |
---|
52 | coneMapper->SetInput( cone->GetOutput() ); |
---|
53 | |
---|
54 | // |
---|
55 | // Create an actor to represent the cone. The actor orchestrates rendering |
---|
56 | // of the mapper's graphics primitives. An actor also refers to properties |
---|
57 | // via a vtkProperty instance, and includes an internal transformation |
---|
58 | // matrix. We set this actor's mapper to be coneMapper which we created |
---|
59 | // above. |
---|
60 | // |
---|
61 | vtkActor *coneActor = vtkActor::New(); |
---|
62 | coneActor->SetMapper( coneMapper ); |
---|
63 | |
---|
64 | // |
---|
65 | // Create the Renderer and assign actors to it. A renderer is like a |
---|
66 | // viewport. It is part or all of a window on the screen and it is |
---|
67 | // responsible for drawing the actors it has. We also set the background |
---|
68 | // color here. |
---|
69 | // |
---|
70 | vtkRenderer *ren1= vtkRenderer::New(); |
---|
71 | ren1->AddActor( coneActor ); |
---|
72 | ren1->SetBackground( 0.1, 0.2, 0.4 ); |
---|
73 | |
---|
74 | // |
---|
75 | // Finally we create the render window which will show up on the screen. |
---|
76 | // We put our renderer into the render window using AddRenderer. We also |
---|
77 | // set the size to be 300 pixels by 300. |
---|
78 | // |
---|
79 | vtkRenderWindow *renWin = vtkRenderWindow::New(); |
---|
80 | renWin->AddRenderer( ren1 ); |
---|
81 | renWin->SetSize( 300, 300 ); |
---|
82 | |
---|
83 | // |
---|
84 | // The vtkRenderWindowInteractor class watches for events (e.g., keypress, |
---|
85 | // mouse) in the vtkRenderWindow. These events are translated into |
---|
86 | // event invocations that VTK understands (see VTK/Common/vtkCommand.h |
---|
87 | // for all events that VTK processes). Then observers of these VTK |
---|
88 | // events can process them as appropriate. |
---|
89 | vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); |
---|
90 | iren->SetRenderWindow(renWin); |
---|
91 | |
---|
92 | // |
---|
93 | // By default the vtkRenderWindowInteractor instantiates an instance |
---|
94 | // of vtkInteractorStyle. vtkInteractorStyle translates a set of events |
---|
95 | // it observes into operations on the camera, actors, and/or properties |
---|
96 | // in the vtkRenderWindow associated with the vtkRenderWinodwInteractor. |
---|
97 | // Here we specify a particular interactor style. |
---|
98 | vtkInteractorStyleTrackballCamera *style = |
---|
99 | vtkInteractorStyleTrackballCamera::New(); |
---|
100 | iren->SetInteractorStyle(style); |
---|
101 | |
---|
102 | // |
---|
103 | // Unlike the previous scripts where we performed some operations and then |
---|
104 | // exited, here we leave an event loop running. The user can use the mouse |
---|
105 | // and keyboard to perform the operations on the scene according to the |
---|
106 | // current interaction style. When the user presses the "e" key, by default |
---|
107 | // an ExitEvent is invoked by the vtkRenderWindowInteractor which is caught |
---|
108 | // and drops out of the event loop (triggered by the Start() method that |
---|
109 | // follows. |
---|
110 | // |
---|
111 | iren->Initialize(); |
---|
112 | iren->Start(); |
---|
113 | |
---|
114 | // |
---|
115 | // Final note: recall that an observers can watch for particular events and |
---|
116 | // take appropriate action. Pressing "u" in the render window causes the |
---|
117 | // vtkRenderWindowInteractor to invoke a UserEvent. This can be caught to |
---|
118 | // popup a GUI, etc. So the Tcl Cone5.tcl example for an idea of how this |
---|
119 | // works. |
---|
120 | |
---|
121 | // |
---|
122 | // Free up any objects we created. All instances in VTK are deleted by |
---|
123 | // using the Delete() method. |
---|
124 | // |
---|
125 | cone->Delete(); |
---|
126 | coneMapper->Delete(); |
---|
127 | coneActor->Delete(); |
---|
128 | ren1->Delete(); |
---|
129 | renWin->Delete(); |
---|
130 | iren->Delete(); |
---|
131 | style->Delete(); |
---|
132 | |
---|
133 | return 0; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|