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