Panda3D
rpLight.cxx
1 /**
2  *
3  * RenderPipeline
4  *
5  * Copyright (c) 2014-2016 tobspr <tobias.springer1@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  */
26 
27 
28 #include "rpLight.h"
29 
30 
31 /**
32  * @brief Constructs a new light with the given type
33  * @details This constructs a new base light with the given light type.
34  * Sub-Classes should call this to initialize all properties.
35  *
36  * @param light_type Type of the light
37  */
39  _light_type = light_type;
40  _needs_update = false;
41  _casts_shadows = false;
42  _slot = -1;
43  _position.fill(0);
44  _color.fill(1);
45  _ies_profile = -1;
46  _source_resolution = 512;
47  _near_plane = 0.5;
48  _energy = 20.0;
49 }
50 
51 /**
52  * @brief Writes the light to a GPUCommand
53  * @details This writes all of the lights data to the given GPUCommand handle.
54  * Subclasses should first call this method, and then append their own
55  * data. This makes sure that for unpacking a light, no information about
56  * the type of the light is required.
57  *
58  * @param cmd The GPUCommand to write to
59  */
61  cmd.push_int(_light_type);
62  cmd.push_int(_ies_profile);
63 
64  if (_casts_shadows) {
65  // If we casts shadows, write the index of the first source, we expect
66  // them to be consecutive
67  nassertv(_shadow_sources.size() >= 0);
68  nassertv(_shadow_sources[0]->has_slot());
69  cmd.push_int(_shadow_sources[0]->get_slot());
70  } else {
71  // If we cast no shadows, just push a negative number
72  cmd.push_int(-1);
73  }
74 
75  cmd.push_vec3(_position);
76 
77  // Get the lights color by multiplying color with energy. Divide by
78  // 100, since 16bit floating point buffers only go up to 65000.0, which
79  // prevents very bright lights
80  cmd.push_vec3(_color * _energy / 100.0);
81 }
82 
83 /**
84  * @brief Light destructor
85  * @details This destructs the light, cleaning up all resourced used. The light
86  * should be detached at this point, because while the Light is attached,
87  * the InternalLightManager holds a reference to prevent it from being
88  * destructed.
89  */
91  nassertv(!has_slot()); // Light still attached - should never happen
93 }
94 
95 /**
96  * @brief Sets the lights color from a given color temperature
97  * @details This sets the lights color, given a temperature. This is more
98  * physically based than setting a user defined color. The color will be
99  * computed from the given temperature.
100  *
101  * @param temperature Light temperature
102  */
103 void RPLight::set_color_from_temperature(float temperature) {
104 
105  // Thanks to rdb for this conversion script
106  float mm = 1000.0 / temperature;
107  float mm2 = mm * mm;
108  float mm3 = mm2 * mm;
109  float x, y;
110 
111  if (temperature < 4000) {
112  x = -0.2661239 * mm3 - 0.2343580 * mm2 + 0.8776956 * mm + 0.179910;
113  } else {
114  x = -3.0258469 * mm3 + 2.1070379 * mm2 + 0.2226347 * mm + 0.240390;
115  }
116 
117  float x2 = x * x;
118  float x3 = x2 * x;
119  if (temperature < 2222) {
120  y = -1.1063814 * x3 - 1.34811020 * x2 + 2.18555832 * x - 0.20219683;
121  } else if (temperature < 4000) {
122  y = -0.9549476 * x3 - 1.37418593 * x2 + 2.09137015 * x - 0.16748867;
123  } else {
124  y = 3.0817580 * x3 - 5.87338670 * x2 + 3.75112997 * x - 0.37001483;
125  }
126 
127  // xyY to XYZ, assuming Y=1.
128  LVecBase3 xyz(x / y, 1, (1 - x - y) / y);
129 
130  // Convert XYZ to linearized sRGB.
131  const static LMatrix3 xyz_to_rgb(
132  3.2406, -0.9689, 0.0557,
133  -1.5372, 1.8758, -0.2050,
134  -0.4986, 0.0415, 1.0570);
135 
136  set_color(xyz_to_rgb.xform(xyz));
137 }
RPLight(LightType light_type)
RenderPipeline.
Definition: rpLight.cxx:38
virtual ~RPLight()
Light destructor.
Definition: rpLight.cxx:90
void set_color_from_temperature(float temperature)
Sets the lights color from a given color temperature.
Definition: rpLight.cxx:103
set_color
Sets the lights color.
Definition: rpLight.h:83
void clear_shadow_sources()
Clears all shadow source.
Definition: rpLight.I:60
bool has_slot() const
Returns whether the light has a slot.
Definition: rpLight.I:106
virtual void write_to_command(GPUCommand &cmd)
Writes the light to a GPUCommand.
Definition: rpLight.cxx:60
void push_vec3(const LVecBase3 &v)
Appends a 3-component floating point vector to the GPUCommand.
Definition: gpuCommand.I:91
LightType
Different types of light.
Definition: rpLight.h:46
Class for storing data to be transferred to the GPU.
Definition: gpuCommand.h:47
int get_slot() const
Returns the slot of the light.
Definition: rpLight.I:118
void push_int(int v)
RenderPipeline.
Definition: gpuCommand.I:37