Panda3D
directionalLight.cxx
1 // Filename: directionalLight.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 "directionalLight.h"
16 #include "orthographicLens.h"
17 #include "graphicsStateGuardianBase.h"
18 #include "bamWriter.h"
19 #include "bamReader.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 
23 TypeHandle DirectionalLight::_type_handle;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: DirectionalLight::CData::make_copy
27 // Access: Public, Virtual
28 // Description:
29 ////////////////////////////////////////////////////////////////////
30 CycleData *DirectionalLight::CData::
31 make_copy() const {
32  return new CData(*this);
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: DirectionalLight::CData::write_datagram
37 // Access: Public, Virtual
38 // Description: Writes the contents of this object to the datagram
39 // for shipping out to a Bam file.
40 ////////////////////////////////////////////////////////////////////
41 void DirectionalLight::CData::
42 write_datagram(BamWriter *, Datagram &dg) const {
43  _specular_color.write_datagram(dg);
44  _point.write_datagram(dg);
45  _direction.write_datagram(dg);
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: DirectionalLight::CData::fillin
50 // Access: Public, Virtual
51 // Description: This internal function is called by make_from_bam to
52 // read in all of the relevant data from the BamFile for
53 // the new Light.
54 ////////////////////////////////////////////////////////////////////
55 void DirectionalLight::CData::
56 fillin(DatagramIterator &scan, BamReader *) {
57  _specular_color.read_datagram(scan);
58  _point.read_datagram(scan);
59  _direction.read_datagram(scan);
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: DirectionalLight::Constructor
64 // Access: Published
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 DirectionalLight::
68 DirectionalLight(const string &name) :
69  LightLensNode(name, new OrthographicLens())
70 {
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: DirectionalLight::Copy Constructor
75 // Access: Protected
76 // Description: Do not call the copy constructor directly; instead,
77 // use make_copy() or copy_subgraph() to make a copy of
78 // a node.
79 ////////////////////////////////////////////////////////////////////
80 DirectionalLight::
81 DirectionalLight(const DirectionalLight &copy) :
82  LightLensNode(copy),
83  _cycler(copy._cycler)
84 {
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: DirectionalLight::make_copy
89 // Access: Public, Virtual
90 // Description: Returns a newly-allocated PandaNode that is a shallow
91 // copy of this one. It will be a different pointer,
92 // but its internal data may or may not be shared with
93 // that of the original PandaNode. No children will be
94 // copied.
95 ////////////////////////////////////////////////////////////////////
97 make_copy() const {
98  return new DirectionalLight(*this);
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: DirectionalLight::xform
103 // Access: Public, Virtual
104 // Description: Transforms the contents of this PandaNode by the
105 // indicated matrix, if it means anything to do so. For
106 // most kinds of PandaNodes, this does nothing.
107 ////////////////////////////////////////////////////////////////////
109 xform(const LMatrix4 &mat) {
111  CDWriter cdata(_cycler);
112  cdata->_point = cdata->_point * mat;
113  cdata->_direction = cdata->_direction * mat;
114  mark_viz_stale();
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: DirectionalLight::write
119 // Access: Public, Virtual
120 // Description:
121 ////////////////////////////////////////////////////////////////////
122 void DirectionalLight::
123 write(ostream &out, int indent_level) const {
124  indent(out, indent_level) << *this << ":\n";
125  indent(out, indent_level + 2)
126  << "color " << get_color() << "\n";
127  indent(out, indent_level + 2)
128  << "specular color " << get_specular_color() << "\n";
129  indent(out, indent_level + 2)
130  << "direction " << get_direction() << "\n";
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: DirectionalLight::get_vector_to_light
135 // Access: Public, Virtual
136 // Description: Computes the vector from a particular vertex to this
137 // light. The exact vector depends on the type of light
138 // (e.g. point lights return a different result than
139 // directional lights).
140 //
141 // The input parameters are the vertex position in
142 // question, expressed in object space, and the matrix
143 // which converts from light space to object space. The
144 // result is expressed in object space.
145 //
146 // The return value is true if the result is successful,
147 // or false if it cannot be computed (e.g. for an
148 // ambient light).
149 ////////////////////////////////////////////////////////////////////
152  const LMatrix4 &to_object_space) {
153  CDReader cdata(_cycler);
154  result = -(cdata->_direction * to_object_space);
155 
156  return true;
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: DirectionalLight::get_class_priority
161 // Access: Published, Virtual
162 // Description: Returns the relative priority associated with all
163 // lights of this class. This priority is used to order
164 // lights whose instance priority (get_priority()) is
165 // the same--the idea is that other things being equal,
166 // AmbientLights (for instance) are less important than
167 // DirectionalLights.
168 ////////////////////////////////////////////////////////////////////
171  return (int)CP_directional_priority;
172 }
173 
174 ////////////////////////////////////////////////////////////////////
175 // Function: DirectionalLight::bind
176 // Access: Public, Virtual
177 // Description:
178 ////////////////////////////////////////////////////////////////////
179 void DirectionalLight::
180 bind(GraphicsStateGuardianBase *gsg, const NodePath &light, int light_id) {
181  gsg->bind_light(this, light, light_id);
182 }
183 
184 ////////////////////////////////////////////////////////////////////
185 // Function: DirectionalLight::register_with_read_factory
186 // Access: Public, Static
187 // Description: Tells the BamReader how to create objects of type
188 // DirectionalLight.
189 ////////////////////////////////////////////////////////////////////
192  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
193 }
194 
195 ////////////////////////////////////////////////////////////////////
196 // Function: DirectionalLight::write_datagram
197 // Access: Public, Virtual
198 // Description: Writes the contents of this object to the datagram
199 // for shipping out to a Bam file.
200 ////////////////////////////////////////////////////////////////////
203  LightLensNode::write_datagram(manager, dg);
204  manager->write_cdata(dg, _cycler);
205 }
206 
207 ////////////////////////////////////////////////////////////////////
208 // Function: DirectionalLight::make_from_bam
209 // Access: Protected, Static
210 // Description: This function is called by the BamReader's factory
211 // when a new object of type DirectionalLight is encountered
212 // in the Bam file. It should create the DirectionalLight
213 // and extract its information from the file.
214 ////////////////////////////////////////////////////////////////////
215 TypedWritable *DirectionalLight::
216 make_from_bam(const FactoryParams &params) {
217  DirectionalLight *node = new DirectionalLight("");
218  DatagramIterator scan;
219  BamReader *manager;
220 
221  parse_params(params, scan, manager);
222  node->fillin(scan, manager);
223 
224  return node;
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: DirectionalLight::fillin
229 // Access: Protected
230 // Description: This internal function is called by make_from_bam to
231 // read in all of the relevant data from the BamFile for
232 // the new DirectionalLight.
233 ////////////////////////////////////////////////////////////////////
234 void DirectionalLight::
235 fillin(DatagramIterator &scan, BamReader *manager) {
236  LightLensNode::fillin(scan, manager);
237  manager->read_cdata(scan, _cycler);
238 }
A light shining from infinitely far away in a particular direction, like sunlight.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
static void register_with_read_factory()
Tells the BamReader how to create objects of type DirectionalLight.
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
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
virtual int get_class_priority() const
Returns the relative priority associated with all lights of this class.
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
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so...
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
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 ...
const LColor & get_specular_color() const FINAL
Returns the color of specular highlights generated by the light.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
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
An orthographic lens.
const LVector3 & get_direction() const
Returns the direction in which the light is aimed.
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
A derivative of Light and of Camera.
Definition: lightLensNode.h:35
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
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
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
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
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.