texProjectorEffect.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Filename: texProjectorEffect.h
  2. // Created by: drose (25Jul04)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef TEXPROJECTOREFFECT_H
  15. #define TEXPROJECTOREFFECT_H
  16. #include "pandabase.h"
  17. #include "renderEffect.h"
  18. #include "luse.h"
  19. #include "nodePath.h"
  20. class LensNode;
  21. ////////////////////////////////////////////////////////////////////
  22. // Class : TexProjectorEffect
  23. // Description : This effect automatically applies a computed texture
  24. // matrix to the specified texture stage, according to
  25. // the relative position of two specified nodes.
  26. //
  27. // The relative transform from the "from" node to the
  28. // "to" node is applied directly to the texture matrix
  29. // each frame. If the "to" node happens to be a
  30. // LensNode, its lens projection matrix is applied as
  31. // well.
  32. //
  33. // This can be used to apply a number of special
  34. // effects. Fundamentally, it may simply be used to
  35. // provide a separate PandaNode that may be adjusted
  36. // (e.g. via a LerpInterval) in order to easily apply a
  37. // linear transformation to an object's texture
  38. // coordinates (rather than having to explicitly call
  39. // NodePath.set_tex_transform() each frame).
  40. //
  41. // In a more sophisticated case, the TexProjectorEffect
  42. // is particularly useful in conjunction with a
  43. // TexGenAttrib that specifies a mode of
  44. // M_world_position (which copies the world position of
  45. // each vertex to the texture coordinates). Then the
  46. // TexProjector can be used to convert these world
  47. // coordinates to the relative coordinates of a
  48. // particular node, causing (for instance) a texture to
  49. // appear to follow a node around as it moves through
  50. // the world. With a LensNode, you can project a
  51. // texture onto the walls, for instance to apply a
  52. // flashlight effect or an image-based shadow.
  53. ////////////////////////////////////////////////////////////////////
  54. class EXPCL_PANDA_PGRAPH TexProjectorEffect : public RenderEffect {
  55. protected:
  56. INLINE TexProjectorEffect();
  57. INLINE TexProjectorEffect(const TexProjectorEffect &copy);
  58. public:
  59. virtual ~TexProjectorEffect();
  60. PUBLISHED:
  61. static CPT(RenderEffect) make();
  62. CPT(RenderEffect) add_stage(TextureStage *stage, const NodePath &from, const NodePath &to) const;
  63. CPT(RenderEffect) remove_stage(TextureStage *stage) const;
  64. bool is_empty() const;
  65. bool has_stage(TextureStage *stage) const;
  66. NodePath get_from(TextureStage *stage) const;
  67. NodePath get_to(TextureStage *stage) const;
  68. public:
  69. virtual void output(ostream &out) const;
  70. virtual bool has_cull_callback() const;
  71. virtual void cull_callback(CullTraverser *trav, CullTraverserData &data,
  72. CPT(TransformState) &node_transform,
  73. CPT(RenderState) &node_state) const;
  74. protected:
  75. virtual int compare_to_impl(const RenderEffect *other) const;
  76. private:
  77. class StageDef {
  78. public:
  79. INLINE StageDef();
  80. INLINE void set_from(const NodePath &from);
  81. void set_to(const NodePath &to);
  82. INLINE int compare_to(const StageDef &other) const;
  83. NodePath _from;
  84. NodePath _to;
  85. LensNode *_to_lens_node;
  86. };
  87. typedef pmap<PT(TextureStage), StageDef> Stages;
  88. Stages _stages;
  89. static CPT(RenderEffect) _empty_effect;
  90. public:
  91. static void register_with_read_factory();
  92. virtual void write_datagram(BamWriter *manager, Datagram &dg);
  93. virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
  94. protected:
  95. static TypedWritable *make_from_bam(const FactoryParams &params);
  96. void fillin(DatagramIterator &scan, BamReader *manager);
  97. public:
  98. static TypeHandle get_class_type() {
  99. return _type_handle;
  100. }
  101. static void init_type() {
  102. RenderEffect::init_type();
  103. register_type(_type_handle, "TexProjectorEffect",
  104. RenderEffect::get_class_type());
  105. }
  106. virtual TypeHandle get_type() const {
  107. return get_class_type();
  108. }
  109. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  110. private:
  111. static TypeHandle _type_handle;
  112. };
  113. #include "texProjectorEffect.I"
  114. #endif