forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkExternalOpenGLRenderer.cxx
379 lines (335 loc) · 10.7 KB
/
vtkExternalOpenGLRenderer.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*=========================================================================
Program: Visualization Toolkit
Module: vtkExternalOpenGLRenderer.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 "vtkExternalOpenGLRenderer.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkExternalLight.h"
#include "vtkExternalOpenGLCamera.h"
#include "vtkLightCollection.h"
#include "vtkLightCollection.h"
#include "vtkLight.h"
#include "vtkMath.h"
#include "vtkMatrix4x4.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGL.h"
#include "vtkRenderWindow.h"
#include "vtkTexture.h"
#define MAX_LIGHTS 8
vtkStandardNewMacro(vtkExternalOpenGLRenderer);
//----------------------------------------------------------------------------
vtkExternalOpenGLRenderer::vtkExternalOpenGLRenderer()
{
this->PreserveColorBuffer = 1;
this->PreserveDepthBuffer = 1;
this->SetAutomaticLightCreation(0);
this->ExternalLights = vtkLightCollection::New();
}
//----------------------------------------------------------------------------
vtkExternalOpenGLRenderer::~vtkExternalOpenGLRenderer()
{
this->ExternalLights->Delete();
this->ExternalLights = NULL;
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::Render(void)
{
GLdouble mv[16],p[16];
glGetDoublev(GL_MODELVIEW_MATRIX,mv);
glGetDoublev(GL_PROJECTION_MATRIX,p);
vtkExternalOpenGLCamera* camera = vtkExternalOpenGLCamera::SafeDownCast(
this->GetActiveCameraAndResetIfCreated());
camera->SetProjectionTransformMatrix(p);
camera->SetViewTransformMatrix(mv);
vtkMatrix4x4* matrix = vtkMatrix4x4::New();
matrix->DeepCopy(mv);
matrix->Transpose();
matrix->Invert();
// Synchronize camera viewUp
double viewUp[4] = {0.0, 1.0, 0.0, 0.0}, newViewUp[4];
matrix->MultiplyPoint(viewUp, newViewUp);
vtkMath::Normalize(newViewUp);
camera->SetViewUp(newViewUp);
// Synchronize camera position
double position[4] = {0.0, 0.0, 1.0, 1.0}, newPosition[4];
matrix->MultiplyPoint(position, newPosition);
if (newPosition[3] != 0.0)
{
newPosition[0] /= newPosition[3];
newPosition[1] /= newPosition[3];
newPosition[2] /= newPosition[3];
newPosition[3] = 1.0;
}
camera->SetPosition(newPosition);
// Synchronize focal point
double focalPoint[4] = {0.0, 0.0, 0.0, 1.0}, newFocalPoint[4];
matrix->MultiplyPoint(focalPoint, newFocalPoint);
camera->SetFocalPoint(newFocalPoint);
matrix->Delete();
// Lights
// Query lights existing in the external context
// and tweak them based on vtkExternalLight objects added by the user
GLenum curLight;
for (curLight = GL_LIGHT0;
curLight < GL_LIGHT0 + MAX_LIGHTS;
curLight++)
{
GLboolean status;
glGetBooleanv(curLight, &status);
int l_ind = static_cast<int> (curLight - GL_LIGHT0);
vtkLight* light = NULL;
bool light_created = false;
light = vtkLight::SafeDownCast(
this->GetLights()->GetItemAsObject(l_ind));
if (light)
{
if (!status)
{
// This is the case when we have a VTK light in the scene but no
// external light corresponding to that index in the context.
// In this case, we remove the VTK light as well.
light->SwitchOff();
this->RemoveLight(light);
// No need to go forward
continue;
}
}
else
{
// No matching light found in the VTK light collection
if (status)
{
// Create a new light only if one is present in the external context
light = vtkLight::New();
// Headlight because VTK will apply transform matrices
light->SetLightTypeToHeadlight();
light_created = true;
}
else
{
// No need to go forward as this light is not being used
continue;
}
}
// Find out if there is an external light object associated with this
// light index.
vtkCollectionSimpleIterator sit;
vtkExternalLight* eLight;
vtkExternalLight* curExtLight = NULL;
for (this->ExternalLights->InitTraversal(sit);
(eLight = vtkExternalLight::SafeDownCast(
this->ExternalLights->GetNextLight(sit))); )
{
if (eLight &&
(static_cast<GLenum>(eLight->GetLightIndex()) == curLight))
{
curExtLight = eLight;
break;
}
}
if (curExtLight &&
(curExtLight->GetReplaceMode() == vtkExternalLight::ALL_PARAMS))
{
// If the replace mode is all parameters, blatantly overwrite the
// parameters of existing/new light
light->DeepCopy(curExtLight);
}
else
{
GLfloat info[4];
// Set color parameters
if (curExtLight && curExtLight->GetIntensitySet())
{
light->SetIntensity(curExtLight->GetIntensity());
}
if (curExtLight && curExtLight->GetAmbientColorSet())
{
light->SetAmbientColor(curExtLight->GetAmbientColor());
}
else
{
glGetLightfv(curLight, GL_AMBIENT, info);
light->SetAmbientColor(info[0], info[1], info[2]);
}
if (curExtLight && curExtLight->GetDiffuseColorSet())
{
light->SetDiffuseColor(curExtLight->GetDiffuseColor());
}
else
{
glGetLightfv(curLight, GL_DIFFUSE, info);
light->SetDiffuseColor(info[0], info[1], info[2]);
}
if (curExtLight && curExtLight->GetSpecularColorSet())
{
light->SetSpecularColor(curExtLight->GetSpecularColor());
}
else
{
glGetLightfv(curLight, GL_SPECULAR, info);
light->SetSpecularColor(info[0], info[1], info[2]);
}
// Position, focal point and positional
glGetLightfv(curLight, GL_POSITION, info);
if (curExtLight && curExtLight->GetPositionalSet())
{
light->SetPositional(curExtLight->GetPositional());
}
else
{
light->SetPositional(info[3] > 0.0 ? 1 : 0);
}
if (!light->GetPositional())
{
if (curExtLight && curExtLight->GetFocalPointSet())
{
light->SetFocalPoint(curExtLight->GetFocalPoint());
if (curExtLight->GetPositionSet())
{
light->SetPosition(curExtLight->GetPosition());
}
else
{
light->SetPosition(info[0], info[1], info[2]);
}
}
else
{
light->SetFocalPoint(0, 0, 0);
if (curExtLight && curExtLight->GetPositionSet())
{
light->SetPosition(curExtLight->GetPosition());
}
else
{
light->SetPosition(-info[0], -info[1], -info[2]);
}
}
}
else
{
if (curExtLight && curExtLight->GetPositionSet())
{
light->SetPosition(curExtLight->GetPosition());
}
else
{
light->SetPosition(info[0], info[1], info[2]);
}
// Attenuation
if (curExtLight && curExtLight->GetAttenuationValuesSet())
{
light->SetAttenuationValues(curExtLight->GetAttenuationValues());
}
else
{
glGetLightfv(curLight, GL_CONSTANT_ATTENUATION, &info[0]);
glGetLightfv(curLight, GL_LINEAR_ATTENUATION, &info[1]);
glGetLightfv(curLight, GL_QUADRATIC_ATTENUATION, &info[2]);
light->SetAttenuationValues(info[0], info[1], info[2]);
}
// Cutoff
if (curExtLight && curExtLight->GetConeAngleSet())
{
light->SetConeAngle(curExtLight->GetConeAngle());
}
else
{
glGetLightfv(curLight, GL_SPOT_CUTOFF, &info[0]);
light->SetConeAngle(info[0]);
}
if (light->GetConeAngle() < 180.0)
{
// Exponent
if (curExtLight && curExtLight->GetExponentSet())
{
light->SetExponent(curExtLight->GetExponent());
}
else
{
glGetLightfv(curLight, GL_SPOT_EXPONENT, &info[0]);
light->SetExponent(info[0]);
}
// Direction
if (curExtLight && curExtLight->GetFocalPointSet())
{
light->SetFocalPoint(curExtLight->GetFocalPoint());
}
else
{
glGetLightfv(curLight, GL_SPOT_DIRECTION, info);
for (unsigned int i = 0; i < 3; ++i)
{
info[i] += light->GetPosition()[i];
}
light->SetFocalPoint(info[0], info[1], info[2]);
}
}
}
}
// If we created a new VTK light, add it to the collection
if (light_created)
{
this->AddLight(light);
light->Delete();
}
}
// Forward the call to the Superclass
this->Superclass::Render();
}
//----------------------------------------------------------------------------
vtkCamera* vtkExternalOpenGLRenderer::MakeCamera()
{
vtkCamera* cam = vtkExternalOpenGLCamera::New();
this->InvokeEvent(vtkCommand::CreateCameraEvent, cam);
return cam;
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::AddExternalLight(vtkExternalLight *light)
{
if (!light)
{
return;
}
vtkExternalLight* aLight;
vtkCollectionSimpleIterator sit;
for (this->ExternalLights->InitTraversal(sit);
(aLight = vtkExternalLight::SafeDownCast(
this->ExternalLights->GetNextLight(sit))); )
{
if (aLight && (aLight->GetLightIndex() == light->GetLightIndex()))
{
vtkErrorMacro( << "Attempting to add light with index " <<
light->GetLightIndex() <<
". But light with same index already exists.");
return;
}
}
this->ExternalLights->AddItem(light);
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::RemoveExternalLight(vtkExternalLight *light)
{
this->ExternalLights->RemoveItem(light);
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::RemoveAllExternalLights()
{
this->ExternalLights->RemoveAllItems();
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "External Lights:\n";
this->ExternalLights->PrintSelf(os, indent.GetNextIndent());
}