renderEffects.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Filename: renderEffects.h
  2. // Created by: drose (14Mar02)
  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 RENDEREFFECTS_H
  19. #define RENDEREFFECTS_H
  20. #include "pandabase.h"
  21. #include "transformState.h"
  22. #include "renderState.h"
  23. #include "renderEffect.h"
  24. #include "typedWritableReferenceCount.h"
  25. #include "pointerTo.h"
  26. #include "ordered_vector.h"
  27. #include "reMutex.h"
  28. #include "pmutex.h"
  29. class CullTraverser;
  30. class CullTraverserData;
  31. class FactoryParams;
  32. ////////////////////////////////////////////////////////////////////
  33. // Class : RenderEffects
  34. // Description : This represents a unique collection of RenderEffect
  35. // objects that correspond to a particular renderable
  36. // state.
  37. //
  38. // You should not attempt to create or modify a
  39. // RenderEffects object directly. Instead, call one of
  40. // the make() functions to create one for you. And
  41. // instead of modifying a RenderEffects object, create a
  42. // new one.
  43. ////////////////////////////////////////////////////////////////////
  44. class EXPCL_PANDA RenderEffects : public TypedWritableReferenceCount {
  45. protected:
  46. RenderEffects();
  47. private:
  48. RenderEffects(const RenderEffects &copy);
  49. void operator = (const RenderEffects &copy);
  50. public:
  51. virtual ~RenderEffects();
  52. bool safe_to_transform() const;
  53. virtual CPT(TransformState) prepare_flatten_transform(const TransformState *net_transform) const;
  54. bool safe_to_combine() const;
  55. CPT(RenderEffects) xform(const LMatrix4f &mat) const;
  56. PUBLISHED:
  57. bool operator < (const RenderEffects &other) const;
  58. INLINE bool is_empty() const;
  59. INLINE int get_num_effects() const;
  60. INLINE const RenderEffect *get_effect(int n) const;
  61. int find_effect(TypeHandle type) const;
  62. static CPT(RenderEffects) make_empty();
  63. static CPT(RenderEffects) make(const RenderEffect *effect);
  64. static CPT(RenderEffects) make(const RenderEffect *effect1,
  65. const RenderEffect *effect2);
  66. static CPT(RenderEffects) make(const RenderEffect *effect1,
  67. const RenderEffect *effect2,
  68. const RenderEffect *effect3);
  69. static CPT(RenderEffects) make(const RenderEffect *effect1,
  70. const RenderEffect *effect2,
  71. const RenderEffect *effect3,
  72. const RenderEffect *effect4);
  73. CPT(RenderEffects) add_effect(const RenderEffect *effect) const;
  74. CPT(RenderEffects) remove_effect(TypeHandle type) const;
  75. const RenderEffect *get_effect(TypeHandle type) const;
  76. void output(ostream &out) const;
  77. void write(ostream &out, int indent_level) const;
  78. static int get_num_states();
  79. static void list_states(ostream &out);
  80. static bool validate_states();
  81. public:
  82. INLINE bool has_decal() const;
  83. INLINE bool has_show_bounds() const;
  84. INLINE bool has_show_tight_bounds() const;
  85. INLINE bool has_cull_callback() const;
  86. void cull_callback(CullTraverser *trav, CullTraverserData &data,
  87. CPT(TransformState) &node_transform,
  88. CPT(RenderState) &node_state) const;
  89. INLINE bool has_adjust_transform() const;
  90. void adjust_transform(CPT(TransformState) &net_transform,
  91. CPT(TransformState) &node_transform,
  92. PandaNode *node) const;
  93. static void init_states();
  94. private:
  95. static CPT(RenderEffects) return_new(RenderEffects *state);
  96. void determine_decal();
  97. void determine_show_bounds();
  98. void determine_cull_callback();
  99. void determine_adjust_transform();
  100. private:
  101. // This mutex protects _states. It also protects any modification
  102. // to the cache, which is encoded in _composition_cache and
  103. // _invert_composition_cache.
  104. static ReMutex *_states_lock;
  105. typedef pset<const RenderEffects *, indirect_less<const RenderEffects *> > States;
  106. static States *_states;
  107. static CPT(RenderEffects) _empty_state;
  108. // This iterator records the entry corresponding to this RenderEffects
  109. // object in the above global set. We keep the iterator around so
  110. // we can remove it when the RenderEffects destructs.
  111. States::iterator _saved_entry;
  112. private:
  113. // This is the actual data within the RenderEffects: a set of
  114. // RenderEffects.
  115. class Effect {
  116. public:
  117. INLINE Effect(const RenderEffect *effect);
  118. INLINE Effect();
  119. INLINE Effect(TypeHandle type);
  120. INLINE Effect(const Effect &copy);
  121. INLINE void operator = (const Effect &copy);
  122. INLINE bool operator < (const Effect &other) const;
  123. INLINE int compare_to(const Effect &other) const;
  124. TypeHandle _type;
  125. CPT(RenderEffect) _effect;
  126. };
  127. typedef ov_set<Effect> Effects;
  128. Effects _effects;
  129. enum Flags {
  130. F_checked_decal = 0x0001,
  131. F_has_decal = 0x0002,
  132. F_checked_show_bounds = 0x0004,
  133. F_has_show_bounds = 0x0008,
  134. F_has_show_tight_bounds = 0x0010,
  135. F_checked_cull_callback = 0x0020,
  136. F_has_cull_callback = 0x0040,
  137. F_checked_adjust_transform = 0x0080,
  138. F_has_adjust_transform = 0x0100,
  139. };
  140. int _flags;
  141. // This mutex protects _flags, and all of the above computed values.
  142. Mutex _lock;
  143. public:
  144. static void register_with_read_factory();
  145. virtual void write_datagram(BamWriter *manager, Datagram &dg);
  146. virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
  147. static TypedWritable *change_this(TypedWritable *old_ptr, BamReader *manager);
  148. virtual void finalize(BamReader *manager);
  149. protected:
  150. static TypedWritable *make_from_bam(const FactoryParams &params);
  151. void fillin(DatagramIterator &scan, BamReader *manager);
  152. public:
  153. static TypeHandle get_class_type() {
  154. return _type_handle;
  155. }
  156. static void init_type() {
  157. TypedWritableReferenceCount::init_type();
  158. register_type(_type_handle, "RenderEffects",
  159. TypedWritableReferenceCount::get_class_type());
  160. }
  161. virtual TypeHandle get_type() const {
  162. return get_class_type();
  163. }
  164. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  165. private:
  166. static TypeHandle _type_handle;
  167. };
  168. INLINE ostream &operator << (ostream &out, const RenderEffects &state) {
  169. state.output(out);
  170. return out;
  171. }
  172. #include "renderEffects.I"
  173. #endif