Panda3D
directionalLight.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 directionalLight.cxx
10  * @author mike
11  * @date 1997-01-09
12  */
13 
14 #include "directionalLight.h"
15 #include "orthographicLens.h"
17 #include "bamWriter.h"
18 #include "bamReader.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle DirectionalLight::_type_handle;
23 
24 /**
25  *
26  */
27 CycleData *DirectionalLight::CData::
28 make_copy() const {
29  return new CData(*this);
30 }
31 
32 /**
33  * Writes the contents of this object to the datagram for shipping out to a
34  * Bam file.
35  */
36 void DirectionalLight::CData::
37 write_datagram(BamWriter *, Datagram &dg) const {
38  _specular_color.write_datagram(dg);
39  _point.write_datagram(dg);
40  _direction.write_datagram(dg);
41 }
42 
43 /**
44  * This internal function is called by make_from_bam to read in all of the
45  * relevant data from the BamFile for the new Light.
46  */
47 void DirectionalLight::CData::
48 fillin(DatagramIterator &scan, BamReader *) {
49  _specular_color.read_datagram(scan);
50  _point.read_datagram(scan);
51  _direction.read_datagram(scan);
52 }
53 
54 /**
55  *
56  */
57 DirectionalLight::
58 DirectionalLight(const std::string &name) :
59  LightLensNode(name, new OrthographicLens()) {
60  _lenses[0]._lens->set_interocular_distance(0);
61 }
62 
63 /**
64  * Do not call the copy constructor directly; instead, use make_copy() or
65  * copy_subgraph() to make a copy of a node.
66  */
67 DirectionalLight::
68 DirectionalLight(const DirectionalLight &copy) :
69  LightLensNode(copy),
70  _cycler(copy._cycler)
71 {
72 }
73 
74 /**
75  * Returns a newly-allocated PandaNode that is a shallow copy of this one. It
76  * will be a different pointer, but its internal data may or may not be shared
77  * with that of the original PandaNode. No children will be copied.
78  */
80 make_copy() const {
81  return new DirectionalLight(*this);
82 }
83 
84 /**
85  * Transforms the contents of this PandaNode by the indicated matrix, if it
86  * means anything to do so. For most kinds of PandaNodes, this does nothing.
87  */
89 xform(const LMatrix4 &mat) {
91  CDWriter cdata(_cycler);
92  cdata->_point = cdata->_point * mat;
93  cdata->_direction = cdata->_direction * mat;
94  mark_viz_stale();
95 }
96 
97 /**
98  *
99  */
100 void DirectionalLight::
101 write(std::ostream &out, int indent_level) const {
102  indent(out, indent_level) << *this << ":\n";
103  indent(out, indent_level + 2)
104  << "color " << get_color() << "\n";
105  if (_has_specular_color) {
106  indent(out, indent_level + 2)
107  << "specular color " << get_specular_color() << "\n";
108  }
109  indent(out, indent_level + 2)
110  << "direction " << get_direction() << "\n";
111 }
112 
113 /**
114  * Computes the vector from a particular vertex to this light. The exact
115  * vector depends on the type of light (e.g. point lights return a different
116  * result than directional lights).
117  *
118  * The input parameters are the vertex position in question, expressed in
119  * object space, and the matrix which converts from light space to object
120  * space. The result is expressed in object space.
121  *
122  * The return value is true if the result is successful, or false if it cannot
123  * be computed (e.g. for an ambient light).
124  */
126 get_vector_to_light(LVector3 &result, const LPoint3 &,
127  const LMatrix4 &to_object_space) {
128  CDReader cdata(_cycler);
129  result = -(cdata->_direction * to_object_space);
130 
131  return true;
132 }
133 
134 /**
135  * Returns the relative priority associated with all lights of this class.
136  * This priority is used to order lights whose instance priority
137  * (get_priority()) is the same--the idea is that other things being equal,
138  * AmbientLights (for instance) are less important than DirectionalLights.
139  */
142  return (int)CP_directional_priority;
143 }
144 
145 /**
146  *
147  */
148 void DirectionalLight::
149 bind(GraphicsStateGuardianBase *gsg, const NodePath &light, int light_id) {
150  gsg->bind_light(this, light, light_id);
151 }
152 
153 /**
154  * Tells the BamReader how to create objects of type DirectionalLight.
155  */
158  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
159 }
160 
161 /**
162  * Writes the contents of this object to the datagram for shipping out to a
163  * Bam file.
164  */
167  LightLensNode::write_datagram(manager, dg);
168  if (manager->get_file_minor_ver() >= 39) {
169  dg.add_bool(_has_specular_color);
170  }
171  manager->write_cdata(dg, _cycler);
172 }
173 
174 /**
175  * This function is called by the BamReader's factory when a new object of
176  * type DirectionalLight is encountered in the Bam file. It should create the
177  * DirectionalLight and extract its information from the file.
178  */
179 TypedWritable *DirectionalLight::
180 make_from_bam(const FactoryParams &params) {
181  DirectionalLight *node = new DirectionalLight("");
182  DatagramIterator scan;
183  BamReader *manager;
184 
185  parse_params(params, scan, manager);
186  node->fillin(scan, manager);
187 
188  return node;
189 }
190 
191 /**
192  * This internal function is called by make_from_bam to read in all of the
193  * relevant data from the BamFile for the new DirectionalLight.
194  */
195 void DirectionalLight::
196 fillin(DatagramIterator &scan, BamReader *manager) {
197  LightLensNode::fillin(scan, manager);
198 
199  if (manager->get_file_minor_ver() >= 39) {
200  _has_specular_color = scan.get_bool();
201  } else {
202  _has_specular_color = true;
203  }
204 
205  manager->read_cdata(scan, _cycler);
206 }
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:64
bool get_bool()
Extracts a boolean value.
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: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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:35
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:425
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
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
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:34
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An orthographic lens.
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:177
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:53
get_direction
Returns the direction in which the light is aimed.
A derivative of Light and of Camera.
Definition: lightLensNode.h:33
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.
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:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
const LColor & get_specular_color() const final
Returns the color of specular highlights generated by the light.