OpenSceneGraph/include/osgFX/Effect

218 lines
7.0 KiB
Plaintext
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
2003-08-27 06:09:15 +08:00
*
* 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
2005-04-29 14:32:13 +08:00
#ifndef OSGFX__effect
#define OSGFX__effect
2003-08-27 06:09:15 +08:00
#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.
2003-08-27 06:09:15 +08:00
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 {
2003-08-27 06:09:15 +08:00
public:
Effect();
2005-04-29 14:32:13 +08:00
Effect(const Effect& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
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"; }
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
/** get the name of this Effect */
2003-08-27 06:09:15 +08:00
virtual const char *effectName() const = 0;
2005-04-29 14:32:13 +08:00
/** get a brief description of this Effect*/
2003-08-27 06:09:15 +08:00
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() {}
2005-04-29 14:32:13 +08:00
/** get the number of techniques defined for this Effect */
2003-08-27 06:09:15 +08:00
inline int getNumTechniques() const;
2005-04-29 14:32:13 +08:00
/** get the i-th Technique */
inline Technique* getTechnique(int i);
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
/** get the i-th const Technique */
inline const Technique* getTechnique(int i) const;
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
/** get the index of the currently selected Technique */
2003-08-27 06:09:15 +08:00
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 */
2005-04-29 14:32:13 +08:00
virtual void traverse(osg::NodeVisitor& nv);
/** default traversal */
2005-04-29 14:32:13 +08:00
inline void inherited_traverse(osg::NodeVisitor& nv);
2003-08-27 06:09:15 +08:00
protected:
virtual ~Effect();
2003-08-27 06:09:15 +08:00
Effect &operator=(const Effect &) { return *this; }
2003-08-27 06:09:15 +08:00
/** force rebuilding of techniques on next traversal */
inline void dirtyTechniques();
2005-04-29 14:32:13 +08:00
/** add a technique to the Effect */
inline void addTechnique(Technique* tech);
2003-08-27 06:09:15 +08:00
/**
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;
2003-08-27 06:09:15 +08:00
private:
friend class Validator;
2005-04-29 14:32:13 +08:00
bool _enabled;
2003-08-27 06:09:15 +08:00
typedef std::vector<osg::ref_ptr<Technique> > Technique_list;
2005-04-29 14:32:13 +08:00
Technique_list _techs;
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
mutable osg::buffered_value<int> _sel_tech;
2003-08-27 06:09:15 +08:00
// use int instead of bool to avoid errors
2005-04-29 14:32:13 +08:00
mutable osg::buffered_value<int> _tech_selected;
2005-04-29 14:32:13 +08:00
int _global_sel_tech;
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
bool _techs_defined;
2003-08-27 06:09:15 +08:00
2005-04-29 14:32:13 +08:00
osg::ref_ptr<osg::Geode> _dummy_for_validation;
2003-08-27 06:09:15 +08:00
void build_dummy_node();
};
// INLINE METHODS
inline bool Effect::getEnabled() const
{
2005-04-29 14:32:13 +08:00
return _enabled;
2003-08-27 06:09:15 +08:00
}
inline void Effect::setEnabled(bool v)
{
2005-04-29 14:32:13 +08:00
_enabled = v;
2003-08-27 06:09:15 +08:00
}
inline int Effect::getNumTechniques() const
{
2005-04-29 14:32:13 +08:00
return static_cast<int>(_techs.size());
2003-08-27 06:09:15 +08:00
}
2005-04-29 14:32:13 +08:00
inline Technique* Effect::getTechnique(int i)
2003-08-27 06:09:15 +08:00
{
2005-04-29 14:32:13 +08:00
return _techs[i].get();
2003-08-27 06:09:15 +08:00
}
2005-04-29 14:32:13 +08:00
inline const Technique* Effect::getTechnique(int i) const
2003-08-27 06:09:15 +08:00
{
2005-04-29 14:32:13 +08:00
return _techs[i].get();
2003-08-27 06:09:15 +08:00
}
inline int Effect::getSelectedTechnique() const
{
2005-04-29 14:32:13 +08:00
return _global_sel_tech;
2003-08-27 06:09:15 +08:00
}
inline void Effect::selectTechnique(int i)
{
2005-04-29 14:32:13 +08:00
_global_sel_tech = i;
2003-08-27 06:09:15 +08:00
}
2005-04-29 14:32:13 +08:00
inline void Effect::addTechnique(Technique* tech)
2003-08-27 06:09:15 +08:00
{
2005-04-29 14:32:13 +08:00
_techs.push_back(tech);
2003-08-27 06:09:15 +08:00
}
inline void Effect::dirtyTechniques()
{
2005-04-29 14:32:13 +08:00
_techs_defined = false;
2003-08-27 06:09:15 +08:00
}
2005-04-29 14:32:13 +08:00
inline void Effect::inherited_traverse(osg::NodeVisitor& nv)
{
typedef osg::Group inherited;
inherited::traverse(nv);
}
2003-08-27 06:09:15 +08:00
}
#endif