1 | /*========================================================================= |
---|
2 | |
---|
3 | Program: Visualization Toolkit |
---|
4 | Module: $RCSfile: Cone4.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 demonstrates the creation of multiple actors and the |
---|
17 | // manipulation of their properties and transformations. It is a |
---|
18 | // derivative of Cone.tcl, see that example for more information. |
---|
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 "vtkCamera.h" |
---|
26 | #include "vtkActor.h" |
---|
27 | #include "vtkRenderer.h" |
---|
28 | #include "vtkProperty.h" |
---|
29 | |
---|
30 | int main( int argc, char *argv[] ) |
---|
31 | { |
---|
32 | // |
---|
33 | // Next we create an instance of vtkConeSource and set some of its |
---|
34 | // properties. The instance of vtkConeSource "cone" is part of a |
---|
35 | // visualization pipeline (it is a source process object); it produces data |
---|
36 | // (output type is vtkPolyData) which other filters may process. |
---|
37 | // |
---|
38 | vtkConeSource *cone = vtkConeSource::New(); |
---|
39 | cone->SetHeight( 3.0 ); |
---|
40 | cone->SetRadius( 1.0 ); |
---|
41 | cone->SetResolution( 10 ); |
---|
42 | |
---|
43 | // |
---|
44 | // In this example we terminate the pipeline with a mapper process object. |
---|
45 | // (Intermediate filters such as vtkShrinkPolyData could be inserted in |
---|
46 | // between the source and the mapper.) We create an instance of |
---|
47 | // vtkPolyDataMapper to map the polygonal data into graphics primitives. We |
---|
48 | // connect the output of the cone souece to the input of this mapper. |
---|
49 | // |
---|
50 | vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); |
---|
51 | coneMapper->SetInput( cone->GetOutput() ); |
---|
52 | |
---|
53 | // |
---|
54 | // Create an actor to represent the first cone. The actor's properties are |
---|
55 | // modified to give it different surface properties. By default, an actor |
---|
56 | // is create with a property so the GetProperty() method can be used. |
---|
57 | // |
---|
58 | vtkActor *coneActor = vtkActor::New(); |
---|
59 | coneActor->SetMapper( coneMapper ); |
---|
60 | coneActor->GetProperty()->SetColor(0.2, 0.63, 0.79); |
---|
61 | coneActor->GetProperty()->SetDiffuse(0.7); |
---|
62 | coneActor->GetProperty()->SetSpecular(0.4); |
---|
63 | coneActor->GetProperty()->SetSpecularPower(20); |
---|
64 | |
---|
65 | // |
---|
66 | // Create a property and directly manipulate it. Assign it to the |
---|
67 | // second actor. |
---|
68 | // |
---|
69 | vtkProperty *property = vtkProperty::New(); |
---|
70 | property->SetColor(1.0, 0.3882, 0.2784); |
---|
71 | property->SetDiffuse(0.7); |
---|
72 | property->SetSpecular(0.4); |
---|
73 | property->SetSpecularPower(20); |
---|
74 | |
---|
75 | // |
---|
76 | // Create a second actor and a property. The property is directly |
---|
77 | // manipulated and then assigned to the actor. In this way, a single |
---|
78 | // property can be shared among many actors. Note also that we use the |
---|
79 | // same mapper as the first actor did. This way we avoid duplicating |
---|
80 | // geometry, which may save lots of memory if the geoemtry is large. |
---|
81 | vtkActor *coneActor2 = vtkActor::New(); |
---|
82 | coneActor2->SetMapper(coneMapper); |
---|
83 | coneActor2->GetProperty()->SetColor(0.2, 0.63, 0.79); |
---|
84 | coneActor2->SetProperty(property); |
---|
85 | coneActor2->SetPosition(0, 2, 0); |
---|
86 | |
---|
87 | // |
---|
88 | // Create the Renderer and assign actors to it. A renderer is like a |
---|
89 | // viewport. It is part or all of a window on the screen and it is |
---|
90 | // responsible for drawing the actors it has. We also set the background |
---|
91 | // color here. |
---|
92 | // |
---|
93 | vtkRenderer *ren1= vtkRenderer::New(); |
---|
94 | ren1->AddActor( coneActor ); |
---|
95 | ren1->AddActor( coneActor2 ); |
---|
96 | ren1->SetBackground( 0.1, 0.2, 0.4 ); |
---|
97 | |
---|
98 | // |
---|
99 | // Finally we create the render window which will show up on the screen. |
---|
100 | // We put our renderer into the render window using AddRenderer. We also |
---|
101 | // set the size to be 300 pixels by 300. |
---|
102 | // |
---|
103 | vtkRenderWindow *renWin = vtkRenderWindow::New(); |
---|
104 | renWin->AddRenderer( ren1 ); |
---|
105 | renWin->SetSize( 300, 300 ); |
---|
106 | |
---|
107 | // |
---|
108 | // Now we loop over 360 degreeees and render the cone each time. |
---|
109 | // |
---|
110 | int i; |
---|
111 | for (i = 0; i < 360; ++i) |
---|
112 | { |
---|
113 | // render the image |
---|
114 | renWin->Render(); |
---|
115 | // rotate the active camera by one degree |
---|
116 | ren1->GetActiveCamera()->Azimuth( 1 ); |
---|
117 | } |
---|
118 | |
---|
119 | // |
---|
120 | // Free up any objects we created. All instances in VTK are deleted by |
---|
121 | // using the Delete() method. |
---|
122 | // |
---|
123 | cone->Delete(); |
---|
124 | coneMapper->Delete(); |
---|
125 | coneActor->Delete(); |
---|
126 | property->Delete(); |
---|
127 | coneActor2->Delete(); |
---|
128 | ren1->Delete(); |
---|
129 | renWin->Delete(); |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|