Panda3D

directionalLight.cxx

00001 // Filename: directionalLight.cxx
00002 // Created by:  mike (09Jan97)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "directionalLight.h"
00016 #include "orthographicLens.h"
00017 #include "graphicsStateGuardianBase.h"
00018 #include "bamWriter.h"
00019 #include "bamReader.h"
00020 #include "datagram.h"
00021 #include "datagramIterator.h"
00022 
00023 TypeHandle DirectionalLight::_type_handle;
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: DirectionalLight::CData::make_copy
00027 //       Access: Public, Virtual
00028 //  Description:
00029 ////////////////////////////////////////////////////////////////////
00030 CycleData *DirectionalLight::CData::
00031 make_copy() const {
00032   return new CData(*this);
00033 }
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: DirectionalLight::CData::write_datagram
00037 //       Access: Public, Virtual
00038 //  Description: Writes the contents of this object to the datagram
00039 //               for shipping out to a Bam file.
00040 ////////////////////////////////////////////////////////////////////
00041 void DirectionalLight::CData::
00042 write_datagram(BamWriter *, Datagram &dg) const {
00043   _specular_color.write_datagram(dg);
00044   _point.write_datagram(dg);
00045   _direction.write_datagram(dg);
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: DirectionalLight::CData::fillin
00050 //       Access: Public, Virtual
00051 //  Description: This internal function is called by make_from_bam to
00052 //               read in all of the relevant data from the BamFile for
00053 //               the new Light.
00054 ////////////////////////////////////////////////////////////////////
00055 void DirectionalLight::CData::
00056 fillin(DatagramIterator &scan, BamReader *) {
00057   _specular_color.read_datagram(scan);
00058   _point.read_datagram(scan);
00059   _direction.read_datagram(scan);
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: DirectionalLight::Constructor
00064 //       Access: Published
00065 //  Description:
00066 ////////////////////////////////////////////////////////////////////
00067 DirectionalLight::
00068 DirectionalLight(const string &name) : 
00069   LightLensNode(name, new OrthographicLens())
00070 {
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //     Function: DirectionalLight::Copy Constructor
00075 //       Access: Protected
00076 //  Description: Do not call the copy constructor directly; instead,
00077 //               use make_copy() or copy_subgraph() to make a copy of
00078 //               a node.
00079 ////////////////////////////////////////////////////////////////////
00080 DirectionalLight::
00081 DirectionalLight(const DirectionalLight &copy) :
00082   LightLensNode(copy),
00083   _cycler(copy._cycler)
00084 {
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: DirectionalLight::make_copy
00089 //       Access: Public, Virtual
00090 //  Description: Returns a newly-allocated PandaNode that is a shallow
00091 //               copy of this one.  It will be a different pointer,
00092 //               but its internal data may or may not be shared with
00093 //               that of the original PandaNode.  No children will be
00094 //               copied.
00095 ////////////////////////////////////////////////////////////////////
00096 PandaNode *DirectionalLight::
00097 make_copy() const {
00098   return new DirectionalLight(*this);
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: DirectionalLight::xform
00103 //       Access: Public, Virtual
00104 //  Description: Transforms the contents of this PandaNode by the
00105 //               indicated matrix, if it means anything to do so.  For
00106 //               most kinds of PandaNodes, this does nothing.
00107 ////////////////////////////////////////////////////////////////////
00108 void DirectionalLight::
00109 xform(const LMatrix4 &mat) {
00110   LightLensNode::xform(mat);
00111   CDWriter cdata(_cycler);
00112   cdata->_point = cdata->_point * mat;
00113   cdata->_direction = cdata->_direction * mat;
00114   mark_viz_stale();
00115 }
00116 
00117 ////////////////////////////////////////////////////////////////////
00118 //     Function: DirectionalLight::write
00119 //       Access: Public, Virtual
00120 //  Description:
00121 ////////////////////////////////////////////////////////////////////
00122 void DirectionalLight::
00123 write(ostream &out, int indent_level) const {
00124   indent(out, indent_level) << *this << ":\n";
00125   indent(out, indent_level + 2)
00126     << "color " << get_color() << "\n";
00127   indent(out, indent_level + 2)
00128     << "specular color " << get_specular_color() << "\n";
00129   indent(out, indent_level + 2)
00130     << "direction " << get_direction() << "\n";
00131 }
00132 
00133 ////////////////////////////////////////////////////////////////////
00134 //     Function: DirectionalLight::get_vector_to_light
00135 //       Access: Public, Virtual
00136 //  Description: Computes the vector from a particular vertex to this
00137 //               light.  The exact vector depends on the type of light
00138 //               (e.g. point lights return a different result than
00139 //               directional lights).
00140 //
00141 //               The input parameters are the vertex position in
00142 //               question, expressed in object space, and the matrix
00143 //               which converts from light space to object space.  The
00144 //               result is expressed in object space.
00145 //
00146 //               The return value is true if the result is successful,
00147 //               or false if it cannot be computed (e.g. for an
00148 //               ambient light).
00149 ////////////////////////////////////////////////////////////////////
00150 bool DirectionalLight::
00151 get_vector_to_light(LVector3 &result, const LPoint3 &, 
00152                     const LMatrix4 &to_object_space) {
00153   CDReader cdata(_cycler);
00154   result = -(cdata->_direction * to_object_space);
00155 
00156   return true;
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: DirectionalLight::get_class_priority
00161 //       Access: Published, Virtual
00162 //  Description: Returns the relative priority associated with all
00163 //               lights of this class.  This priority is used to order
00164 //               lights whose instance priority (get_priority()) is
00165 //               the same--the idea is that other things being equal,
00166 //               AmbientLights (for instance) are less important than
00167 //               DirectionalLights.
00168 ////////////////////////////////////////////////////////////////////
00169 int DirectionalLight::
00170 get_class_priority() const {
00171   return (int)CP_directional_priority;
00172 }
00173 
00174 ////////////////////////////////////////////////////////////////////
00175 //     Function: DirectionalLight::bind
00176 //       Access: Public, Virtual
00177 //  Description:
00178 ////////////////////////////////////////////////////////////////////
00179 void DirectionalLight::
00180 bind(GraphicsStateGuardianBase *gsg, const NodePath &light, int light_id) {
00181   gsg->bind_light(this, light, light_id);
00182 }
00183 
00184 ////////////////////////////////////////////////////////////////////
00185 //     Function: DirectionalLight::register_with_read_factory
00186 //       Access: Public, Static
00187 //  Description: Tells the BamReader how to create objects of type
00188 //               DirectionalLight.
00189 ////////////////////////////////////////////////////////////////////
00190 void DirectionalLight::
00191 register_with_read_factory() {
00192   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: DirectionalLight::write_datagram
00197 //       Access: Public, Virtual
00198 //  Description: Writes the contents of this object to the datagram
00199 //               for shipping out to a Bam file.
00200 ////////////////////////////////////////////////////////////////////
00201 void DirectionalLight::
00202 write_datagram(BamWriter *manager, Datagram &dg) {
00203   LightLensNode::write_datagram(manager, dg);
00204   manager->write_cdata(dg, _cycler);
00205 }
00206 
00207 ////////////////////////////////////////////////////////////////////
00208 //     Function: DirectionalLight::make_from_bam
00209 //       Access: Protected, Static
00210 //  Description: This function is called by the BamReader's factory
00211 //               when a new object of type DirectionalLight is encountered
00212 //               in the Bam file.  It should create the DirectionalLight
00213 //               and extract its information from the file.
00214 ////////////////////////////////////////////////////////////////////
00215 TypedWritable *DirectionalLight::
00216 make_from_bam(const FactoryParams &params) {
00217   DirectionalLight *node = new DirectionalLight("");
00218   DatagramIterator scan;
00219   BamReader *manager;
00220 
00221   parse_params(params, scan, manager);
00222   node->fillin(scan, manager);
00223 
00224   return node;
00225 }
00226 
00227 ////////////////////////////////////////////////////////////////////
00228 //     Function: DirectionalLight::fillin
00229 //       Access: Protected
00230 //  Description: This internal function is called by make_from_bam to
00231 //               read in all of the relevant data from the BamFile for
00232 //               the new DirectionalLight.
00233 ////////////////////////////////////////////////////////////////////
00234 void DirectionalLight::
00235 fillin(DatagramIterator &scan, BamReader *manager) {
00236   LightLensNode::fillin(scan, manager);
00237   manager->read_cdata(scan, _cycler);
00238 }
 All Classes Functions Variables Enumerations