pointLight.cxx 8.8 KB

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