Panda3D

light.cxx
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file light.cxx
10 * @author mike
11 * @date 1997-01-09
12 */
13
14 #include "light.h"
15 #include "bamWriter.h"
16 #include "bamReader.h"
17 #include "datagram.h"
18 #include "datagramIterator.h"
19
20 UpdateSeq Light::_sort_seq;
21
22 TypeHandle Light::_type_handle;
23
24
25 /**
26 *
27 */
28 CycleData *Light::CData::
29 make_copy() const {
30 return new CData(*this);
31 }
32
33 /**
34 * Writes the contents of this object to the datagram for shipping out to a
35 * Bam file.
36 */
37 void Light::CData::
38 write_datagram(BamWriter *, Datagram &dg) const {
39 _color.write_datagram(dg);
40 }
41
42 /**
43 * This internal function is called by make_from_bam to read in all of the
44 * relevant data from the BamFile for the new Light.
45 */
46 void Light::CData::
47 fillin(DatagramIterator &scan, BamReader *) {
48 _color.read_datagram(scan);
49 }
50
51 /**
52 *
53 */
54 Light::
55 ~Light() {
56 }
57
58 /**
59 * Returns true if this is an AmbientLight, false if it is some other kind of
60 * light.
61 */
62 bool Light::
63 is_ambient_light() const {
64 return false;
65 }
66
67 /**
68 * Sets the color temperature of the light in kelvins. This will recalculate
69 * the light's color.
70 *
71 * The default value is 6500 K, corresponding to a perfectly white light
72 * assuming a D65 white point.
73 *
74 * @since 1.10.0
75 */
76 void Light::
77 set_color_temperature(PN_stdfloat temperature) {
78 if (_has_color_temperature && _color_temperature == temperature) {
79 return;
80 }
81
82 _has_color_temperature = true;
83 _color_temperature = temperature;
84
85 // Recalculate the color.
86 PN_stdfloat x, y;
87
88 if (temperature == 6500) {
89 // sRGB D65 white point.
90 x = 0.31271;
91 y = 0.32902;
92
93 } else {
94 PN_stdfloat mm = 1000.0 / temperature;
95 PN_stdfloat mm2 = mm * mm;
96 PN_stdfloat mm3 = mm2 * mm;
97
98 if (temperature < 4000) {
99 x = -0.2661239 * mm3 - 0.2343580 * mm2 + 0.8776956 * mm + 0.179910;
100 } else {
101 x = -3.0258469 * mm3 + 2.1070379 * mm2 + 0.2226347 * mm + 0.240390;
102 }
103
104 PN_stdfloat x2 = x * x;
105 PN_stdfloat x3 = x2 * x;
106 if (temperature < 2222) {
107 y = -1.1063814 * x3 - 1.34811020 * x2 + 2.18555832 * x - 0.20219683;
108 } else if (temperature < 4000) {
109 y = -0.9549476 * x3 - 1.37418593 * x2 + 2.09137015 * x - 0.16748867;
110 } else {
111 y = 3.0817580 * x3 - 5.87338670 * x2 + 3.75112997 * x - 0.37001483;
112 }
113 }
114
115 // xyY to XYZ, assuming Y=1.
116 LVecBase3 xyz(x / y, 1, (1 - x - y) / y);
117
118 // Convert XYZ to linearized sRGB.
119 const static LMatrix3 xyz_to_rgb(
120 3.2406255, -0.9689307, 0.0557101,
121 -1.537208, 1.8757561, -0.2040211,
122 -0.4986286, 0.0415175, 1.0569959);
123
124 LColor color(xyz_to_rgb.xform(xyz), 1);
125
126 CDWriter cdata(_cycler);
127 cdata->_color = color;
128 mark_viz_stale();
129 }
130
131 /**
132 * For spotlights, returns the exponent that controls the amount of light
133 * falloff from the center of the spotlight. For other kinds of lights,
134 * returns 0.
135 */
136 PN_stdfloat Light::
137 get_exponent() const {
138 return 0;
139 }
140
141 /**
142 * Returns the color of specular highlights generated by the light. This
143 * value is meaningless for ambient lights.
144 */
145 const LColor &Light::
146 get_specular_color() const {
147 static const LColor white(1, 1, 1, 1);
148 return white;
149 }
150
151 /**
152 * Returns the terms of the attenuation equation for the light. These are, in
153 * order, the constant, linear, and quadratic terms based on the distance from
154 * the point to the vertex.
155 */
156 const LVecBase3 &Light::
157 get_attenuation() const {
158 static const LVecBase3 no_atten(1, 0, 0);
159 return no_atten;
160 }
161
162 /**
163 * This is called when the light is added to a LightAttrib.
164 */
165 void Light::
166 attrib_ref() {
167 }
168
169 /**
170 * This is called when the light is removed from a LightAttrib.
171 */
172 void Light::
173 attrib_unref() {
174 }
175
176 /**
177 * Computes the vector from a particular vertex to this light. The exact
178 * vector depends on the type of light (e.g. point lights return a different
179 * result than directional lights).
180 *
181 * The input parameters are the vertex position in question, expressed in
182 * object space, and the matrix which converts from light space to object
183 * space. The result is expressed in object space.
184 *
185 * The return value is true if the result is successful, or false if it cannot
186 * be computed (e.g. for an ambient light).
187 */
188 bool Light::
189 get_vector_to_light(LVector3 &, const LPoint3 &, const LMatrix4 &) {
190 return false;
191 }
192
193 /**
194 * Returns a GeomNode that may be rendered to visualize the Light. This is
195 * used during the cull traversal to render the Lights that have been made
196 * visible.
197 */
198 GeomNode *Light::
199 get_viz() {
200 CDLockedReader cdata(_cycler);
201 if (cdata->_viz_geom_stale) {
202 CDWriter cdata_w(_cycler, cdata);
203
204 cdata_w->_viz_geom = new GeomNode("viz");
205 fill_viz_geom(cdata_w->_viz_geom);
206 cdata_w->_viz_geom_stale = false;
207 }
208 return cdata->_viz_geom;
209 }
210
211 /**
212 * Fills the indicated GeomNode up with Geoms suitable for rendering this
213 * light.
214 */
215 void Light::
216 fill_viz_geom(GeomNode *) {
217 }
218
219 /**
220 * Writes the contents of this object to the datagram for shipping out to a
221 * Bam file.
222 */
223 void Light::
224 write_datagram(BamWriter *manager, Datagram &dg) {
225 if (manager->get_file_minor_ver() >= 39) {
226 dg.add_bool(_has_color_temperature);
227 if (_has_color_temperature) {
228 dg.add_stdfloat(_color_temperature);
229 } else {
230 manager->write_cdata(dg, _cycler);
231 }
232 } else {
233 manager->write_cdata(dg, _cycler);
234 }
235 dg.add_int32(_priority);
236 }
237
238 /**
239 * This internal function is called by make_from_bam to read in all of the
240 * relevant data from the BamFile for the new Light.
241 */
242 void Light::
243 fillin(DatagramIterator &scan, BamReader *manager) {
244 if (manager->get_file_minor_ver() >= 39) {
245 _has_color_temperature = scan.get_bool();
246 } else {
247 _has_color_temperature = false;
248 }
249 if (_has_color_temperature) {
250 set_color_temperature(scan.get_stdfloat());
251 } else {
252 manager->read_cdata(scan, _cycler);
253 }
254 _priority = scan.get_int32();
255 }
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
BamReader::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
BamReader::read_cdata
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:695
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
BamWriter::write_cdata
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:425
BamWriter::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition: bamWriter.I:59
CycleDataLockedReader
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
Definition: cycleDataLockedReader.h:40
CycleDataWriter
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
Definition: cycleDataWriter.h:34
CycleData
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
DatagramIterator::get_stdfloat
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
Definition: datagramIterator.I:242
DatagramIterator::get_bool
bool get_bool()
Extracts a boolean value.
Definition: datagramIterator.I:48
DatagramIterator::get_int32
int32_t get_int32()
Extracts a signed 32-bit integer.
Definition: datagramIterator.I:107
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
Datagram::add_int32
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:67
Datagram::add_stdfloat
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition: datagram.I:133
Datagram::add_bool
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:34
GeomNode
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
Light::get_vector_to_light
virtual bool get_vector_to_light(LVector3 &result, const LPoint3 &from_object_point, const LMatrix4 &to_object_space)
Computes the vector from a particular vertex to this light.
Definition: light.cxx:189
Light::attrib_ref
virtual void attrib_ref()
This is called when the light is added to a LightAttrib.
Definition: light.cxx:166
Light::get_exponent
virtual PN_stdfloat get_exponent() const
For spotlights, returns the exponent that controls the amount of light falloff from the center of the...
Definition: light.cxx:137
Light::get_attenuation
virtual const LVecBase3 & get_attenuation() const
Returns the terms of the attenuation equation for the light.
Definition: light.cxx:157
Light::is_ambient_light
virtual bool is_ambient_light() const
Returns true if this is an AmbientLight, false if it is some other kind of light.
Definition: light.cxx:63
Light::get_specular_color
virtual const LColor & get_specular_color() const
Returns the color of specular highlights generated by the light.
Definition: light.cxx:146
Light::attrib_unref
virtual void attrib_unref()
This is called when the light is removed from a LightAttrib.
Definition: light.cxx:173
Light::set_color_temperature
set_color_temperature
Sets the color temperature of the light in kelvins.
Definition: light.h:55
Light::get_viz
GeomNode * get_viz()
Returns a GeomNode that may be rendered to visualize the Light.
Definition: light.cxx:199
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
UpdateSeq
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
light.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.