Panda3D

textureAttrib.h

00001 // Filename: textureAttrib.h
00002 // Created by:  drose (21Feb02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef TEXTUREATTRIB_H
00016 #define TEXTUREATTRIB_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "renderAttrib.h"
00021 #include "texture.h"
00022 #include "textureStage.h"
00023 #include "updateSeq.h"
00024 #include "ordered_vector.h"
00025 #include "vector_int.h"
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //       Class : TextureAttrib
00029 // Description : Indicates the set of TextureStages and their
00030 //               associated Textures that should be applied to (or
00031 //               removed from) a node.
00032 ////////////////////////////////////////////////////////////////////
00033 class EXPCL_PANDA_PGRAPH TextureAttrib : public RenderAttrib {
00034 protected:
00035   INLINE TextureAttrib();
00036   INLINE TextureAttrib(const TextureAttrib &copy);
00037 
00038 PUBLISHED:
00039   // These methods are used to create a simple, single-textured layer.
00040   // For multitexture, use the multitexture interfaces, further below.
00041   static CPT(RenderAttrib) make(Texture *tex);
00042   static CPT(RenderAttrib) make_off();
00043   static CPT(RenderAttrib) make_default();
00044 
00045   INLINE bool is_off() const;
00046   INLINE Texture *get_texture() const;
00047 
00048   // The following methods define the new multitexture mode for
00049   // TextureAttrib.  Each TextureAttrib can add or remove individual
00050   // texture stages from the complete set of textures that are to be
00051   // applied; this is similar to the mechanism of LightAttrib.
00052   static CPT(RenderAttrib) make();
00053   static CPT(RenderAttrib) make_all_off();
00054 
00055   INLINE int get_num_on_stages() const;
00056   INLINE TextureStage *get_on_stage(int n) const;
00057   MAKE_SEQ(get_on_stages, get_num_on_stages, get_on_stage);
00058   INLINE int get_num_on_ff_stages() const;
00059   INLINE TextureStage *get_on_ff_stage(int n) const;
00060   MAKE_SEQ(get_on_ff_stages, get_num_on_ff_stages, get_on_ff_stage);
00061   INLINE int get_ff_tc_index(int n) const;
00062   INLINE bool has_on_stage(TextureStage *stage) const;
00063   INLINE Texture *get_on_texture(TextureStage *stage) const;
00064   INLINE int get_on_stage_override(TextureStage *stage) const;
00065 
00066   int find_on_stage(const TextureStage *stage) const;
00067 
00068   INLINE int get_num_off_stages() const;
00069   INLINE TextureStage *get_off_stage(int n) const;
00070   MAKE_SEQ(get_off_stages, get_num_off_stages, get_off_stage);
00071   INLINE bool has_off_stage(TextureStage *stage) const;
00072   INLINE bool has_all_off() const;
00073 
00074   INLINE bool is_identity() const;
00075 
00076   CPT(RenderAttrib) add_on_stage(TextureStage *stage, Texture *tex, int override = 0) const;
00077   CPT(RenderAttrib) remove_on_stage(TextureStage *stage) const;
00078   CPT(RenderAttrib) add_off_stage(TextureStage *stage, int override = 0) const;
00079   CPT(RenderAttrib) remove_off_stage(TextureStage *stage) const;
00080   CPT(RenderAttrib) unify_texture_stages(TextureStage *stage) const;
00081 
00082 public:
00083   CPT(TextureAttrib) filter_to_max(int max_texture_stages) const;
00084 
00085   virtual bool lower_attrib_can_override() const;
00086   virtual void output(ostream &out) const;
00087 
00088   virtual bool has_cull_callback() const;
00089   virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
00090 
00091 protected:
00092   virtual int compare_to_impl(const RenderAttrib *other) const;
00093   virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
00094   virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
00095 
00096 private:
00097   INLINE void check_sorted() const;
00098   void sort_on_stages();
00099 
00100 private:
00101   class StageNode {
00102   public:
00103     INLINE StageNode(const TextureStage *stage, 
00104                      unsigned int implicit_sort = 0,
00105                      int override = 0);
00106 
00107     PT(TextureStage) _stage;
00108     PT(Texture) _texture;
00109     int _ff_tc_index;
00110     unsigned int _implicit_sort;
00111     int _override;
00112   };
00113 
00114   class CompareTextureStagePriorities {
00115   public:
00116     INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
00117   };
00118 
00119   class CompareTextureStageSort {
00120   public:
00121     INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
00122   };
00123 
00124   class CompareTextureStagePointer {
00125   public:
00126     INLINE bool operator () (const TextureAttrib::StageNode &a, const TextureAttrib::StageNode &b) const;
00127   };
00128 
00129   typedef ov_set<StageNode, CompareTextureStagePointer> Stages;
00130   Stages _on_stages;  // set of all "on" stages, indexed by pointer.
00131 
00132   typedef pvector<StageNode *> RenderStages;
00133   RenderStages _render_stages;      // all "on" stages, sorted in render order.
00134   RenderStages _render_ff_stages;   // fixed-function stages only, in render order.
00135   unsigned int _next_implicit_sort;
00136   
00137   Stages _off_stages;
00138   bool _off_all_stages;
00139 
00140   typedef pmap< int, CPT(TextureAttrib) > Filtered;
00141   Filtered _filtered;
00142 
00143   UpdateSeq _sort_seq;
00144   UpdateSeq _filtered_seq;
00145 
00146   static CPT(RenderAttrib) _empty_attrib;
00147   static CPT(RenderAttrib) _all_off_attrib;
00148 
00149 PUBLISHED:
00150   static int get_class_slot() {
00151     return _attrib_slot;
00152   }
00153   virtual int get_slot() const {
00154     return get_class_slot();
00155   }
00156 
00157 public:
00158   static void register_with_read_factory();
00159   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00160   virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
00161 
00162 protected:
00163   static TypedWritable *make_from_bam(const FactoryParams &params);
00164   void fillin(DatagramIterator &scan, BamReader *manager);
00165   
00166 public:
00167   static TypeHandle get_class_type() {
00168     return _type_handle;
00169   }
00170   static void init_type() {
00171     RenderAttrib::init_type();
00172     register_type(_type_handle, "TextureAttrib",
00173                   RenderAttrib::get_class_type());
00174     _attrib_slot = register_slot(_type_handle, 30, make_default);
00175   }
00176   virtual TypeHandle get_type() const {
00177     return get_class_type();
00178   }
00179   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00180 
00181 private:
00182   static TypeHandle _type_handle;
00183   static int _attrib_slot;
00184 };
00185 
00186 #include "textureAttrib.I"
00187 
00188 #endif
00189 
 All Classes Functions Variables Enumerations