eggNode.I 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Filename: eggNode.I
  2. // Created by: drose (10Feb99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: EggNode::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. INLINE EggNode::
  24. EggNode(const string &name) : EggNamedObject(name) {
  25. _parent = NULL;
  26. _depth = 0;
  27. _under_flags = 0;
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: EggNode::Copy constructor
  31. // Access: Public
  32. // Description:
  33. ////////////////////////////////////////////////////////////////////
  34. INLINE EggNode::
  35. EggNode(const EggNode &copy) : EggNamedObject(copy) {
  36. _parent = NULL;
  37. _depth = 0;
  38. _under_flags = 0;
  39. }
  40. ////////////////////////////////////////////////////////////////////
  41. // Function: EggNode::Copy assignment operator
  42. // Access: Public
  43. // Description:
  44. ////////////////////////////////////////////////////////////////////
  45. INLINE EggNode &EggNode::
  46. operator = (const EggNode &copy) {
  47. EggNamedObject::operator = (copy);
  48. update_under(0);
  49. return *this;
  50. }
  51. ////////////////////////////////////////////////////////////////////
  52. // Function: EggNode::get_parent
  53. // Access: Public
  54. // Description:
  55. ////////////////////////////////////////////////////////////////////
  56. INLINE EggGroupNode *EggNode::
  57. get_parent() const {
  58. return _parent;
  59. }
  60. ////////////////////////////////////////////////////////////////////
  61. // Function: EggNode::get_depth
  62. // Access: Public
  63. // Description: Returns the number of nodes above this node in the
  64. // egg hierarchy.
  65. ////////////////////////////////////////////////////////////////////
  66. INLINE int EggNode::
  67. get_depth() const {
  68. return _depth;
  69. }
  70. ////////////////////////////////////////////////////////////////////
  71. // Function: EggNode::is_under_instance
  72. // Access: Public
  73. // Description: Returns true if there is an <Instance> node somewhere
  74. // in the egg tree at or above this node, false
  75. // otherwise.
  76. ////////////////////////////////////////////////////////////////////
  77. INLINE bool EggNode::
  78. is_under_instance() const {
  79. return (_under_flags & UF_under_instance) != 0;
  80. }
  81. ////////////////////////////////////////////////////////////////////
  82. // Function: EggNode::is_under_transform
  83. // Access: Public
  84. // Description: Returns true if there is a <Transform> entry somewhere
  85. // in the egg tree at or above this node, false
  86. // otherwise.
  87. ////////////////////////////////////////////////////////////////////
  88. INLINE bool EggNode::
  89. is_under_transform() const {
  90. return (_under_flags & UF_under_transform) != 0;
  91. }
  92. ////////////////////////////////////////////////////////////////////
  93. // Function: EggNode::is_local_coord
  94. // Access: Public
  95. // Description: Returns true if this node's vertices are not in the
  96. // global coordinate space. This will be the case if
  97. // there was an <Instance> node under a transform at or
  98. // above this node.
  99. ////////////////////////////////////////////////////////////////////
  100. INLINE bool EggNode::
  101. is_local_coord() const {
  102. return (_under_flags & UF_local_coord) != 0;
  103. }
  104. ////////////////////////////////////////////////////////////////////
  105. // Function: EggNode::get_vertex_frame
  106. // Access: Public
  107. // Description: Returns the coordinate frame of the vertices
  108. // referenced by primitives at or under this node. This
  109. // is not the same as get_node_frame().
  110. //
  111. // Generally, vertices in an egg file are stored in the
  112. // global coordinate space, regardless of the transforms
  113. // defined at each node. Thus, get_vertex_frame() will
  114. // usually return the identity transform (global
  115. // coordinate space). However, primitives under an
  116. // <Instance> entry reference their vertices in the
  117. // coordinate system under effect at the time of the
  118. // <Instance>. Thus, nodes under an <Instance> entry
  119. // may return this non-identity matrix.
  120. //
  121. // Specifically, this may return a non-identity matrix
  122. // only if is_local_coord() is true.
  123. ////////////////////////////////////////////////////////////////////
  124. INLINE const LMatrix4d &EggNode::
  125. get_vertex_frame() const {
  126. if (_vertex_frame == (LMatrix4d *)NULL) {
  127. return LMatrix4d::ident_mat();
  128. } else {
  129. return *_vertex_frame;
  130. }
  131. }
  132. ////////////////////////////////////////////////////////////////////
  133. // Function: EggNode::get_node_frame
  134. // Access: Public
  135. // Description: Returns the coordinate frame of the node itself.
  136. // This is simply the net product of all transformations
  137. // up to the root.
  138. ////////////////////////////////////////////////////////////////////
  139. INLINE const LMatrix4d &EggNode::
  140. get_node_frame() const {
  141. if (_node_frame == (LMatrix4d *)NULL) {
  142. return LMatrix4d::ident_mat();
  143. } else {
  144. return *_node_frame;
  145. }
  146. }
  147. ////////////////////////////////////////////////////////////////////
  148. // Function: EggNode::get_vertex_frame_inv
  149. // Access: Public
  150. // Description: Returns the inverse of the matrix returned by
  151. // get_vertex_frame(). See get_vertex_frame().
  152. ////////////////////////////////////////////////////////////////////
  153. INLINE const LMatrix4d &EggNode::
  154. get_vertex_frame_inv() const {
  155. if (_vertex_frame_inv == (LMatrix4d *)NULL) {
  156. return LMatrix4d::ident_mat();
  157. } else {
  158. return *_vertex_frame_inv;
  159. }
  160. }
  161. ////////////////////////////////////////////////////////////////////
  162. // Function: EggNode::get_node_frame_inv
  163. // Access: Public
  164. // Description: Returns the inverse of the matrix returned by
  165. // get_node_frame(). See get_node_frame().
  166. ////////////////////////////////////////////////////////////////////
  167. INLINE const LMatrix4d &EggNode::
  168. get_node_frame_inv() const {
  169. if (_node_frame_inv == (LMatrix4d *)NULL) {
  170. return LMatrix4d::ident_mat();
  171. } else {
  172. return *_node_frame_inv;
  173. }
  174. }
  175. ////////////////////////////////////////////////////////////////////
  176. // Function: EggNode::get_vertex_to_node
  177. // Access: Public
  178. // Description: Returns the transformation matrix suitable for
  179. // converting the vertices as read from the egg file
  180. // into the coordinate space of the node. This is the
  181. // same thing as:
  182. //
  183. // get_vertex_frame() * get_node_frame_inv()
  184. //
  185. ////////////////////////////////////////////////////////////////////
  186. INLINE const LMatrix4d &EggNode::
  187. get_vertex_to_node() const {
  188. if (_vertex_to_node == (LMatrix4d *)NULL) {
  189. return LMatrix4d::ident_mat();
  190. } else {
  191. return *_vertex_to_node;
  192. }
  193. }
  194. ////////////////////////////////////////////////////////////////////
  195. // Function: EggNode::get_node_to_vertex
  196. // Access: Public
  197. // Description: Returns the transformation matrix suitable for
  198. // converting vertices in the coordinate space of the
  199. // node to the appropriate coordinate space for storing
  200. // in the egg file. This is the same thing as:
  201. //
  202. // get_node_frame() * get_vertex_frame_inv()
  203. //
  204. ////////////////////////////////////////////////////////////////////
  205. INLINE const LMatrix4d &EggNode::
  206. get_node_to_vertex() const {
  207. if (_node_to_vertex == (LMatrix4d *)NULL) {
  208. return LMatrix4d::ident_mat();
  209. } else {
  210. return *_node_to_vertex;
  211. }
  212. }
  213. ////////////////////////////////////////////////////////////////////
  214. // Function: EggNode::get_vertex_frame_ptr
  215. // Access: Public
  216. // Description: Returns either a NULL pointer or a unique pointer
  217. // shared by nodes with the same get_vertex_frame()
  218. // matrix.
  219. ////////////////////////////////////////////////////////////////////
  220. INLINE const LMatrix4d *EggNode::
  221. get_vertex_frame_ptr() const {
  222. return _vertex_frame;
  223. }
  224. ////////////////////////////////////////////////////////////////////
  225. // Function: EggNode::get_node_frame_ptr
  226. // Access: Public
  227. // Description: Returns either a NULL pointer or a unique pointer
  228. // shared by nodes with the same get_node_frame()
  229. // matrix.
  230. ////////////////////////////////////////////////////////////////////
  231. INLINE const LMatrix4d *EggNode::
  232. get_node_frame_ptr() const {
  233. return _node_frame;
  234. }
  235. ////////////////////////////////////////////////////////////////////
  236. // Function: EggNode::get_vertex_frame_inv_ptr
  237. // Access: Public
  238. // Description: Returns either a NULL pointer or a unique pointer
  239. // shared by nodes with the same get_vertex_frame_inv()
  240. // matrix.
  241. ////////////////////////////////////////////////////////////////////
  242. INLINE const LMatrix4d *EggNode::
  243. get_vertex_frame_inv_ptr() const {
  244. return _vertex_frame_inv;
  245. }
  246. ////////////////////////////////////////////////////////////////////
  247. // Function: EggNode::get_node_frame_inv_ptr
  248. // Access: Public
  249. // Description: Returns either a NULL pointer or a unique pointer
  250. // shared by nodes with the same get_node_frame_inv()
  251. // matrix.
  252. ////////////////////////////////////////////////////////////////////
  253. INLINE const LMatrix4d *EggNode::
  254. get_node_frame_inv_ptr() const {
  255. return _node_frame_inv;
  256. }
  257. ////////////////////////////////////////////////////////////////////
  258. // Function: EggNode::get_vertex_to_node_ptr
  259. // Access: Public
  260. // Description: Returns either a NULL pointer or a unique pointer
  261. // shared by nodes with the same get_vertex_to_node()
  262. // matrix.
  263. ////////////////////////////////////////////////////////////////////
  264. INLINE const LMatrix4d *EggNode::
  265. get_vertex_to_node_ptr() const {
  266. return _vertex_to_node;
  267. }
  268. ////////////////////////////////////////////////////////////////////
  269. // Function: EggNode::get_node_to_vertex_ptr
  270. // Access: Public
  271. // Description: Returns either a NULL pointer or a unique pointer
  272. // shared by nodes with the same get_node_to_vertex()
  273. // matrix.
  274. ////////////////////////////////////////////////////////////////////
  275. INLINE const LMatrix4d *EggNode::
  276. get_node_to_vertex_ptr() const {
  277. return _node_to_vertex;
  278. }
  279. ////////////////////////////////////////////////////////////////////
  280. // Function: EggNode::transform
  281. // Access: Public
  282. // Description: Applies the indicated transformation to the node and
  283. // all of its descendants.
  284. ////////////////////////////////////////////////////////////////////
  285. INLINE void EggNode::
  286. transform(const LMatrix4d &mat) {
  287. LMatrix4d inv = invert(mat);
  288. r_transform(mat, inv, CS_default);
  289. r_transform_vertices(mat);
  290. // Now we have to recompute the under_flags to ensure that all the
  291. // cached relative matrices are correct.
  292. update_under(0);
  293. }
  294. ////////////////////////////////////////////////////////////////////
  295. // Function: EggNode::transform_vertices_only
  296. // Access: Public
  297. // Description: Applies the indicated transformation only to vertices
  298. // that appear in global space within vertex pools at
  299. // this node and below. Joints and other transforms are
  300. // not affected, nor are local vertices.
  301. ////////////////////////////////////////////////////////////////////
  302. INLINE void EggNode::
  303. transform_vertices_only(const LMatrix4d &mat) {
  304. r_transform_vertices(mat);
  305. }
  306. ////////////////////////////////////////////////////////////////////
  307. // Function: EggNode::flatten_transforms
  308. // Access: Public
  309. // Description: Removes any transform and instance records from this
  310. // node in the scene graph and below. If an instance
  311. // node is encountered, removes the instance and applies
  312. // the transform to its vertices, duplicating vertices
  313. // if necessary.
  314. //
  315. // Since this function may result in duplicated
  316. // vertices, it may be a good idea to call
  317. // remove_unused_vertices() after calling this.
  318. ////////////////////////////////////////////////////////////////////
  319. INLINE void EggNode::
  320. flatten_transforms() {
  321. r_flatten_transforms();
  322. update_under(0);
  323. }