Panda3D
pointLight.cxx
1 // Filename: pointLight.cxx
2 // Created by: mike (09Jan97)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pointLight.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "bamWriter.h"
18 #include "bamReader.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle PointLight::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: PointLight::CData::make_copy
26 // Access: Public, Virtual
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 CycleData *PointLight::CData::
30 make_copy() const {
31  return new CData(*this);
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: PointLight::CData::write_datagram
36 // Access: Public, Virtual
37 // Description: Writes the contents of this object to the datagram
38 // for shipping out to a Bam file.
39 ////////////////////////////////////////////////////////////////////
40 void PointLight::CData::
41 write_datagram(BamWriter *, Datagram &dg) const {
42  _specular_color.write_datagram(dg);
43  _attenuation.write_datagram(dg);
44  _point.write_datagram(dg);
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: PointLight::CData::fillin
49 // Access: Public, Virtual
50 // Description: This internal function is called by make_from_bam to
51 // read in all of the relevant data from the BamFile for
52 // the new Light.
53 ////////////////////////////////////////////////////////////////////
54 void PointLight::CData::
55 fillin(DatagramIterator &scan, BamReader *) {
56  _specular_color.read_datagram(scan);
57  _attenuation.read_datagram(scan);
58  _point.read_datagram(scan);
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: PointLight::Constructor
63 // Access: Published
64 // Description:
65 ////////////////////////////////////////////////////////////////////
66 PointLight::
67 PointLight(const string &name) :
68  LightLensNode(name)
69 {
70  PT(Lens) lens;
71  lens = new PerspectiveLens(90, 90);
72  lens->set_view_vector(1, 0, 0, 0, 0, 1);
73  set_lens(0, lens);
74  lens = new PerspectiveLens(90, 90);
75  lens->set_view_vector(-1, 0, 0, 0, 0, 1);
76  set_lens(1, lens);
77  lens = new PerspectiveLens(90, 90);
78  lens->set_view_vector(0, 1, 0, 0, 0, 1);
79  set_lens(2, lens);
80  lens = new PerspectiveLens(90, 90);
81  lens->set_view_vector(0, -1, 0, 0, 0, 1);
82  set_lens(3, lens);
83  lens = new PerspectiveLens(90, 90);
84  lens->set_view_vector(0, 0, 1, 0, 0, 1);
85  set_lens(4, lens);
86  lens = new PerspectiveLens(90, 90);
87  lens->set_view_vector(0, 0, -1, 0, 0, 1);
88  set_lens(5, lens);
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: PointLight::Copy Constructor
93 // Access: Protected
94 // Description: Do not call the copy constructor directly; instead,
95 // use make_copy() or copy_subgraph() to make a copy of
96 // a node.
97 ////////////////////////////////////////////////////////////////////
98 PointLight::
99 PointLight(const PointLight &copy) :
100  LightLensNode(copy),
101  _cycler(copy._cycler)
102 {
103  PT(Lens) lens;
104  lens = new PerspectiveLens(90, 90);
105  lens->set_view_vector(1, 0, 0, 0, 0, 1);
106  set_lens(0, lens);
107  lens = new PerspectiveLens(90, 90);
108  lens->set_view_vector(-1, 0, 0, 0, 0, 1);
109  set_lens(1, lens);
110  lens = new PerspectiveLens(90, 90);
111  lens->set_view_vector(0, 1, 0, 0, 0, 1);
112  set_lens(2, lens);
113  lens = new PerspectiveLens(90, 90);
114  lens->set_view_vector(0, -1, 0, 0, 0, 1);
115  set_lens(3, lens);
116  lens = new PerspectiveLens(90, 90);
117  lens->set_view_vector(0, 0, 1, 0, 0, 1);
118  set_lens(4, lens);
119  lens = new PerspectiveLens(90, 90);
120  lens->set_view_vector(0, 0, -1, 0, 0, 1);
121  set_lens(5, lens);
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: PointLight::make_copy
126 // Access: Public, Virtual
127 // Description: Returns a newly-allocated PandaNode that is a shallow
128 // copy of this one. It will be a different pointer,
129 // but its internal data may or may not be shared with
130 // that of the original PandaNode. No children will be
131 // copied.
132 ////////////////////////////////////////////////////////////////////
134 make_copy() const {
135  return new PointLight(*this);
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function: PointLight::xform
140 // Access: Public, Virtual
141 // Description: Transforms the contents of this PandaNode by the
142 // indicated matrix, if it means anything to do so. For
143 // most kinds of PandaNodes, this does nothing.
144 ////////////////////////////////////////////////////////////////////
145 void PointLight::
146 xform(const LMatrix4 &mat) {
148  CDWriter cdata(_cycler);
149  cdata->_point = cdata->_point * mat;
150  mark_viz_stale();
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: PointLight::write
155 // Access: Public, Virtual
156 // Description:
157 ////////////////////////////////////////////////////////////////////
158 void PointLight::
159 write(ostream &out, int indent_level) const {
160  indent(out, indent_level) << *this << ":\n";
161  indent(out, indent_level + 2)
162  << "color " << get_color() << "\n";
163  indent(out, indent_level + 2)
164  << "specular color " << get_specular_color() << "\n";
165  indent(out, indent_level + 2)
166  << "attenuation " << get_attenuation() << "\n";
167 }
168 
169 ////////////////////////////////////////////////////////////////////
170 // Function: PointLight::get_vector_to_light
171 // Access: Public, Virtual
172 // Description: Computes the vector from a particular vertex to this
173 // light. The exact vector depends on the type of light
174 // (e.g. point lights return a different result than
175 // directional lights).
176 //
177 // The input parameters are the vertex position in
178 // question, expressed in object space, and the matrix
179 // which converts from light space to object space. The
180 // result is expressed in object space.
181 //
182 // The return value is true if the result is successful,
183 // or false if it cannot be computed (e.g. for an
184 // ambient light).
185 ////////////////////////////////////////////////////////////////////
186 bool PointLight::
187 get_vector_to_light(LVector3 &result, const LPoint3 &from_object_point,
188  const LMatrix4 &to_object_space) {
189  CDReader cdata(_cycler);
190  LPoint3 point = cdata->_point * to_object_space;
191 
192  result = point - from_object_point;
193  return true;
194 }
195 
196 ////////////////////////////////////////////////////////////////////
197 // Function: PointLight::get_class_priority
198 // Access: Published, Virtual
199 // Description: Returns the relative priority associated with all
200 // lights of this class. This priority is used to order
201 // lights whose instance priority (get_priority()) is
202 // the same--the idea is that other things being equal,
203 // AmbientLights (for instance) are less important than
204 // DirectionalLights.
205 ////////////////////////////////////////////////////////////////////
206 int PointLight::
208  return (int)CP_point_priority;
209 }
210 
211 ////////////////////////////////////////////////////////////////////
212 // Function: PointLight::bind
213 // Access: Public, Virtual
214 // Description:
215 ////////////////////////////////////////////////////////////////////
216 void PointLight::
217 bind(GraphicsStateGuardianBase *gsg, const NodePath &light, int light_id) {
218  gsg->bind_light(this, light, light_id);
219 }
220 
221 ////////////////////////////////////////////////////////////////////
222 // Function: PointLight::register_with_read_factory
223 // Access: Public, Static
224 // Description: Tells the BamReader how to create objects of type
225 // PointLight.
226 ////////////////////////////////////////////////////////////////////
227 void PointLight::
229  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
230 }
231 
232 ////////////////////////////////////////////////////////////////////
233 // Function: PointLight::write_datagram
234 // Access: Public, Virtual
235 // Description: Writes the contents of this object to the datagram
236 // for shipping out to a Bam file.
237 ////////////////////////////////////////////////////////////////////
238 void PointLight::
240  LightLensNode::write_datagram(manager, dg);
241  manager->write_cdata(dg, _cycler);
242 }
243 
244 ////////////////////////////////////////////////////////////////////
245 // Function: PointLight::make_from_bam
246 // Access: Protected, Static
247 // Description: This function is called by the BamReader's factory
248 // when a new object of type PointLight is encountered
249 // in the Bam file. It should create the PointLight
250 // and extract its information from the file.
251 ////////////////////////////////////////////////////////////////////
252 TypedWritable *PointLight::
253 make_from_bam(const FactoryParams &params) {
254  PointLight *node = new PointLight("");
255  DatagramIterator scan;
256  BamReader *manager;
257 
258  parse_params(params, scan, manager);
259  node->fillin(scan, manager);
260 
261  return node;
262 }
263 
264 ////////////////////////////////////////////////////////////////////
265 // Function: PointLight::fillin
266 // Access: Protected
267 // Description: This internal function is called by make_from_bam to
268 // read in all of the relevant data from the BamFile for
269 // the new PointLight.
270 ////////////////////////////////////////////////////////////////////
271 void PointLight::
272 fillin(DatagramIterator &scan, BamReader *manager) {
273  LightLensNode::fillin(scan, manager);
274  manager->read_cdata(scan, _cycler);
275 }
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: pointLight.cxx:187
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so...
Definition: pointLight.cxx:146
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:45
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: pointLight.cxx:239
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:753
void set_lens(Lens *lens)
Sets up the LensNode using this particular Lens pointer.
Definition: lensNode.I:47
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:398
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pointLight.cxx:134
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
A perspective-type lens: a normal camera.
const LColor & get_specular_color() const FINAL
Returns the color of specular highlights generated by the light.
Definition: pointLight.I:49
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
static void register_with_read_factory()
Tells the BamReader how to create objects of type PointLight.
Definition: pointLight.cxx:228
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
const LVecBase3 & get_attenuation() const FINAL
Returns the terms of the attenuation equation for the light.
Definition: pointLight.I:75
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so...
Definition: lensNode.cxx:61
virtual int get_class_priority() const
Returns the relative priority associated with all lights of this class.
Definition: pointLight.cxx:207
A derivative of Light and of Camera.
Definition: lightLensNode.h:35
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram. ...
const LColor & get_color() const
Returns the basic color of the light.
Definition: light.I:70
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A light originating from a single point in space, and shining in all directions.
Definition: pointLight.h:27