modelNode.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 modelNode.cxx
  10. * @author drose
  11. * @date 2002-03-16
  12. */
  13. #include "modelNode.h"
  14. #include "bamReader.h"
  15. #include "datagram.h"
  16. #include "datagramIterator.h"
  17. TypeHandle ModelNode::_type_handle;
  18. /**
  19. * Returns a newly-allocated Node that is a shallow copy of this one. It will
  20. * be a different Node pointer, but its internal data may or may not be shared
  21. * with that of the original Node.
  22. */
  23. PandaNode *ModelNode::
  24. make_copy() const {
  25. return new ModelNode(*this);
  26. }
  27. /**
  28. * Collapses this PandaNode with the other PandaNode, if possible, and returns
  29. * a pointer to the combined PandaNode, or NULL if the two PandaNodes cannot
  30. * safely be combined.
  31. *
  32. * The return value may be this, other, or a new PandaNode altogether.
  33. *
  34. * This function is called from GraphReducer::flatten(), and need not deal
  35. * with children; its job is just to decide whether to collapse the two
  36. * PandaNodes and what the collapsed PandaNode should look like.
  37. */
  38. PandaNode *ModelNode::
  39. combine_with(PandaNode *other) {
  40. if (_preserve_transform == PT_drop_node) {
  41. // If we have PT_drop_node set, we always yield to the other node.
  42. return other;
  43. }
  44. return PandaNode::combine_with(other);
  45. }
  46. /**
  47. * Returns true if it is generally safe to flatten out this particular kind of
  48. * Node by duplicating instances, false otherwise (for instance, a Camera
  49. * cannot be safely flattened, because the Camera pointer itself is
  50. * meaningful).
  51. */
  52. bool ModelNode::
  53. safe_to_flatten() const {
  54. return _preserve_transform == PT_drop_node;
  55. }
  56. /**
  57. * Returns true if a flatten operation may safely continue past this node, or
  58. * false if nodes below this node may not be molested.
  59. */
  60. bool ModelNode::
  61. safe_to_flatten_below() const {
  62. return _preserve_transform != PT_no_touch;
  63. }
  64. /**
  65. * Returns true if it is generally safe to transform this particular kind of
  66. * Node by calling the xform() method, false otherwise. For instance, it's
  67. * usually a bad idea to attempt to xform a Character.
  68. */
  69. bool ModelNode::
  70. safe_to_transform() const {
  71. return _preserve_transform == PT_none || _preserve_transform == PT_drop_node;
  72. }
  73. /**
  74. * Returns true if it is safe to automatically adjust the transform on this
  75. * kind of node. Usually, this is only a bad idea if the user expects to find
  76. * a particular transform on the node.
  77. *
  78. * ModelNodes with the preserve_transform flag set are presently the only
  79. * kinds of nodes that should not have their transform even adjusted.
  80. */
  81. bool ModelNode::
  82. safe_to_modify_transform() const {
  83. return _preserve_transform != PT_local && _preserve_transform != PT_no_touch;
  84. }
  85. /**
  86. * Returns true if it is generally safe to combine this particular kind of
  87. * PandaNode with other kinds of PandaNodes of compatible type, adding
  88. * children or whatever. For instance, an LODNode should not be combined with
  89. * any other PandaNode, because its set of children is meaningful.
  90. */
  91. bool ModelNode::
  92. safe_to_combine() const {
  93. return _preserve_transform == PT_drop_node;
  94. }
  95. /**
  96. * Returns true if the node's name has extrinsic meaning and must be preserved
  97. * across a flatten operation, false otherwise.
  98. */
  99. bool ModelNode::
  100. preserve_name() const {
  101. return _preserve_transform != PT_drop_node && _preserve_transform != PT_no_touch;
  102. }
  103. /**
  104. * Returns the union of all attributes from SceneGraphReducer::AttribTypes
  105. * that may not safely be applied to the vertices of this node. If this is
  106. * nonzero, these attributes must be dropped at this node as a state change.
  107. *
  108. * This is a generalization of safe_to_transform().
  109. */
  110. int ModelNode::
  111. get_unsafe_to_apply_attribs() const {
  112. return _preserve_attributes;
  113. }
  114. /**
  115. * Tells the BamReader how to create objects of type ModelNode.
  116. */
  117. void ModelNode::
  118. register_with_read_factory() {
  119. BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
  120. }
  121. /**
  122. * This tests the transform to make sure it's within the specified limits.
  123. * It's done so we can assert to see when an invalid transform is being
  124. * applied.
  125. */
  126. void ModelNode::
  127. test_transform(const TransformState *ts) const {
  128. LPoint3 pos(ts->get_pos());
  129. nassertv(pos[0] < _transform_limit);
  130. nassertv(pos[0] > -_transform_limit);
  131. nassertv(pos[1] < _transform_limit);
  132. nassertv(pos[1] > -_transform_limit);
  133. nassertv(pos[2] < _transform_limit);
  134. nassertv(pos[2] > -_transform_limit);
  135. }
  136. /**
  137. * node hook. This function handles outside (non-physics) actions on the
  138. * actor and updates the internal representation of the node. i.e. copy from
  139. * PandaNode to PhysicsObject
  140. */
  141. void ModelNode::
  142. transform_changed() {
  143. PandaNode::transform_changed();
  144. // get the transform
  145. CPT(TransformState) transform = get_transform();
  146. if (_transform_limit > 0.0) {
  147. test_transform(transform);
  148. }
  149. }
  150. /**
  151. * Writes the contents of this object to the datagram for shipping out to a
  152. * Bam file.
  153. */
  154. void ModelNode::
  155. write_datagram(BamWriter *manager, Datagram &dg) {
  156. PandaNode::write_datagram(manager, dg);
  157. dg.add_uint8((int)_preserve_transform);
  158. dg.add_uint16(_preserve_attributes);
  159. }
  160. /**
  161. * This function is called by the BamReader's factory when a new object of
  162. * type ModelNode is encountered in the Bam file. It should create the
  163. * ModelNode and extract its information from the file.
  164. */
  165. TypedWritable *ModelNode::
  166. make_from_bam(const FactoryParams &params) {
  167. ModelNode *node = new ModelNode("");
  168. DatagramIterator scan;
  169. BamReader *manager;
  170. parse_params(params, scan, manager);
  171. node->fillin(scan, manager);
  172. return node;
  173. }
  174. /**
  175. * This internal function is called by make_from_bam to read in all of the
  176. * relevant data from the BamFile for the new ModelNode.
  177. */
  178. void ModelNode::
  179. fillin(DatagramIterator &scan, BamReader *manager) {
  180. PandaNode::fillin(scan, manager);
  181. _preserve_transform = (PreserveTransform)scan.get_uint8();
  182. _preserve_attributes = scan.get_uint16();
  183. }