Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgraph/renderState.h

Go to the documentation of this file.
00001 // Filename: renderState.h
00002 // Created by:  drose (21Feb02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef RENDERSTATE_H
00020 #define RENDERSTATE_H
00021 
00022 #include "pandabase.h"
00023 
00024 #include "renderAttrib.h"
00025 #include "typedWritableReferenceCount.h"
00026 #include "pointerTo.h"
00027 #include "indirectLess.h"
00028 #include "ordered_vector.h"
00029 
00030 class GraphicsStateGuardianBase;
00031 class FogAttrib;
00032 class CullBinAttrib;
00033 class TransparencyAttrib;
00034 class FactoryParams;
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //       Class : RenderState
00038 // Description : This represents a unique collection of RenderAttrib
00039 //               objects that correspond to a particular renderable
00040 //               state.
00041 //
00042 //               You should not attempt to create or modify a
00043 //               RenderState object directly.  Instead, call one of
00044 //               the make() functions to create one for you.  And
00045 //               instead of modifying a RenderState object, create a
00046 //               new one.
00047 ////////////////////////////////////////////////////////////////////
00048 class EXPCL_PANDA RenderState : public TypedWritableReferenceCount {
00049 protected:
00050   RenderState();
00051 
00052 private:
00053   RenderState(const RenderState &copy);
00054   void operator = (const RenderState &copy);
00055 
00056 public:
00057   virtual ~RenderState();
00058 
00059   bool operator < (const RenderState &other) const;
00060 
00061 PUBLISHED:
00062   INLINE bool is_empty() const;
00063   INLINE int get_num_attribs() const;
00064   INLINE const RenderAttrib *get_attrib(int n) const;
00065   INLINE int get_override(int n) const;
00066 
00067   int find_attrib(TypeHandle type) const;
00068 
00069   static CPT(RenderState) make_empty();
00070   static CPT(RenderState) make(const RenderAttrib *attrib, int override = 0);
00071   static CPT(RenderState) make(const RenderAttrib *attrib1,
00072                                const RenderAttrib *attrib2, int override = 0);
00073   static CPT(RenderState) make(const RenderAttrib *attrib1,
00074                                const RenderAttrib *attrib2,
00075                                const RenderAttrib *attrib3, int override = 0);
00076   static CPT(RenderState) make(const RenderAttrib *attrib1,
00077                                const RenderAttrib *attrib2,
00078                                const RenderAttrib *attrib3,
00079                                const RenderAttrib *attrib4, int override = 0);
00080   static CPT(RenderState) make(const RenderAttrib * const *attrib,
00081                                int num_attribs, int override = 0);
00082 
00083   CPT(RenderState) compose(const RenderState *other) const;
00084   CPT(RenderState) invert_compose(const RenderState *other) const;
00085 
00086   CPT(RenderState) add_attrib(const RenderAttrib *attrib, int override = 0) const;
00087   CPT(RenderState) remove_attrib(TypeHandle type) const;
00088 
00089   CPT(RenderState) adjust_all_priorities(int adjustment) const;
00090 
00091   const RenderAttrib *get_attrib(TypeHandle type) const;
00092 
00093   void output(ostream &out) const;
00094   void write(ostream &out, int indent_level) const;
00095 
00096   static int get_max_priority();
00097 
00098   static int get_num_states();
00099   static int get_num_unused_states();
00100   static int clear_cache();
00101 
00102 public:
00103   INLINE int get_bin_index() const;
00104   INLINE int get_draw_order() const;
00105   INLINE const FogAttrib *get_fog() const;
00106   INLINE const CullBinAttrib *get_bin() const;
00107   INLINE const TransparencyAttrib *get_transparency() const;
00108 
00109   CPT(RenderState) issue_delta_modify(const RenderState *other, 
00110                                       GraphicsStateGuardianBase *gsg) const;
00111   CPT(RenderState) issue_delta_set(const RenderState *other, 
00112                                    GraphicsStateGuardianBase *gsg) const;
00113 
00114   static void bin_removed(int bin_index);
00115 
00116 private:
00117   static CPT(RenderState) return_new(RenderState *state);
00118   CPT(RenderState) do_compose(const RenderState *other) const;
00119   CPT(RenderState) do_invert_compose(const RenderState *other) const;
00120   void determine_bin_index();
00121   void determine_fog();
00122   void determine_bin();
00123   void determine_transparency();
00124 
00125   INLINE void set_destructing();
00126   INLINE bool is_destructing() const;
00127 
00128 private:
00129   typedef pset<const RenderState *, IndirectLess<RenderState> > States;
00130   static States *_states;
00131   static CPT(RenderState) _empty_state;
00132 
00133   // This iterator records the entry corresponding to this RenderState
00134   // object in the above global set.  We keep the iterator around so
00135   // we can remove it when the RenderState destructs.
00136   States::iterator _saved_entry;
00137 
00138   // This data structure manages the job of caching the composition of
00139   // two RenderStates.  It's complicated because we have to be sure to
00140   // remove the entry if *either* of the input RenderStates destructs.
00141   // To implement this, we always record Composition entries in pairs,
00142   // one in each of the two involved RenderState objects.
00143   class Composition {
00144   public:
00145     INLINE Composition();
00146     INLINE Composition(const Composition &copy);
00147 
00148     CPT(RenderState) _result;
00149   };
00150     
00151   typedef pmap<const RenderState *, Composition> CompositionCache;
00152   CompositionCache _composition_cache;
00153   CompositionCache _invert_composition_cache;
00154 
00155   // Thise pointer is used to cache the result of compose(this).  This
00156   // has to be a special case, because we have to handle the reference
00157   // counts carefully so that we don't leak.  Most of the time, the
00158   // result of compose(this) is this, which should not be reference
00159   // counted, but other times the result is something else (which
00160   // should be).
00161   const RenderState *_self_compose;
00162 
00163 private:
00164   // This is the actual data within the RenderState: a set of
00165   // RenderAttribs.
00166   class Attribute {
00167   public:
00168     INLINE Attribute(const RenderAttrib *attrib, int override);
00169     INLINE Attribute(int override);
00170     INLINE Attribute(TypeHandle type);
00171     INLINE Attribute(const Attribute &copy);
00172     INLINE void operator = (const Attribute &copy);
00173     INLINE bool operator < (const Attribute &other) const;
00174     INLINE int compare_to(const Attribute &other) const;
00175 
00176     TypeHandle _type;
00177     CPT(RenderAttrib) _attrib;
00178     int _override;
00179   };
00180   typedef ov_set<Attribute> Attributes;
00181   Attributes _attributes;
00182 
00183   // We cache the index to the associated CullBin, if there happens to
00184   // be a CullBinAttrib in the state.
00185   int _bin_index;
00186   int _draw_order;
00187 
00188   // We also cache the pointer to some critical attribs stored in the
00189   // state, if they exist.
00190   const FogAttrib *_fog;
00191   const CullBinAttrib *_bin;
00192   const TransparencyAttrib *_transparency;
00193 
00194   enum Flags {
00195     F_checked_bin_index    = 0x0001,
00196     F_checked_fog          = 0x0002,
00197     F_checked_bin          = 0x0004,
00198     F_checked_transparency = 0x0008,
00199     F_is_destructing       = 0x8000,
00200   };
00201   unsigned short _flags;
00202 
00203 public:
00204   static void register_with_read_factory();
00205   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00206   virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
00207   static TypedWritable *change_this(TypedWritable *old_ptr, BamReader *manager);
00208   virtual void finalize();
00209 
00210 protected:
00211   static TypedWritable *make_from_bam(const FactoryParams &params);
00212   void fillin(DatagramIterator &scan, BamReader *manager);
00213   
00214 public:
00215   static TypeHandle get_class_type() {
00216     return _type_handle;
00217   }
00218   static void init_type() {
00219     TypedWritableReferenceCount::init_type();
00220     register_type(_type_handle, "RenderState",
00221                   TypedWritableReferenceCount::get_class_type());
00222   }
00223   virtual TypeHandle get_type() const {
00224     return get_class_type();
00225   }
00226   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00227 
00228 private:
00229   static TypeHandle _type_handle;
00230 };
00231 
00232 INLINE ostream &operator << (ostream &out, const RenderState &state) {
00233   state.output(out);
00234   return out;
00235 }
00236 
00237 #include "renderState.I"
00238 
00239 #endif
00240 

Generated on Fri May 2 00:42:19 2003 for Panda by doxygen1.3