VTK  9.3.1
Light.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 #pragma once
4 
5 #include "vtkLogger.h"
6 
7 #include "../Types.h"
8 #include "Object.h"
9 
10 #include <VisRTX.h>
11 #include <cassert>
12 #include <string>
13 
14 namespace RTW
15 {
16 VTK_ABI_NAMESPACE_BEGIN
17  class Light : public Object
18  {
19  friend class Renderer;
20 
21  public:
23  : Object(RTW_LIGHT)
24  {
25  VisRTX::Context* rtx = VisRTX_GetContext();
26 
27  if (type == "DirectionalLight" || type == "distant")
28  this->light = rtx->CreateDirectionalLight();
29  else if (type == "PointLight" || type == "point" || type == "SphereLight" || type == "sphere")
30  this->light = rtx->CreateSphericalLight();
31  else if (type == "SpotLight" || type == "spot")
32  this->light = rtx->CreateSpotLight();
33  else if (type == "QuadLight" || type == "quad")
34  this->light = rtx->CreateQuadLight();
35  else if (type == "AmbientLight" || type == "ambient")
36  this->light = rtx->CreateAmbientLight();
37  else if (type == "HDRILight" || type == "hdri")
38  this->light = rtx->CreateHDRILight();
39  else
40  {
41  vtkLogF(ERROR, "VisRTX Error: Unhandled light type \"%s\"", type.c_str());
42  assert(false);
43  }
44  }
45 
47  {
48  this->light->Release();
49  }
50 
52  {
53  switch(this->light->GetType())
54  {
55  case VisRTX::LightType::AMBIENT:
56  return "ambient";
57  case VisRTX::LightType::DIRECTIONAL:
58  return "distant";
59  case VisRTX::LightType::SPHERICAL:
60  return "sphere";
61  case VisRTX::LightType::SPOT:
62  return "spot";
63  case VisRTX::LightType::QUAD:
64  return "quad";
65  case VisRTX::LightType::HDRI:
66  return "hdri";
67  default:
68  return "unknown";
69  }
70  }
71 
72  void Commit() override
73  {
74  VisRTX::Vec3f color;
75  if (this->GetVec3f({ "color" }, &color))
76  this->light->SetColor(color);
77 
78  float intensity;
79  if (this->GetFloat({ "intensity" }, &intensity))
80  this->light->SetIntensity(intensity);
81 
82  /*
83  * Directional
84  */
85  if (this->light->GetType() == VisRTX::LightType::DIRECTIONAL)
86  {
87  VisRTX::DirectionalLight* dirLight = dynamic_cast<VisRTX::DirectionalLight*>(this->light);
88 
89  VisRTX::Vec3f direction;
90  if (this->GetVec3f({ "direction" }, &direction))
91  dirLight->SetDirection(direction);
92 
93  float angularDiameter;
94  if (this->GetFloat({ "angularDiameter" }, &angularDiameter))
95  dirLight->SetAngularDiameter(angularDiameter);
96  }
97 
98  /*
99  * Spherical
100  */
101  else if (this->light->GetType() == VisRTX::LightType::SPHERICAL)
102  {
103  VisRTX::SphericalLight* sphereLight = dynamic_cast<VisRTX::SphericalLight*>(this->light);
104 
105  VisRTX::Vec3f position;
106  if (this->GetVec3f({ "position" }, &position))
107  sphereLight->SetPosition(position);
108 
109  float radius;
110  if (this->GetFloat({ "radius" }, &radius))
111  sphereLight->SetRadius(radius);
112  }
113 
114  /*
115  * Spot
116  */
117  else if (this->light->GetType() == VisRTX::LightType::SPOT)
118  {
119  VisRTX::SpotLight* spot = dynamic_cast<VisRTX::SpotLight*>(this->light);
120 
121  VisRTX::Vec3f position;
122  if (this->GetVec3f({ "position" }, &position))
123  spot->SetPosition(position);
124 
125  VisRTX::Vec3f direction;
126  if (this->GetVec3f({ "direction" }, &direction))
127  spot->SetDirection(direction);
128 
129  float openingAngle;
130  if (this->GetFloat({ "openingAngle" }, &openingAngle))
131  spot->SetOpeningAngle(openingAngle);
132 
133  float penumbraAngle;
134  if (this->GetFloat({ "penumbraAngle" }, &penumbraAngle))
135  spot->SetPenumbraAngle(penumbraAngle);
136 
137  float radius;
138  if (this->GetFloat({ "radius" }, &radius))
139  spot->SetRadius(radius);
140  }
141 
142  /*
143  * Quad
144  */
145  else if (this->light->GetType() == VisRTX::LightType::QUAD)
146  {
147  VisRTX::QuadLight* quad = dynamic_cast<VisRTX::QuadLight*>(this->light);
148 
149  VisRTX::Vec3f position, edge1, edge2;
150  if (this->GetVec3f({ "position" }, &position) && this->GetVec3f({ "edge1" }, &edge1) && this->GetVec3f({ "edge2" }, &edge2))
151  quad->SetRect(position, edge1, edge2);
152 
153  quad->SetTwoSided(false);
154  }
155 
156  /*
157  * HDRI
158  */
159  else if (this->light->GetType() == VisRTX::LightType::HDRI)
160  {
161  VisRTX::HDRILight* hdri = dynamic_cast<VisRTX::HDRILight*>(this->light);
162 
163  Texture* texture = this->GetObject<Texture>({ "map" });
164  if (texture)
165  hdri->SetTexture(texture->texture);
166 
167  VisRTX::Vec3f direction;
168  if (this->GetVec3f({ "dir", "direction" }, &direction))
169  hdri->SetDirection(direction);
170 
171  VisRTX::Vec3f up;
172  if (this->GetVec3f({ "up" }, &up))
173  hdri->SetUp(up);
174  }
175  }
176 
177  private:
178  VisRTX::Light* light = nullptr;
179  };
180 VTK_ABI_NAMESPACE_END
181 }
std::string GetType()
Definition: Light.h:51
void Commit() override
Definition: Light.h:72
~Light()
Definition: Light.h:46
#define vtkLogF(verbosity_name,...)
Add to log given the verbosity level.
Definition: vtkLogger.h:456
VisRTX::Vec3f GetVec3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
Definition: Object.h:204
Definition: Backend.h:7
Light(const std::string &type)
Definition: Light.h:22
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:136