Panda3D
 All Classes Functions Variables Enumerations
texProjectorEffect.h
00001 // Filename: texProjectorEffect.h
00002 // Created by:  drose (25Jul04)
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 TEXPROJECTOREFFECT_H
00016 #define TEXPROJECTOREFFECT_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "renderEffect.h"
00021 #include "luse.h"
00022 #include "nodePath.h"
00023 
00024 class LensNode;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //       Class : TexProjectorEffect
00028 // Description : This effect automatically applies a computed texture
00029 //               matrix to the specified texture stage, according to
00030 //               the relative position of two specified nodes.
00031 //
00032 //               The relative transform from the "from" node to the
00033 //               "to" node is applied directly to the texture matrix
00034 //               each frame.  If the "to" node happens to be a
00035 //               LensNode, its lens projection matrix is applied as
00036 //               well.
00037 //
00038 //               This can be used to apply a number of special
00039 //               effects.  Fundamentally, it may simply be used to
00040 //               provide a separate PandaNode that may be adjusted
00041 //               (e.g. via a LerpInterval) in order to easily apply a
00042 //               linear transformation to an object's texture
00043 //               coordinates (rather than having to explicitly call
00044 //               NodePath.set_tex_transform() each frame).
00045 //
00046 //               In a more sophisticated case, the TexProjectorEffect
00047 //               is particularly useful in conjunction with a
00048 //               TexGenAttrib that specifies a mode of
00049 //               M_world_position (which copies the world position of
00050 //               each vertex to the texture coordinates).  Then the
00051 //               TexProjector can be used to convert these world
00052 //               coordinates to the relative coordinates of a
00053 //               particular node, causing (for instance) a texture to
00054 //               appear to follow a node around as it moves through
00055 //               the world.  With a LensNode, you can project a
00056 //               texture onto the walls, for instance to apply a
00057 //               flashlight effect or an image-based shadow.
00058 ////////////////////////////////////////////////////////////////////
00059 class EXPCL_PANDA_PGRAPH TexProjectorEffect : public RenderEffect {
00060 protected:
00061   INLINE TexProjectorEffect();
00062   INLINE TexProjectorEffect(const TexProjectorEffect &copy);
00063 
00064 public:
00065   virtual ~TexProjectorEffect();
00066 
00067 PUBLISHED:
00068   static CPT(RenderEffect) make();
00069 
00070   CPT(RenderEffect) add_stage(TextureStage *stage, const NodePath &from, const NodePath &to) const;
00071   CPT(RenderEffect) remove_stage(TextureStage *stage) const;
00072 
00073   bool is_empty() const;
00074   bool has_stage(TextureStage *stage) const;
00075 
00076   NodePath get_from(TextureStage *stage) const;
00077   NodePath get_to(TextureStage *stage) const;
00078 
00079 public:
00080   virtual void output(ostream &out) const;
00081 
00082   virtual bool has_cull_callback() const;
00083   virtual void cull_callback(CullTraverser *trav, CullTraverserData &data,
00084                              CPT(TransformState) &node_transform,
00085                              CPT(RenderState) &node_state) const;
00086 
00087 protected:
00088   virtual int compare_to_impl(const RenderEffect *other) const;
00089 
00090 private:
00091   class StageDef {
00092   public:
00093     INLINE StageDef();
00094     INLINE void set_from(const NodePath &from);
00095     void set_to(const NodePath &to);
00096 
00097     INLINE int compare_to(const StageDef &other) const;
00098 
00099     NodePath _from;
00100     NodePath _to;
00101     LensNode *_to_lens_node;
00102   };
00103 
00104   typedef pmap<PT(TextureStage), StageDef> Stages;
00105   Stages _stages;
00106 
00107   static CPT(RenderEffect) _empty_effect;
00108 
00109 public:
00110   static void register_with_read_factory();
00111   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00112   virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
00113 
00114 protected:
00115   static TypedWritable *make_from_bam(const FactoryParams &params);
00116   void fillin(DatagramIterator &scan, BamReader *manager);
00117   
00118 public:
00119   static TypeHandle get_class_type() {
00120     return _type_handle;
00121   }
00122   static void init_type() {
00123     RenderEffect::init_type();
00124     register_type(_type_handle, "TexProjectorEffect",
00125                   RenderEffect::get_class_type());
00126   }
00127   virtual TypeHandle get_type() const {
00128     return get_class_type();
00129   }
00130   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00131 
00132 private:
00133   static TypeHandle _type_handle;
00134 };
00135 
00136 #include "texProjectorEffect.I"
00137 
00138 #endif
00139 
 All Classes Functions Variables Enumerations