vtkRenderer.cxx 53.6 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkRenderer.cxx
Ken Martin's avatar
Ken Martin committed
5

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Ken Martin's avatar
Ken Martin committed
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Ken Martin's avatar
Ken Martin committed
13
14

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
15
#include "vtkRenderer.h"
16
17

#include "vtkAssemblyNode.h"
18
#include "vtkAssemblyPath.h"
19
20
#include "vtkCamera.h"
#include "vtkCommand.h"
21
#include "vtkCuller.h"
22
#include "vtkCullerCollection.h"
23
#include "vtkFrustumCoverageCuller.h"
Ken Martin's avatar
Ken Martin committed
24
#include "vtkGraphicsFactory.h"
25
#include "vtkLight.h"
26
#include "vtkLightCollection.h"
27
#include "vtkMath.h"
28
#include "vtkMatrix4x4.h"
29
30
#include "vtkOutputWindow.h"
#include "vtkPicker.h"
31
#include "vtkAreaPicker.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
32
#include "vtkProp3DCollection.h"
33
34
35
#include "vtkRenderWindow.h"
#include "vtkTimerLog.h"
#include "vtkVolume.h"
36
#include "vtkPropCollection.h"
37
38
39
#include "vtkIdentColoredPainter.h"
#include "vtkPainterPolyDataMapper.h"
#include "vtkPolyDataPainter.h"
Brad King's avatar
Brad King committed
40

41
vtkCxxRevisionMacro(vtkRenderer, "1.235");
42
43

vtkCxxSetObjectMacro(vtkRenderer, IdentPainter, vtkIdentColoredPainter);
44
45
46
47
48

//----------------------------------------------------------------------------
// Needed when we don't use the vtkStandardNewMacro.
vtkInstantiatorNewMacro(vtkRenderer);
//----------------------------------------------------------------------------
Brad King's avatar
Brad King committed
49

Ken Martin's avatar
Ken Martin committed
50
// Create a vtkRenderer with a black background, a white ambient light, 
51
52
// two-sided lighting turned on, a viewport of (0,0,1,1), and backface culling
// turned off.
Ken Martin's avatar
Ken Martin committed
53
vtkRenderer::vtkRenderer()
Ken Martin's avatar
Ken Martin committed
54
{
55
  this->PickedProp   = NULL;
Ken Martin's avatar
Ken Martin committed
56
57
58
59
60
61
  this->ActiveCamera = NULL;

  this->Ambient[0] = 1;
  this->Ambient[1] = 1;
  this->Ambient[2] = 1;

62
  this->AllocatedRenderTime = 100;
63
  this->TimeFactor = 1.0;
Ken Martin's avatar
Ken Martin committed
64
  
Ken Martin's avatar
Ken Martin committed
65
  this->CreatedLight = NULL;
66
  this->AutomaticLightCreation = 1;
Ken Martin's avatar
Ken Martin committed
67
  
68
69
70
  this->TwoSidedLighting        = 1;
  this->BackingStore            = 0;
  this->BackingImage            = NULL;
71
72
  this->BackingStoreSize[0]     = -1;
  this->BackingStoreSize[1]     = -1;  
73
  this->LastRenderTimeInSeconds = -1.0;
74
  
75
  this->RenderWindow = NULL;
76
77
  this->Lights  =  vtkLightCollection::New();
  this->Actors  =  vtkActorCollection::New();
78
  this->Volumes = vtkVolumeCollection::New();
Ken Martin's avatar
Ken Martin committed
79

80
81
  this->LightFollowCamera = 1;

82
  this->NumberOfPropsRendered = 0;
83
84

  this->PropArray                = NULL;
85
86
87
88
  this->PropArrayCount = 0;

  this->PathArray = NULL;
  this->PathArrayCount = 0;
89

90
  this->Layer                    = 0;
91
92
93
94
95
96
97
98

  this->ComputedVisiblePropBounds[0] = VTK_DOUBLE_MAX;
  this->ComputedVisiblePropBounds[1] = -VTK_DOUBLE_MAX;
  this->ComputedVisiblePropBounds[2] = VTK_DOUBLE_MAX;
  this->ComputedVisiblePropBounds[3] = -VTK_DOUBLE_MAX;
  this->ComputedVisiblePropBounds[4] = VTK_DOUBLE_MAX;
  this->ComputedVisiblePropBounds[5] = -VTK_DOUBLE_MAX;

99
  this->Interactive              = 1;
100
  this->Cullers = vtkCullerCollection::New();  
101
102
  vtkFrustumCoverageCuller *cull = vtkFrustumCoverageCuller::New();
  this->Cullers->AddItem(cull);
103
  cull->Delete();  
104
105
106
  
  // a value of 0 indicates it is uninitialized
  this->NearClippingPlaneTolerance = 0;
107
108

  this->Erase = 1;
Kenneth Moreland's avatar
   
Kenneth Moreland committed
109
  this->Draw = 1;
110
111
112
113
114

  this->SelectMode = vtkRenderer::NOT_SELECTING;
  this->SelectConst = 1;
  this->PropsSelectedFrom = NULL;
  this->PropsSelectedFromCount = 0;
115
  this->IdentPainter = NULL;
116
117
118
119
120
  
  this->UseDepthPeeling=0;
  this->OcclusionRatio=0.0;
  this->MaximumNumberOfPeels=4;
  this->LastRenderingUsedDepthPeeling=0;
Ken Martin's avatar
Ken Martin committed
121
122
}

