File: TestMonitors.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (261 lines) | stat: -rw-r--r-- 8,205 bytes parent folder | download | duplicates (7)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestMonitors.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkLight.h"
#include "vtkOpenGLLightMonitor.h"
#include "vtkOpenGLModelViewProjectionMonitor.h"
#include "vtkBackgroundColorMonitor.h"
#include "vtkLightingHelper.h"

int TestMonitors(int argc, char* argv[])
{
  (void)argc;
  (void)argv;

  cerr << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl << endl;

  // initialize a scene with a bunch of spheres
  vtkSmartPointer<vtkSphereSource> sphere =
    vtkSmartPointer<vtkSphereSource>::New();
  sphere->SetThetaResolution(100);
  sphere->SetPhiResolution(50);

  vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  sphereMapper->SetInputConnection(sphere->GetOutputPort());

  vtkSmartPointer<vtkActor> sphere1 =
    vtkSmartPointer<vtkActor>::New();
  sphere1->SetMapper(sphereMapper);
  sphere1->GetProperty()->SetColor(1,0,0);
  sphere1->GetProperty()->SetAmbient(0.3);
  sphere1->GetProperty()->SetDiffuse(0.0);
  sphere1->GetProperty()->SetSpecular(1.0);
  sphere1->GetProperty()->SetSpecularPower(5.0);

  vtkSmartPointer<vtkActor> sphere2 =
    vtkSmartPointer<vtkActor>::New();
  sphere2->SetMapper(sphereMapper);
  sphere2->GetProperty()->SetColor(1,0,0);
  sphere2->GetProperty()->SetAmbient(0.3);
  sphere2->GetProperty()->SetDiffuse(0.0);
  sphere2->GetProperty()->SetSpecular(1.0);
  sphere2->GetProperty()->SetSpecularPower(10.0);
  sphere2->AddPosition(1.25,0,0);

  vtkSmartPointer<vtkActor> sphere3 =
    vtkSmartPointer<vtkActor>::New();
  sphere3->SetMapper(sphereMapper);
  sphere3->GetProperty()->SetColor(1,0,0);
  sphere3->GetProperty()->SetAmbient(0.3);
  sphere3->GetProperty()->SetDiffuse(0.0);
  sphere3->GetProperty()->SetSpecular(1.0);
  sphere3->GetProperty()->SetSpecularPower(20.0);
  sphere3->AddPosition(2.5,0,0);

  vtkSmartPointer<vtkActor> sphere4 =
    vtkSmartPointer<vtkActor>::New();
  sphere4->SetMapper(sphereMapper);
  sphere4->GetProperty()->SetColor(1,0,0);
  sphere4->GetProperty()->SetAmbient(0.3);
  sphere4->GetProperty()->SetDiffuse(0.0);
  sphere4->GetProperty()->SetSpecular(1.0);
  sphere4->GetProperty()->SetSpecularPower(40.0);
  sphere4->AddPosition(3.75,0,0);

  vtkSmartPointer<vtkActor> sphere5 =
    vtkSmartPointer<vtkActor>::New();
  sphere5->SetMapper(sphereMapper);
  sphere5->GetProperty()->SetColor(1,0,0);
  sphere5->GetProperty()->SetAmbient(0.3);
  sphere5->GetProperty()->SetDiffuse(0.0);
  sphere5->GetProperty()->SetSpecular(0.5);
  sphere5->GetProperty()->SetSpecularPower(5.0);
  sphere5->AddPosition(0.0,1.25,0);

  vtkSmartPointer<vtkActor> sphere6 =
    vtkSmartPointer<vtkActor>::New();
  sphere6->SetMapper(sphereMapper);
  sphere6->GetProperty()->SetColor(1,0,0);
  sphere6->GetProperty()->SetAmbient(0.3);
  sphere6->GetProperty()->SetDiffuse(0.0);
  sphere6->GetProperty()->SetSpecular(0.5);
  sphere6->GetProperty()->SetSpecularPower(10.0);
  sphere6->AddPosition(1.25,1.25,0);

  vtkSmartPointer<vtkActor> sphere7 =
    vtkSmartPointer<vtkActor>::New();
  sphere7->SetMapper(sphereMapper);
  sphere7->GetProperty()->SetColor(1,0,0);
  sphere7->GetProperty()->SetAmbient(0.3);
  sphere7->GetProperty()->SetDiffuse(0.0);
  sphere7->GetProperty()->SetSpecular(0.5);
  sphere7->GetProperty()->SetSpecularPower(20.0);
  sphere7->AddPosition(2.5,1.25,0);

  vtkSmartPointer<vtkActor> sphere8 =
    vtkSmartPointer<vtkActor>::New();
  sphere8->SetMapper(sphereMapper);
  sphere8->GetProperty()->SetColor(1,0,0);
  sphere8->GetProperty()->SetAmbient(0.3);
  sphere8->GetProperty()->SetDiffuse(0.0);
  sphere8->GetProperty()->SetSpecular(0.5);
  sphere8->GetProperty()->SetSpecularPower(40.0);
  sphere8->AddPosition(3.75,1.25,0);

  vtkSmartPointer<vtkRenderer> ren1 =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->AddRenderer(ren1);

  ren1->AddActor(sphere1);
  ren1->AddActor(sphere2);
  ren1->AddActor(sphere3);
  ren1->AddActor(sphere4);
  ren1->AddActor(sphere5);
  ren1->AddActor(sphere6);
  ren1->AddActor(sphere7);
  ren1->AddActor(sphere8);

  vtkSmartPointer<vtkLight> light =
    vtkSmartPointer<vtkLight>::New();
  ren1->AddLight(light);

  vtkSmartPointer<vtkLight> light2 =
    vtkSmartPointer<vtkLight>::New();
  ren1->AddLight(light2);

  renWin->SetSize(400, 200);

  // push all of VTK state to OpenGL.
  renWin->Render();

  // now create the monitors
  // for lighting
  // for gl modelview and projection matrices
  // for the background color
  // and initialize them from current state
  cerr
    << "Lights" << endl
    << "================" << endl;
  const int nLights = vtkLightingHelper::VTK_MAX_LIGHTS;
  vtkSmartPointer<vtkOpenGLLightMonitor> lightMonitor[nLights];
  for (int i=0; i<nLights; ++i)
   {
   lightMonitor[i] = vtkSmartPointer<vtkOpenGLLightMonitor>::New();
   lightMonitor[i]->SetLightId(i);
   lightMonitor[i]->Update();
   lightMonitor[i]->Print(cerr);
   }

  cerr
    << "Matrices" << endl
    << "================" << endl;

  vtkSmartPointer<vtkOpenGLModelViewProjectionMonitor> matrixMonitor
    = vtkSmartPointer<vtkOpenGLModelViewProjectionMonitor>::New();

  matrixMonitor->Update();
  matrixMonitor->Print(cerr);

  cerr
    << "Background Color" << endl
    << "================" << endl;

  vtkSmartPointer<vtkBackgroundColorMonitor> backgroundColorMonitor
    = vtkSmartPointer<vtkBackgroundColorMonitor>::New();

  backgroundColorMonitor->Update(ren1);
  backgroundColorMonitor->Print(cerr);

  // update the scene so that lights, background color
  // and modelview projection matrices are modified
  ren1->SetBackground2(0.1, 0.2, 0.4);
  ren1->SetGradientBackground(1);

  light->SetFocalPoint(1.875,0.6125,0);
  light->SetPosition(0.875,1.6125,1);

  light2->SetFocalPoint(-100,-100,-100);
  light2->SetPosition(100,100,100);

  ren1->GetActiveCamera()->SetFocalPoint(0,0,0);
  ren1->GetActiveCamera()->SetPosition(0,0,1);
  ren1->GetActiveCamera()->SetViewUp(0,1,0);
  ren1->GetActiveCamera()->ParallelProjectionOn();
  ren1->ResetCamera();
  ren1->GetActiveCamera()->SetParallelScale(1.5);

  // push all of VTK state to OpenGL.
  renWin->Render();

  // verify that we can detect the changes
  // in lighting
  // in the modelview and projectionmatrices
  // in the background color
  // all should have been updated
  cerr
    << "Lights" << endl
    << "================" << endl;
  bool lightsChanged = false;
  for (int i=0; i<nLights; ++i)
   {
   if (lightMonitor[i]->StateChanged())
     {
     cerr << "this light was changed..." << endl;
     lightsChanged = true;
     }
   lightMonitor[i]->Print(cerr);
   }

  cerr
    << "Matrices" << endl
    << "================" << endl;

  bool matricesChanged = matrixMonitor->StateChanged();
  matrixMonitor->Print(cerr);

  cerr
    << "Background Color" << endl
    << "================" << endl;

  bool colorChanged = backgroundColorMonitor->StateChanged(ren1);
  backgroundColorMonitor->Print(cerr);

  cerr
    << "Test results" << endl
    << "================" << endl
    << "detected lights changed..." << (lightsChanged?"yes":"no") << endl
    << "detected matrices changed..." << (matricesChanged?"yes":"no") << endl
    << "detected background color changed..." << (colorChanged?"yes":"no") << endl
    << endl;

  if (!(lightsChanged && matricesChanged && colorChanged))
    {
    cerr << "Test fails" << endl;
    return 1;
    }

  cerr << "Test passes" << endl;
  return 0;
}