pandaNode.h 33 KB

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