Technique 5.48 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
/* -*-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_TECHNIQUE_
#define OSGFX_TECHNIQUE_

#include <osgFX/Export>

#include <osg/Referenced>
#include <osg/State>
#include <osg/Group>
#include <osg/NodeVisitor>

#include <vector>
#include <string>

/**
 An helper macro that defines the methods techniqueName() and 
 techniqueDescription() making them return the strings passed as parameters.
 */
#define META_Technique(name, description) \
    inline virtual const char *techniqueName() { return name; } \
    inline virtual const char *techniqueDescription() { return description; }


namespace osgFX
{

    class Effect;

    /**
     This is the base class for effect techniques. A technique represents one
     of the possible ways to implement a special effect. This base class is
     abstract, you will have to subclass your own techniques for your custom
     effects.
     Derived classes will have to implement the define_passes() method to
     configure the rendering pass(es) that make up the technique. Usually
     you will create one StateSet object for each rendering pass and then
     you'll call addPass(stateset).
     The validate() method should return true if the technique is valid within
     the current rendering context, false otherwise. The default implementation
     of validate() calls getRequiredExtensions() and tests whether all required
     extensions are supported or not, returning false if at least one extension
     is not supported.
     */
    class OSGFX_EXPORT Technique: public osg::Referenced {
    public:
        Technique();

        /** get the name of this Technique */
        virtual const char *techniqueName()        { return "Default"; }

        /** get a brief description of this Technique */
        virtual const char *techniqueDescription() { return "This is the default technique"; }

        /**
         collect the GL extension strings which are required for this technique
         to work properly. This method is called from the default implementation
         of validate().
         */
        virtual void getRequiredExtensions(std::vector<std::string>&  /*extensions*/) const {};

        /**
         tests whether this technique is valid for the current rendering context.
         The default behavior is to call getRequiredExtensions() and check for
         extension availability.
         */
        virtual bool validate(osg::State& ) const;

        /** get the number of rendering passes defined in this Technique */
        inline int getNumPasses() const;

        /** get the StateSet object associated to the i-th pass */
        inline osg::StateSet* getPassStateSet(int i);

        /** get the const StateSet object associated to the i-th pass */
        inline const osg::StateSet* getPassStateSet(int i) const;

        /** 
         traverse children with multipass if necessary. By default this method
         simply calls the protected method traverse_implementation(); you can
         override it to change the default behavior.
         Don't call this method directly as it is called by osgFX::Effect
         */
        inline virtual void traverse(osg::NodeVisitor& nv, Effect* fx);
        
    protected:
        Technique(const Technique &): osg::Referenced() {}    // copying is nonsense ;)
        virtual ~Technique() {}
        Technique &operator=(const Technique &)  { return *this; }

        /** force rebuilding of pass nodes on next traversal */
        inline void dirtyPasses();

        /** create a new pass node, add it to the technique and associate a StateSet */
        void addPass(osg::StateSet* ss = 0);
        
        /** optional: return a node that overrides the child node on a specified pass */
        inline virtual osg::Node* getOverrideChild(int)  { return 0; }

        /**
         define the rendering passes that make up this technique. You must
         implement this method in derived classes to add the required passes.
         */
        virtual void define_passes() = 0;
        
        /**
         traverse children with multipass if necessary. Don't call this method
         directly unless you are in a customized version of traverse().
         */
        void traverse_implementation(osg::NodeVisitor& nv, Effect* fx);

    private:
        typedef std::vector<osg::ref_ptr<osg::StateSet> > Pass_list;
        Pass_list _passes;
    };

    // INLINE METHODS

    inline int Technique::getNumPasses() const
    {
        return static_cast<int>(_passes.size());
    }

    inline osg::StateSet* Technique::getPassStateSet(int i)
    {
        return _passes[i].get();
    }

    inline const osg::StateSet* Technique::getPassStateSet(int i) const
    {
        return _passes[i].get();
    }
      
    inline void Technique::dirtyPasses()
    {
        _passes.clear();
    }
    
    inline void Technique::traverse(osg::NodeVisitor& nv, Effect* fx)
    {
        traverse_implementation(nv, fx);
    }

}

#endif