Overte C++ Documentation
Light.h
1 //
2 // Light.h
3 // libraries/graphics/src/graphics
4 //
5 // Created by Sam Gateau on 12/10/2014.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_model_Light_h
12 #define hifi_model_Light_h
13 
14 #include <bitset>
15 #include <map>
16 
17 #include <glm/glm.hpp>
18 #include "Transform.h"
19 #include "gpu/Resource.h"
20 #include "gpu/Texture.h"
21 
22 namespace graphics {
23 typedef gpu::BufferView UniformBufferView;
24 typedef gpu::TextureView TextureView;
25 typedef glm::vec3 Vec3;
26 typedef glm::vec4 Vec4;
27 typedef glm::quat Quat;
28 
29 
30 class Light {
31 public:
32 
33  struct LightVolume {
34  vec3 position { 0.f };
35  float radius { 1.0f };
36  vec3 direction { 0.f, 0.f, -1.f };
37  float spotCos { -1.f };
38 
39  bool isPoint() const { return bool(spotCos < 0.f); }
40  bool isSpot() const { return bool(spotCos >= 0.f); }
41 
42  vec3 getPosition() const { return position; }
43  float getRadius() const { return radius; }
44  float getRadiusSquare() const { return radius * radius; }
45  vec3 getDirection() const { return direction; }
46 
47  float getSpotAngleCos() const { return spotCos; }
48  vec2 getSpotAngleCosSin() const { return vec2(spotCos, sqrt(1.f - spotCos * spotCos)); }
49  };
50 
51 
52  struct LightIrradiance {
53  vec3 color { 1.f };
54  float intensity { 1.f };
55  float falloffRadius { 0.1f };
56  float cutoffRadius { 0.1f };
57  float falloffSpot { 1.f };
58  float spare1;
59 
60  vec3 getColor() const { return color; }
61  float getIntensity() const { return intensity; }
62  vec3 getIrradiance() const { return color * intensity; }
63  float getFalloffRadius() const { return falloffRadius; }
64  float getCutoffRadius() const { return cutoffRadius; }
65  float getFalloffSpot() const { return falloffSpot; }
66  };
67 
68 
69 
70  enum Type {
71  AMBIENT = 0,
72  SUN,
73  POINT,
74  SPOT,
75 
76  NUM_TYPES,
77  };
78 
79  typedef Vec3 Color;
80 
81  enum FlagBit {
82  COLOR_BIT = 0,
83  INTENSITY_BIT,
84  RANGE_BIT,
85  SPOT_BIT,
86  TRANSFORM_BIT,
87 
88  NUM_FLAGS,
89  };
90  typedef std::bitset<NUM_FLAGS> Flags;
91 
92  Light();
93  Light(const Light& light);
94  Light& operator= (const Light& light);
95  virtual ~Light();
96 
97  void setType(Type type);
98  Type getType() const { return _type; }
99 
100  void setPosition(const Vec3& position);
101  const Vec3& getPosition() const { return _transform.getTranslation(); }
102 
103  void setDirection(const Vec3& direction);
104  const Vec3& getDirection() const;
105 
106  void setCastShadows(const bool castShadows);
107  bool getCastShadows() const;
108 
109  void setShadowsMaxDistance(const float maxDistance);
110  float getShadowsMaxDistance() const;
111 
112  void setShadowBias(float bias);
113  float getShadowBias() const;
114 
115  void setOrientation(const Quat& orientation);
116  const glm::quat& getOrientation() const { return _transform.getRotation(); }
117 
118  const Color& getColor() const { return _lightSchemaBuffer->irradiance.color; }
119  void setColor(const Color& color);
120 
121  float getIntensity() const { return _lightSchemaBuffer->irradiance.intensity; }
122  void setIntensity(float intensity);
123 
124  bool isRanged() const { return (getType() == POINT) || (getType() == SPOT); }
125 
126  // FalloffRradius is the physical radius of the light sphere through which energy shines,
127  // expressed in meters. It is used only to calculate the falloff curve of the light.
128  // Actual rendered lights will all have surface radii approaching 0.
129  void setFalloffRadius(float radius);
130  float getFalloffRadius() const { return _lightSchemaBuffer->irradiance.falloffRadius; }
131 
132  // Maximum radius is the cutoff radius of the light energy, expressed in meters.
133  // It is used to bound light entities, and *will not* affect the falloff curve of the light.
134  // Setting it low will result in a noticeable cutoff.
135  void setMaximumRadius(float radius);
136  float getMaximumRadius() const { return _lightSchemaBuffer->volume.radius; }
137 
138  // Spot properties
139  bool isSpot() const { return getType() == SPOT; }
140  void setSpotAngle(float angle);
141  float getSpotAngle() const { return acos(_lightSchemaBuffer->volume.getSpotAngleCos()); }
142  glm::vec2 getSpotAngleCosSin() const { return _lightSchemaBuffer->volume.getSpotAngleCosSin(); }
143  void setSpotExponent(float exponent);
144  float getSpotExponent() const { return _lightSchemaBuffer->irradiance.falloffSpot; }
145 
146  // If the light has an ambient (Indirect) component, then the Ambientintensity can be used to control its contribution to the lighting
147  void setAmbientIntensity(float intensity);
148  float getAmbientIntensity() const { return _ambientSchemaBuffer->intensity; }
149 
150  // Spherical Harmonics storing the Ambient lighting approximation used for the Sun typed light
151  void setAmbientSphere(const gpu::SphericalHarmonics& sphere);
152  const gpu::SphericalHarmonics& getAmbientSphere() const { return _ambientSchemaBuffer->ambientSphere; }
153  void setAmbientSpherePreset(gpu::SphericalHarmonics::Preset preset);
154 
155  void setAmbientMap(const gpu::TexturePointer& ambientMap);
156  gpu::TexturePointer getAmbientMap() const { return _ambientMap; }
157 
158  void setAmbientMapNumMips(uint16_t numMips);
159  uint16_t getAmbientMapNumMips() const { return (uint16_t) _ambientSchemaBuffer->mapNumMips; }
160 
161  void setTransform(const glm::mat4& transform);
162 
163  // Light Schema
164  class LightSchema {
165  public:
166  LightVolume volume;
167  LightIrradiance irradiance;
168  };
169 
170  class AmbientSchema {
171  public:
172  float intensity { 0.0f };
173  float mapNumMips { 0.0f };
174  float spare1;
175  float spare2;
176 
177  gpu::SphericalHarmonics ambientSphere;
178  glm::mat4 transform;
179  };
180 
181  using LightSchemaBuffer = gpu::StructBuffer<LightSchema>;
182  using AmbientSchemaBuffer = gpu::StructBuffer<AmbientSchema>;
183 
184  const LightSchemaBuffer& getLightSchemaBuffer() const { return _lightSchemaBuffer; }
185  const AmbientSchemaBuffer& getAmbientSchemaBuffer() const { return _ambientSchemaBuffer; }
186 
187 protected:
188 
189  Flags _flags{ 0 };
190 
191  LightSchemaBuffer _lightSchemaBuffer;
192  AmbientSchemaBuffer _ambientSchemaBuffer;
193 
194  Transform _transform;
195 
196  gpu::TexturePointer _ambientMap;
197 
198  Type _type { SUN };
199  float _spotCos { -1.0f }; // stored here to be able to reset the spot angle when turning the type spot on/off
200 
201  float _shadowsMaxDistance { 40.0f };
202  float _shadowBias { 0.5f };
203  bool _castShadows{ false };
204 
205  void updateLightRadius();
206 };
207 typedef std::shared_ptr< Light > LightPointer;
208 
209 };
210 
211 #endif
Provides the Quat scripting interface.
Definition: Quat.h:61
Provides the Vec3 scripting interface.
Definition: Vec3.h:80