| 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 | |
|---|
| 15 | #ifndef OSGFX__effect |
|---|
| 16 | #define OSGFX__effect |
|---|
| 17 | |
|---|
| 18 | #include <osgFX/Export> |
|---|
| 19 | #include <osgFX/Technique> |
|---|
| 20 | |
|---|
| 21 | #include <osg/buffered_value> |
|---|
| 22 | #include <osg/ref_ptr> |
|---|
| 23 | #include <osg/Node> |
|---|
| 24 | #include <osg/Group> |
|---|
| 25 | #include <osg/Geode> |
|---|
| 26 | #include <osg/OccluderNode> |
|---|
| 27 | |
|---|
| 28 | #include <vector> |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | An helper macro that defines the methods like effectName() and effectDescription() |
|---|
| 32 | making them return the strings passed as parameters, after the usual library name |
|---|
| 33 | and class name. |
|---|
| 34 | */ |
|---|
| 35 | #define META_Effect(library, classname, effectname, effectdescription, effectauthor) \ |
|---|
| 36 | META_Node(library, classname) \ |
|---|
| 37 | virtual const char *effectName() const { return effectname; } \ |
|---|
| 38 | virtual const char *effectDescription() const { return effectdescription; } \ |
|---|
| 39 | virtual const char *effectAuthor() const { return effectauthor; } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | namespace osgFX |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | The base class for special effects. An effect is basically a collection of |
|---|
| 47 | state attributes and an interface for configuring them in a predefined |
|---|
| 48 | fashion. The Effect class does more however, as it handles multipass |
|---|
| 49 | rendering transparently and it allows more than one "technique" to be |
|---|
| 50 | defined. Each technique tries to implement the effect in a different way, |
|---|
| 51 | often using different OpenGL extensions. The active technique can be |
|---|
| 52 | selected either manually, with selectTechnique(), or automatically, in which |
|---|
| 53 | case the first technique that is supported by all active rendering contexts |
|---|
| 54 | is chosen. |
|---|
| 55 | If you are an Effect user, then simply use it as a node group. Create an |
|---|
| 56 | instance of your desired effect, add it to your scene graph and call its |
|---|
| 57 | addChild() method to add a child node as you would do with a Group. |
|---|
| 58 | If you are an Effect developer, you will have to implement the method |
|---|
| 59 | define_techniques() to define the different techniques that can be used |
|---|
| 60 | for obtaining the desired effect. In define_techniques() you will usually |
|---|
| 61 | create one or more instances of custom classes derived from Technique and |
|---|
| 62 | you will add them to the effect with addTechnique(). The order is important: |
|---|
| 63 | techniques added first will have higher priority and will be used first as |
|---|
| 64 | soon as all rendering contexts support it. |
|---|
| 65 | */ |
|---|
| 66 | class OSGFX_EXPORT Effect: public osg::Group { |
|---|
| 67 | public: |
|---|
| 68 | Effect(); |
|---|
| 69 | Effect(const Effect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); |
|---|
| 70 | |
|---|
| 71 | virtual inline bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Effect*>(obj) != NULL; } |
|---|
| 72 | virtual inline const char* libraryName() const { return "osgFX"; } |
|---|
| 73 | virtual inline const char* className() const { return "Effect"; } |
|---|
| 74 | |
|---|
| 75 | /** get the name of this Effect */ |
|---|
| 76 | virtual const char *effectName() const = 0; |
|---|
| 77 | |
|---|
| 78 | /** get a brief description of this Effect*/ |
|---|
| 79 | virtual const char *effectDescription() const = 0; |
|---|
| 80 | |
|---|
| 81 | /** get the effect author's name */ |
|---|
| 82 | virtual const char *effectAuthor() const = 0; |
|---|
| 83 | |
|---|
| 84 | /** get whether the effect is enabled or not */ |
|---|
| 85 | inline bool getEnabled() const; |
|---|
| 86 | |
|---|
| 87 | /** set whether the effect is enabled or not */ |
|---|
| 88 | inline void setEnabled(bool v); |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | optional: set effect parameters to produce a visually significant |
|---|
| 92 | result to be used in demo applications like osgfxbrowser. Default |
|---|
| 93 | is to do nothing. |
|---|
| 94 | */ |
|---|
| 95 | inline virtual void setUpDemo() {} |
|---|
| 96 | |
|---|
| 97 | /** get the number of techniques defined for this Effect */ |
|---|
| 98 | inline int getNumTechniques() const; |
|---|
| 99 | |
|---|
| 100 | /** get the i-th Technique */ |
|---|
| 101 | inline Technique* getTechnique(int i); |
|---|
| 102 | |
|---|
| 103 | /** get the i-th const Technique */ |
|---|
| 104 | inline const Technique* getTechnique(int i) const; |
|---|
| 105 | |
|---|
| 106 | /** get the index of the currently selected Technique */ |
|---|
| 107 | inline int getSelectedTechnique() const; |
|---|
| 108 | |
|---|
| 109 | enum TechniqueSelection { |
|---|
| 110 | AUTO_DETECT = -1 |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | /** select a technique or enable automatic detection */ |
|---|
| 114 | inline void selectTechnique(int i = AUTO_DETECT); |
|---|
| 115 | |
|---|
| 116 | /** custom traversal */ |
|---|
| 117 | virtual void traverse(osg::NodeVisitor& nv); |
|---|
| 118 | |
|---|
| 119 | /** default traversal */ |
|---|
| 120 | inline void inherited_traverse(osg::NodeVisitor& nv); |
|---|
| 121 | |
|---|
| 122 | protected: |
|---|
| 123 | virtual ~Effect(); |
|---|
| 124 | Effect &operator=(const Effect &) { return *this; } |
|---|
| 125 | |
|---|
| 126 | /** force rebuilding of techniques on next traversal */ |
|---|
| 127 | inline void dirtyTechniques(); |
|---|
| 128 | |
|---|
| 129 | /** add a technique to the Effect */ |
|---|
| 130 | inline void addTechnique(Technique* tech); |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | abstract method to be implemented in derived classes; its purpose |
|---|
| 134 | if to create the techniques that can be used for obtaining the |
|---|
| 135 | desired effect. You will usually call addTechnique() inside |
|---|
| 136 | this method. |
|---|
| 137 | */ |
|---|
| 138 | virtual bool define_techniques() = 0; |
|---|
| 139 | |
|---|
| 140 | private: |
|---|
| 141 | friend class Validator; |
|---|
| 142 | |
|---|
| 143 | bool _enabled; |
|---|
| 144 | |
|---|
| 145 | typedef std::vector<osg::ref_ptr<Technique> > Technique_list; |
|---|
| 146 | Technique_list _techs; |
|---|
| 147 | |
|---|
| 148 | mutable osg::buffered_value<int> _sel_tech; |
|---|
| 149 | |
|---|
| 150 | // use int instead of bool to avoid errors |
|---|
| 151 | mutable osg::buffered_value<int> _tech_selected; |
|---|
| 152 | |
|---|
| 153 | int _global_sel_tech; |
|---|
| 154 | |
|---|
| 155 | bool _techs_defined; |
|---|
| 156 | |
|---|
| 157 | osg::ref_ptr<osg::Geode> _dummy_for_validation; |
|---|
| 158 | |
|---|
| 159 | void build_dummy_node(); |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | // INLINE METHODS |
|---|
| 163 | |
|---|
| 164 | inline bool Effect::getEnabled() const |
|---|
| 165 | { |
|---|
| 166 | return _enabled; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | inline void Effect::setEnabled(bool v) |
|---|
| 170 | { |
|---|
| 171 | _enabled = v; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | inline int Effect::getNumTechniques() const |
|---|
| 175 | { |
|---|
| 176 | return static_cast<int>(_techs.size()); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | inline Technique* Effect::getTechnique(int i) |
|---|
| 180 | { |
|---|
| 181 | return _techs[i].get(); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | inline const Technique* Effect::getTechnique(int i) const |
|---|
| 185 | { |
|---|
| 186 | return _techs[i].get(); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | inline int Effect::getSelectedTechnique() const |
|---|
| 190 | { |
|---|
| 191 | return _global_sel_tech; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | inline void Effect::selectTechnique(int i) |
|---|
| 195 | { |
|---|
| 196 | _global_sel_tech = i; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | inline void Effect::addTechnique(Technique* tech) |
|---|
| 200 | { |
|---|
| 201 | _techs.push_back(tech); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | inline void Effect::dirtyTechniques() |
|---|
| 205 | { |
|---|
| 206 | _techs_defined = false; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | inline void Effect::inherited_traverse(osg::NodeVisitor& nv) |
|---|
| 210 | { |
|---|
| 211 | typedef osg::Group inherited; |
|---|
| 212 | inherited::traverse(nv); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | #endif |
|---|