Ken Martin's avatar
Ken Martin committed
123
124
vtkRenderer::~vtkRenderer()
{
125
126
  this->SetRenderWindow( NULL );
  
127
128
129
130
131
132
133
134
135
136
137
138
  if (this->ActiveCamera)
    {
    this->ActiveCamera->UnRegister(this);
    this->ActiveCamera = NULL;
    }

  if (this->CreatedLight)
    {
    this->CreatedLight->UnRegister(this);
    this->CreatedLight = NULL;
    }

Bill Lorensen's avatar
Bill Lorensen committed
139
140
141
142
  if (this->BackingImage)
    {
    delete [] this->BackingImage;
    }
143
  
Ken Martin's avatar
Ken Martin committed
144
145
  this->Actors->Delete();
  this->Actors = NULL;
146
147
  this->Volumes->Delete();
  this->Volumes = NULL;
Ken Martin's avatar
Ken Martin committed
148
149
  this->Lights->Delete();
  this->Lights = NULL;
150
151
  this->Cullers->Delete();
  this->Cullers = NULL;
152
153
154
155
156
157
158

  if ( this->PropsSelectedFrom)
    {
    delete [] this->PropsSelectedFrom;
    this->PropsSelectedFrom = NULL;
    }
  this->PropsSelectedFromCount = 0;
159
160
161
  if (this->IdentPainter)
    {
    this->IdentPainter->Delete();
Dave Demarle's avatar
Dave Demarle committed
162
    this->IdentPainter = NULL;
163
164
    }

Ken Martin's avatar
Ken Martin committed
165
166
}

167
168
// return the correct type of Renderer 
vtkRenderer *vtkRenderer::New()
169
170
{ 
  // First try to create the object from the vtkObjectFactory
Ken Martin's avatar
Ken Martin committed
171
172
  vtkObject* ret = vtkGraphicsFactory::CreateInstance("vtkRenderer");
  return (vtkRenderer *)ret;
173
174
}

