1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkOptiXLightNode.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 #include "vtkOptiXLightNode.h"
16 
17 #include "vtkCollectionIterator.h"
18 #include "vtkLight.h"
19 #include "vtkMath.h"
20 #include "vtkObjectFactory.h"
21 #include "vtkOptiXRendererNode.h"
22 
23 #include <optixu/optixpp_namespace.h>
24 #include <CUDA/Light.h>
25 
26 #include <vector>
27 
28 //============================================================================
29 double vtkOptiXLightNode::LightScale = 1.0;
30 
31 //------------------------------------------------------------------------------
32 vtkStandardNewMacro(vtkOptiXLightNode);
33 
34 //------------------------------------------------------------------------------
vtkOptiXLightNode()35 vtkOptiXLightNode::vtkOptiXLightNode()
36 {
37 }
38 
39 //------------------------------------------------------------------------------
~vtkOptiXLightNode()40 vtkOptiXLightNode::~vtkOptiXLightNode()
41 {
42 }
43 
44 //------------------------------------------------------------------------------
SetLightScale(double s)45 void vtkOptiXLightNode::SetLightScale(double s)
46 {
47   vtkOptiXLightNode::LightScale = s;
48 }
49 
50 //------------------------------------------------------------------------------
GetLightScale()51 double vtkOptiXLightNode::GetLightScale()
52 {
53   return vtkOptiXLightNode::LightScale;
54 }
55 
56 //------------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)57 void vtkOptiXLightNode::PrintSelf(ostream& os, vtkIndent indent)
58 {
59   this->Superclass::PrintSelf(os, indent);
60 }
61 
62 //------------------------------------------------------------------------------
Render(bool prepass)63 void vtkOptiXLightNode::Render(bool prepass)
64 {
65   if (prepass)
66   {
67     vtkOptiXRendererNode *orn =
68       static_cast<vtkOptiXRendererNode *>(
69         this->GetFirstAncestorOfType("vtkOptiXRendererNode"));
70 
71     vtkLight *vlight = vtkLight::SafeDownCast(this->GetRenderable());
72 
73     if( vlight->GetSwitch() == 0 ||
74       vtkOptiXLightNode::LightScale <= 0.0f ||
75       vlight->GetIntensity() <= 0.0 )
76     {
77       // Ignoring light
78       return;
79     }
80 
81     const optix::float3 color = optix::make_float3(
82       static_cast<float>( vlight->GetDiffuseColor()[0] ),
83       static_cast<float>( vlight->GetDiffuseColor()[1] ),
84       static_cast<float>( vlight->GetDiffuseColor()[2] )
85       );
86 
87     const float intensity = static_cast<float>(
88       vtkOptiXLightNode::LightScale *
89       vlight->GetIntensity()
90       );
91 
92     vtkopt::Light light;
93     light.color = color*intensity;
94     light.pos   = optix::make_float3( 0.0f );
95     light.dir   = optix::make_float3( 0.0f );
96 
97     if (vlight->GetPositional())
98     {
99       double px, py, pz;
100       vlight->GetTransformedPosition(px, py, pz);
101 
102       light.type = vtkopt::Light::POSITIONAL;
103       light.pos  = optix::make_float3(
104         static_cast<float>(px),
105         static_cast<float>(py),
106         static_cast<float>(pz)
107         );
108     }
109     else
110     {
111       double px, py, pz;
112       vlight->GetTransformedPosition(px, py, pz);
113 
114       double fx, fy, fz;
115       vlight->GetTransformedFocalPoint(fx, fy, fz);
116 
117       light.type = vtkopt::Light::DIRECTIONAL;
118       light.dir  = optix::make_float3(
119         static_cast<float>( fx - px ),
120         static_cast<float>( fy - py ),
121         static_cast<float>( fz - pz )
122         );
123       light.dir = optix::normalize( light.dir );
124     }
125     orn->AddLight( light );
126   }
127 }
128