1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestOptiXLights.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // This test verifies that lighting works as expected with optix.
16 // When advanced materials are exposed in optix, it will also validate
17 // refractions and reflections
18 //
19 // The command line arguments are:
20 // -I        => run in interactive mode; unless this is used, the program will
21 //              not allow interaction and exit
22 //              In interactive mode it responds to the keys listed
23 //              vtkOptiXTestInteractor.h
24 
25 #include "vtkTestUtilities.h"
26 
27 #include "vtkActor.h"
28 #include "vtkCamera.h"
29 #include "vtkLight.h"
30 #include "vtkOpenGLRenderer.h"
31 #include "vtkOptiXPass.h"
32 #include "vtkOptiXRendererNode.h"
33 #include "vtkPlaneSource.h"
34 #include "vtkPLYReader.h"
35 #include "vtkPolyDataMapper.h"
36 #include "vtkPolyDataNormals.h"
37 #include "vtkProperty.h"
38 #include "vtkRenderWindow.h"
39 #include "vtkRenderWindowInteractor.h"
40 #include "vtkSmartPointer.h"
41 #include "vtkSphereSource.h"
42 
43 #include "vtkOptiXTestInteractor.h"
44 
TestOptiXLights(int argc,char * argv[])45 int TestOptiXLights(int argc, char* argv[])
46 {
47   vtkSmartPointer<vtkRenderWindowInteractor> iren =
48     vtkSmartPointer<vtkRenderWindowInteractor>::New();
49   vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
50   iren->SetRenderWindow(renWin);
51   vtkSmartPointer<vtkOpenGLRenderer> renderer =
52     vtkSmartPointer<vtkOpenGLRenderer>::New();
53   renderer->AutomaticLightCreationOff();
54   renWin->AddRenderer(renderer);
55 
56   const char* fileName =
57     vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/bunny.ply");
58   vtkSmartPointer<vtkPLYReader> polysource = vtkSmartPointer<vtkPLYReader>::New();
59   polysource->SetFileName(fileName);
60 
61   //measure so we can place things sensibly
62   polysource->Update();
63   double bds[6];
64   polysource->GetOutput()->GetBounds(bds);
65   double x0 = bds[0]*2;
66   double x1 = bds[1]*2;
67   double y0 = bds[2];
68   double y1 = bds[3]*2;
69   double z0 = bds[4];
70   double z1 = bds[5]*4;
71 
72   vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New();
73   normals->SetInputConnection(polysource->GetOutputPort());
74 
75   vtkSmartPointer<vtkPolyDataMapper> mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
76   mapper->SetInputConnection(normals->GetOutputPort());
77   vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New();
78   actor->SetMapper(mapper);
79   actor->GetProperty()->SetColor(1.0,1.0,1.0);
80   actor->GetProperty()->SetAmbient(0.1);
81   actor->GetProperty()->SetDiffuse(1);
82   actor->GetProperty()->SetSpecularColor(1,1,1);
83   actor->GetProperty()->SetSpecular(0.9);
84   actor->GetProperty()->SetSpecularPower(500);
85   renderer->AddActor(actor);
86 
87   vtkSmartPointer<vtkPlaneSource> backwall = vtkSmartPointer<vtkPlaneSource>::New();
88   backwall->SetOrigin(x0,y0,z0);
89   backwall->SetPoint1(x1,y0,z0);
90   backwall->SetPoint2(x0,y1,z0);
91   mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
92   mapper->SetInputConnection(backwall->GetOutputPort());
93   actor=vtkSmartPointer<vtkActor>::New();
94   actor->SetMapper(mapper);
95   actor->GetProperty()->SetColor(1.0,1.0,1.0);
96   actor->GetProperty()->SetAmbient(0.1);
97   actor->GetProperty()->SetDiffuse(1);
98   actor->GetProperty()->SetSpecular(0);
99   renderer->AddActor(actor);
100 
101   vtkSmartPointer<vtkPlaneSource> floor = vtkSmartPointer<vtkPlaneSource>::New();
102   floor->SetOrigin(x0,y0,z0);
103   floor->SetPoint1(x0,y0,z1);
104   floor->SetPoint2(x1,y0,z0);
105   mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
106   mapper->SetInputConnection(floor->GetOutputPort());
107   actor=vtkSmartPointer<vtkActor>::New();
108   actor->GetProperty()->SetColor(1.0,1.0,1.0);
109   actor->GetProperty()->SetAmbient(0.1);
110   actor->GetProperty()->SetDiffuse(1);
111   actor->GetProperty()->SetSpecular(0);
112   actor->SetMapper(mapper);
113   renderer->AddActor(actor);
114 
115   vtkSmartPointer<vtkPlaneSource> left = vtkSmartPointer<vtkPlaneSource>::New();
116   left->SetOrigin(x0,y0,z0);
117   left->SetPoint1(x0,y1,z0);
118   left->SetPoint2(x0,y0,z1);
119   mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
120   mapper->SetInputConnection(left->GetOutputPort());
121   actor=vtkSmartPointer<vtkActor>::New();
122   actor->GetProperty()->SetColor(1.0,1.0,1.0);
123   actor->GetProperty()->SetAmbient(0.1);
124   actor->GetProperty()->SetDiffuse(1);
125   actor->GetProperty()->SetSpecular(0);
126   actor->SetMapper(mapper);
127   renderer->AddActor(actor);
128 
129   vtkSmartPointer<vtkSphereSource> magnifier = vtkSmartPointer<vtkSphereSource>::New();
130   //TODO: use PathTracer_Dielectric material for this when available
131   magnifier->SetCenter(x0+(x1-x0)*0.6, y0+(y1-y0)*0.2, z0+(z1-z0)*0.7);
132   magnifier->SetRadius((x1-x0)*0.05);
133   magnifier->SetPhiResolution(30);
134   magnifier->SetThetaResolution(30);
135   mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
136   mapper->SetInputConnection(magnifier->GetOutputPort());
137   actor=vtkSmartPointer<vtkActor>::New();
138   actor->GetProperty()->SetColor(1.0,1.0,1.0);
139   actor->GetProperty()->SetAmbient(0.1);
140   actor->GetProperty()->SetDiffuse(1);
141   actor->GetProperty()->SetSpecular(0);
142   actor->SetMapper(mapper);
143   renderer->AddActor(actor);
144 
145   vtkSmartPointer<vtkSphereSource> discoball = vtkSmartPointer<vtkSphereSource>::New();
146   //TODO: use PathTracer_Metal material for this when available
147   discoball->SetCenter(x0+(x1-x0)*0.5, y0+(y1-y0)*0.85, z0+(z1-z0)*0.5);
148   discoball->SetRadius((x1-x0)*0.1);
149   discoball->SetPhiResolution(30);
150   discoball->SetThetaResolution(30);
151   mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
152   mapper->SetInputConnection(discoball->GetOutputPort());
153   actor=vtkSmartPointer<vtkActor>::New();
154   actor->GetProperty()->SetColor(1.0,1.0,1.0);
155   actor->GetProperty()->SetAmbient(0.1);
156   actor->GetProperty()->SetDiffuse(1);
157   actor->GetProperty()->SetSpecular(0);
158   actor->SetMapper(mapper);
159   renderer->AddActor(actor);
160 
161   vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
162   //blue light casting shadows from infinity toward bottom left back corner
163   light->PositionalOff();
164   light->SetPosition(x0+(x1-x0)*1, y0+(y1-y0)*1, z0+(z1+z0)*1);
165   light->SetFocalPoint(x0,           y0,           z0);
166   light->SetLightTypeToSceneLight();
167   light->SetColor(0.0,0.0,1.0);
168   light->SetIntensity(0.3);
169   light->SwitchOn();
170   renderer->AddLight(light);
171 
172   light = vtkSmartPointer<vtkLight>::New();
173   //red light casting shadows from top to bottom
174   light->PositionalOn();
175   double t = 1.8; //adjust t to see effect of positional
176   light->SetPosition(  x0+(x1-x0)*0.5, y0+(y1-y0)*t, z0+(z1-z0)*0.5);
177   light->SetFocalPoint(x0+(x1-x0)*0.5, y0+(y1-y0)*0, z0+(z1-z0)*0.5);
178   light->SetLightTypeToSceneLight();
179   light->SetColor(1.0,0.0,0.0);
180   light->SetIntensity(0.3);
181   light->SwitchOn();
182   renderer->AddLight(light);
183 
184   light = vtkSmartPointer<vtkLight>::New();
185   //green light following camera
186   light->PositionalOn();
187   light->SetLightTypeToHeadlight();
188   light->SetColor(0.0,1.0,0.0);
189   light->SetIntensity(0.3);
190   light->SwitchOn();
191   renderer->AddLight(light);
192 
193   renderer->SetBackground(0.0,0.0,0.0);
194   renWin->SetSize(400,400);
195 
196   vtkSmartPointer<vtkOptiXPass> optix=vtkSmartPointer<vtkOptiXPass>::New();
197   renderer->SetPass(optix);
198 
199   //increase image quality from default (otherwise subsampling artifacts)
200   renWin->Render();
201   vtkOptiXRendererNode *renNode = optix->GetSceneGraph();
202   renderer->UseShadowsOn();
203   renNode->SetMaxFrames(5, renderer);
204   renNode->SetSamplesPerPixel(4, renderer);
205 
206   vtkSmartPointer<vtkOptiXTestInteractor> style =
207     vtkSmartPointer<vtkOptiXTestInteractor>::New();
208   style->SetPipelineControlPoints(renderer, optix, nullptr);
209   iren->SetInteractorStyle(style);
210   style->SetCurrentRenderer(renderer);
211 
212   iren->Start();
213 
214   return 0;
215 }
216