Ken Martin's avatar
Ken Martin committed
175
176
177
// Concrete render method.
void vtkRenderer::Render(void)
{
178
179
180
  double   t1, t2;
  int      i;
  vtkProp  *aProp;
181
  int *size;
182

Kenneth Moreland's avatar
   
Kenneth Moreland committed
183
184
185
186
187
188
189
  // If Draw is not on, ignore the render.
  if (!this->Draw)
    {
    vtkDebugMacro("Ignoring render because Draw is off.");
    return;
    }

190
  t1 = vtkTimerLog::GetUniversalTime();
191

Ken Martin's avatar
Ken Martin committed
192
  this->InvokeEvent(vtkCommand::StartEvent,NULL);
Ken Martin's avatar
Ken Martin committed
193

194
195
  size = this->RenderWindow->GetSize();
  
Ken Martin's avatar
Ken Martin committed
196
197
198
199
  // if backing store is on and we have a stored image
  if (this->BackingStore && this->BackingImage &&
      this->MTime < this->RenderTime &&
      this->ActiveCamera->GetMTime() < this->RenderTime &&
200
201
202
      this->RenderWindow->GetMTime() < this->RenderTime &&
      this->BackingStoreSize[0] == size[0] &&
      this->BackingStoreSize[1] == size[1])      
Ken Martin's avatar
Ken Martin committed
203
204
205
206
207
    {
    int mods = 0;
    vtkLight *light;
    
    // now we just need to check the lights and actors
Ken Martin's avatar
Ken Martin committed
208
209
210
    vtkCollectionSimpleIterator sit;
    for(this->Lights->InitTraversal(sit); 
        (light = this->Lights->GetNextLight(sit)); )
Ken Martin's avatar
Ken Martin committed
211
212
      {
      if (light->GetSwitch() && 
213
214
215
          light->GetMTime() > this->RenderTime)
        {
        mods = 1;
216
        goto completed_mod_check;
217
        }
Ken Martin's avatar
Ken Martin committed
218
      }
Ken Martin's avatar
Ken Martin committed
219
220
221
    vtkCollectionSimpleIterator pit;
    for (this->Props->InitTraversal(pit); 
         (aProp = this->Props->GetNextProp(pit)); )
Ken Martin's avatar
Ken Martin committed
222
223
      {
      // if it's invisible, we can skip the rest 
Ken Martin's avatar
Ken Martin committed
224
      if (aProp->GetVisibility())
225
226
227
228
        {
        if (aProp->GetRedrawMTime() > this->RenderTime)
          {
          mods = 1;
229
          goto completed_mod_check;
230
231
          }
        }
Ken Martin's avatar
Ken Martin committed
232
233
      }
    
234
235
    completed_mod_check:

Ken Martin's avatar
Ken Martin committed
236
237
    if (!mods)
      {
Bill Lorensen's avatar
Bill Lorensen committed
238
      int rx1, ry1, rx2, ry2;
Ken Martin's avatar
Ken Martin committed
239
240
241
      
      // backing store should be OK, lets use it
      // calc the pixel range for the renderer
Bill Lorensen's avatar
Bill Lorensen committed
242
243
244
245
246
      rx1 = (int)(this->Viewport[0]*(this->RenderWindow->GetSize()[0] - 1));
      ry1 = (int)(this->Viewport[1]*(this->RenderWindow->GetSize()[1] - 1));
      rx2 = (int)(this->Viewport[2]*(this->RenderWindow->GetSize()[0] - 1));
      ry2 = (int)(this->Viewport[3]*(this->RenderWindow->GetSize()[1] - 1));
      this->RenderWindow->SetPixelData(rx1,ry1,rx2,ry2,this->BackingImage,0);
Ken Martin's avatar
Ken Martin committed
247
      this->InvokeEvent(vtkCommand::EndEvent,NULL);
Ken Martin's avatar
Ken Martin committed
248
249
250
      return;
      }
    }
251
252
253
254
255
256
257
258
259

  // Create the initial list of visible props
  // This will be passed through AllocateTime(), where
  // a time is allocated for each prop, and the list
  // maybe re-ordered by the cullers. Also create the
  // sublists for the props that need ray casting, and
  // the props that need to be rendered into an image.
  // Fill these in later (in AllocateTime) - get a 
  // count of them there too
260
261
  if ( this->Props->GetNumberOfItems() > 0 )
    {
262
    this->PropArray = new vtkProp *[this->Props->GetNumberOfItems()];
263
    }
Will Schroeder's avatar
Will Schroeder committed
264
265
266
267
  else
    {
    this->PropArray = NULL;
    }
268

269
  this->PropArrayCount = 0;
Ken Martin's avatar
Ken Martin committed
270
  vtkCollectionSimpleIterator pit;
271
  for ( this->Props->InitTraversal(pit); 
Andy Cedilnik's avatar
Andy Cedilnik committed
272
        (aProp = this->Props->GetNextProp(pit)); )
273
274
275
276
277
278
    {
    if ( aProp->GetVisibility() )
      {
      this->PropArray[this->PropArrayCount++] = aProp;
      }
    }
Ken Martin's avatar
Ken Martin committed
279
  
280
281
282
283
  if ( this->PropArrayCount == 0 )
    {
    vtkDebugMacro( << "There are no visible props!" );
    }
284
285
  else
    {
286
    // Call all the culling methods to set allocated time
287
    // for each prop and re-order the prop list if desired
288

289
290
    this->AllocateTime();
    }
291

Ken Martin's avatar
Ken Martin committed
292
293
294
  // do the render library specific stuff
  this->DeviceRender();

295
296
297
298
299
300
301
302
  // If we aborted, restore old estimated times
  // Setting the allocated render time to zero also sets the 
  // estimated render time to zero, so that when we add back
  // in the old value we have set it correctly.
  if ( this->RenderWindow->GetAbortRender() )
    {
    for ( i = 0; i < this->PropArrayCount; i++ )
      {
303
      this->PropArray[i]->RestoreEstimatedRenderTime();
304
305
306
      }
    }

307
308
  // Clean up the space we allocated before. If the PropArray exists,
  // they all should exist
309
  if ( this->PropArray)
310
311
    {
    delete [] this->PropArray;
312
    this->PropArray                = NULL;
313
    }
314

Ken Martin's avatar
Ken Martin committed
315
316
  if (this->BackingStore)
    {
Bill Lorensen's avatar
Bill Lorensen committed
317
318
319
320
    if (this->BackingImage)
      {
      delete [] this->BackingImage;
      }
Ken Martin's avatar
Ken Martin committed
321
    
Bill Lorensen's avatar
Bill Lorensen committed
322
    int rx1, ry1, rx2, ry2;
Ken Martin's avatar
Ken Martin committed
323
324
325
    
    // backing store should be OK, lets use it
    // calc the pixel range for the renderer
326
327
328
329
    rx1 = (int)(this->Viewport[0]*(size[0] - 1));
    ry1 = (int)(this->Viewport[1]*(size[1] - 1));
    rx2 = (int)(this->Viewport[2]*(size[0] - 1));
    ry2 = (int)(this->Viewport[3]*(size[1] - 1));
Bill Lorensen's avatar
Bill Lorensen committed
330
    this->BackingImage = this->RenderWindow->GetPixelData(rx1,ry1,rx2,ry2,0);
331
332
    this->BackingStoreSize[0] = size[0];
    this->BackingStoreSize[1] = size[1];
Ken Martin's avatar
Ken Martin committed
333
    }
334
    
335

336
337
338
339
340
341
342
  // If we aborted, do not record the last render time.
  // Lets play around with determining the acuracy of the 
  // EstimatedRenderTimes.  We can try to adjust for bad 
  // estimates with the TimeFactor.
  if ( ! this->RenderWindow->GetAbortRender() )
    {
    // Measure the actual RenderTime
343
    t2 = vtkTimerLog::GetUniversalTime();
Ken Martin's avatar
Ken Martin committed
344
    this->LastRenderTimeInSeconds = (double) (t2 - t1);
345

346
    if (this->LastRenderTimeInSeconds == 0.0)
347
      {
348
      this->LastRenderTimeInSeconds = 0.0001;
349
      }
350
    this->TimeFactor = this->AllocatedRenderTime/this->LastRenderTimeInSeconds;
351
352
    }
}
353

