Effect 6.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/
//osgFX - Copyright (C) 2003 Marco Jez

#ifndef OSGFX__effect
#define OSGFX__effect

#include <osgFX/Export>
#include <osgFX/Technique>

#include <osg/buffered_value>
#include <osg/ref_ptr>
#include <osg/Node>
#include <osg/Group>
#include <osg/Geode>
#include <osg/OccluderNode>

#include <vector>

/**
 An helper macro that defines the methods like effectName() and effectDescription()
 making them return the strings passed as parameters, after the usual library name
 and class name.
 */
#define META_Effect(library, classname, effectname, effectdescription, effectauthor) \
    META_Node(library, classname) \
    virtual const char *effectName() const        { return effectname; } \
    virtual const char *effectDescription() const { return effectdescription; } \
    virtual const char *effectAuthor() const      { return effectauthor; }


namespace osgFX
{

    /**
     The base class for special effects. An effect is basically a collection of
     state attributes and an interface for configuring them in a predefined
     fashion. The Effect class does more however, as it handles multipass
     rendering transparently and it allows more than one "technique" to be
     defined. Each technique tries to implement the effect in a different way,
     often using different OpenGL extensions. The active technique can be
     selected either manually, with selectTechnique(), or automatically, in which
     case the first technique that is supported by all active rendering contexts 
     is chosen.
     If you are an Effect user, then simply use it as a node group. Create an 
     instance of your desired effect, add it to your scene graph and call its 
     addChild() method to add a child node as you would do with a Group.
     If you are an Effect developer, you will have to implement the method
     define_techniques() to define the different techniques that can be used
     for obtaining the desired effect. In define_techniques() you will usually
     create one or more instances of custom classes derived from Technique and
     you will add them to the effect with addTechnique(). The order is important:
     techniques added first will have higher priority and will be used first as
     soon as all rendering contexts support it.
     */
    class OSGFX_EXPORT Effect: public osg::Group {
    public:
        Effect();
        Effect(const Effect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);

        virtual inline bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Effect*>(obj) != NULL; }
        virtual inline const char* libraryName() const { return "osgFX"; }
        virtual inline const char* className() const { return "Effect"; }

        /** get the name of this Effect */
        virtual const char *effectName() const        = 0;
        
        /** get a brief description of this Effect*/
        virtual const char *effectDescription() const = 0;

        /** get the effect author's name */
        virtual const char *effectAuthor() const      = 0;

        /** get whether the effect is enabled or not */
        inline bool getEnabled() const;

        /** set whether the effect is enabled or not */
        inline void setEnabled(bool v);
        
        /**
         optional: set effect parameters to produce a visually significant
         result to be used in demo applications like osgfxbrowser. Default
         is to do nothing.
        */
        inline virtual void setUpDemo()   {}

        /** get the number of techniques defined for this Effect */
        inline int getNumTechniques() const;
        
        /** get the i-th Technique */
        inline Technique* getTechnique(int i);

        /** get the i-th const Technique */
        inline const Technique* getTechnique(int i) const;

        /** get the index of the currently selected Technique */
        inline int getSelectedTechnique() const;

        enum TechniqueSelection {
            AUTO_DETECT = -1
        };

        /** select a technique or enable automatic detection */
        inline void selectTechnique(int i = AUTO_DETECT);

        /** custom traversal */
        virtual void traverse(osg::NodeVisitor& nv);        

        /** default traversal */
        inline void inherited_traverse(osg::NodeVisitor& nv);

    protected:
        virtual ~Effect();
        Effect &operator=(const Effect &) { return *this; }
       
        /** force rebuilding of techniques on next traversal */
        inline void dirtyTechniques();

        /** add a technique to the Effect */
        inline void addTechnique(Technique* tech);

        /**
         abstract method to be implemented in derived classes; its purpose
         if to create the techniques that can be used for obtaining the
         desired effect. You will usually call addTechnique() inside
         this method.
         */
        virtual bool define_techniques() = 0;
        
    private:
        friend class Validator;

        bool _enabled;

        typedef std::vector<osg::ref_ptr<Technique> > Technique_list;
        Technique_list _techs;

        mutable osg::buffered_value<int> _sel_tech;

        // use int instead of bool to avoid errors
        mutable osg::buffered_value<int> _tech_selected;
        
        int _global_sel_tech;

        bool _techs_defined;

        osg::ref_ptr<osg::Geode> _dummy_for_validation;
        
        void build_dummy_node();
    };

    // INLINE METHODS

    inline bool Effect::getEnabled() const
    {
        return _enabled;
    }

    inline void Effect::setEnabled(bool v)
    {
        _enabled = v;
    }
    
    inline int Effect::getNumTechniques() const
    {
        return static_cast<int>(_techs.size());
    }

    inline Technique* Effect::getTechnique(int i)
    {
        return _techs[i].get();
    }

    inline const Technique* Effect::getTechnique(int i) const
    {
        return _techs[i].get();
    }

    inline int Effect::getSelectedTechnique() const
    {
        return _global_sel_tech;
    }

    inline void Effect::selectTechnique(int i)
    {
        _global_sel_tech = i;
    }

    inline void Effect::addTechnique(Technique* tech)
    {
        _techs.push_back(tech);
    }
    
    inline void Effect::dirtyTechniques()
    {
        _techs_defined = false;
    }
    
    inline void Effect::inherited_traverse(osg::NodeVisitor& nv)
    {
        typedef osg::Group inherited;
        inherited::traverse(nv);
    }

}

#endif