lodNode.I 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Filename: lodNode.I
  2. // Created by: drose (06Mar02)
  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: LODNode::CData::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. INLINE LODNode::CData::
  24. CData() :
  25. _center(0.0f, 0.0f, 0.0f),
  26. _lowest(0),
  27. _highest(0),
  28. _got_force_switch(false),
  29. _force_switch(0)
  30. {
  31. }
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: LODNode::CData::Copy Constructor
  34. // Access: Public
  35. // Description:
  36. ////////////////////////////////////////////////////////////////////
  37. INLINE LODNode::CData::
  38. CData(const LODNode::CData &copy) :
  39. _center(copy._center),
  40. _switch_vector(copy._switch_vector),
  41. _lowest(copy._lowest),
  42. _highest(copy._highest),
  43. _got_force_switch(copy._got_force_switch),
  44. _force_switch(copy._force_switch)
  45. {
  46. }
  47. ////////////////////////////////////////////////////////////////////
  48. // Function: LODNode::Switch::Constructor
  49. // Access: Public
  50. // Description:
  51. ////////////////////////////////////////////////////////////////////
  52. INLINE LODNode::Switch::
  53. Switch(float in, float out) {
  54. set_range(in, out);
  55. }
  56. ////////////////////////////////////////////////////////////////////
  57. // Function: LODNode::Switch::get_in
  58. // Access: Public
  59. // Description:
  60. ////////////////////////////////////////////////////////////////////
  61. INLINE float LODNode::Switch::
  62. get_in() const {
  63. return _in;
  64. }
  65. ////////////////////////////////////////////////////////////////////
  66. // Function: LODNode::Switch::get_out
  67. // Access: Public
  68. // Description:
  69. ////////////////////////////////////////////////////////////////////
  70. INLINE float LODNode::Switch::
  71. get_out() const {
  72. return _out;
  73. }
  74. ////////////////////////////////////////////////////////////////////
  75. // Function: LODNode::Switch::set_range
  76. // Access: Public
  77. // Description:
  78. ////////////////////////////////////////////////////////////////////
  79. INLINE void LODNode::Switch::
  80. set_range(float in, float out) {
  81. _in = in;
  82. _out = out;
  83. }
  84. ////////////////////////////////////////////////////////////////////
  85. // Function: LODNode::Switch::in_range
  86. // Access: Public
  87. // Description: Computes the distance between two points and returns
  88. // true if the result is within the range for the LOD.
  89. ////////////////////////////////////////////////////////////////////
  90. INLINE bool LODNode::Switch::
  91. in_range(float dist) const {
  92. return (dist >= _out && dist < _in);
  93. }
  94. ////////////////////////////////////////////////////////////////////
  95. // Function: LODNode::Switch::rescale
  96. // Access: Public
  97. // Description: Scales the switching distances by the indicated factor.
  98. ////////////////////////////////////////////////////////////////////
  99. INLINE void LODNode::Switch::
  100. rescale(float factor) {
  101. _in *= factor;
  102. _out *= factor;
  103. }
  104. ////////////////////////////////////////////////////////////////////
  105. // Function: LODNode::Switch::write_datagram
  106. // Access: Public
  107. // Description: Writes the contents of the Switch out to the
  108. // datagram, presumably in preparation to writing to a
  109. // Bam file.
  110. ////////////////////////////////////////////////////////////////////
  111. INLINE void LODNode::Switch::
  112. write_datagram(Datagram &destination) const {
  113. destination.add_float32(_in);
  114. destination.add_float32(_out);
  115. }
  116. ////////////////////////////////////////////////////////////////////
  117. // Function: LODNode::Switch::read_datagram
  118. // Access: Public
  119. // Description: Reads the contents of the Switch from the datagram,
  120. // presumably in response to reading a Bam file.
  121. ////////////////////////////////////////////////////////////////////
  122. INLINE void LODNode::Switch::
  123. read_datagram(DatagramIterator &source) {
  124. _in = source.get_float32();
  125. _out = source.get_float32();
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: LODNode::Constructor
  129. // Access: Published
  130. // Description:
  131. ////////////////////////////////////////////////////////////////////
  132. INLINE LODNode::
  133. LODNode(const string &name) :
  134. PandaNode(name)
  135. {
  136. }
  137. ////////////////////////////////////////////////////////////////////
  138. // Function: LODNode::Copy Constructor
  139. // Access: Protected
  140. // Description:
  141. ////////////////////////////////////////////////////////////////////
  142. INLINE LODNode::
  143. LODNode(const LODNode &copy) :
  144. PandaNode(copy),
  145. _cycler(copy._cycler)
  146. {
  147. }
  148. ////////////////////////////////////////////////////////////////////
  149. // Function: LODNode::add_switch
  150. // Access: Published
  151. // Description: Adds a switch range to the LODNode. This implies
  152. // that the corresponding child node has been parented
  153. // to the node.
  154. //
  155. // The sense of in vs. out distances is as if the object
  156. // were coming towards you from far away: it switches
  157. // "in" at the far distance, and switches "out" at the
  158. // close distance. Thus, "in" should be larger than
  159. // "out".
  160. ////////////////////////////////////////////////////////////////////
  161. INLINE void LODNode::
  162. add_switch(float in, float out) {
  163. CDWriter cdata(_cycler);
  164. cdata->_switch_vector.push_back(Switch(in, out));
  165. cdata->check_limits();
  166. }
  167. ////////////////////////////////////////////////////////////////////
  168. // Function: LODNode::set_switch
  169. // Access: Published
  170. // Description: Changes the switching range of a particular child of
  171. // the LODNode. See add_switch().
  172. ////////////////////////////////////////////////////////////////////
  173. INLINE bool LODNode::
  174. set_switch(int index, float in, float out) {
  175. CDWriter cdata(_cycler);
  176. nassertr(index >= 0 && index < (int)cdata->_switch_vector.size(), false);
  177. cdata->_switch_vector[index].set_range(in, out);
  178. cdata->check_limits();
  179. return true;
  180. }
  181. ////////////////////////////////////////////////////////////////////
  182. // Function: LODNode::clear_switches
  183. // Access: Published
  184. // Description: Removes the set of switching ranges for the LODNode,
  185. // presumably in conjunction with removing all of its
  186. // children. See add_switch().
  187. ////////////////////////////////////////////////////////////////////
  188. INLINE void LODNode::
  189. clear_switches(void) {
  190. CDWriter cdata(_cycler);
  191. cdata->_switch_vector.clear();
  192. cdata->_lowest = 0;
  193. cdata->_highest = 0;
  194. }
  195. ////////////////////////////////////////////////////////////////////
  196. // Function: LODNode::get_num_switches
  197. // Access: Published
  198. // Description: Returns the number of switch ranges added to the
  199. // LODNode. This should correspond to the number of
  200. // children of the node in order for the LODNode to
  201. // function correctly.
  202. ////////////////////////////////////////////////////////////////////
  203. INLINE int LODNode::
  204. get_num_switches() const {
  205. CDReader cdata(_cycler);
  206. return cdata->_switch_vector.size();
  207. }
  208. ////////////////////////////////////////////////////////////////////
  209. // Function: LODNode::get_in
  210. // Access: Published
  211. // Description: Returns the "in" distance of the indicated switch
  212. // range. This should be larger than the "out" distance
  213. // of the same range.
  214. ////////////////////////////////////////////////////////////////////
  215. INLINE float LODNode::
  216. get_in(int index) const {
  217. CDReader cdata(_cycler);
  218. nassertr(index >= 0 && index < (int)cdata->_switch_vector.size(), 0.0);
  219. return cdata->_switch_vector[index].get_in();
  220. }
  221. ////////////////////////////////////////////////////////////////////
  222. // Function: LODNode::get_out
  223. // Access: Published
  224. // Description: Returns the "out" distance of the indicated switch
  225. // range. This should be smaller than the "in" distance
  226. // of the same range.
  227. ////////////////////////////////////////////////////////////////////
  228. INLINE float LODNode::
  229. get_out(int index) const {
  230. CDReader cdata(_cycler);
  231. nassertr(index >= 0 && index < (int)cdata->_switch_vector.size(), 0.0);
  232. return cdata->_switch_vector[index].get_out();
  233. }
  234. ////////////////////////////////////////////////////////////////////
  235. // Function: LODNode::get_lowest_switch
  236. // Access: Published
  237. // Description: Returns the index number of the child with the lowest
  238. // level of detail; that is, the one that is designed to
  239. // be seen from the farthest away. This is usually the
  240. // first child, but it is not necessarily so.
  241. ////////////////////////////////////////////////////////////////////
  242. INLINE int LODNode::
  243. get_lowest_switch() const {
  244. CDReader cdata(_cycler);
  245. return cdata->_lowest;
  246. }
  247. ////////////////////////////////////////////////////////////////////
  248. // Function: LODNode::get_highest_switch
  249. // Access: Published
  250. // Description: Returns the index number of the child with the highest
  251. // level of detail; that is, the one that is designed to
  252. // be seen from the closest to the camera. This is
  253. // usually the last child, but it is not necessarily so.
  254. ////////////////////////////////////////////////////////////////////
  255. INLINE int LODNode::
  256. get_highest_switch() const {
  257. CDReader cdata(_cycler);
  258. return cdata->_highest;
  259. }
  260. ////////////////////////////////////////////////////////////////////
  261. // Function: LODNode::force_switch
  262. // Access: Published
  263. // Description: Forces the LODNode to show the indicated level
  264. // instead of the level that would normally be shown
  265. // based on the distance from the camera.
  266. ////////////////////////////////////////////////////////////////////
  267. INLINE void LODNode::
  268. force_switch(int index) {
  269. CDWriter cdata(_cycler);
  270. cdata->_force_switch = index;
  271. cdata->_got_force_switch = true;
  272. }
  273. ////////////////////////////////////////////////////////////////////
  274. // Function: LODNode::clear_force_switch
  275. // Access: Published
  276. // Description: Undoes the effect of a previous call to
  277. // force_switch() and releases the LODNode to once again
  278. // display the normal level.
  279. ////////////////////////////////////////////////////////////////////
  280. INLINE void LODNode::
  281. clear_force_switch() {
  282. CDWriter cdata(_cycler);
  283. cdata->_got_force_switch = false;
  284. }
  285. ////////////////////////////////////////////////////////////////////
  286. // Function: LODNode::set_center
  287. // Access: Published
  288. // Description: Specifies the center of the LOD. This is the point
  289. // that is compared to the camera (in camera space) to
  290. // determine the particular LOD that should be chosen.
  291. ////////////////////////////////////////////////////////////////////
  292. INLINE void LODNode::
  293. set_center(const LPoint3f &center) {
  294. CDWriter cdata(_cycler);
  295. cdata->_center = center;
  296. }
  297. ////////////////////////////////////////////////////////////////////
  298. // Function: LODNode::get_center
  299. // Access: Published
  300. // Description: Returns the center of the LOD. This is the point
  301. // that is compared to the camera (in camera space) to
  302. // determine the particular LOD that should be chosen.
  303. ////////////////////////////////////////////////////////////////////
  304. INLINE const LPoint3f &LODNode::
  305. get_center() const {
  306. CDReader cdata(_cycler);
  307. return cdata->_center;
  308. }