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 "vtkCellArray.h"
23 #include "vtkLightKit.h"
24 #include "vtkNew.h"
25 #include "vtkOpenGLRenderer.h"
26 #include "vtkOpenGLTexture.h"
27 #include "vtkPLYReader.h"
28 #include "vtkPlaneSource.h"
29 #include "vtkPointData.h"
30 #include "vtkPolyDataMapper.h"
31 #include "vtkProperty.h"
32 #include "vtkRegressionTestImage.h"
33 #include "vtkRenderWindow.h"
34 #include "vtkRenderWindowInteractor.h"
35 #include "vtkRenderer.h"
36 #include "vtkShadowMapBakerPass.h"
37 #include "vtkTestUtilities.h"
38 #include "vtkTextureObject.h"
39 #include "vtkTimerLog.h"
40 
41 
42 //----------------------------------------------------------------------------
TestShadowMapBakerPass(int argc,char * argv[])43 int TestShadowMapBakerPass(int argc, char *argv[])
44 {
45   vtkNew<vtkActor> actor;
46   vtkNew<vtkRenderer> renderer;
47   vtkNew<vtkPolyDataMapper> mapper;
48   renderer->SetBackground(0.3, 0.4, 0.6);
49   vtkNew<vtkRenderWindow> renderWindow;
50   renderWindow->SetSize(600, 600);
51   renderWindow->AddRenderer(renderer);
52   renderer->AddActor(actor);
53   vtkNew<vtkRenderWindowInteractor>  iren;
54   iren->SetRenderWindow(renderWindow);
55   vtkNew<vtkLightKit> lightKit;
56   lightKit->AddLightsToRenderer(renderer);
57 
58   const char* fileName =
59     vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/dragon.ply");
60   vtkNew<vtkPLYReader> reader;
61   reader->SetFileName(fileName);
62   reader->Update();
63 
64   delete [] fileName;
65 
66   mapper->SetInputConnection(reader->GetOutputPort());
67   //mapper->SetInputConnection(norms->GetOutputPort());
68   actor->SetMapper(mapper);
69   actor->GetProperty()->SetAmbientColor(0.2, 0.2, 1.0);
70   actor->GetProperty()->SetDiffuseColor(1.0, 0.65, 0.7);
71   actor->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
72   actor->GetProperty()->SetSpecular(0.5);
73   actor->GetProperty()->SetDiffuse(0.7);
74   actor->GetProperty()->SetAmbient(0.5);
75   actor->GetProperty()->SetSpecularPower(20.0);
76   actor->GetProperty()->SetOpacity(1.0);
77   //actor->GetProperty()->SetRepresentationToWireframe();
78 
79   renderWindow->SetMultiSamples(0);
80 
81   vtkNew<vtkShadowMapBakerPass> bakerPass;
82 
83   // tell the renderer to use our render pass pipeline
84   vtkOpenGLRenderer *glrenderer =
85       vtkOpenGLRenderer::SafeDownCast(renderer);
86   glrenderer->SetPass(bakerPass);
87 
88   vtkNew<vtkTimerLog> timer;
89   timer->StartTimer();
90   renderWindow->Render();
91   timer->StopTimer();
92   double firstRender = timer->GetElapsedTime();
93   cerr << "baking time: " << firstRender << endl;
94 
95   // get a shadow map
96   vtkTextureObject *to = (*bakerPass->GetShadowMaps())[2];
97   // by default the textures have depth comparison on
98   // but for simple display we need to turn it off
99   to->SetDepthTextureCompare(false);
100 
101   // now render this texture so we can see the depth map
102   vtkNew<vtkActor> actor2;
103   vtkNew<vtkPolyDataMapper> mapper2;
104   vtkNew<vtkOpenGLTexture> texture;
105   texture->SetTextureObject(to);
106   actor2->SetTexture(texture);
107   actor2->SetMapper(mapper2);
108 
109   vtkNew<vtkPlaneSource> plane;
110   mapper2->SetInputConnection(plane->GetOutputPort());
111   renderer->RemoveActor(actor);
112   renderer->AddActor(actor2);
113   glrenderer->SetPass(nullptr);
114 
115   renderer->ResetCamera();
116   renderer->GetActiveCamera()->Zoom(2.0);
117   renderWindow->Render();
118 
119   int retVal = vtkRegressionTestImage( renderWindow );
120   if ( retVal == vtkRegressionTester::DO_INTERACTOR)
121   {
122     iren->Start();
123   }
124 
125   bakerPass->ReleaseGraphicsResources(renderWindow);
126   return !retVal;
127 }
128