Panda3D
|
00001 // Filename: pointLight.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 "pointLight.h" 00016 #include "graphicsStateGuardianBase.h" 00017 #include "bamWriter.h" 00018 #include "bamReader.h" 00019 #include "datagram.h" 00020 #include "datagramIterator.h" 00021 00022 TypeHandle PointLight::_type_handle; 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Function: PointLight::CData::make_copy 00026 // Access: Public, Virtual 00027 // Description: 00028 //////////////////////////////////////////////////////////////////// 00029 CycleData *PointLight::CData:: 00030 make_copy() const { 00031 return new CData(*this); 00032 } 00033 00034 //////////////////////////////////////////////////////////////////// 00035 // Function: PointLight::CData::write_datagram 00036 // Access: Public, Virtual 00037 // Description: Writes the contents of this object to the datagram 00038 // for shipping out to a Bam file. 00039 //////////////////////////////////////////////////////////////////// 00040 void PointLight::CData:: 00041 write_datagram(BamWriter *, Datagram &dg) const { 00042 _specular_color.write_datagram(dg); 00043 _attenuation.write_datagram(dg); 00044 _point.write_datagram(dg); 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: PointLight::CData::fillin 00049 // Access: Public, Virtual 00050 // Description: This internal function is called by make_from_bam to 00051 // read in all of the relevant data from the BamFile for 00052 // the new Light. 00053 //////////////////////////////////////////////////////////////////// 00054 void PointLight::CData:: 00055 fillin(DatagramIterator &scan, BamReader *) { 00056 _specular_color.read_datagram(scan); 00057 _attenuation.read_datagram(scan); 00058 _point.read_datagram(scan); 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: PointLight::Constructor 00063 // Access: Published 00064 // Description: 00065 //////////////////////////////////////////////////////////////////// 00066 PointLight:: 00067 PointLight(const string &name) : 00068 LightLensNode(name) 00069 { 00070 PT(Lens) lens; 00071 lens = new PerspectiveLens(90, 90); 00072 lens->set_view_vector(1, 0, 0, 0, 0, 1); 00073 set_lens(0, lens); 00074 lens = new PerspectiveLens(90, 90); 00075 lens->set_view_vector(-1, 0, 0, 0, 0, 1); 00076 set_lens(1, lens); 00077 lens = new PerspectiveLens(90, 90); 00078 lens->set_view_vector(0, 1, 0, 0, 0, 1); 00079 set_lens(2, lens); 00080 lens = new PerspectiveLens(90, 90); 00081 lens->set_view_vector(0, -1, 0, 0, 0, 1); 00082 set_lens(3, lens); 00083 lens = new PerspectiveLens(90, 90); 00084 lens->set_view_vector(0, 0, 1, 0, 0, 1); 00085 set_lens(4, lens); 00086 lens = new PerspectiveLens(90, 90); 00087 lens->set_view_vector(0, 0, -1, 0, 0, 1); 00088 set_lens(5, lens); 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: PointLight::Copy Constructor 00093 // Access: Protected 00094 // Description: Do not call the copy constructor directly; instead, 00095 // use make_copy() or copy_subgraph() to make a copy of 00096 // a node. 00097 //////////////////////////////////////////////////////////////////// 00098 PointLight:: 00099 PointLight(const PointLight ©) : 00100 LightLensNode(copy), 00101 _cycler(copy._cycler) 00102 { 00103 PT(Lens) lens; 00104 lens = new PerspectiveLens(90, 90); 00105 lens->set_view_vector(1, 0, 0, 0, 0, 1); 00106 set_lens(0, lens); 00107 lens = new PerspectiveLens(90, 90); 00108 lens->set_view_vector(-1, 0, 0, 0, 0, 1); 00109 set_lens(1, lens); 00110 lens = new PerspectiveLens(90, 90); 00111 lens->set_view_vector(0, 1, 0, 0, 0, 1); 00112 set_lens(2, lens); 00113 lens = new PerspectiveLens(90, 90); 00114 lens->set_view_vector(0, -1, 0, 0, 0, 1); 00115 set_lens(3, lens); 00116 lens = new PerspectiveLens(90, 90); 00117 lens->set_view_vector(0, 0, 1, 0, 0, 1); 00118 set_lens(4, lens); 00119 lens = new PerspectiveLens(90, 90); 00120 lens->set_view_vector(0, 0, -1, 0, 0, 1); 00121 set_lens(5, lens); 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function: PointLight::make_copy 00126 // Access: Public, Virtual 00127 // Description: Returns a newly-allocated PandaNode that is a shallow 00128 // copy of this one. It will be a different pointer, 00129 // but its internal data may or may not be shared with 00130 // that of the original PandaNode. No children will be 00131 // copied. 00132 //////////////////////////////////////////////////////////////////// 00133 PandaNode *PointLight:: 00134 make_copy() const { 00135 return new PointLight(*this); 00136 } 00137 00138 //////////////////////////////////////////////////////////////////// 00139 // Function: PointLight::xform 00140 // Access: Public, Virtual 00141 // Description: Transforms the contents of this PandaNode by the 00142 // indicated matrix, if it means anything to do so. For 00143 // most kinds of PandaNodes, this does nothing. 00144 //////////////////////////////////////////////////////////////////// 00145 void PointLight:: 00146 xform(const LMatrix4 &mat) { 00147 LightLensNode::xform(mat); 00148 CDWriter cdata(_cycler); 00149 cdata->_point = cdata->_point * mat; 00150 mark_viz_stale(); 00151 } 00152 00153 //////////////////////////////////////////////////////////////////// 00154 // Function: PointLight::write 00155 // Access: Public, Virtual 00156 // Description: 00157 //////////////////////////////////////////////////////////////////// 00158 void PointLight:: 00159 write(ostream &out, int indent_level) const { 00160 indent(out, indent_level) << *this << ":\n"; 00161 indent(out, indent_level + 2) 00162 << "color " << get_color() << "\n"; 00163 indent(out, indent_level + 2) 00164 << "specular color " << get_specular_color() << "\n"; 00165 indent(out, indent_level + 2) 00166 << "attenuation " << get_attenuation() << "\n"; 00167 } 00168 00169 //////////////////////////////////////////////////////////////////// 00170 // Function: PointLight::get_vector_to_light 00171 // Access: Public, Virtual 00172 // Description: Computes the vector from a particular vertex to this 00173 // light. The exact vector depends on the type of light 00174 // (e.g. point lights return a different result than 00175 // directional lights). 00176 // 00177 // The input parameters are the vertex position in 00178 // question, expressed in object space, and the matrix 00179 // which converts from light space to object space. The 00180 // result is expressed in object space. 00181 // 00182 // The return value is true if the result is successful, 00183 // or false if it cannot be computed (e.g. for an 00184 // ambient light). 00185 //////////////////////////////////////////////////////////////////// 00186 bool PointLight:: 00187 get_vector_to_light(LVector3 &result, const LPoint3 &from_object_point, 00188 const LMatrix4 &to_object_space) { 00189 CDReader cdata(_cycler); 00190 LPoint3 point = cdata->_point * to_object_space; 00191 00192 result = point - from_object_point; 00193 return true; 00194 } 00195 00196 //////////////////////////////////////////////////////////////////// 00197 // Function: PointLight::get_class_priority 00198 // Access: Published, Virtual 00199 // Description: Returns the relative priority associated with all 00200 // lights of this class. This priority is used to order 00201 // lights whose instance priority (get_priority()) is 00202 // the same--the idea is that other things being equal, 00203 // AmbientLights (for instance) are less important than 00204 // DirectionalLights. 00205 //////////////////////////////////////////////////////////////////// 00206 int PointLight:: 00207 get_class_priority() const { 00208 return (int)CP_point_priority; 00209 } 00210 00211 //////////////////////////////////////////////////////////////////// 00212 // Function: PointLight::bind 00213 // Access: Public, Virtual 00214 // Description: 00215 //////////////////////////////////////////////////////////////////// 00216 void PointLight:: 00217 bind(GraphicsStateGuardianBase *gsg, const NodePath &light, int light_id) { 00218 gsg->bind_light(this, light, light_id); 00219 } 00220 00221 //////////////////////////////////////////////////////////////////// 00222 // Function: PointLight::register_with_read_factory 00223 // Access: Public, Static 00224 // Description: Tells the BamReader how to create objects of type 00225 // PointLight. 00226 //////////////////////////////////////////////////////////////////// 00227 void PointLight:: 00228 register_with_read_factory() { 00229 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00230 } 00231 00232 //////////////////////////////////////////////////////////////////// 00233 // Function: PointLight::write_datagram 00234 // Access: Public, Virtual 00235 // Description: Writes the contents of this object to the datagram 00236 // for shipping out to a Bam file. 00237 //////////////////////////////////////////////////////////////////// 00238 void PointLight:: 00239 write_datagram(BamWriter *manager, Datagram &dg) { 00240 LightLensNode::write_datagram(manager, dg); 00241 manager->write_cdata(dg, _cycler); 00242 } 00243 00244 //////////////////////////////////////////////////////////////////// 00245 // Function: PointLight::make_from_bam 00246 // Access: Protected, Static 00247 // Description: This function is called by the BamReader's factory 00248 // when a new object of type PointLight is encountered 00249 // in the Bam file. It should create the PointLight 00250 // and extract its information from the file. 00251 //////////////////////////////////////////////////////////////////// 00252 TypedWritable *PointLight:: 00253 make_from_bam(const FactoryParams ¶ms) { 00254 PointLight *node = new PointLight(""); 00255 DatagramIterator scan; 00256 BamReader *manager; 00257 00258 parse_params(params, scan, manager); 00259 node->fillin(scan, manager); 00260 00261 return node; 00262 } 00263 00264 //////////////////////////////////////////////////////////////////// 00265 // Function: PointLight::fillin 00266 // Access: Protected 00267 // Description: This internal function is called by make_from_bam to 00268 // read in all of the relevant data from the BamFile for 00269 // the new PointLight. 00270 //////////////////////////////////////////////////////////////////// 00271 void PointLight:: 00272 fillin(DatagramIterator &scan, BamReader *manager) { 00273 LightLensNode::fillin(scan, manager); 00274 manager->read_cdata(scan, _cycler); 00275 }