VTK  9.0.3
Light.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Types.h"
4 #include "Object.h"
5 
6 #include <VisRTX.h>
7 #include <cassert>
8 #include <string>
9 
10 namespace RTW
11 {
12  class Light : public Object
13  {
14  friend class Renderer;
15 
16  public:
18  {
19  VisRTX::Context* rtx = VisRTX_GetContext();
20 
21  if (type == "DirectionalLight" || type == "distant")
22  this->light = rtx->CreateDirectionalLight();
23  else if (type == "PointLight" || type == "point" || type == "SphereLight" || type == "sphere")
24  this->light = rtx->CreateSphericalLight();
25  else if (type == "SpotLight" || type == "spot")
26  this->light = rtx->CreateSpotLight();
27  else if (type == "QuadLight" || type == "quad")
28  this->light = rtx->CreateQuadLight();
29  else if (type == "AmbientLight" || type == "ambient")
30  this->light = rtx->CreateAmbientLight();
31  else if (type == "HDRILight" || type == "hdri")
32  this->light = rtx->CreateHDRILight();
33  else
34  {
35  std::cerr << "Error: Unhandled light type \"" << type << "\"" << std::endl;
36  assert(false);
37  }
38  }
39 
41  {
42  this->light->Release();
43  }
44 
45  void Commit() override
46  {
47  VisRTX::Vec3f color;
48  if (this->Get3f({ "color" }, &color))
49  this->light->SetColor(color);
50 
51  float intensity;
52  if (this->Get1f({ "intensity" }, &intensity))
53  this->light->SetIntensity(intensity);
54 
55  /*
56  * Directional
57  */
58  if (this->light->GetType() == VisRTX::LightType::DIRECTIONAL)
59  {
60  VisRTX::DirectionalLight* dirLight = dynamic_cast<VisRTX::DirectionalLight*>(this->light);
61 
62  VisRTX::Vec3f direction;
63  if (this->Get3f({ "direction" }, &direction))
64  dirLight->SetDirection(direction);
65 
66  float angularDiameter;
67  if (this->Get1f({ "angularDiameter" }, &angularDiameter))
68  dirLight->SetAngularDiameter(angularDiameter);
69  }
70 
71  /*
72  * Spherical
73  */
74  else if (this->light->GetType() == VisRTX::LightType::SPHERICAL)
75  {
76  VisRTX::SphericalLight* sphereLight = dynamic_cast<VisRTX::SphericalLight*>(this->light);
77 
78  VisRTX::Vec3f position;
79  if (this->Get3f({ "position" }, &position))
80  sphereLight->SetPosition(position);
81 
82  float radius;
83  if (this->Get1f({ "radius" }, &radius))
84  sphereLight->SetRadius(radius);
85  }
86 
87  /*
88  * Spot
89  */
90  else if (this->light->GetType() == VisRTX::LightType::SPOT)
91  {
92  VisRTX::SpotLight* spot = dynamic_cast<VisRTX::SpotLight*>(this->light);
93 
94  VisRTX::Vec3f position;
95  if (this->Get3f({ "position" }, &position))
96  spot->SetPosition(position);
97 
98  VisRTX::Vec3f direction;
99  if (this->Get3f({ "direction" }, &direction))
100  spot->SetDirection(direction);
101 
102  float openingAngle;
103  if (this->Get1f({ "openingAngle" }, &openingAngle))
104  spot->SetOpeningAngle(openingAngle);
105 
106  float penumbraAngle;
107  if (this->Get1f({ "penumbraAngle" }, &penumbraAngle))
108  spot->SetPenumbraAngle(penumbraAngle);
109 
110  float radius;
111  if (this->Get1f({ "radius" }, &radius))
112  spot->SetRadius(radius);
113  }
114 
115  /*
116  * Quad
117  */
118  else if (this->light->GetType() == VisRTX::LightType::QUAD)
119  {
120  VisRTX::QuadLight* quad = dynamic_cast<VisRTX::QuadLight*>(this->light);
121 
122  VisRTX::Vec3f position, edge1, edge2;
123  if (this->Get3f({ "position" }, &position) && this->Get3f({ "edge1" }, &edge1) && this->Get3f({ "edge2" }, &edge2))
124  quad->SetRect(position, edge1, edge2);
125 
126  quad->SetTwoSided(false);
127  }
128 
129  /*
130  * HDRI
131  */
132  else if (this->light->GetType() == VisRTX::LightType::HDRI)
133  {
134  VisRTX::HDRILight* hdri = dynamic_cast<VisRTX::HDRILight*>(this->light);
135 
136  Texture* texture = this->GetObject<Texture>({ "map" });
137  if (texture)
138  hdri->SetTexture(texture->texture);
139 
140  VisRTX::Vec3f direction;
141  if (this->Get3f({ "dir", "direction" }, &direction))
142  hdri->SetDirection(direction);
143 
144  VisRTX::Vec3f up;
145  if (this->Get3f({ "up" }, &up))
146  hdri->SetUp(up);
147  }
148  }
149 
150  private:
151  VisRTX::Light* light = nullptr;
152  };
153 }
void Commit() override
Definition: Light.h:45
~Light()
Definition: Light.h:40
Light(const std::string &type)
Definition: Light.h:17
VisRTX::Vec3f Get3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
Definition: Object.h:192
float Get1f(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:124
Definition: Backend.h:6
@ SpotLight
Definition: vtkX3D.h:164
@ DirectionalLight
Definition: vtkX3D.h:93
@ direction
Definition: vtkX3D.h:266
@ type
Definition: vtkX3D.h:522
@ color
Definition: vtkX3D.h:227
@ radius
Definition: vtkX3D.h:258
@ intensity
Definition: vtkX3D.h:389
@ position
Definition: vtkX3D.h:267
@ string
Definition: vtkX3D.h:496