1 // Copyright (C) 2008 - 2009 Tim Moore timoore@redhat.com
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
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 GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #ifndef SIMGEAR_EFFECT_HXX
18 #define SIMGEAR_EFFECT_HXX 1
23 #include <boost/unordered_map.hpp>
26 #include <osgDB/ReaderWriter>
28 #include <simgear/props/props.hxx>
29 #include <simgear/scene/util/UpdateOnceCallback.hxx>
49 * Object to be initialized at some point after an effect -- and its
50 * containing effect geode -- are hooked into the scene graph. Some
51 * things, like manipulations of the global property tree, are are
52 * only safe in the update process.
55 class InitializeWhenAdded
58 InitializeWhenAdded() : _initialized(false) {};
59 virtual ~InitializeWhenAdded() {};
60 void initOnAdd(Effect* effect, SGPropertyNode* propRoot)
63 initOnAddImpl(effect, propRoot);
67 bool getInitialized() const { return _initialized; }
69 virtual void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot) = 0;
73 class Effect : public osg::Object
76 META_Object(simgear,Effect)
78 Effect(const Effect& rhs,
79 const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
80 osg::StateSet* getDefaultStateSet();
81 std::vector<osg::ref_ptr<Technique> > techniques;
82 SGPropertyNode_ptr root;
83 // Pointer to the parameters node, if it exists
84 SGPropertyNode_ptr parametersProp;
85 Technique* chooseTechnique(osg::RenderInfo* renderInfo);
86 virtual void resizeGLObjectBuffers(unsigned int maxSize);
87 virtual void releaseGLObjects(osg::State* state = 0) const;
89 * Build the techniques from the effect properties.
91 bool realizeTechniques(const osgDB::ReaderWriter::Options* options = 0);
93 * Updaters that should be derefed when the effect is
94 * deleted. Updaters arrange to be run by listening on properties
97 struct Updater : public virtual SGReferenced
101 void addUpdater(Updater* data) { _extraData.push_back(data); }
102 // Callback that is added to the effect geode to initialize the
104 friend struct InitializeCallback;
105 struct InitializeCallback : public UpdateOnceCallback
107 void doUpdate(osg::Node* node, osg::NodeVisitor* nv);
110 std::vector<SGSharedPtr<Updater> > _extraData;
112 // Support for a cache of effects that inherit from this one, so
113 // Effect objects with the same parameters and techniques can be
117 Key(SGPropertyNode* unmerged_, const osgDB::FilePathList& paths_)
118 : unmerged(unmerged_), paths(paths_)
121 const SGPropertyNode_ptr unmerged;
122 const osgDB::FilePathList paths;
125 bool operator()(const Key& lhs, const Key& rhs) const;
128 typedef boost::unordered_map<Key, osg::ref_ptr<Effect>, boost::hash<Key>,
137 friend size_t hash_value(const Key& key);
138 friend Effect* makeEffect(SGPropertyNode* prop, bool realizeTechniques,
139 const osgDB::ReaderWriter::Options* options);
142 // Automatic support for boost hash function
143 size_t hash_value(const Effect::Key&);
146 Effect* makeEffect(const std::string& name,
147 bool realizeTechniques,
148 const osgDB::ReaderWriter::Options* options = 0);
150 Effect* makeEffect(SGPropertyNode* prop,
151 bool realizeTechniques,
152 const osgDB::ReaderWriter::Options* options = 0);
154 bool makeParametersFromStateSet(SGPropertyNode* paramRoot,
155 const osg::StateSet* ss);
160 * The function that implements effect property tree inheritance.
162 void mergePropertyTrees(SGPropertyNode* resultNode,
163 const SGPropertyNode* left,
164 const SGPropertyNode* right);