Panda3D

renderAttrib.h

00001 // Filename: renderAttrib.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 RENDERATTRIB_H
00016 #define RENDERATTRIB_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "typedWritableReferenceCount.h"
00021 #include "renderAttribRegistry.h"
00022 #include "pointerTo.h"
00023 #include "simpleHashMap.h"
00024 #include "lightReMutex.h"
00025 #include "pStatCollector.h"
00026 
00027 class AttribSlots;
00028 class GraphicsStateGuardianBase;
00029 class CullTraverser;
00030 class CullTraverserData;
00031 class RenderState;
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //       Class : RenderAttrib
00035 // Description : This is the base class for a number of render
00036 //               attributes (other than transform) that may be set on
00037 //               scene graph nodes to control the appearance of
00038 //               geometry.  This includes TextureAttrib, ColorAttrib,
00039 //               etc.
00040 //
00041 //               RenderAttrib represents render attributes that always
00042 //               propagate down to the leaves without regard to the
00043 //               particular node they are assigned to.  A RenderAttrib
00044 //               will have the same effect on a leaf node whether it
00045 //               is assigned to the graph at the leaf or several nodes
00046 //               above.  This is different from RenderEffect, which
00047 //               represents a particular render property that is
00048 //               applied immediately to the node on which it is
00049 //               encountered, like billboarding or decaling.
00050 //
00051 //               You should not attempt to create or modify a
00052 //               RenderAttrib directly; instead, use the make() method
00053 //               of the appropriate kind of attrib you want.  This
00054 //               will allocate and return a new RenderAttrib of the
00055 //               appropriate type, and it may share pointers if
00056 //               possible.  Do not modify the new RenderAttrib if you
00057 //               wish to change its properties; instead, create a new
00058 //               one.
00059 ////////////////////////////////////////////////////////////////////
00060 class EXPCL_PANDA_PGRAPH RenderAttrib : public TypedWritableReferenceCount {
00061 protected:
00062   RenderAttrib();
00063 private:
00064   RenderAttrib(const RenderAttrib &copy);
00065   void operator = (const RenderAttrib &copy);
00066 
00067 public:
00068   virtual ~RenderAttrib();
00069 
00070 PUBLISHED:
00071   INLINE CPT(RenderAttrib) compose(const RenderAttrib *other) const;
00072   INLINE CPT(RenderAttrib) invert_compose(const RenderAttrib *other) const;
00073   virtual bool lower_attrib_can_override() const;
00074 
00075 public:
00076   INLINE bool always_reissue() const;
00077 
00078   virtual bool has_cull_callback() const;
00079   virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
00080 
00081 PUBLISHED:
00082   INLINE int compare_to(const RenderAttrib &other) const;
00083   INLINE size_t get_hash() const;
00084   INLINE CPT(RenderAttrib) get_unique() const;
00085   INLINE CPT(RenderAttrib) get_auto_shader_attrib(const RenderState *state) const;
00086 
00087   virtual bool unref() const;
00088 
00089   virtual void output(ostream &out) const;
00090   virtual void write(ostream &out, int indent_level) const;
00091 
00092   static int get_num_attribs();
00093   static void list_attribs(ostream &out);
00094   static int garbage_collect();
00095   static bool validate_attribs();
00096 
00097   virtual int get_slot() const=0;
00098 
00099   enum PandaCompareFunc {   // intentionally defined to match D3DCMPFUNC
00100     M_none=0,           // alpha-test disabled (always-draw)
00101     M_never,            // Never draw.
00102     M_less,             // incoming < reference_alpha
00103     M_equal,            // incoming == reference_alpha
00104     M_less_equal,       // incoming <= reference_alpha
00105     M_greater,          // incoming > reference_alpha
00106     M_not_equal,        // incoming != reference_alpha
00107     M_greater_equal,    // incoming >= reference_alpha
00108     M_always            // Always draw.  
00109   };
00110 
00111   // This is the enumerated type for TexGenAttrib.  It is inherited
00112   // into TexGenAttrib.  It is defined up at this level only to avoid
00113   // circular dependencies in the header files.
00114   enum TexGenMode {
00115     M_off,
00116 
00117     // In the types below, "eye" means the coordinate space of the
00118     // observing camera, and "world" means world coordinates, e.g. the
00119     // coordinate space of the root of the graph.
00120 
00121     // Sphere maps are classic static reflection maps.  They are
00122     // supported on just about any hardware, and require a precomputed
00123     // 360-degree fisheye image.  Sphere maps only make sense in eye
00124     // coordinate space.
00125     M_eye_sphere_map,
00126 
00127     // Cube maps are a modern improvement on the sphere map; they
00128     // don't suffer from any polar singularities, but they require six
00129     // texture images.  They can also be generated dynamically for
00130     // real-time reflections (see GraphicsOutput::make_cube_map()).
00131     // Typically, a statically-generated cube map will be in eye
00132     // space, while a dynamically-generated map will be in world
00133     // space.
00134 
00135     // Cube mapping is not supported on all hardware.
00136     M_world_cube_map,
00137     M_eye_cube_map,
00138 
00139     // Normal maps are most useful for applying diffuse lighting
00140     // effects via a pregenerated cube map.
00141     M_world_normal,
00142     M_eye_normal,
00143 
00144     // Position maps convert XYZ coordinates directly to texture
00145     // coordinates.  This is particularly useful for implementing
00146     // projective texturing (see NodePath::project_texture()).
00147     M_world_position,
00148     M_unused,  // formerly M_object_position, now deprecated.
00149     M_eye_position,
00150 
00151     // With M_point_sprite, texture coordinates will be generated for
00152     // large points in the range (0,0) - (1,1) from upper-left to
00153     // lower-right across the point's face.  Without this, each point
00154     // will have just a single uniform texture coordinate value across
00155     // its face.
00156 
00157     // Unfortunately, the generated texture coordinates are inverted
00158     // (upside-down) from Panda's usual convention, but this is what
00159     // the graphics card manufacturers decided to use.  You could use
00160     // a texture matrix to re-invert the texture, but that will
00161     // probably force the sprites' vertices to be computed in the CPU.
00162     // You'll have to paint your textures upside-down if you want true
00163     // hardware sprites.
00164     M_point_sprite,
00165 
00166     // M_light_vector generates special 3-d texture coordinates that
00167     // represent the vector to a particular Light in the scene graph,
00168     // expressed in each vertex's tangent space.  This is used to
00169     // implement bumpmapping.  It is always computed on the CPU.
00170 
00171     // This requires a Light to be specified to the TexGenAttrib.  It
00172     // also requires each vertex to define a normal, as well as a
00173     // tangent and binormal for the particular named texture
00174     // coordinate set.
00175     M_light_vector,
00176 
00177     // M_constant generates the same fixed texture coordinates at each
00178     // vertex.  Not terribly useful, of course, except for certain
00179     // special effects involving moving a flat color over an object.
00180     M_constant,
00181   };
00182 
00183 protected:
00184   static CPT(RenderAttrib) return_new(RenderAttrib *attrib);
00185   static CPT(RenderAttrib) return_unique(RenderAttrib *attrib);
00186   virtual int compare_to_impl(const RenderAttrib *other) const;
00187   virtual size_t get_hash_impl() const;
00188   virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
00189   virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
00190   virtual CPT(RenderAttrib) get_auto_shader_attrib_impl(const RenderState *state) const;
00191   void output_comparefunc(ostream &out, PandaCompareFunc fn) const;
00192 
00193 public:
00194   INLINE static int register_slot(TypeHandle type_handle, int sort,
00195                                   RenderAttribRegistry::MakeDefaultFunc *make_default_func);
00196 
00197 private:
00198   void release_new();
00199 
00200 protected:
00201   bool _always_reissue;
00202 
00203 public:
00204   static void init_attribs();
00205 
00206 private:
00207   // This mutex protects _attribs.
00208   static LightReMutex *_attribs_lock;
00209   class Empty {
00210   };
00211   typedef SimpleHashMap<const RenderAttrib *, Empty, indirect_compare_to_hash<const RenderAttrib *> > Attribs;
00212   static Attribs *_attribs;
00213 
00214   int _saved_entry;
00215 
00216   // This keeps track of our current position through the garbage
00217   // collection cycle.
00218   static int _garbage_index;
00219 
00220   static PStatCollector _garbage_collect_pcollector;
00221 
00222 public:
00223   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00224   static TypedWritable *change_this(TypedWritable *old_ptr, BamReader *manager);
00225   virtual void finalize(BamReader *manager);
00226 
00227 protected:
00228   static TypedWritable *new_from_bam(RenderAttrib *attrib, BamReader *manager);
00229   void fillin(DatagramIterator &scan, BamReader *manager);
00230   
00231 public:
00232   static TypeHandle get_class_type() {
00233     return _type_handle;
00234   }
00235   static void init_type() {
00236     TypedWritableReferenceCount::init_type();
00237     register_type(_type_handle, "RenderAttrib",
00238                   TypedWritableReferenceCount::get_class_type());
00239   }
00240   virtual TypeHandle get_type() const {
00241     return get_class_type();
00242   }
00243   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00244 
00245 private:
00246   static TypeHandle _type_handle;
00247 };
00248 
00249 INLINE ostream &operator << (ostream &out, const RenderAttrib &attrib) {
00250   attrib.output(out);
00251   return out;
00252 }
00253 
00254 #include "renderAttrib.I"
00255 
00256 #endif
00257 
 All Classes Functions Variables Enumerations