pandaNode.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // Filename: pandaNode.h
  2. // Created by: drose (20Feb02)
  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. #ifndef PANDANODE_H
  19. #define PANDANODE_H
  20. #include "pandabase.h"
  21. #include "cycleData.h"
  22. #include "cycleDataReader.h"
  23. #include "cycleDataWriter.h"
  24. #include "pipelineCycler.h"
  25. #include "renderState.h"
  26. #include "renderEffects.h"
  27. #include "transformState.h"
  28. #include "drawMask.h"
  29. #include "typedWritable.h"
  30. #include "boundedObject.h"
  31. #include "collideMask.h"
  32. #include "namable.h"
  33. #include "referenceCount.h"
  34. #include "luse.h"
  35. #include "ordered_vector.h"
  36. #include "pointerTo.h"
  37. #include "nodePointerTo.h"
  38. #include "pointerToArray.h"
  39. #include "notify.h"
  40. #ifdef HAVE_PYTHON
  41. #undef HAVE_LONG_LONG // NSPR and Python both define this.
  42. #undef _POSIX_C_SOURCE
  43. #include <Python.h>
  44. #endif // HAVE_PYTHON
  45. class NodePathComponent;
  46. class CullTraverser;
  47. class CullTraverserData;
  48. class Light;
  49. class FactoryParams;
  50. class AccumulatedAttribs;
  51. class GeomTransformer;
  52. ////////////////////////////////////////////////////////////////////
  53. // Class : PandaNode
  54. // Description : A basic node of the scene graph or data graph. This
  55. // is the base class of all specialized nodes, and also
  56. // serves as a generic node with no special properties.
  57. ////////////////////////////////////////////////////////////////////
  58. class EXPCL_PANDA PandaNode : public TypedWritable, public Namable,
  59. public BoundedObject,
  60. virtual public ReferenceCount {
  61. PUBLISHED:
  62. PandaNode(const string &name);
  63. virtual ~PandaNode();
  64. protected:
  65. PandaNode(const PandaNode &copy);
  66. private:
  67. void operator = (const PandaNode &copy);
  68. public:
  69. virtual PandaNode *make_copy() const;
  70. virtual bool safe_to_flatten() const;
  71. virtual bool safe_to_transform() const;
  72. virtual bool safe_to_modify_transform() const;
  73. virtual bool safe_to_combine() const;
  74. virtual bool safe_to_flatten_below() const;
  75. virtual bool preserve_name() const;
  76. virtual int get_unsafe_to_apply_attribs() const;
  77. virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
  78. int attrib_types,
  79. GeomTransformer &transformer);
  80. virtual void xform(const LMatrix4f &mat);
  81. virtual PandaNode *combine_with(PandaNode *other);
  82. virtual CPT(TransformState)
  83. calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point,
  84. bool &found_any,
  85. const TransformState *transform) const;
  86. virtual bool has_cull_callback() const;
  87. virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
  88. virtual bool has_selective_visibility() const;
  89. virtual int get_first_visible_child() const;
  90. virtual int get_next_visible_child(int n) const;
  91. virtual bool has_single_child_visibility() const;
  92. virtual int get_visible_child() const;
  93. PUBLISHED:
  94. PT(PandaNode) copy_subgraph() const;
  95. INLINE int get_num_parents() const;
  96. INLINE PandaNode *get_parent(int n) const;
  97. INLINE int find_parent(PandaNode *node) const;
  98. INLINE int get_num_children() const;
  99. INLINE PandaNode *get_child(int n) const;
  100. INLINE int get_child_sort(int n) const;
  101. int find_child(PandaNode *node) const;
  102. void add_child(PandaNode *child_node, int sort = 0);
  103. void remove_child(int n);
  104. bool remove_child(PandaNode *child_node);
  105. bool replace_child(PandaNode *orig_child, PandaNode *new_child);
  106. INLINE bool stash_child(PandaNode *child_node);
  107. void stash_child(int child_index);
  108. INLINE bool unstash_child(PandaNode *child_node);
  109. void unstash_child(int stashed_index);
  110. INLINE int get_num_stashed() const;
  111. INLINE PandaNode *get_stashed(int n) const;
  112. INLINE int get_stashed_sort(int n) const;
  113. int find_stashed(PandaNode *node) const;
  114. void add_stashed(PandaNode *child_node, int sort = 0);
  115. void remove_stashed(int n);
  116. void remove_all_children();
  117. void steal_children(PandaNode *other);
  118. void copy_children(PandaNode *other);
  119. INLINE void set_attrib(const RenderAttrib *attrib, int override = 0);
  120. INLINE const RenderAttrib *get_attrib(TypeHandle type) const;
  121. INLINE bool has_attrib(TypeHandle type) const;
  122. INLINE void clear_attrib(TypeHandle type);
  123. INLINE void set_effect(const RenderEffect *effect);
  124. INLINE const RenderEffect *get_effect(TypeHandle type) const;
  125. INLINE bool has_effect(TypeHandle type) const;
  126. INLINE void clear_effect(TypeHandle type);
  127. INLINE void set_state(const RenderState *state);
  128. INLINE const RenderState *get_state() const;
  129. INLINE void clear_state();
  130. INLINE void set_effects(const RenderEffects *effects);
  131. INLINE const RenderEffects *get_effects() const;
  132. INLINE void clear_effects();
  133. INLINE void set_transform(const TransformState *transform);
  134. INLINE const TransformState *get_transform() const;
  135. INLINE void clear_transform();
  136. INLINE void set_prev_transform(const TransformState *transform);
  137. INLINE const TransformState *get_prev_transform() const;
  138. INLINE void reset_prev_transform();
  139. INLINE void set_tag(const string &key, const string &value);
  140. INLINE string get_tag(const string &key) const;
  141. INLINE bool has_tag(const string &key) const;
  142. INLINE void clear_tag(const string &key);
  143. #ifdef HAVE_PYTHON
  144. void set_python_tag(const string &key, PyObject *value);
  145. PyObject *get_python_tag(const string &key) const;
  146. bool has_python_tag(const string &key) const;
  147. void clear_python_tag(const string &key);
  148. #endif // HAVE_PYTHON
  149. INLINE bool has_tags() const;
  150. void copy_tags(PandaNode *other);
  151. void list_tags(ostream &out, const string &separator = "\n") const;
  152. INLINE void set_draw_mask(DrawMask mask);
  153. INLINE DrawMask get_draw_mask() const;
  154. INLINE void set_into_collide_mask(CollideMask mask);
  155. INLINE CollideMask get_into_collide_mask() const;
  156. virtual CollideMask get_legal_collide_mask() const;
  157. INLINE CollideMask get_net_collide_mask() const;
  158. INLINE const RenderAttrib *get_off_clip_planes() const;
  159. virtual void output(ostream &out) const;
  160. virtual void write(ostream &out, int indent_level) const;
  161. INLINE void ls(ostream &out, int indent_level) const;
  162. // A node has two bounding volumes: the BoundedObject it inherits
  163. // from is the "external" bound and represents the node and all of
  164. // its children, while the _internal_bound object is the "internal"
  165. // bounds and represents only the node itself.
  166. // We remap the inherited set_bound() and get_bound() functions so
  167. // that set_bound() to a type sets the type of the external bound,
  168. // while set_bound() to a specific bounding volume sets the volume
  169. // of the *internal* bound. At the same time, get_bound() returns
  170. // the external bound. Although it might seem strange and confusing
  171. // to do this, this is actually the natural way the user thinks
  172. // about nodes and bounding volumes.
  173. INLINE void set_bound(BoundingVolumeType type);
  174. INLINE void set_bound(const BoundingVolume &volume);
  175. INLINE const BoundingVolume &get_bound() const;
  176. INLINE const BoundingVolume &get_internal_bound() const;
  177. virtual bool is_geom_node() const;
  178. virtual bool is_lod_node() const;
  179. virtual Light *as_light();
  180. protected:
  181. // Inherited from BoundedObject
  182. virtual void propagate_stale_bound();
  183. virtual BoundingVolume *recompute_bound();
  184. // Local to PandaNode
  185. virtual BoundingVolume *recompute_internal_bound();
  186. INLINE void changed_internal_bound();
  187. virtual void parents_changed();
  188. virtual void children_changed();
  189. virtual void transform_changed();
  190. virtual void state_changed();
  191. virtual void draw_mask_changed();
  192. INLINE void add_net_collide_mask(CollideMask mask);
  193. typedef pmap<PandaNode *, PandaNode *> InstanceMap;
  194. virtual PT(PandaNode) r_copy_subgraph(InstanceMap &inst_map) const;
  195. virtual void r_copy_children(const PandaNode *from, InstanceMap &inst_map);
  196. // This is the bounding volume around the contents of the node
  197. // itself (without including all of the node's children).
  198. // BoundedObject is itself cycled, so we don't need to protect it.
  199. BoundedObject _internal_bound;
  200. private:
  201. class CData;
  202. // parent-child manipulation for NodePath support. Don't try to
  203. // call these directly.
  204. static PT(NodePathComponent) attach(NodePathComponent *parent,
  205. PandaNode *child, int sort);
  206. static void detach(NodePathComponent *child);
  207. static bool reparent(NodePathComponent *new_parent,
  208. NodePathComponent *child, int sort, bool as_stashed);
  209. static PT(NodePathComponent) get_component(NodePathComponent *parent,
  210. PandaNode *child);
  211. static PT(NodePathComponent) get_top_component(PandaNode *child,
  212. bool force);
  213. PT(NodePathComponent) get_generic_component(bool accept_ambiguity);
  214. PT(NodePathComponent) r_get_generic_component(bool accept_ambiguity, bool &ambiguity_detected);
  215. void delete_component(NodePathComponent *component);
  216. static void sever_connection(PandaNode *parent_node, PandaNode *child_node);
  217. static void new_connection(PandaNode *parent_node, PandaNode *child_node);
  218. void fix_path_lengths(const CData *cdata);
  219. void r_list_descendants(ostream &out, int indent_level) const;
  220. public:
  221. // This must be declared public so that VC6 will allow the nested
  222. // CData class to access it.
  223. class EXPCL_PANDA DownConnection {
  224. public:
  225. INLINE DownConnection(PandaNode *child, int sort);
  226. INLINE bool operator < (const DownConnection &other) const;
  227. INLINE PandaNode *get_child() const;
  228. INLINE void set_child(PandaNode *child);
  229. INLINE int get_sort() const;
  230. private:
  231. // Child pointers are reference counted. That way, holding a
  232. // pointer to the root of a subgraph keeps the entire subgraph
  233. // around.
  234. PT(PandaNode) _child;
  235. int _sort;
  236. };
  237. private:
  238. typedef ov_multiset<DownConnection> Down;
  239. class EXPCL_PANDA UpConnection {
  240. public:
  241. INLINE UpConnection(PandaNode *child);
  242. INLINE bool operator < (const UpConnection &other) const;
  243. INLINE PandaNode *get_parent() const;
  244. private:
  245. // Parent pointers are not reference counted. That way, parents and
  246. // children do not circularly reference each other.
  247. PandaNode *_parent;
  248. };
  249. typedef ov_set<UpConnection> Up;
  250. // We also maintain a set of NodePathComponents in the node. This
  251. // represents the set of instances of this node that we have
  252. // requested a NodePath for. We don't keep reference counts; when
  253. // each NodePathComponent destructs, it removes itself from this
  254. // set.
  255. typedef phash_set<NodePathComponent *, pointer_hash> Paths;
  256. // This is used to maintain a table of keyed data on each node, for
  257. // the user's purposes.
  258. typedef phash_map<string, string, string_hash> TagData;
  259. #ifdef HAVE_PYTHON
  260. typedef phash_map<string, PyObject *, string_hash> PythonTagData;
  261. #endif // HAVE_PYTHON
  262. // This is the data that must be cycled between pipeline stages.
  263. class EXPCL_PANDA CData : public CycleData {
  264. public:
  265. INLINE CData();
  266. CData(const CData &copy);
  267. virtual ~CData();
  268. virtual CycleData *make_copy() const;
  269. virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
  270. virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
  271. virtual void fillin(DatagramIterator &scan, BamReader *manager);
  272. virtual TypeHandle get_parent_type() const {
  273. return PandaNode::get_class_type();
  274. }
  275. #ifdef HAVE_PYTHON
  276. void inc_py_refs();
  277. void dec_py_refs();
  278. #endif
  279. void write_up_list(const Up &up_list,
  280. BamWriter *manager, Datagram &dg) const;
  281. void write_down_list(const Down &down_list,
  282. BamWriter *manager, Datagram &dg) const;
  283. int complete_up_list(Up &up_list,
  284. TypedWritable **p_list, BamReader *manager);
  285. int complete_down_list(Down &down_list,
  286. TypedWritable **p_list, BamReader *manager);
  287. void fillin_up_list(Up &up_list,
  288. DatagramIterator &scan, BamReader *manager);
  289. void fillin_down_list(Down &down_list,
  290. DatagramIterator &scan, BamReader *manager);
  291. Down _down;
  292. Down _stashed;
  293. Up _up;
  294. Paths _paths;
  295. NCPT(RenderState) _state;
  296. CPT(RenderEffects) _effects;
  297. NCPT(TransformState) _transform;
  298. NCPT(TransformState) _prev_transform;
  299. TagData _tag_data;
  300. #ifdef HAVE_PYTHON
  301. PythonTagData _python_tag_data;
  302. #endif // HAVE_PYTHON
  303. // This is the draw_mask of this particular node.
  304. DrawMask _draw_mask;
  305. // This is the mask that indicates which CollisionNodes may detect
  306. // a collision with this particular node. By default it is zero
  307. // for an ordinary PandaNode, and all bits on for a CollisionNode
  308. // or GeomNode.
  309. CollideMask _into_collide_mask;
  310. // This is the union of all into_collide_mask bits for any nodes
  311. // at and below this level. It's conceptually similar to a
  312. // bounding volume--it represents the bounding volume of this node
  313. // in the space of collision bits--and it needs to be updated for
  314. // the same reasons the bounding volume needs to be updated. So
  315. // we update them together.
  316. CollideMask _net_collide_mask;
  317. // This is a ClipPlaneAttrib that represents the union of all clip
  318. // planes that have been turned *off* at and below this level. As
  319. // above, it's similar to a bounding volume, and is updated at the
  320. // same time. TODO: fix the circular reference counts involved
  321. // here.
  322. CPT(RenderAttrib) _off_clip_planes;
  323. bool _fixed_internal_bound;
  324. };
  325. PipelineCycler<CData> _cycler;
  326. typedef CycleDataReader<CData> CDReader;
  327. typedef CycleDataWriter<CData> CDWriter;
  328. public:
  329. // Use this interface when you want to walk through the list of
  330. // children. This saves a tiny bit of overhead between each step,
  331. // by keeping the PipelineCycler open for reading the whole time.
  332. // However, it does not protect you from self-modifying loops.
  333. class EXPCL_PANDA Children {
  334. public:
  335. INLINE Children(const CDReader &cdata);
  336. INLINE Children(const Children &copy);
  337. INLINE void operator = (const Children &copy);
  338. INLINE int get_num_children() const;
  339. INLINE PandaNode *get_child(int n) const;
  340. private:
  341. CDReader _cdata;
  342. };
  343. INLINE Children get_children() const;
  344. // This interface *does* protect you from self-modifying loops, by
  345. // copying the list of children.
  346. class EXPCL_PANDA ChildrenCopy {
  347. public:
  348. ChildrenCopy(const CDReader &cdata);
  349. INLINE ChildrenCopy(const ChildrenCopy &copy);
  350. INLINE void operator = (const ChildrenCopy &copy);
  351. INLINE int get_num_children() const;
  352. INLINE PandaNode *get_child(int n) const;
  353. private:
  354. typedef PTA(PT(PandaNode)) List;
  355. List _list;
  356. };
  357. INLINE ChildrenCopy get_children_copy() const;
  358. public:
  359. static void register_with_read_factory();
  360. virtual void write_datagram(BamWriter *manager, Datagram &dg);
  361. void write_recorder(BamWriter *manager, Datagram &dg);
  362. protected:
  363. static TypedWritable *make_from_bam(const FactoryParams &params);
  364. void fillin(DatagramIterator &scan, BamReader *manager);
  365. void fillin_recorder(DatagramIterator &scan, BamReader *manager);
  366. public:
  367. static TypeHandle get_class_type() {
  368. return _type_handle;
  369. }
  370. static void init_type() {
  371. TypedWritable::init_type();
  372. BoundedObject::init_type();
  373. ReferenceCount::init_type();
  374. register_type(_type_handle, "PandaNode",
  375. TypedWritable::get_class_type(),
  376. BoundedObject::get_class_type(),
  377. ReferenceCount::get_class_type());
  378. }
  379. virtual TypeHandle get_type() const {
  380. return get_class_type();
  381. }
  382. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  383. private:
  384. static TypeHandle _type_handle;
  385. friend class PandaNode::Children;
  386. friend class NodePath;
  387. friend class NodePathComponent;
  388. friend class WorkingNodePath;
  389. };
  390. INLINE ostream &operator << (ostream &out, const PandaNode &node) {
  391. node.output(out);
  392. return out;
  393. }
  394. #include "pandaNode.I"
  395. #endif