354
355
// ----------------------------------------------------------------------------
// Description:
356
357
// Render translucent polygonal geometry. Default implementation just call
// UpdateTranslucentPolygonalGeometry().
358
359
// Subclasses of vtkRenderer that can deal with depth peeling must
// override this method.
360
void vtkRenderer::DeviceRenderTranslucentPolygonalGeometry()
361
{
362
363
  // Have to be set before a call to UpdateTranslucentPolygonalGeometry()
  // because UpdateTranslucentPolygonalGeometry() will eventually call
364
365
366
  // vtkOpenGLActor::Render() that uses this flag.
  this->LastRenderingUsedDepthPeeling=0;
  
367
  this->UpdateTranslucentPolygonalGeometry();
368
369
370
}

// ----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
371
double vtkRenderer::GetAllocatedRenderTime()
372
373
374
375
{
  return this->AllocatedRenderTime;
}

Ken Martin's avatar
Ken Martin committed
376
double vtkRenderer::GetTimeFactor()
377
378
{
  return this->TimeFactor;
Ken Martin's avatar
Ken Martin committed
379
380
}

381
// Ask active camera to load its view matrix.
382
int vtkRenderer::UpdateCamera ()
383
384
385
386
387
{
  if (!this->ActiveCamera)
    {
    vtkDebugMacro(<< "No cameras are on, creating one.");
    // the get method will automagically create a camera
388
389
390
391
392
393
    // and reset it since one hasn't been specified yet.
    // If is very unlikely that this can occur - if this
    // renderer is part of a vtkRenderWindow, the camera
    // will already have been created as part of the
    // DoStereoRender() method.
    this->GetActiveCameraAndResetIfCreated();
394
395
396
    }

  // update the viewing transformation
397
398
  this->ActiveCamera->Render((vtkRenderer *)this); 
  
399
400
401
  return 1;
}

402
int vtkRenderer::UpdateLightsGeometryToFollowCamera()
403
404
405
406
407
{
  vtkCamera *camera;
  vtkLight *light;
  vtkMatrix4x4 *lightMatrix;

408
409
410
  // only update the light's geometry if this Renderer is tracking
  // this lights.  That allows one renderer to view the lights that
  // another renderer is setting up.
411
  camera = this->GetActiveCameraAndResetIfCreated();
412
413
  lightMatrix = camera->GetCameraLightTransformMatrix();

Ken Martin's avatar
Ken Martin committed
414
415
416
  vtkCollectionSimpleIterator sit;
  for(this->Lights->InitTraversal(sit); 
      (light = this->Lights->GetNextLight(sit)); )
417
418
419
    {
    if (light->LightTypeIsSceneLight())
      {
420
421
422
      // Do nothing. Don't reset the transform matrix because applications
      // may have set a custom matrix. Only reset the transform matrix in
      // vtkLight::SetLightTypeToSceneLight()
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
      }
    else if (light->LightTypeIsHeadlight())
      {
      // update position and orientation of light to match camera.
      light->SetPosition(camera->GetPosition());
      light->SetFocalPoint(camera->GetFocalPoint());
      }
    else if (light->LightTypeIsCameraLight())
      {
      light->SetTransformMatrix(lightMatrix);
      }
    else 
      {
      vtkErrorMacro(<< "light has unknown light type");
      }
    }
  return 1;
}

int vtkRenderer::UpdateLightGeometry()
{
444
  if (this->LightFollowCamera) 
445
    {
446
447
448
    // only update the light's geometry if this Renderer is tracking
    // this lights.  That allows one renderer to view the lights that
    // another renderer is setting up.
449
450
  
    return this->UpdateLightsGeometryToFollowCamera();
451
    }
452
  
453
454
455
  return 1;
}

456
457
458
// Do all outer culling to set allocated time for each prop.
// Possibly re-order the actor list.
void vtkRenderer::AllocateTime()
459
{
460
  int          initialized = 0;
Ken Martin's avatar
Ken Martin committed
461
462
  double        renderTime;
  double        totalTime;
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  int          i;
  vtkCuller    *aCuller;
  vtkProp      *aProp;

  // Give each of the cullers a chance to modify allocated rendering time
  // for the entire set of props. Each culler returns the total time given
  // by AllocatedRenderTime for all props. Each culler is required to
  // place any props that have an allocated render time of 0.0 
  // at the end of the list. The PropArrayCount value that is
  // returned is the number of non-zero, visible actors.
  // Some cullers may do additional sorting of the list (by distance,
  // importance, etc).
  //
  // The first culler will initialize all the allocated render times. 
  // Any subsequent culling will multiply the new render time by the 
  // existing render time for an actor.
479

480
  totalTime = this->PropArrayCount;
481
  this->ComputeAspect();
482

483
484
485
486
487
  // It is very likely that the culler framework will call our
  // GetActiveCamera (say, to get the view frustrum planes for example).
  // This does not reset the camera anymore. If no camera has been 
  // created though, we want it not only to be created but also reset
  // so that it behaves nicely for people who never bother with the camera
488
489
490
491
  // (i.e. neither call GetActiveCamera or ResetCamera). Of course,
  // it is very likely that the camera has already been created 
  // (guaranteed if this renderer is being rendered as part of a
  // vtkRenderWindow).
492
493
494

  if ( this->Cullers->GetNumberOfItems())
    {
495
    this->GetActiveCameraAndResetIfCreated();
496
497
    }

498
499
  vtkCollectionSimpleIterator sit;
  for (this->Cullers->InitTraversal(sit);
Ken Martin's avatar
Ken Martin committed
500
       (aCuller=this->Cullers->GetNextCuller(sit));)
501
    {
502
    totalTime =
503
      aCuller->Cull((vtkRenderer *)this, 
504
505
                    this->PropArray, this->PropArrayCount,
                    initialized );
506
507
    }

508
509
  // loop through all props and set the AllocatedRenderTime
  for ( i = 0; i < this->PropArrayCount; i++ )
510
    {
511
512
513
514
    aProp = this->PropArray[i];

    // If we don't have an outer cull method in any of the cullers,
    // then the allocated render time has not yet been initialized
515
    renderTime = (initialized)?(aProp->GetRenderTimeMultiplier()):(1.0);
516
517
518
519
520
521

    // We need to divide by total time so that the total rendering time
    // (all prop's AllocatedRenderTime added together) would be equal
    // to the renderer's AllocatedRenderTime.
    aProp->
      SetAllocatedRenderTime(( renderTime / totalTime ) * 
522
                             this->AllocatedRenderTime, 
523
                             this );  
524
525
    }
}
526

