1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestOSPRayLights.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 ospray.
16 // When advanced materials are exposed in ospray, 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 //              vtkOSPRayTestInteractor.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 "vtkOSPRayPass.h"
32 #include "vtkOSPRayRendererNode.h"
33 #include "vtkPlaneSource.h"
34 #include "vtkPLYReader.h"
35 #include "vtkPolyDataMapper.h"
36 #include "vtkPolyDataNormals.h"
37 #include "vtkProperty.h"
38 #include "vtkRenderer.h"
39 #include "vtkRenderWindow.h"
40 #include "vtkRenderWindowInteractor.h"
41 #include "vtkSmartPointer.h"
42 #include "vtkSphereSource.h"
43 
44 #include "vtkOSPRayTestInteractor.h"
45 
TestOSPRayLights(int argc,char * argv[])46 int TestOSPRayLights(int argc, char* argv[])
47 {
48   vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
49   vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
50   iren->SetRenderWindow(renWin);
51   vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
52   renderer->AutomaticLightCreationOff();
53   renWin->AddRenderer(renderer);
54 
55   const char* fileName =
56     vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/bunny.ply");
57   vtkSmartPointer<vtkPLYReader> polysource = vtkSmartPointer<vtkPLYReader>::New();
58   polysource->SetFileName(fileName);
59 
60   //measure so we can place things sensibly
61   polysource->Update();
62   double bds[6];
63   polysource->GetOutput()->GetBounds(bds);
64   double x0 = bds[0]*2;
65   double x1 = bds[1]*2;
66   double y0 = bds[2];
67   double y1 = bds[3]*2;
68   double z0 = bds[4];
69   double z1 = bds[5]*4;
70 
71   //TODO: ospray acts strangely without these such that Diff and Spec are not 0..255 instead of 0..1
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<vtkOSPRayPass> ospray=vtkSmartPointer<vtkOSPRayPass>::New();
197   renderer->SetPass(ospray);
198 
199   //increase image quality from default (otherwise subsampling artifacts)
200   renWin->Render();
201   renderer->UseShadowsOn();
202   vtkOSPRayRendererNode::SetMaxFrames(5, renderer);
203   vtkOSPRayRendererNode::SetSamplesPerPixel(4, renderer);
204 
205   vtkSmartPointer<vtkOSPRayTestInteractor> style =
206     vtkSmartPointer<vtkOSPRayTestInteractor>::New();
207   style->SetPipelineControlPoints(renderer, ospray, nullptr);
208   iren->SetInteractorStyle(style);
209   style->SetCurrentRenderer(renderer);
210 
211   iren->Start();
212 
213   return 0;
214 }
215