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