527
528
529
530
531
// Ask actors to render themselves. As a side effect will cause 
// visualization network to update.
int vtkRenderer::UpdateGeometry()
{
  int        i;
532
  
533
  this->NumberOfPropsRendered = 0;
534

535
536
  if ( this->PropArrayCount == 0 ) 
    {
537
    this->InvokeEvent(vtkCommand::EndEvent,NULL);
538
539
    return 0;
    }
540

541
542
543
  if (this->SelectMode != vtkRenderer::NOT_SELECTING)
    {
    //we are doing a visible polygon selection instead of a normal render
544
545
546
547
548
549
550
551
552
    int ret = this->UpdateGeometryForSelection();

    this->InvokeEvent(vtkCommand::EndEvent,NULL);
    this->RenderTime.Modified();

    vtkDebugMacro( << "Rendered " << 
                   this->NumberOfPropsRendered << " actors" );

    return ret;
553
554
    }

555
556
557
558
559
  // We can render everything because if it was
  // not visible it would not have been put in the
  // list in the first place, and if it was allocated
  // no time (culled) it would have been removed from
  // the list
560

561
  // loop through props and give them a chance to 
562
563
  // render themselves as opaque geometry
  for ( i = 0; i < this->PropArrayCount; i++ )
564
    { 
565
    this->NumberOfPropsRendered += 
566
567
      this->PropArray[i]->RenderOpaqueGeometry(this);
    }
568
  
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  // do the render library specific stuff about translucent polygonal geometry.
  // As it can be expensive, do a quick check if we can skip this step
  int hasTranslucentPolygonalGeometry=0;
  for ( i = 0; !hasTranslucentPolygonalGeometry && i < this->PropArrayCount;
        i++ )
    { 
    hasTranslucentPolygonalGeometry=
      this->PropArray[i]->HasTranslucentPolygonalGeometry();
    }
  if(hasTranslucentPolygonalGeometry)
    {
    this->DeviceRenderTranslucentPolygonalGeometry();
    }
  
  
584
    
585
586
587
588
589
590
591
592
  // loop through props and give them a chance to 
  // render themselves as volumetric geometry.
  for ( i = 0; i < this->PropArrayCount; i++ )
    {
    this->NumberOfPropsRendered += 
      this->PropArray[i]->RenderVolumetricGeometry(this);
    }
  
593
594
595
596
  // loop through props and give them a chance to 
  // render themselves as an overlay (or underlay)
  for ( i = 0; i < this->PropArrayCount; i++ )
    {
597
    this->NumberOfPropsRendered += 
598
599
600
601
602
603
      this->PropArray[i]->RenderOverlay(this);
    }

  this->InvokeEvent(vtkCommand::EndEvent,NULL);
  this->RenderTime.Modified();

604
  vtkDebugMacro( << "Rendered " << 
605
                    this->NumberOfPropsRendered << " actors" );
606

607
  return  this->NumberOfPropsRendered;
608
609
}

610
611
// ----------------------------------------------------------------------------
// Description:
612
// Ask all props to update and draw any translucent polygonal
613
614
615
616
// geometry. This includes both vtkActors and vtkVolumes
// Return the number of rendered props.
// It is called once with alpha blending technique. It is called multiple
// times with depth peeling technique.
617
int vtkRenderer::UpdateTranslucentPolygonalGeometry()
618
619
620
621
622
623
{
  int result=0;
  // loop through props and give them a chance to 
  // render themselves as translucent geometry
  for (int i = 0; i < this->PropArrayCount; i++ )
    {
624
    int rendered=this->PropArray[i]->RenderTranslucentPolygonalGeometry(this);
625
626
627
628
629
630
631
    this->NumberOfPropsRendered += rendered;
    result+=rendered;
    }
  return result;
}

// ----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
632
633
634
635
636
vtkWindow *vtkRenderer::GetVTKWindow()
{
  return this->RenderWindow;
}

