forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkLightActor.cxx
309 lines (265 loc) · 7.99 KB
/
vtkLightActor.cxx
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLightActor.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 "vtkLightActor.h"
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkCameraActor.h"
#include "vtkConeSource.h"
#include "vtkCamera.h"
#include "vtkLight.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkProperty.h"
#include "vtkBoundingBox.h"
vtkStandardNewMacro(vtkLightActor);
vtkCxxSetObjectMacro(vtkLightActor, Light, vtkLight);
// ----------------------------------------------------------------------------
vtkLightActor::vtkLightActor()
{
this->Light=0;
this->ClippingRange[0]=0.5;
this->ClippingRange[1]=10.0;
this->ConeSource=0;
this->ConeMapper=0;
this->ConeActor=0;
this->CameraLight=0;
this->FrustumActor=0;
this->BoundingBox=new vtkBoundingBox;
}
// ----------------------------------------------------------------------------
vtkLightActor::~vtkLightActor()
{
this->SetLight(0);
if(this->ConeActor!=0)
{
this->ConeActor->Delete();
}
if(this->ConeMapper!=0)
{
this->ConeMapper->Delete();
}
if(this->FrustumActor!=0)
{
this->FrustumActor->Delete();
}
if(this->ConeSource!=0)
{
this->ConeSource->Delete();
}
if(this->CameraLight!=0)
{
this->CameraLight->Delete();
}
delete this->BoundingBox;
}
// ----------------------------------------------------------------------------
// Description:
// Set/Get the location of the near and far clipping planes along the
// direction of projection. Both of these values must be positive.
// Initial values are (0.5,11.0)
void vtkLightActor::SetClippingRange(double dNear,
double dFar)
{
this->ClippingRange[0]=dNear;
this->ClippingRange[1]=dFar;
}
// ----------------------------------------------------------------------------
void vtkLightActor::SetClippingRange(const double a[2])
{
this->SetClippingRange(a[0], a[1]);
}
// ----------------------------------------------------------------------------
// Description:
// Support the standard render methods.
int vtkLightActor::RenderOpaqueGeometry(vtkViewport *viewport)
{
this->UpdateViewProps();
int result=0;
if(this->ConeActor!=0 && this->ConeActor->GetMapper()!=0)
{
result=this->ConeActor->RenderOpaqueGeometry(viewport);
result+=this->FrustumActor->RenderOpaqueGeometry(viewport);
}
return result;
}
// ----------------------------------------------------------------------------
// Description:
// Does this prop have some translucent polygonal geometry? No.
int vtkLightActor::HasTranslucentPolygonalGeometry()
{
return false;
}
//-----------------------------------------------------------------------------
void vtkLightActor::ReleaseGraphicsResources(vtkWindow *window)
{
if(this->ConeActor!=0)
{
this->ConeActor->ReleaseGraphicsResources(window);
this->FrustumActor->ReleaseGraphicsResources(window);
}
}
//-------------------------------------------------------------------------
// Get the bounds for this Actor as (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax).
double *vtkLightActor::GetBounds()
{
this->Bounds[0] = this->Bounds[2] = this->Bounds[4] = VTK_DOUBLE_MAX;
this->Bounds[1] = this->Bounds[3] = this->Bounds[5] = -VTK_DOUBLE_MAX;
this->UpdateViewProps();
this->BoundingBox->Reset();
if(this->ConeActor!=0)
{
if(this->ConeActor->GetUseBounds())
{
this->BoundingBox->AddBounds(this->ConeActor->GetBounds());
}
if(this->FrustumActor->GetUseBounds())
{
this->BoundingBox->AddBounds(this->FrustumActor->GetBounds());
}
}
int i=0;
while(i<6)
{
this->Bounds[i]=this->BoundingBox->GetBound(i);
++i;
}
if(this->Bounds[0]==VTK_DOUBLE_MAX)
{
// we cannot initialize the Bounds the same way vtkBoundingBox does because
// vtkProp3D::GetLength() does not check if the Bounds are initialized or
// not and makes a call to sqrt(). This call to sqrt with invalid values
// would raise a floating-point overflow exception (notably on BCC).
// As vtkMath::UninitializeBounds initialized finite unvalid bounds, it
// passes silently and GetLength() returns 0.
vtkMath::UninitializeBounds(this->Bounds);
}
return this->Bounds;
}
//-------------------------------------------------------------------------
vtkMTimeType vtkLightActor::GetMTime()
{
vtkMTimeType mTime=this->Superclass::GetMTime();
if(this->Light!=0)
{
vtkMTimeType time;
time = this->Light->GetMTime();
if(time>mTime)
{
mTime=time;
}
}
return mTime;
}
// ----------------------------------------------------------------------------
void vtkLightActor::UpdateViewProps()
{
if(this->Light==0)
{
vtkDebugMacro(<< "no light.");
return;
}
double angle=this->Light->GetConeAngle();
if(this->Light->GetPositional() && angle<180.0)
{
if(this->ConeSource==0)
{
this->ConeSource=vtkConeSource::New();
}
this->ConeSource->SetResolution(24);
double *pos=this->Light->GetPosition();
double *f=this->Light->GetFocalPoint();
double direction[3];
int i=0;
while(i<3)
{
direction[i]=pos[i]-f[i];
++i;
}
double height=1.0;
double center[3]; //=pos
double n=vtkMath::Norm(direction);
// cone center is the middle of its axis, not the center of the base...
i=0;
while(i<3)
{
center[i]=pos[i]-0.5*height/n*direction[i];
++i;
}
this->ConeSource->SetCenter(center);
this->ConeSource->SetDirection(direction);
this->ConeSource->SetHeight(height);
this->ConeSource->SetAngle(angle);
if(this->ConeMapper==0)
{
this->ConeMapper=vtkPolyDataMapper::New();
this->ConeMapper->SetInputConnection(this->ConeSource->GetOutputPort());
this->ConeMapper->SetScalarVisibility(0);
}
if(this->ConeActor==0)
{
this->ConeActor=vtkActor::New();
this->ConeActor->SetMapper(this->ConeMapper);
}
this->ConeActor->SetVisibility(this->Light->GetSwitch());
vtkProperty *p=this->ConeActor->GetProperty();
p->SetLighting(false);
p->SetColor(this->Light->GetDiffuseColor());
p->SetRepresentationToWireframe();
if(this->CameraLight==0)
{
this->CameraLight=vtkCamera::New();
}
this->CameraLight->SetPosition(this->Light->GetPosition());
this->CameraLight->SetFocalPoint(this->Light->GetFocalPoint());
this->CameraLight->SetViewUp(0.0,1.0,0.0);
// view angle is an aperture, but cone (or light) angle is between
// the axis of the cone and a ray along the edge of the cone.
this->CameraLight->SetViewAngle(angle*2.0);
// initial clip=(0.1,1000). near>0, far>near);
this->CameraLight->SetClippingRange(this->ClippingRange);
if(this->FrustumActor==0)
{
this->FrustumActor=vtkCameraActor::New();
}
this->FrustumActor->SetCamera(this->CameraLight);
this->FrustumActor->SetWidthByHeightRatio(1.0); // camera light is square
this->FrustumActor->SetUseBounds(false);
}
else
{
if(this->ConeActor)
{
this->ConeActor->SetMapper(0);
}
if(this->FrustumActor)
{
this->FrustumActor->SetCamera(0);
}
vtkErrorMacro(<< "not a spotlight.");
return;
}
}
//-------------------------------------------------------------------------
void vtkLightActor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Light: ";
if(this->Light==0)
{
os << "(none)" << endl;
}
else
{
this->Light->PrintSelf(os,indent);
}
os << indent << "ClippingRange: " << this->ClippingRange[0] << ","
<< this->ClippingRange[1] << endl;
}