1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4 
5   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6   All rights reserved.
7   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8 
9      This software is distributed WITHOUT ANY WARRANTY; without even
10      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11      PURPOSE.  See the above copyright notice for more information.
12 
13 =========================================================================*/
14 // test baking shadow maps
15 //
16 // The command line arguments are:
17 // -I        => run in interactive mode; unless this is used, the program will
18 //              not allow interaction and exit
19 
20 #include "vtkActor.h"
21 #include "vtkCamera.h"
22 #include "vtkCameraPass.h"
23 #include "vtkCellArray.h"
24 #include "vtkLight.h"
25 #include "vtkNew.h"
26 #include "vtkOpenGLRenderer.h"
27 #include "vtkOpenGLTexture.h"
28 #include "vtkPLYReader.h"
29 #include "vtkPlaneSource.h"
30 #include "vtkPolyDataMapper.h"
31 #include "vtkProperty.h"
32 #include "vtkRegressionTestImage.h"
33 #include "vtkRenderPassCollection.h"
34 #include "vtkRenderStepsPass.h"
35 #include "vtkRenderWindow.h"
36 #include "vtkRenderWindowInteractor.h"
37 #include "vtkRenderer.h"
38 #include "vtkSequencePass.h"
39 #include "vtkShadowMapBakerPass.h"
40 #include "vtkShadowMapPass.h"
41 #include "vtkTestUtilities.h"
42 #include "vtkTimerLog.h"
43 
44 //----------------------------------------------------------------------------
TestShadowMapPass(int argc,char * argv[])45 int TestShadowMapPass(int argc, char *argv[])
46 {
47   vtkNew<vtkRenderer> renderer;
48   renderer->SetBackground(0.3, 0.4, 0.6);
49   vtkNew<vtkRenderWindow> renderWindow;
50   renderWindow->SetSize(600, 600);
51   renderWindow->AddRenderer(renderer);
52   vtkNew<vtkRenderWindowInteractor>  iren;
53   iren->SetRenderWindow(renderWindow);
54 
55   vtkNew<vtkLight> light1;
56   light1->SetFocalPoint(0,0,0);
57   light1->SetPosition(0,1,0.2);
58   light1->SetColor(0.95,0.97,1.0);
59   light1->SetIntensity(0.8);
60   renderer->AddLight(light1);
61 
62   vtkNew<vtkLight> light2;
63   light2->SetFocalPoint(0,0,0);
64   light2->SetPosition(1.0,1.0,1.0);
65   light2->SetColor(1.0,0.8,0.7);
66   light2->SetIntensity(0.3);
67   renderer->AddLight(light2);
68 
69   const char* fileName =
70     vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/dragon.ply");
71   vtkNew<vtkPLYReader> reader;
72   reader->SetFileName(fileName);
73   reader->Update();
74 
75   delete [] fileName;
76 
77   vtkNew<vtkPolyDataMapper> mapper;
78   mapper->SetInputConnection(reader->GetOutputPort());
79 
80   vtkNew<vtkActor> actor;
81   actor->SetMapper(mapper);
82   actor->GetProperty()->SetAmbientColor(0.135, 0.2225, 0.3);
83   actor->GetProperty()->SetDiffuseColor(0.54, 0.89, 0.63);
84   actor->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
85   actor->GetProperty()->SetSpecular(0.51);
86   actor->GetProperty()->SetDiffuse(0.7);
87   actor->GetProperty()->SetAmbient(0.7);
88   actor->GetProperty()->SetSpecularPower(30.0);
89   actor->GetProperty()->SetOpacity(1.0);
90   renderer->AddActor(actor);
91   //actor->GetProperty()->SetRepresentationToWireframe();
92 
93   // add a plane
94   vtkNew<vtkPlaneSource> plane;
95   const double *plybounds = mapper->GetBounds();
96   plane->SetOrigin(-0.2, plybounds[2], -0.2);
97   plane->SetPoint1( 0.2, plybounds[2], -0.2);
98   plane->SetPoint2(-0.2, plybounds[2],  0.2);
99   vtkNew<vtkPolyDataMapper> planeMapper;
100   planeMapper->SetInputConnection(plane->GetOutputPort());
101   vtkNew<vtkActor> planeActor;
102   planeActor->SetMapper(planeMapper);
103   renderer->AddActor(planeActor);
104 
105   renderWindow->SetMultiSamples(0);
106 
107   vtkNew<vtkShadowMapPass> shadows;
108 
109   vtkNew<vtkSequencePass> seq;
110   vtkNew<vtkRenderPassCollection> passes;
111   passes->AddItem(shadows->GetShadowMapBakerPass());
112   passes->AddItem(shadows);
113   seq->SetPasses(passes);
114 
115   vtkNew<vtkCameraPass> cameraP;
116   cameraP->SetDelegatePass(seq);
117 
118   // tell the renderer to use our render pass pipeline
119   vtkOpenGLRenderer *glrenderer =
120     vtkOpenGLRenderer::SafeDownCast(renderer);
121   glrenderer->SetPass(cameraP);
122 
123   vtkNew<vtkTimerLog> timer;
124   timer->StartTimer();
125   renderWindow->Render();
126   timer->StopTimer();
127   double firstRender = timer->GetElapsedTime();
128   cerr << "first render time: " << firstRender << endl;
129 
130   timer->StartTimer();
131   int numRenders = 8;
132   for (int i = 0; i < numRenders; ++i)
133   {
134     renderer->GetActiveCamera()->Azimuth(80.0/numRenders);
135     renderer->GetActiveCamera()->Elevation(80.0/numRenders);
136     renderWindow->Render();
137   }
138   timer->StopTimer();
139   double elapsed = timer->GetElapsedTime();
140   cerr << "interactive render time: " << elapsed / numRenders << endl;
141   unsigned int numTris = reader->GetOutput()->GetPolys()->GetNumberOfCells();
142   cerr << "number of triangles: " <<  numTris << endl;
143   cerr << "triangles per second: " <<  numTris*(numRenders/elapsed) << endl;
144 
145   renderer->GetActiveCamera()->SetPosition(-0.2,0.2,1);
146   renderer->GetActiveCamera()->SetFocalPoint(0,0,0);
147   renderer->GetActiveCamera()->SetViewUp(0,1,0);
148   renderer->GetActiveCamera()->OrthogonalizeViewUp();
149   renderer->ResetCamera();
150   renderer->GetActiveCamera()->Zoom(2.5);
151   renderWindow->Render();
152 
153   int retVal = vtkRegressionTestImage( renderWindow );
154   if ( retVal == vtkRegressionTester::DO_INTERACTOR)
155   {
156     iren->Start();
157   }
158 
159   return !retVal;
160 }
161