Ken Martin's avatar
Ken Martin committed
637
// Specify the camera to use for this renderer.
Ken Martin's avatar
Ken Martin committed
638
void vtkRenderer::SetActiveCamera(vtkCamera *cam)
Ken Martin's avatar
Ken Martin committed
639
{
640
  if (this->ActiveCamera == cam)
Ken Martin's avatar
Ken Martin committed
641
    {
642
    return;
Ken Martin's avatar
Ken Martin committed
643
    }
644
645
646
647
648
649
650
651
652
653
654
655
656

  if (this->ActiveCamera)
    {
    this->ActiveCamera->UnRegister(this);
    this->ActiveCamera = NULL;
    }
  if (cam)
    {
    cam->Register(this);
    }

  this->ActiveCamera = cam;
  this->Modified();
Ken Martin's avatar
Ken Martin committed
657
658
}

659
//----------------------------------------------------------------------------
660
661
662
663
664
vtkCamera* vtkRenderer::MakeCamera()
{
  return vtkCamera::New();
}
  
665
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
666
vtkCamera *vtkRenderer::GetActiveCamera()
667
{
Ken Martin's avatar
Ken Martin committed
668
669
  if ( this->ActiveCamera == NULL )
    {
670
    vtkCamera *cam = this->MakeCamera();
Charles Law's avatar
Charles Law committed
671
672
    this->SetActiveCamera(cam);
    cam->Delete();
673
674
675
    // The following line has been commented out as it has a lot of
    // side effects (like computing the bounds of all props, which will
    // eventually call UpdateInformation() on data objects, etc).
676
    // Instead, the rendering code has been updated to internally use
677
    // GetActiveCameraAndResetIfCreated which will reset the camera
678
679
    // if it gets created
    // this->ResetCamera();
Ken Martin's avatar
Ken Martin committed
680
681
    }

682
683
684
  return this->ActiveCamera;
}

685
//----------------------------------------------------------------------------
686
vtkCamera *vtkRenderer::GetActiveCameraAndResetIfCreated()
687
688
689
690
691
692
693
694
695
{
  if (this->ActiveCamera == NULL)
    {
    this->GetActiveCamera();
    this->ResetCamera();
    }
  return this->ActiveCamera;
}

696
697
698
//----------------------------------------------------------------------------
void vtkRenderer::AddActor(vtkProp* p)
{
699
  this->AddViewProp(p);
700
701
702
703
704
}

//----------------------------------------------------------------------------
void vtkRenderer::AddVolume(vtkProp* p)
{
705
  this->AddViewProp(p);
706
707
708
709
710
711
}

//----------------------------------------------------------------------------
void vtkRenderer::RemoveActor(vtkProp* p)
{
  this->Actors->RemoveItem(p);
712
  this->RemoveViewProp(p);
713
714
715
716
717
718
}

//----------------------------------------------------------------------------
void vtkRenderer::RemoveVolume(vtkProp* p)
{
  this->Volumes->RemoveItem(p);
719
  this->RemoveViewProp(p);
720
721
}

Will Schroeder's avatar
Will Schroeder committed
722
// Add a light to the list of lights.
Will Schroeder's avatar
Will Schroeder committed
723
void vtkRenderer::AddLight(vtkLight *light)
Ken Martin's avatar
Ken Martin committed
724
{
725
  this->Lights->AddItem(light);
Ken Martin's avatar
Ken Martin committed
726
727
}

Ken Martin's avatar
Ken Martin committed
728
729
// look through the props and get all the actors
vtkActorCollection *vtkRenderer::GetActors()
Ken Martin's avatar
Ken Martin committed
730
{
Ken Martin's avatar
Ken Martin committed
731
732
733
734
735
  vtkProp *aProp;
  
  // clear the collection first
  this->Actors->RemoveAllItems();
  
Ken Martin's avatar
Ken Martin committed
736
737
738
  vtkCollectionSimpleIterator pit;
  for (this->Props->InitTraversal(pit); 
       (aProp = this->Props->GetNextProp(pit)); )
Ken Martin's avatar
Ken Martin committed
739
740
741
742
    {
    aProp->GetActors(this->Actors);
    }
  return this->Actors;
Ken Martin's avatar
Ken Martin committed
743
744
}

745
// look through the props and get all the volumes
Ken Martin's avatar
Ken Martin committed
746
vtkVolumeCollection *vtkRenderer::GetVolumes()
747
{
Ken Martin's avatar
Ken Martin committed
748
749
750
751
752
  vtkProp *aProp;
  
  // clear the collection first
  this->Volumes->RemoveAllItems();
  
Ken Martin's avatar
Ken Martin committed
753
754
755
  vtkCollectionSimpleIterator pit;
  for (this->Props->InitTraversal(pit); 
       (aProp = this->Props->GetNextProp(pit)); )
Ken Martin's avatar
Ken Martin committed
756
757
758
759
    {
    aProp->GetVolumes(this->Volumes);
    }
  return this->Volumes;
760
761
}

Will Schroeder's avatar
Will Schroeder committed
762
// Remove a light from the list of lights.
Will Schroeder's avatar
Will Schroeder committed
763
void vtkRenderer::RemoveLight(vtkLight *light)
Ken Martin's avatar
Ken Martin committed
764
{
765
  this->Lights->RemoveItem(light);
Ken Martin's avatar
Ken Martin committed
766
767
}

