vtkOSPRayLightNode.cxx 8.81 KB
Newer Older
1
2
3
/*=========================================================================

  Program:   Visualization Toolkit
David DeMarle's avatar
David DeMarle committed
4
  Module:    vtkOSPRayLightNode.cxx
5
6
7
8
9
10
11
12
13
14

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
David DeMarle's avatar
David DeMarle committed
15
#include "vtkOSPRayLightNode.h"
16

17
#include "vtkCamera.h"
18
#include "vtkCollectionIterator.h"
19
#include "vtkInformation.h"
20
#include "vtkInformationDoubleKey.h"
21
#include "vtkInformationIntegerKey.h"
22
#include "vtkLight.h"
23
#include "vtkMath.h"
24
#include "vtkOSPRayCameraNode.h"
David DeMarle's avatar
David DeMarle committed
25
#include "vtkOSPRayRendererNode.h"
26
#include "vtkObjectFactory.h"
27
28
#include "vtkOpenGLRenderer.h"
#include "vtkTransform.h"
29

30
31
#include <vector>

32
vtkInformationKeyMacro(vtkOSPRayLightNode, IS_AMBIENT, Integer);
33
vtkInformationKeyMacro(vtkOSPRayLightNode, RADIUS, Double);
34

35
//============================================================================
David DeMarle's avatar
David DeMarle committed
36
double vtkOSPRayLightNode::LightScale = 1.0;
37
38

//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
39
vtkStandardNewMacro(vtkOSPRayLightNode);
40
41

//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
42
vtkOSPRayLightNode::vtkOSPRayLightNode()
43
{
David E. DeMarle's avatar
David E. DeMarle committed
44
  this->OLight = nullptr;
45
46
47
}

//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
48
vtkOSPRayLightNode::~vtkOSPRayLightNode()
49
{
50
  vtkOSPRayRendererNode* orn = vtkOSPRayRendererNode::GetRendererNode(this);
Tim Biedert's avatar
Tim Biedert committed
51
52
  if (orn)
  {
53
    RTW::Backend* backend = orn->GetBackend();
54
    if (backend != nullptr)
55
      ospRelease((OSPLight)this->OLight);
Tim Biedert's avatar
Tim Biedert committed
56
  }
57
58
59
}

//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
60
void vtkOSPRayLightNode::SetLightScale(double s)
61
{
David DeMarle's avatar
David DeMarle committed
62
  vtkOSPRayLightNode::LightScale = s;
63
64
65
}

//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
66
double vtkOSPRayLightNode::GetLightScale()
67
{
David DeMarle's avatar
David DeMarle committed
68
  return vtkOSPRayLightNode::LightScale;
69
}
70

71
//----------------------------------------------------------------------------
72
void vtkOSPRayLightNode::SetIsAmbient(int value, vtkLight* light)
73
74
75
76
77
{
  if (!light)
  {
    return;
  }
78
  vtkInformation* info = light->GetInformation();
79
80
81
82
  info->Set(vtkOSPRayLightNode::IS_AMBIENT(), value);
}

//----------------------------------------------------------------------------
83
int vtkOSPRayLightNode::GetIsAmbient(vtkLight* light)
84
85
86
87
88
{
  if (!light)
  {
    return 0;
  }
89
  vtkInformation* info = light->GetInformation();
90
91
92
93
94
95
96
  if (info && info->Has(vtkOSPRayLightNode::IS_AMBIENT()))
  {
    return (info->Get(vtkOSPRayLightNode::IS_AMBIENT()));
  }
  return 0;
}

97
//----------------------------------------------------------------------------
98
void vtkOSPRayLightNode::SetRadius(double value, vtkLight* light)
99
100
101
102
103
{
  if (!light)
  {
    return;
  }
104
  vtkInformation* info = light->GetInformation();
105
106
107
108
  info->Set(vtkOSPRayLightNode::RADIUS(), value);
}

//----------------------------------------------------------------------------
109
double vtkOSPRayLightNode::GetRadius(vtkLight* light)
110
111
112
113
114
{
  if (!light)
  {
    return 0.0;
  }
115
  vtkInformation* info = light->GetInformation();
116
117
118
119
120
121
122
  if (info && info->Has(vtkOSPRayLightNode::RADIUS()))
  {
    return (info->Get(vtkOSPRayLightNode::RADIUS()));
  }
  return 0.0;
}

123
//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
124
void vtkOSPRayLightNode::PrintSelf(ostream& os, vtkIndent indent)
125
126
127
{
  this->Superclass::PrintSelf(os, indent);
}
128

129
//----------------------------------------------------------------------------
David DeMarle's avatar
David DeMarle committed
130
void vtkOSPRayLightNode::Render(bool prepass)
131
132
{
  if (prepass)
133
  {
134
135
    vtkOSPRayRendererNode* orn =
      static_cast<vtkOSPRayRendererNode*>(this->GetFirstAncestorOfType("vtkOSPRayRendererNode"));
136

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    vtkOpenGLRenderer* ren = vtkOpenGLRenderer::SafeDownCast(orn->GetRenderable());
    vtkTransform* userLightTransfo = ren->GetUserLightTransform();

    vtkNew<vtkMatrix4x4> camTransfo;
    vtkNew<vtkMatrix4x4> invCamTransfo;
    if (userLightTransfo)
    {
      vtkOSPRayCameraNode* ocam =
        static_cast<vtkOSPRayCameraNode*>(orn->GetFirstChildOfType("vtkOSPRayCameraNode"));

      vtkCamera* cam = vtkCamera::SafeDownCast(ocam->GetRenderable());

      cam->GetModelViewTransformObject()->GetMatrix(camTransfo);
      vtkMatrix4x4::Invert(camTransfo, invCamTransfo);
    }

153
    RTW::Backend* backend = orn->GetBackend();
Tim Biedert's avatar
Tim Biedert committed
154
    if (backend == nullptr)
155
      return;
Tim Biedert's avatar
Tim Biedert committed
156
157
    OSPLight ospLight;

158
    vtkLight* light = vtkLight::SafeDownCast(this->GetRenderable());
159

160
    float color[3] = { 0.0, 0.0, 0.0 };
161
    if (light->GetSwitch())
162
    {
163
164
165
      color[0] = static_cast<float>(light->GetDiffuseColor()[0]);
      color[1] = static_cast<float>(light->GetDiffuseColor()[1]);
      color[2] = static_cast<float>(light->GetDiffuseColor()[2]);
166
    }
167
168
    if (vtkOSPRayLightNode::GetIsAmbient(light))
    {
169
      ospLight = ospNewLight("ambient");
170
171
172
      color[0] = static_cast<float>(light->GetDiffuseColor()[0]);
      color[1] = static_cast<float>(light->GetDiffuseColor()[1]);
      color[2] = static_cast<float>(light->GetDiffuseColor()[2]);
173
      ospSetVec3f(ospLight, "color", color[0], color[1], color[2]);
174
175
      float fI = static_cast<float>(
        0.13f * vtkOSPRayLightNode::LightScale * light->GetIntensity() * vtkMath::Pi());
176
      ospSetFloat(ospLight, "intensity", fI);
177
178
179
180
      ospCommit(ospLight);
      orn->AddLight(ospLight);
    }
    else if (light->GetPositional())
181
    {
182
183
184
185
186
187
188
189
190
191
      double position[4];
      light->GetPosition(position);
      position[3] = 1.0;

      if (light->LightTypeIsCameraLight())
      {
        light->TransformPoint(position, position);
      }

      if (!light->LightTypeIsSceneLight() && userLightTransfo)
192
      {
193
194
195
        camTransfo->MultiplyPoint(position, position);
        userLightTransfo->TransformPoint(position, position);
        invCamTransfo->MultiplyPoint(position, position);
196
      }
197

198
      float coneAngle = static_cast<float>(light->GetConeAngle());
199
      if (coneAngle <= 0.0 || coneAngle >= 90.0)
200
      {
Carson Brownlee's avatar
Carson Brownlee committed
201
        ospLight = ospNewLight("sphere");
202
203
204
      }
      else
      {
Carson Brownlee's avatar
Carson Brownlee committed
205
        ospLight = ospNewLight("spot");
206
207
208
209
210
        double focalPoint[4];
        light->GetFocalPoint(focalPoint);
        focalPoint[3] = 1.0;

        if (light->LightTypeIsCameraLight())
211
        {
212
          light->TransformPoint(focalPoint, focalPoint);
213
        }
214
215
216
217
218
219
220
221

        if (!light->LightTypeIsSceneLight() && userLightTransfo)
        {
          camTransfo->MultiplyPoint(focalPoint, focalPoint);
          userLightTransfo->TransformPoint(focalPoint, focalPoint);
          invCamTransfo->MultiplyPoint(focalPoint, focalPoint);
        }

222
        double direction[3];
223
224
225
        vtkMath::Subtract(focalPoint, position, direction);
        vtkMath::Normalize(direction);

226
        ospSetVec3f(ospLight, "direction", direction[0], direction[1], direction[2]);
227
        // OpenGL interprets this as a half-angle. Mult by 2 for consistency.
228
        ospSetFloat(ospLight, "openingAngle", 2 * coneAngle);
229
        // TODO: penumbraAngle
230
      }
231
      ospSetVec3f(ospLight, "color", color[0], color[1], color[2]);
232
233
      float fI =
        static_cast<float>(vtkOSPRayLightNode::LightScale * light->GetIntensity() * vtkMath::Pi());
234
235
      ospSetInt(ospLight, "isVisible", 0);
      ospSetFloat(ospLight, "intensity", fI);
236

237
      ospSetVec3f(ospLight, "position", position[0], position[1], position[2]);
238
      float r = static_cast<float>(vtkOSPRayLightNode::GetRadius(light));
239
      ospSetFloat(ospLight, "radius", r);
240
241
      ospCommit(ospLight);
      orn->AddLight(ospLight);
242
    }
243
    else
244
    {
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      double position[3];
      double focalPoint[3];
      light->GetPosition(position);
      light->GetFocalPoint(focalPoint);

      double direction[4];
      vtkMath::Subtract(focalPoint, position, direction);
      vtkMath::Normalize(direction);
      direction[3] = 0.0;

      if (light->LightTypeIsCameraLight())
      {
        light->TransformVector(direction, direction);
      }

      if (!light->LightTypeIsSceneLight() && userLightTransfo)
261
      {
262
263
264
        camTransfo->MultiplyPoint(direction, direction);
        userLightTransfo->TransformNormal(direction, direction);
        invCamTransfo->MultiplyPoint(direction, direction);
265
      }
266

Carson Brownlee's avatar
Carson Brownlee committed
267
      ospLight = ospNewLight("distant");
268
      ospSetVec3f(ospLight, "color", color[0], color[1], color[2]);
269
270
      float fI =
        static_cast<float>(vtkOSPRayLightNode::LightScale * light->GetIntensity() * vtkMath::Pi());
271
272
      ospSetFloat(ospLight, "intensity", fI);
      ospSetVec3f(ospLight, "direction", direction[0], direction[1], direction[2]);
273
      float r = static_cast<float>(vtkOSPRayLightNode::GetRadius(light));
274
      ospSetFloat(ospLight, "angularDiameter", r);
275
276
277
      ospCommit(ospLight);
      orn->AddLight(ospLight);
    }
David E. DeMarle's avatar
David E. DeMarle committed
278
    this->OLight = ospLight;
279
  }
280
}