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