768
769
770
771
772
773
// Remove all lights from the list of lights.
void vtkRenderer::RemoveAllLights()
{
  this->Lights->RemoveAllItems();
}

774
775
776
// Add an culler to the list of cullers.
void vtkRenderer::AddCuller(vtkCuller *culler)
{
777
  this->Cullers->AddItem(culler);
778
779
780
781
782
}

// Remove an actor from the list of cullers.
void vtkRenderer::RemoveCuller(vtkCuller *culler)
{
783
  this->Cullers->RemoveItem(culler);
784
785
}

786
787
788
789
790
vtkLight *vtkRenderer::MakeLight()
{
  return vtkLight::New();
}

791
void vtkRenderer::CreateLight(void)
Ken Martin's avatar
Ken Martin committed
792
{
793
794
795
796
797
  if ( !this->AutomaticLightCreation )
    {
    return;
    }

798
799
800
801
802
803
  if (this->CreatedLight)
    {
    this->CreatedLight->UnRegister(this);
    this->CreatedLight = NULL;
    }

Ken Martin's avatar
Ken Martin committed
804
805
  // I do not see why UnRegister is used on CreatedLight, but lets be
  // consistent.
806
  vtkLight *l = this->MakeLight();
Charles Law's avatar
Charles Law committed
807
  this->CreatedLight = l;
808
  this->CreatedLight->Register(this);
809
  this->AddLight(this->CreatedLight);
Charles Law's avatar
Charles Law committed
810
  l->Delete();
811
812
813
814
815

  this->CreatedLight->SetLightTypeToHeadlight();

  // set these values just to have a good default should LightFollowCamera
  // be turned off.
816
817
  this->CreatedLight->SetPosition(this->GetActiveCamera()->GetPosition());
  this->CreatedLight->SetFocalPoint(this->GetActiveCamera()->GetFocalPoint());
Ken Martin's avatar
Ken Martin committed
818
819
}

Michalel Halle's avatar
Michalel Halle committed
820
// Compute the bounds of the visible props
Ken Martin's avatar
Ken Martin committed
821
void vtkRenderer::ComputeVisiblePropBounds( double allBounds[6] )
822
{
823
  vtkProp    *prop;
Ken Martin's avatar
Ken Martin committed
824
  double      *bounds;
825
  int        nothingVisible=1;
826

Ken Martin's avatar
Ken Martin committed
827
828
  allBounds[0] = allBounds[2] = allBounds[4] = VTK_DOUBLE_MAX;
  allBounds[1] = allBounds[3] = allBounds[5] = -VTK_DOUBLE_MAX;
829
  
830
  // loop through all props
Ken Martin's avatar
Ken Martin committed
831
832
833
  vtkCollectionSimpleIterator pit;
  for (this->Props->InitTraversal(pit); 
       (prop = this->Props->GetNextProp(pit)); )
834
    {
835
    // if it's invisible, or has no geometry, we can skip the rest 
836
    if ( prop->GetVisibility() )
837
      {
838
      bounds = prop->GetBounds();
839
      // make sure we haven't got bogus bounds
Ken Martin's avatar
Ken Martin committed
840
      if ( bounds != NULL && vtkMath::AreBoundsInitialized(bounds))
841
842
        {
        nothingVisible = 0;
Ken Martin's avatar
Ken Martin committed
843
        
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
        if (bounds[0] < allBounds[0])
          {
          allBounds[0] = bounds[0]; 
          }
        if (bounds[1] > allBounds[1])
          {
          allBounds[1] = bounds[1]; 
          }
        if (bounds[2] < allBounds[2])
          {
          allBounds[2] = bounds[2]; 
          }
        if (bounds[3] > allBounds[3])
          {
          allBounds[3] = bounds[3]; 
          }
        if (bounds[4] < allBounds[4])
          {
          allBounds[4] = bounds[4]; 
          }
        if (bounds[5] > allBounds[5])
          {
          allBounds[5] = bounds[5]; 
          }
        }//not bogus
869
870
871
      }
    }
  
872
873
  if ( nothingVisible )
    {
Ken Martin's avatar
Ken Martin committed
874
    vtkMath::UninitializeBounds(allBounds);
875
876
877
878
879
    vtkDebugMacro(<< "Can't compute bounds, no 3D props are visible");
    return;
    }
}

Ken Martin's avatar
Ken Martin committed
880
double *vtkRenderer::ComputeVisiblePropBounds()
881
882
883
884
885
  {
  this->ComputeVisiblePropBounds(this->ComputedVisiblePropBounds);
  return this->ComputedVisiblePropBounds;
  }

886
887
888
889
890
891
// Automatically set up the camera based on the visible actors.
// The camera will reposition itself to view the center point of the actors,
// and move along its initial view plane normal (i.e., vector defined from 
// camera position to focal point) so that all of the actors can be seen.
void vtkRenderer::ResetCamera()
{
Ken Martin's avatar
Ken Martin committed
892
  double      allBounds[6];
893
894
895

  this->ComputeVisiblePropBounds( allBounds );

Ken Martin's avatar
Ken Martin committed
896
  if (!vtkMath::AreBoundsInitialized(allBounds))
897
    {
898
    vtkDebugMacro( << "Cannot reset camera!" );
899
    }
900
901
902
903
  else
    {
    this->ResetCamera(allBounds);
    }
Charles Law's avatar
Charles Law committed
904
905
906
907

  // Here to let parallel/distributed compositing intercept 
  // and do the right thing.
  this->InvokeEvent(vtkCommand::ResetCameraEvent,this);
908
909
}

