pandaNode.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. // Filename: pandaNode.h
  2. // Created by: drose (20Feb02)
  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. #ifndef PANDANODE_H
  15. #define PANDANODE_H
  16. #include "pandabase.h"
  17. #include "cycleData.h"
  18. #include "cycleDataReader.h"
  19. #include "cycleDataWriter.h"
  20. #include "cycleDataLockedStageReader.h"
  21. #include "cycleDataStageReader.h"
  22. #include "cycleDataStageWriter.h"
  23. #include "pipelineCycler.h"
  24. #include "renderState.h"
  25. #include "renderEffects.h"
  26. #include "transformState.h"
  27. #include "drawMask.h"
  28. #include "typedWritable.h"
  29. #include "collideMask.h"
  30. #include "namable.h"
  31. #include "referenceCount.h"
  32. #include "luse.h"
  33. #include "ordered_vector.h"
  34. #include "pointerTo.h"
  35. #include "nodePointerTo.h"
  36. #include "pointerToArray.h"
  37. #include "pnotify.h"
  38. #include "updateSeq.h"
  39. #include "deletedChain.h"
  40. #include "pandaNodeChain.h"
  41. #include "pStatCollector.h"
  42. #include "copyOnWriteObject.h"
  43. #include "copyOnWritePointer.h"
  44. #include "lightReMutex.h"
  45. #include "extension.h"
  46. #ifdef HAVE_PYTHON
  47. #undef _POSIX_C_SOURCE
  48. #include <Python.h>
  49. #endif // HAVE_PYTHON
  50. class NodePathComponent;
  51. class CullTraverser;
  52. class CullTraverserData;
  53. class Light;
  54. class FactoryParams;
  55. class AccumulatedAttribs;
  56. class GeomTransformer;
  57. class GraphicsStateGuardianBase;
  58. ////////////////////////////////////////////////////////////////////
  59. // Class : PandaNode
  60. // Description : A basic node of the scene graph or data graph. This
  61. // is the base class of all specialized nodes, and also
  62. // serves as a generic node with no special properties.
  63. ////////////////////////////////////////////////////////////////////
  64. class EXPCL_PANDA_PGRAPH PandaNode : public TypedWritableReferenceCount,
  65. public Namable, public LinkedListNode {
  66. PUBLISHED:
  67. explicit PandaNode(const string &name);
  68. virtual ~PandaNode();
  69. //published so that characters can be combined.
  70. virtual PandaNode *combine_with(PandaNode *other);
  71. protected:
  72. PandaNode(const PandaNode &copy);
  73. private:
  74. void operator = (const PandaNode &copy);
  75. public:
  76. virtual PandaNode *dupe_for_flatten() const;
  77. virtual bool safe_to_flatten() const;
  78. virtual bool safe_to_transform() const;
  79. virtual bool safe_to_modify_transform() const;
  80. virtual bool safe_to_combine() const;
  81. virtual bool safe_to_combine_children() const;
  82. virtual bool safe_to_flatten_below() const;
  83. virtual bool preserve_name() const;
  84. virtual int get_unsafe_to_apply_attribs() const;
  85. virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
  86. int attrib_types,
  87. GeomTransformer &transformer);
  88. virtual void xform(const LMatrix4 &mat);
  89. virtual CPT(TransformState)
  90. calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point,
  91. bool &found_any,
  92. const TransformState *transform,
  93. Thread *current_thread = Thread::get_current_thread()) const;
  94. virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
  95. virtual bool has_selective_visibility() const;
  96. virtual int get_first_visible_child() const;
  97. virtual int get_next_visible_child(int n) const;
  98. virtual bool has_single_child_visibility() const;
  99. virtual int get_visible_child() const;
  100. virtual bool is_renderable() const;
  101. virtual void add_for_draw(CullTraverser *trav, CullTraverserData &data);
  102. PUBLISHED:
  103. virtual PandaNode *make_copy() const;
  104. PT(PandaNode) copy_subgraph(Thread *current_thread = Thread::get_current_thread()) const;
  105. EXTENSION(PT(PandaNode) __copy__() const);
  106. EXTENSION(PyObject *__deepcopy__(PyObject *self, PyObject *memo) const);
  107. INLINE int get_num_parents(Thread *current_thread = Thread::get_current_thread()) const;
  108. INLINE PandaNode *get_parent(int n, Thread *current_thread = Thread::get_current_thread()) const;
  109. INLINE int find_parent(PandaNode *node, Thread *current_thread = Thread::get_current_thread()) const;
  110. MAKE_SEQ(get_parents, get_num_parents, get_parent);
  111. INLINE int get_num_children(Thread *current_thread = Thread::get_current_thread()) const;
  112. INLINE PandaNode *get_child(int n, Thread *current_thread = Thread::get_current_thread()) const;
  113. INLINE int get_child_sort(int n, Thread *current_thread = Thread::get_current_thread()) const;
  114. INLINE int find_child(PandaNode *node, Thread *current_thread = Thread::get_current_thread()) const;
  115. MAKE_SEQ(get_children, get_num_children, get_child);
  116. int count_num_descendants() const;
  117. void add_child(PandaNode *child_node, int sort = 0,
  118. Thread *current_thread = Thread::get_current_thread());
  119. void remove_child(int child_index, Thread *current_thread = Thread::get_current_thread());
  120. bool remove_child(PandaNode *child_node, Thread *current_thread = Thread::get_current_thread());
  121. bool replace_child(PandaNode *orig_child, PandaNode *new_child,
  122. Thread *current_thread = Thread::get_current_thread());
  123. INLINE bool stash_child(PandaNode *child_node,
  124. Thread *current_thread = Thread::get_current_thread());
  125. void stash_child(int child_index,
  126. Thread *current_thread = Thread::get_current_thread());
  127. INLINE bool unstash_child(PandaNode *child_node,
  128. Thread *current_thread = Thread::get_current_thread());
  129. void unstash_child(int stashed_index,
  130. Thread *current_thread = Thread::get_current_thread());
  131. INLINE int get_num_stashed(Thread *current_thread = Thread::get_current_thread()) const;
  132. INLINE PandaNode *get_stashed(int n, Thread *current_thread = Thread::get_current_thread()) const;
  133. INLINE int get_stashed_sort(int n, Thread *current_thread = Thread::get_current_thread()) const;
  134. INLINE int find_stashed(PandaNode *node, Thread *current_thread = Thread::get_current_thread()) const;
  135. MAKE_SEQ(get_stashed, get_num_stashed, get_stashed);
  136. void add_stashed(PandaNode *child_node, int sort = 0, Thread *current_thread = Thread::get_current_thread());
  137. void remove_stashed(int child_index, Thread *current_thread = Thread::get_current_thread());
  138. void remove_all_children(Thread *current_thread = Thread::get_current_thread());
  139. void steal_children(PandaNode *other, Thread *current_thread = Thread::get_current_thread());
  140. void copy_children(PandaNode *other, Thread *current_thread = Thread::get_current_thread());
  141. void set_attrib(const RenderAttrib *attrib, int override = 0);
  142. INLINE CPT(RenderAttrib) get_attrib(TypeHandle type) const;
  143. INLINE CPT(RenderAttrib) get_attrib(int slot) const;
  144. INLINE bool has_attrib(TypeHandle type) const;
  145. INLINE bool has_attrib(int slot) const;
  146. INLINE void clear_attrib(TypeHandle type);
  147. void clear_attrib(int slot);
  148. void set_effect(const RenderEffect *effect);
  149. INLINE CPT(RenderEffect) get_effect(TypeHandle type) const;
  150. INLINE bool has_effect(TypeHandle type) const;
  151. void clear_effect(TypeHandle type);
  152. void set_state(const RenderState *state, Thread *current_thread = Thread::get_current_thread());
  153. INLINE CPT(RenderState) get_state(Thread *current_thread = Thread::get_current_thread()) const;
  154. INLINE void clear_state(Thread *current_thread = Thread::get_current_thread());
  155. void set_effects(const RenderEffects *effects, Thread *current_thread = Thread::get_current_thread());
  156. INLINE CPT(RenderEffects) get_effects(Thread *current_thread = Thread::get_current_thread()) const;
  157. INLINE void clear_effects(Thread *current_thread = Thread::get_current_thread());
  158. void set_transform(const TransformState *transform, Thread *current_thread = Thread::get_current_thread());
  159. INLINE CPT(TransformState) get_transform(Thread *current_thread = Thread::get_current_thread()) const;
  160. INLINE void clear_transform(Thread *current_thread = Thread::get_current_thread());
  161. void set_prev_transform(const TransformState *transform, Thread *current_thread = Thread::get_current_thread());
  162. INLINE CPT(TransformState) get_prev_transform(Thread *current_thread = Thread::get_current_thread()) const;
  163. void reset_prev_transform(Thread *current_thread = Thread::get_current_thread());
  164. INLINE bool has_dirty_prev_transform() const;
  165. static void reset_all_prev_transform(Thread *current_thread = Thread::get_current_thread());
  166. void set_tag(const string &key, const string &value,
  167. Thread *current_thread = Thread::get_current_thread());
  168. INLINE string get_tag(const string &key,
  169. Thread *current_thread = Thread::get_current_thread()) const;
  170. INLINE bool has_tag(const string &key,
  171. Thread *current_thread = Thread::get_current_thread()) const;
  172. void clear_tag(const string &key,
  173. Thread *current_thread = Thread::get_current_thread());
  174. void get_tag_keys(vector_string &keys) const;
  175. EXTENSION(PyObject *get_tag_keys() const);
  176. EXTENSION(void set_python_tag(const string &key, PyObject *value));
  177. EXTENSION(PyObject *get_python_tag(const string &key) const);
  178. EXTENSION(bool has_python_tag(const string &key) const);
  179. EXTENSION(void clear_python_tag(const string &key));
  180. EXTENSION(void get_python_tag_keys(vector_string &keys) const);
  181. EXTENSION(PyObject *get_python_tag_keys() const);
  182. INLINE bool has_tags() const;
  183. void copy_tags(PandaNode *other);
  184. void list_tags(ostream &out, const string &separator = "\n") const;
  185. int compare_tags(const PandaNode *other) const;
  186. void copy_all_properties(PandaNode *other);
  187. void replace_node(PandaNode *other);
  188. enum UnexpectedChange {
  189. UC_parents = 0x001,
  190. UC_children = 0x002,
  191. UC_transform = 0x004,
  192. UC_state = 0x008,
  193. UC_draw_mask = 0x010,
  194. };
  195. void set_unexpected_change(unsigned int flags);
  196. unsigned int get_unexpected_change(unsigned int flags) const;
  197. void clear_unexpected_change(unsigned int flags);
  198. INLINE static DrawMask get_overall_bit();
  199. INLINE static DrawMask get_all_camera_mask();
  200. INLINE bool is_overall_hidden() const;
  201. INLINE void set_overall_hidden(bool overall_hidden);
  202. void adjust_draw_mask(DrawMask show_mask,
  203. DrawMask hide_mask,
  204. DrawMask clear_mask);
  205. INLINE DrawMask get_draw_control_mask() const;
  206. INLINE DrawMask get_draw_show_mask() const;
  207. DrawMask get_net_draw_control_mask() const;
  208. DrawMask get_net_draw_show_mask() const;
  209. void set_into_collide_mask(CollideMask mask);
  210. INLINE CollideMask get_into_collide_mask() const;
  211. virtual CollideMask get_legal_collide_mask() const;
  212. CollideMask get_net_collide_mask(Thread *current_thread = Thread::get_current_thread()) const;
  213. CPT(RenderAttrib) get_off_clip_planes(Thread *current_thread = Thread::get_current_thread()) const;
  214. void prepare_scene(GraphicsStateGuardianBase *gsg, const RenderState *node_state);
  215. bool is_scene_root() const;
  216. bool is_under_scene_root() const;
  217. virtual void output(ostream &out) const;
  218. virtual void write(ostream &out, int indent_level) const;
  219. INLINE void ls(ostream &out, int indent_level) const;
  220. // A node has three bounding volumes: an "external" bounding volume
  221. // that represents the node and all of its children, an "internal"
  222. // bounding volume which represents only the node itself (and is
  223. // usually empty, unless a specific node type sets it otherwise),
  224. // and a "user" bounding volume which is specified by the user.
  225. // We define set_bounds() and get_bounds() functions so that
  226. // set_bounds() sets the user bounding volume, while get_bounds()
  227. // returns the external bounding volume. Although it might seem
  228. // strange and confusing to do this, this is actually the natural
  229. // way the user thinks about nodes and bounding volumes.
  230. void set_bounds_type(BoundingVolume::BoundsType bounds_type);
  231. BoundingVolume::BoundsType get_bounds_type() const;
  232. void set_bounds(const BoundingVolume *volume);
  233. void set_bound(const BoundingVolume *volume);
  234. INLINE void clear_bounds();
  235. CPT(BoundingVolume) get_bounds(Thread *current_thread = Thread::get_current_thread()) const;
  236. CPT(BoundingVolume) get_bounds(UpdateSeq &seq, Thread *current_thread = Thread::get_current_thread()) const;
  237. int get_nested_vertices(Thread *current_thread = Thread::get_current_thread()) const;
  238. INLINE CPT(BoundingVolume) get_internal_bounds(Thread *current_thread = Thread::get_current_thread()) const;
  239. INLINE int get_internal_vertices(Thread *current_thread = Thread::get_current_thread()) const;
  240. void mark_bounds_stale(Thread *current_thread = Thread::get_current_thread()) const;
  241. void mark_internal_bounds_stale(Thread *current_thread = Thread::get_current_thread());
  242. INLINE bool is_bounds_stale() const;
  243. INLINE void set_final(bool flag);
  244. INLINE bool is_final(Thread *current_thread = Thread::get_current_thread()) const;
  245. virtual bool is_geom_node() const;
  246. virtual bool is_lod_node() const;
  247. virtual bool is_collision_node() const;
  248. virtual Light *as_light();
  249. virtual bool is_ambient_light() const;
  250. enum FancyBits {
  251. FB_transform = 0x0001,
  252. FB_state = 0x0002,
  253. FB_effects = 0x0004,
  254. FB_tag = 0x0010,
  255. FB_draw_mask = 0x0020,
  256. FB_cull_callback = 0x0040,
  257. };
  258. INLINE int get_fancy_bits(Thread *current_thread = Thread::get_current_thread()) const;
  259. PUBLISHED:
  260. static PT(PandaNode) decode_from_bam_stream(const string &data, BamReader *reader = NULL);
  261. protected:
  262. class BoundsData;
  263. INLINE CPT(BoundingVolume) get_user_bounds(int pipeline_stage, Thread *current_thread) const;
  264. CPT(BoundingVolume) get_internal_bounds(int pipeline_stage, Thread *current_thread) const;
  265. int get_internal_vertices(int pipeline_stage, Thread *current_thread) const;
  266. void set_internal_bounds(const BoundingVolume *volume);
  267. INLINE void mark_bounds_stale(int pipeline_stage, Thread *current_thread) const;
  268. void force_bounds_stale(Thread *current_thread = Thread::get_current_thread());
  269. void force_bounds_stale(int pipeline_stage, Thread *current_thread);
  270. INLINE void mark_internal_bounds_stale(int pipeline_stage, Thread *current_thread);
  271. virtual void r_mark_geom_bounds_stale(Thread *current_thread);
  272. virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
  273. int &internal_vertices,
  274. int pipeline_stage,
  275. Thread *current_thread) const;
  276. virtual void parents_changed();
  277. virtual void children_changed();
  278. virtual void transform_changed();
  279. virtual void state_changed();
  280. virtual void draw_mask_changed();
  281. typedef pmap<PandaNode *, PandaNode *> InstanceMap;
  282. virtual PT(PandaNode) r_copy_subgraph(InstanceMap &inst_map,
  283. Thread *current_thread) const;
  284. virtual void r_copy_children(const PandaNode *from, InstanceMap &inst_map,
  285. Thread *current_thread);
  286. void set_cull_callback();
  287. void disable_cull_callback();
  288. public:
  289. virtual void r_prepare_scene(GraphicsStateGuardianBase *gsg,
  290. const RenderState *node_state,
  291. GeomTransformer &transformer,
  292. Thread *current_thread);
  293. protected:
  294. // This is a base class of CData, defined below. It contains just
  295. // the protected (not private) part of CData that will be needed by
  296. // derived classes to implement compute_internal_bounds().
  297. class EXPCL_PANDA_PGRAPH BoundsData : public CycleData {
  298. protected:
  299. INLINE BoundsData();
  300. INLINE BoundsData(const BoundsData &copy);
  301. INLINE void copy_bounds(const BoundsData &copy);
  302. public:
  303. // This is the "internal" bounding volume, which is normally
  304. // empty, but which a particular PandaNode subclass may define to
  305. // be any arbitrary volume, by calling set_internal_bounds() or by
  306. // overriding compute_internal_bounds().
  307. CPT(BoundingVolume) _internal_bounds;
  308. int _internal_vertices;
  309. UpdateSeq _internal_bounds_mark; // incremented on mark_stale
  310. UpdateSeq _internal_bounds_computed; // set to above when computing
  311. };
  312. private:
  313. class CData;
  314. INLINE int do_find_parent(PandaNode *node, const CData *cdata) const;
  315. bool stage_remove_child(PandaNode *child_node, int pipeline_stage,
  316. Thread *current_thread);
  317. bool stage_replace_child(PandaNode *orig_child, PandaNode *new_child,
  318. int pipeline_stage, Thread *current_thread);
  319. void quick_add_new_child(PandaNode *child_node, int sort,
  320. Thread *current_thread);
  321. INLINE bool verify_child_no_cycles(PandaNode *child_node);
  322. void report_cycle(PandaNode *node);
  323. bool find_node_above(PandaNode *node);
  324. // parent-child manipulation for NodePath support. Don't try to
  325. // call these directly.
  326. static PT(NodePathComponent) attach(NodePathComponent *parent,
  327. PandaNode *child, int sort,
  328. int pipeline_stage, Thread *current_thread);
  329. static void detach(NodePathComponent *child, int pipeline_stage, Thread *current_thread);
  330. static void detach_one_stage(NodePathComponent *child, int pipeline_stage, Thread *current_thread);
  331. static bool reparent(NodePathComponent *new_parent,
  332. NodePathComponent *child, int sort, bool as_stashed,
  333. int pipeline_stage, Thread *current_thread);
  334. static bool reparent_one_stage(NodePathComponent *new_parent,
  335. NodePathComponent *child, int sort,
  336. bool as_stashed, int pipeline_stage, Thread *current_thread);
  337. static PT(NodePathComponent) get_component(NodePathComponent *parent,
  338. PandaNode *child,
  339. int pipeline_stage, Thread *current_thread);
  340. static PT(NodePathComponent) get_top_component(PandaNode *child, bool force,
  341. int pipeline_stage, Thread *current_thread);
  342. PT(NodePathComponent) get_generic_component(bool accept_ambiguity,
  343. int pipeline_stage, Thread *current_thread);
  344. PT(NodePathComponent) r_get_generic_component(bool accept_ambiguity,
  345. bool &ambiguity_detected,
  346. int pipeline_stage, Thread *current_thread);
  347. void delete_component(NodePathComponent *component);
  348. static void sever_connection(PandaNode *parent_node, PandaNode *child_node,
  349. int pipeline_stage, Thread *current_thread);
  350. static void new_connection(PandaNode *parent_node, PandaNode *child_node,
  351. int pipeline_stage, Thread *current_thread);
  352. void fix_path_lengths(int pipeline_stage, Thread *current_thread);
  353. void r_list_descendants(ostream &out, int indent_level) const;
  354. INLINE void do_set_dirty_prev_transform();
  355. INLINE void do_clear_dirty_prev_transform();
  356. public:
  357. // This must be declared public so that VC6 will allow the nested
  358. // CData class to access it.
  359. class EXPCL_PANDA_PGRAPH DownConnection {
  360. public:
  361. INLINE DownConnection(PandaNode *child, int sort);
  362. INLINE bool operator < (const DownConnection &other) const;
  363. INLINE PandaNode *get_child() const;
  364. INLINE void set_child(PandaNode *child);
  365. INLINE int get_sort() const;
  366. private:
  367. // Child pointers are reference counted. That way, holding a
  368. // pointer to the root of a subgraph keeps the entire subgraph
  369. // around.
  370. PT(PandaNode) _child;
  371. int _sort;
  372. };
  373. private:
  374. typedef ov_multiset<DownConnection> DownList;
  375. typedef CopyOnWriteObj1< DownList, TypeHandle > Down;
  376. // Store a pointer to the down_list during the bam read pass.
  377. class EXPCL_PANDA_PGRAPH BamReaderAuxDataDown : public BamReaderAuxData {
  378. public:
  379. INLINE BamReaderAuxDataDown();
  380. Down _down_list;
  381. public:
  382. virtual TypeHandle get_type() const {
  383. return get_class_type();
  384. }
  385. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  386. static TypeHandle get_class_type() {
  387. return _type_handle;
  388. }
  389. public:
  390. static void init_type() {
  391. BamReaderAuxData::init_type();
  392. register_type(_type_handle, "BamReaderAuxDataDown",
  393. BamReaderAuxData::get_class_type());
  394. }
  395. private:
  396. static TypeHandle _type_handle;
  397. };
  398. class EXPCL_PANDA_PGRAPH UpConnection {
  399. public:
  400. INLINE UpConnection(PandaNode *child);
  401. INLINE bool operator < (const UpConnection &other) const;
  402. INLINE PandaNode *get_parent() const;
  403. private:
  404. // Parent pointers are not reference counted. That way, parents and
  405. // children do not circularly reference each other.
  406. PandaNode *_parent;
  407. };
  408. typedef ov_set<UpConnection> UpList;
  409. typedef CopyOnWriteObj1< UpList, TypeHandle > Up;
  410. // We also maintain a set of NodePathComponents in the node. This
  411. // represents the set of instances of this node that we have
  412. // requested a NodePath for. We don't keep reference counts; when
  413. // each NodePathComponent destructs, it removes itself from this
  414. // set.
  415. typedef phash_set<NodePathComponent *, pointer_hash> Paths;
  416. // We don't cycle the set of Paths, since these are across all
  417. // threads. A NodePathComponent, once created, is always associated
  418. // with the same node. We do, however, protect the Paths under a mutex.
  419. Paths _paths;
  420. LightReMutex _paths_lock;
  421. bool _dirty_prev_transform;
  422. static PandaNodeChain _dirty_prev_transforms;
  423. // This is used to maintain a table of keyed data on each node, for
  424. // the user's purposes.
  425. typedef phash_map<string, string, string_hash> TagData;
  426. #ifdef HAVE_PYTHON
  427. typedef phash_map<string, PyObject *, string_hash> PythonTagData;
  428. #endif // HAVE_PYTHON
  429. #ifndef NDEBUG
  430. unsigned int _unexpected_change_flags;
  431. #endif // !NDEBUG
  432. // This is the data that must be cycled between pipeline stages.
  433. class EXPCL_PANDA_PGRAPH CData : public BoundsData {
  434. public:
  435. CData();
  436. CData(const CData &copy);
  437. virtual ~CData();
  438. ALLOC_DELETED_CHAIN(CData);
  439. virtual CycleData *make_copy() const;
  440. virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
  441. void update_bam_nested(BamWriter *manager) const;
  442. virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
  443. virtual void fillin(DatagramIterator &scan, BamReader *manager);
  444. virtual TypeHandle get_parent_type() const {
  445. return PandaNode::get_class_type();
  446. }
  447. public:
  448. // This section contains the lightweight parts of the node that
  449. // are likely to change fairly often: transform and state.
  450. NCPT(RenderState) _state;
  451. NCPT(TransformState) _transform;
  452. NCPT(TransformState) _prev_transform;
  453. public:
  454. // This section contains the heavierweight parts of the node that
  455. // are less likely to change as often: tags, collide mask.
  456. INLINE void set_fancy_bit(int bits, bool value);
  457. #ifdef HAVE_PYTHON
  458. void inc_py_refs();
  459. void dec_py_refs();
  460. #endif
  461. CPT(RenderEffects) _effects;
  462. TagData _tag_data;
  463. #ifdef HAVE_PYTHON
  464. PythonTagData _python_tag_data;
  465. #endif // HAVE_PYTHON
  466. // These two together determine the per-camera visibility of this
  467. // node. See adjust_draw_mask() for details.
  468. DrawMask _draw_control_mask, _draw_show_mask;
  469. // This is the mask that indicates which CollisionNodes may detect
  470. // a collision with this particular node. By default it is zero
  471. // for an ordinary PandaNode, and all bits on for a CollisionNode
  472. // or GeomNode.
  473. CollideMask _into_collide_mask;
  474. // The requested bounding volume type.
  475. BoundingVolume::BoundsType _bounds_type;
  476. // This is the user bounding volume, which is only specified by a
  477. // user. It defaults to NULL, which means an empty volume.
  478. CPT(BoundingVolume) _user_bounds;
  479. // See BoundsData, above, for _internal_bounds.
  480. // This is true if the external bounds of this node should be
  481. // deemed "final". See set_final().
  482. bool _final_bounds;
  483. // This bitmask is maintained automatically by the internal
  484. // PandaNode code; it contains a 1 for each "fancy" attribute that
  485. // is set on the node. See enum FancyBits, above.
  486. int _fancy_bits;
  487. public:
  488. // This section contains the data that is accumulated upward from
  489. // the node's children: that is, the external bounding volume, and
  490. // conceptually similar things like the net_collide_mask, etc.
  491. // None of the data in this object is preserved in a bam file.
  492. // This is the union of all into_collide_mask bits for any nodes
  493. // at and below this level.
  494. CollideMask _net_collide_mask;
  495. // These are similar, for the draw mask.
  496. DrawMask _net_draw_control_mask, _net_draw_show_mask;
  497. // This is a ClipPlaneAttrib that represents the union of all clip
  498. // planes that have been turned *off* at and below this level.
  499. // TODO: fix the circular reference counts involved here.
  500. CPT(RenderAttrib) _off_clip_planes;
  501. // The number of vertices rendered by this node and all child
  502. // nodes.
  503. int _nested_vertices;
  504. // This is the bounding volume around the _user_bounds, the
  505. // _internal_bounds, and all of the children's external bounding
  506. // volumes.
  507. CPT(BoundingVolume) _external_bounds;
  508. // When _last_update != _next_update, this cache is stale.
  509. UpdateSeq _last_update, _next_update;
  510. // We don't always update the bounding volume and number of
  511. // nested vertices. This indicates the last time they were changed.
  512. // It is never higher than _last_update.
  513. UpdateSeq _last_bounds_update;
  514. public:
  515. // This section stores the links to other nodes above and below
  516. // this node in the graph.
  517. void write_up_list(const Up &up_list,
  518. BamWriter *manager, Datagram &dg) const;
  519. void write_down_list(const Down &down_list,
  520. BamWriter *manager, Datagram &dg) const;
  521. void update_up_list(const Up &up_list, BamWriter *manager) const;
  522. void update_down_list(const Down &down_list, BamWriter *manager) const;
  523. int complete_up_list(Up &up_list, const string &tag,
  524. TypedWritable **p_list, BamReader *manager);
  525. int complete_down_list(Down &down_list, const string &tag,
  526. TypedWritable **p_list, BamReader *manager);
  527. void fillin_up_list(Up &up_list, const string &tag,
  528. DatagramIterator &scan, BamReader *manager);
  529. void fillin_down_list(Down &down_list, const string &tag,
  530. DatagramIterator &scan, BamReader *manager);
  531. INLINE CPT(Down) get_down() const;
  532. INLINE PT(Down) modify_down();
  533. INLINE CPT(Down) get_stashed() const;
  534. INLINE PT(Down) modify_stashed();
  535. INLINE CPT(Up) get_up() const;
  536. INLINE PT(Up) modify_up();
  537. private:
  538. // We store the child lists by reference, so we can copy them
  539. // quickly. We perform copy-on-write when necessary.
  540. COWPT(Down) _down;
  541. COWPT(Down) _stashed;
  542. COWPT(Up) _up;
  543. public:
  544. static TypeHandle get_class_type() {
  545. return _type_handle;
  546. }
  547. static void init_type() {
  548. register_type(_type_handle, "PandaNode::CData");
  549. }
  550. private:
  551. static TypeHandle _type_handle;
  552. };
  553. PipelineCycler<CData> _cycler;
  554. typedef CycleDataReader<CData> CDReader;
  555. typedef CycleDataWriter<CData> CDWriter;
  556. typedef CycleDataLockedStageReader<CData> CDLockedStageReader;
  557. typedef CycleDataStageReader<CData> CDStageReader;
  558. typedef CycleDataStageWriter<CData> CDStageWriter;
  559. int do_find_child(PandaNode *node, const Down *down) const;
  560. CDStageWriter update_cached(bool update_bounds, int pipeline_stage,
  561. CDLockedStageReader &cdata);
  562. static DrawMask _overall_bit;
  563. static PStatCollector _reset_prev_pcollector;
  564. static PStatCollector _update_bounds_pcollector;
  565. public:
  566. // This class is returned from get_children(). Use it to walk
  567. // through the list of children. This is faster, and safer, than
  568. // walking through the children one at a time via
  569. // get_num_children()/get_child(), since the list of children is
  570. // saved out ahead of time, rather than having to reacquire the lock
  571. // with each iteration, or to keep the lock held for the entire
  572. // pass.
  573. class EXPCL_PANDA_PGRAPH Children {
  574. public:
  575. INLINE Children();
  576. INLINE Children(const CData *cdata);
  577. INLINE Children(const Children &copy);
  578. INLINE void operator = (const Children &copy);
  579. #ifdef USE_MOVE_SEMANTICS
  580. INLINE Children(Children &&from) NOEXCEPT;
  581. INLINE void operator = (Children &&from) NOEXCEPT;
  582. #endif
  583. INLINE int get_num_children() const;
  584. INLINE PandaNode *get_child(int n) const;
  585. INLINE int get_child_sort(int n) const;
  586. private:
  587. CPT(Down) _down;
  588. };
  589. // Similarly for stashed children.
  590. class EXPCL_PANDA_PGRAPH Stashed {
  591. public:
  592. INLINE Stashed();
  593. INLINE Stashed(const CData *cdata);
  594. INLINE Stashed(const Stashed &copy);
  595. INLINE void operator = (const Stashed &copy);
  596. #ifdef USE_MOVE_SEMANTICS
  597. INLINE Stashed(Stashed &&from) NOEXCEPT;
  598. INLINE void operator = (Stashed &&from) NOEXCEPT;
  599. #endif
  600. INLINE int get_num_stashed() const;
  601. INLINE PandaNode *get_stashed(int n) const;
  602. INLINE int get_stashed_sort(int n) const;
  603. private:
  604. CPT(Down) _stashed;
  605. };
  606. // This class is returned from get_parents().
  607. class EXPCL_PANDA_PGRAPH Parents {
  608. public:
  609. INLINE Parents();
  610. INLINE Parents(const CData *cdata);
  611. INLINE Parents(const Parents &copy);
  612. INLINE void operator = (const Parents &copy);
  613. #ifdef USE_MOVE_SEMANTICS
  614. INLINE Parents(Parents &&from) NOEXCEPT;
  615. INLINE void operator = (Parents &&from) NOEXCEPT;
  616. #endif
  617. INLINE int get_num_parents() const;
  618. INLINE PandaNode *get_parent(int n) const;
  619. private:
  620. CPT(Up) _up;
  621. };
  622. INLINE Children get_children(Thread *current_thread = Thread::get_current_thread()) const;
  623. INLINE Stashed get_stashed(Thread *current_thread = Thread::get_current_thread()) const;
  624. INLINE Parents get_parents(Thread *current_thread = Thread::get_current_thread()) const;
  625. typedef bool SceneRootFunc(const PandaNode *);
  626. static void set_scene_root_func(SceneRootFunc *func);
  627. private:
  628. static SceneRootFunc *_scene_root_func;
  629. public:
  630. static void register_with_read_factory();
  631. virtual void write_datagram(BamWriter *manager, Datagram &dg);
  632. virtual void update_bam_nested(BamWriter *manager);
  633. void write_recorder(BamWriter *manager, Datagram &dg);
  634. protected:
  635. static TypedWritable *make_from_bam(const FactoryParams &params);
  636. void fillin(DatagramIterator &scan, BamReader *manager);
  637. void fillin_recorder(DatagramIterator &scan, BamReader *manager);
  638. public:
  639. static TypeHandle get_class_type() {
  640. return _type_handle;
  641. }
  642. static void init_type() {
  643. TypedWritable::init_type();
  644. ReferenceCount::init_type();
  645. Namable::init_type();
  646. register_type(_type_handle, "PandaNode",
  647. TypedWritable::get_class_type(),
  648. ReferenceCount::get_class_type(),
  649. Namable::get_class_type());
  650. CData::init_type();
  651. Down::init_type();
  652. Up::init_type();
  653. BamReaderAuxDataDown::init_type();
  654. }
  655. virtual TypeHandle get_type() const {
  656. return get_class_type();
  657. }
  658. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  659. private:
  660. static TypeHandle _type_handle;
  661. #ifndef DO_PIPELINING
  662. friend class PandaNode::Children;
  663. friend class PandaNode::Stashed;
  664. #endif
  665. friend class NodePath;
  666. friend class NodePathComponent;
  667. friend class WorkingNodePath;
  668. friend class PandaNodePipelineReader;
  669. friend class EggLoader;
  670. friend class Extension<PandaNode>;
  671. };
  672. ////////////////////////////////////////////////////////////////////
  673. // Class : PandaNodePipelineReader
  674. // Description : Encapsulates the data from a PandaNode,
  675. // pre-fetched for one stage of the pipeline.
  676. ////////////////////////////////////////////////////////////////////
  677. class EXPCL_PANDA_PGRAPH PandaNodePipelineReader {
  678. public:
  679. INLINE PandaNodePipelineReader(const PandaNode *object, Thread *current_thread);
  680. INLINE PandaNodePipelineReader(const PandaNodePipelineReader &copy);
  681. INLINE void operator = (const PandaNodePipelineReader &copy);
  682. public:
  683. INLINE ~PandaNodePipelineReader();
  684. ALLOC_DELETED_CHAIN(PandaNodePipelineReader);
  685. INLINE const PandaNode *get_node() const;
  686. INLINE Thread *get_current_thread() const;
  687. INLINE void release();
  688. void check_cached(bool update_bounds) const;
  689. INLINE void compose_draw_mask(DrawMask &running_draw_mask) const;
  690. INLINE bool compare_draw_mask(DrawMask running_draw_mask,
  691. DrawMask camera_mask) const;
  692. INLINE int get_num_parents() const;
  693. INLINE PandaNode *get_parent(int n) const;
  694. INLINE int find_parent(PandaNode *node) const;
  695. INLINE int get_num_children() const;
  696. INLINE PandaNode *get_child(int n) const;
  697. INLINE int get_child_sort(int n) const;
  698. INLINE int find_child(PandaNode *node) const;
  699. INLINE int get_num_stashed() const;
  700. INLINE PandaNode *get_stashed(int n) const;
  701. INLINE int get_stashed_sort(int n) const;
  702. INLINE int find_stashed(PandaNode *node) const;
  703. INLINE const RenderState *get_state() const;
  704. INLINE const RenderEffects *get_effects() const;
  705. INLINE const TransformState *get_transform() const;
  706. INLINE const TransformState *get_prev_transform() const;
  707. INLINE string get_tag(const string &key) const;
  708. INLINE bool has_tag(const string &key) const;
  709. INLINE CollideMask get_net_collide_mask() const;
  710. INLINE CPT(RenderAttrib) get_off_clip_planes() const;
  711. INLINE CPT(BoundingVolume) get_bounds() const;
  712. INLINE int get_nested_vertices() const;
  713. INLINE bool is_final() const;
  714. INLINE int get_fancy_bits() const;
  715. INLINE PandaNode::Children get_children() const;
  716. INLINE PandaNode::Stashed get_stashed() const;
  717. INLINE PandaNode::Parents get_parents() const;
  718. private:
  719. const PandaNode *_node;
  720. Thread *_current_thread;
  721. const PandaNode::CData *_cdata;
  722. public:
  723. static TypeHandle get_class_type() {
  724. return _type_handle;
  725. }
  726. static void init_type() {
  727. register_type(_type_handle, "PandaNodePipelineReader");
  728. }
  729. private:
  730. static TypeHandle _type_handle;
  731. };
  732. INLINE ostream &operator << (ostream &out, const PandaNode &node) {
  733. node.output(out);
  734. return out;
  735. }
  736. #include "pandaNode.I"
  737. #endif