Panda3D
Loading...
Searching...
No Matches
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
20UpdateSeq Light::_sort_seq;
21
22TypeHandle Light::_type_handle;
23
24
25/**
26 *
27 */
28CycleData *Light::CData::
29make_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 */
37void Light::CData::
38write_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 */
46void Light::CData::
47fillin(DatagramIterator &scan, BamReader *) {
48 _color.read_datagram(scan);
49}
50
51/**
52 *
53 */
54Light::
55~Light() {
56}
57
58/**
59 * Returns true if this is an AmbientLight, false if it is some other kind of
60 * light.
61 */
63is_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 */
76void Light::
77set_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 cdata->_viz_geom_stale = true;
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 */
136PN_stdfloat Light::
137get_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 */
145const LColor &Light::
146get_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 */
156const LVecBase3 &Light::
157get_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 */
166attrib_ref() {
167}
168
169/**
170 * This is called when the light is removed from a LightAttrib.
171 */
173attrib_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 */
189get_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 */
199get_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 */
215void Light::
216fill_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 */
223void Light::
224write_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 */
242void Light::
243fillin(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) {
251 } else {
252 manager->read_cdata(scan, _cycler);
253 }
254 _priority = scan.get_int32();
255}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition bamReader.I:83
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition bamWriter.I:59
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
A single page of data maintained by a PipelineCycler.
Definition cycleData.h:50
A class to retrieve the individual data elements previously stored in a Datagram.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
bool get_bool()
Extracts a boolean value.
int32_t get_int32()
Extracts a signed 32-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition datagram.I:67
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
A node that holds Geom objects, renderable pieces of geometry.
Definition geomNode.h:34
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
virtual void attrib_ref()
This is called when the light is added to a LightAttrib.
Definition light.cxx:166
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
virtual const LVecBase3 & get_attenuation() const
Returns the terms of the attenuation equation for the light.
Definition light.cxx:157
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
virtual const LColor & get_specular_color() const
Returns the color of specular highlights generated by the light.
Definition light.cxx:146
virtual void attrib_unref()
This is called when the light is removed from a LightAttrib.
Definition light.cxx:173
set_color_temperature
Sets the color temperature of the light in kelvins.
Definition light.h:55
GeomNode * get_viz()
Returns a GeomNode that may be rendered to visualize the Light.
Definition light.cxx:199
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.