Panda3D

Loading...
Searching...
No Matches
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"
16#include "graphicsStateGuardianBase.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::
28make_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::
37write_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::
48fillin(DatagramIterator &scan, BamReader *) {
49 _specular_color.read_datagram(scan);
50 _point.read_datagram(scan);
51 _direction.read_datagram(scan);
52}
53
54/**
55 *
56 */
57DirectionalLight::
58DirectionalLight(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 */
67DirectionalLight::
68DirectionalLight(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 */
79 PandaNode *DirectionalLight::
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 */
88 void DirectionalLight::
89 xform(const LMatrix4 &mat) {
90 LightLensNode::xform(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::
101write(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 */
125 bool DirectionalLight::
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 */
140 int DirectionalLight::
141 get_class_priority() const {
142 return (int)CP_directional_priority;
143}
144
145/**
146 *
147 */
148 void DirectionalLight::
149bind(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 */
156 void DirectionalLight::
157 register_with_read_factory() {
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 */
165 void DirectionalLight::
166 write_datagram(BamWriter *manager, Datagram &dg) {
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::
180make_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::
196fillin(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}
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
parse_params
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
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
BamReader::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition bamReader.I:83
BamReader::read_cdata
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition bamReader.cxx:695
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
BamWriter::write_cdata
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition bamWriter.cxx:425
BamWriter::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition bamWriter.I:59
CycleData
A single page of data maintained by a PipelineCycler.
Definition cycleData.h:50
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition datagramIterator.h:30
DatagramIterator::get_bool
bool get_bool()
Extracts a boolean value.
Definition datagramIterator.I:48
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
Datagram::add_bool
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
DirectionalLight
A light shining from infinitely far away in a particular direction, like sunlight.
Definition directionalLight.h:25
DirectionalLight::get_direction
get_direction
Returns the direction in which the light is aimed.
Definition directionalLight.h:53
DirectionalLight::get_specular_color
get_specular_color
Returns the color of specular highlights generated by the light.
Definition directionalLight.h:45
DirectionalLight::write_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.
Definition directionalLight.cxx:166
DirectionalLight::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type DirectionalLight.
Definition directionalLight.cxx:157
DirectionalLight::get_vector_to_light
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 directionalLight.cxx:126
DirectionalLight::get_class_priority
virtual int get_class_priority() const
Returns the relative priority associated with all lights of this class.
Definition directionalLight.cxx:141
DirectionalLight::xform
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so.
Definition directionalLight.cxx:89
DirectionalLight::make_copy
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition directionalLight.cxx:80
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition factoryParams.h:36
Factory::register_factory
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
GraphicsStateGuardianBase
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
Definition graphicsStateGuardianBase.h:110
LensNode::xform
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
LightLensNode
A derivative of Light and of Camera.
Definition lightLensNode.h:33
LightLensNode::write_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.
Definition lightLensNode.cxx:234
Light::get_color
get_color
Returns the basic color of the light.
Definition light.h:49
NodePath
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition nodePath.h:159
PandaNode
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition typedWritable.h:35
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
directionalLight.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
orthographicLens.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.