Effect 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //osgFX - Copyright (C) 2003 Marco Jez
  14. #ifndef OSGFX__effect
  15. #define OSGFX__effect
  16. #include <osgFX/Export>
  17. #include <osgFX/Technique>
  18. #include <osg/buffered_value>
  19. #include <osg/ref_ptr>
  20. #include <osg/Node>
  21. #include <osg/Group>
  22. #include <osg/Geode>
  23. #include <osg/OccluderNode>
  24. #include <vector>
  25. /**
  26. An helper macro that defines the methods like effectName() and effectDescription()
  27. making them return the strings passed as parameters, after the usual library name
  28. and class name.
  29. */
  30. #define META_Effect(library, classname, effectname, effectdescription, effectauthor) \
  31. META_Node(library, classname) \
  32. virtual const char *effectName() const { return effectname; } \
  33. virtual const char *effectDescription() const { return effectdescription; } \
  34. virtual const char *effectAuthor() const { return effectauthor; }
  35. namespace osgFX
  36. {
  37. /**
  38. The base class for special effects. An effect is basically a collection of
  39. state attributes and an interface for configuring them in a predefined
  40. fashion. The Effect class does more however, as it handles multipass
  41. rendering transparently and it allows more than one "technique" to be
  42. defined. Each technique tries to implement the effect in a different way,
  43. often using different OpenGL extensions. The active technique can be
  44. selected either manually, with selectTechnique(), or automatically, in which
  45. case the first technique that is supported by all active rendering contexts
  46. is chosen.
  47. If you are an Effect user, then simply use it as a node group. Create an
  48. instance of your desired effect, add it to your scene graph and call its
  49. addChild() method to add a child node as you would do with a Group.
  50. If you are an Effect developer, you will have to implement the method
  51. define_techniques() to define the different techniques that can be used
  52. for obtaining the desired effect. In define_techniques() you will usually
  53. create one or more instances of custom classes derived from Technique and
  54. you will add them to the effect with addTechnique(). The order is important:
  55. techniques added first will have higher priority and will be used first as
  56. soon as all rendering contexts support it.
  57. */
  58. class OSGFX_EXPORT Effect: public osg::Group {
  59. public:
  60. Effect();
  61. Effect(const Effect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  62. virtual inline bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Effect*>(obj) != NULL; }
  63. virtual inline const char* libraryName() const { return "osgFX"; }
  64. virtual inline const char* className() const { return "Effect"; }
  65. /** get the name of this Effect */
  66. virtual const char *effectName() const = 0;
  67. /** get a brief description of this Effect*/
  68. virtual const char *effectDescription() const = 0;
  69. /** get the effect author's name */
  70. virtual const char *effectAuthor() const = 0;
  71. /** get whether the effect is enabled or not */
  72. inline bool getEnabled() const;
  73. /** set whether the effect is enabled or not */
  74. inline void setEnabled(bool v);
  75. /**
  76. optional: set effect parameters to produce a visually significant
  77. result to be used in demo applications like osgfxbrowser. Default
  78. is to do nothing.
  79. */
  80. inline virtual void setUpDemo() {}
  81. /** get the number of techniques defined for this Effect */
  82. inline int getNumTechniques() const;
  83. /** get the i-th Technique */
  84. inline Technique* getTechnique(int i);
  85. /** get the i-th const Technique */
  86. inline const Technique* getTechnique(int i) const;
  87. /** get the index of the currently selected Technique */
  88. inline int getSelectedTechnique() const;
  89. enum TechniqueSelection {
  90. AUTO_DETECT = -1
  91. };
  92. /** select a technique or enable automatic detection */
  93. inline void selectTechnique(int i = AUTO_DETECT);
  94. /** custom traversal */
  95. virtual void traverse(osg::NodeVisitor& nv);
  96. /** default traversal */
  97. inline void inherited_traverse(osg::NodeVisitor& nv);
  98. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  99. virtual void releaseGLObjects(osg::State* state = 0) const;
  100. protected:
  101. virtual ~Effect();
  102. Effect &operator=(const Effect &) { return *this; }
  103. /** force rebuilding of techniques on next traversal */
  104. inline void dirtyTechniques();
  105. /** add a technique to the Effect */
  106. inline void addTechnique(Technique* tech);
  107. /**
  108. abstract method to be implemented in derived classes; its purpose
  109. if to create the techniques that can be used for obtaining the
  110. desired effect. You will usually call addTechnique() inside
  111. this method.
  112. */
  113. virtual bool define_techniques() = 0;
  114. private:
  115. friend class Validator;
  116. bool _enabled;
  117. typedef std::vector<osg::ref_ptr<Technique> > Technique_list;
  118. Technique_list _techs;
  119. mutable osg::buffered_value<int> _sel_tech;
  120. // use int instead of bool to avoid errors
  121. mutable osg::buffered_value<int> _tech_selected;
  122. int _global_sel_tech;
  123. bool _techs_defined;
  124. osg::ref_ptr<osg::Geode> _dummy_for_validation;
  125. void build_dummy_node();
  126. };
  127. // INLINE METHODS
  128. inline bool Effect::getEnabled() const
  129. {
  130. return _enabled;
  131. }
  132. inline void Effect::setEnabled(bool v)
  133. {
  134. _enabled = v;
  135. }
  136. inline int Effect::getNumTechniques() const
  137. {
  138. return static_cast<int>(_techs.size());
  139. }
  140. inline Technique* Effect::getTechnique(int i)
  141. {
  142. return _techs[i].get();
  143. }
  144. inline const Technique* Effect::getTechnique(int i) const
  145. {
  146. return _techs[i].get();
  147. }
  148. inline int Effect::getSelectedTechnique() const
  149. {
  150. return _global_sel_tech;
  151. }
  152. inline void Effect::selectTechnique(int i)
  153. {
  154. _global_sel_tech = i;
  155. }
  156. inline void Effect::addTechnique(Technique* tech)
  157. {
  158. _techs.push_back(tech);
  159. }
  160. inline void Effect::dirtyTechniques()
  161. {
  162. _techs_defined = false;
  163. }
  164. inline void Effect::inherited_traverse(osg::NodeVisitor& nv)
  165. {
  166. typedef osg::Group inherited;
  167. inherited::traverse(nv);
  168. }
  169. }
  170. #endif