910
911
912
913
// Automatically set the clipping range of the camera based on the
// visible actors
void vtkRenderer::ResetCameraClippingRange()
{
Ken Martin's avatar
Ken Martin committed
914
  double      allBounds[6];
915
916
917

  this->ComputeVisiblePropBounds( allBounds );

Ken Martin's avatar
Ken Martin committed
918
  if (!vtkMath::AreBoundsInitialized(allBounds))
919
    {
920
    vtkDebugMacro( << "Cannot reset camera clipping range!" );
921
    }
922
923
924
925
  else
    {
    this->ResetCameraClippingRange(allBounds);
    }
Charles Law's avatar
Charles Law committed
926
927
928
929

  // Here to let parallel/distributed compositing intercept 
  // and do the right thing.
  this->InvokeEvent(vtkCommand::ResetCameraClippingRangeEvent,this);
930
931
932
}


933
934
935
936
// Automatically set up the camera based on a specified bounding box
// (xmin,xmax, ymin,ymax, zmin,zmax). Camera will reposition itself so
// that its focal point is the center of the bounding box, and adjust its
// distance and position to preserve its initial view plane normal 
937
// (i.e., vector defined from camera position to focal point). Note: if 
938
939
// the view plane is parallel to the view up axis, the view up axis will
// be reset to one of the three coordinate axes.
Ken Martin's avatar
Ken Martin committed
940
void vtkRenderer::ResetCamera(double bounds[6])
941
{
Ken Martin's avatar
Ken Martin committed
942
943
  double center[3];
  double distance;
944
  double vn[3], *vup;
945

946
  this->GetActiveCamera();
947
948
  if ( this->ActiveCamera != NULL )
    {
Will Schroeder's avatar
Will Schroeder committed
949
    this->ActiveCamera->GetViewPlaneNormal(vn);
950
951
952
    }
  else
    {
Ken Martin's avatar
Ken Martin committed
953
    vtkErrorMacro(<< "Trying to reset non-existant camera");
954
955
956
957
958
959
    return;
    }

  center[0] = (bounds[0] + bounds[1])/2.0;
  center[1] = (bounds[2] + bounds[3])/2.0;
  center[2] = (bounds[4] + bounds[5])/2.0;
960

961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
  double w1 = bounds[1] - bounds[0];
  double w2 = bounds[3] - bounds[2];
  double w3 = bounds[5] - bounds[4];
  w1 *= w1;
  w2 *= w2;
  w3 *= w3;
  double radius = w1 + w2 + w3;

  // If we have just a single point, pick a radius of 1.0
  radius = (radius==0)?(1.0):(radius);

  // compute the radius of the enclosing sphere
  radius = sqrt(radius)*0.5;

  // default so that the bounding sphere fits within the view fustrum
Mathieu Malaterre's avatar
Mathieu Malaterre committed
976

977
978
979
980
981
982
983
984
985
986
987
  // compute the distance from the intersection of the view frustum with the
  // bounding sphere. Basically in 2D draw a circle representing the bounding
  // sphere in 2D then draw a horizontal line going out from the center of
  // the circle. That is the camera view. Then draw a line from the camera
  // position to the point where it intersects the circle. (it will be tangent
  // to the circle at this point, this is important, only go to the tangent
  // point, do not draw all the way to the view plane). Then draw the radius
  // from the tangent point to the center of the circle. You will note that
  // this forms a right triangle with one side being the radius, another being
  // the target distance for the camera, then just find the target dist using
  // a sin.
Mathieu Malaterre's avatar
Mathieu Malaterre committed
988
  distance =
989
    radius/sin(this->ActiveCamera->GetViewAngle()*vtkMath::Pi()/360.0);
990

991
992
  // check view-up vector against view plane normal
  vup = this->ActiveCamera->GetViewUp();
993
  if ( fabs(vtkMath::Dot(vup,vn)) > 0.999 )
994
995
996
997
998
    {
    vtkWarningMacro(<<"Resetting view-up since view plane normal is parallel");
    this->ActiveCamera->SetViewUp(-vup[2], vup[0], vup[1]);
    }

999
  // update the camera
Ken Martin's avatar
Ken Martin committed
1000
  this->ActiveCamera->SetFocalPoint(center[0],center[1],center[2]);
Ken Martin's avatar
Ken Martin committed
1001
  this->ActiveCamera->SetPosition(center[0]+distance*vn[0],
1002
1003
                                  center[1]+distance*vn[1],
                                  center[2]+distance*vn[2]);
1004
1005

  this->ResetCameraClippingRange( bounds );
1006
1007

  // setup default parallel scale
1008
  this->ActiveCamera->SetParallelScale(radius);
1009
}
1010

1011
// Alternative version of ResetCamera(bounds[6]);
1012
1013
void vtkRenderer::ResetCamera(double xmin, double xmax,
                              double ymin, double ymax,
Ken Martin's avatar
Ken Martin committed
1014
                              double zmin, double zmax)
1015
{