Technique 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_TECHNIQUE_
  15. #define OSGFX_TECHNIQUE_
  16. #include <osgFX/Export>
  17. #include <osg/Referenced>
  18. #include <osg/State>
  19. #include <osg/Group>
  20. #include <osg/NodeVisitor>
  21. #include <vector>
  22. #include <string>
  23. /**
  24. An helper macro that defines the methods techniqueName() and
  25. techniqueDescription() making them return the strings passed as parameters.
  26. */
  27. #define META_Technique(name, description) \
  28. inline virtual const char *techniqueName() { return name; } \
  29. inline virtual const char *techniqueDescription() { return description; }
  30. namespace osgFX
  31. {
  32. class Effect;
  33. /**
  34. This is the base class for effect techniques. A technique represents one
  35. of the possible ways to implement a special effect. This base class is
  36. abstract, you will have to subclass your own techniques for your custom
  37. effects.
  38. Derived classes will have to implement the define_passes() method to
  39. configure the rendering pass(es) that make up the technique. Usually
  40. you will create one StateSet object for each rendering pass and then
  41. you'll call addPass(stateset).
  42. The validate() method should return true if the technique is valid within
  43. the current rendering context, false otherwise. The default implementation
  44. of validate() calls getRequiredExtensions() and tests whether all required
  45. extensions are supported or not, returning false if at least one extension
  46. is not supported.
  47. */
  48. class OSGFX_EXPORT Technique: public osg::Referenced {
  49. public:
  50. Technique();
  51. /** get the name of this Technique */
  52. virtual const char *techniqueName() { return "Default"; }
  53. /** get a brief description of this Technique */
  54. virtual const char *techniqueDescription() { return "This is the default technique"; }
  55. /**
  56. collect the GL extension strings which are required for this technique
  57. to work properly. This method is called from the default implementation
  58. of validate().
  59. */
  60. virtual void getRequiredExtensions(std::vector<std::string>& /*extensions*/) const {};
  61. /**
  62. tests whether this technique is valid for the current rendering context.
  63. The default behavior is to call getRequiredExtensions() and check for
  64. extension availability.
  65. */
  66. virtual bool validate(osg::State& ) const;
  67. /** get the number of rendering passes defined in this Technique */
  68. inline int getNumPasses() const;
  69. /** get the StateSet object associated to the i-th pass */
  70. inline osg::StateSet* getPassStateSet(int i);
  71. /** get the const StateSet object associated to the i-th pass */
  72. inline const osg::StateSet* getPassStateSet(int i) const;
  73. /**
  74. traverse children with multipass if necessary. By default this method
  75. simply calls the protected method traverse_implementation(); you can
  76. override it to change the default behavior.
  77. Don't call this method directly as it is called by osgFX::Effect
  78. */
  79. inline virtual void traverse(osg::NodeVisitor& nv, Effect* fx);
  80. protected:
  81. Technique(const Technique &): osg::Referenced() {} // copying is nonsense ;)
  82. virtual ~Technique() {}
  83. Technique &operator=(const Technique &) { return *this; }
  84. /** force rebuilding of pass nodes on next traversal */
  85. inline void dirtyPasses();
  86. /** create a new pass node, add it to the technique and associate a StateSet */
  87. void addPass(osg::StateSet* ss = 0);
  88. /** optional: return a node that overrides the child node on a specified pass */
  89. inline virtual osg::Node* getOverrideChild(int) { return 0; }
  90. /**
  91. define the rendering passes that make up this technique. You must
  92. implement this method in derived classes to add the required passes.
  93. */
  94. virtual void define_passes() = 0;
  95. /**
  96. traverse children with multipass if necessary. Don't call this method
  97. directly unless you are in a customized version of traverse().
  98. */
  99. void traverse_implementation(osg::NodeVisitor& nv, Effect* fx);
  100. private:
  101. typedef std::vector<osg::ref_ptr<osg::StateSet> > Pass_list;
  102. OpenThreads::Mutex _mutex;
  103. OpenThreads::Atomic _passesDefined;
  104. Pass_list _passes;
  105. };
  106. // INLINE METHODS
  107. inline int Technique::getNumPasses() const
  108. {
  109. return _passesDefined!=0 ? static_cast<int>(_passes.size()) : 0;
  110. }
  111. inline osg::StateSet* Technique::getPassStateSet(int i)
  112. {
  113. return _passesDefined!=0 ? _passes[i].get() : 0;
  114. }
  115. inline const osg::StateSet* Technique::getPassStateSet(int i) const
  116. {
  117. return _passesDefined!=0 ? _passes[i].get() : 0;
  118. }
  119. inline void Technique::dirtyPasses()
  120. {
  121. OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _mutex);
  122. _passesDefined.exchange(0);
  123. _passes.clear();
  124. }
  125. inline void Technique::traverse(osg::NodeVisitor& nv, Effect* fx)
  126. {
  127. traverse_implementation(nv, fx);
  128. }
  129. }
  130. #endif