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
22 #include <boost/tr1/unordered_map.hpp>
24 #include <boost/functional/hash.hpp>
27 #include <osgDB/ReaderWriter>
29 #include <simgear/props/props.hxx>
30 #include <simgear/scene/util/UpdateOnceCallback.hxx>
48 class SGReaderWriterXMLOptions;
51 * Object to be initialized at some point after an effect -- and its
52 * containing effect geode -- are hooked into the scene graph. Some
53 * things, like manipulations of the global property tree, are are
54 * only safe in the update process.
57 class InitializeWhenAdded
60 InitializeWhenAdded() : _initialized(false) {};
61 virtual ~InitializeWhenAdded() {};
62 void initOnAdd(Effect* effect, SGPropertyNode* propRoot)
65 initOnAddImpl(effect, propRoot);
69 bool getInitialized() const { return _initialized; }
71 virtual void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot) = 0;
75 class Effect : public osg::Object
78 META_Object(simgear,Effect)
80 Effect(const Effect& rhs,
81 const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
82 osg::StateSet* getDefaultStateSet();
84 // Define what needs to be generated for this effect
91 void setGenerator(Generator what, int where) { generator[what] = where; }
92 int getGenerator(Generator what) const; // Returns -1 if generator should not be used
93 std::map<Generator, int> generator; // What is generated into which attribute location
95 std::vector<osg::ref_ptr<Technique> > techniques;
96 SGPropertyNode_ptr root;
97 // Pointer to the parameters node, if it exists
98 SGPropertyNode_ptr parametersProp;
99 Technique* chooseTechnique(osg::RenderInfo* renderInfo);
100 virtual void resizeGLObjectBuffers(unsigned int maxSize);
101 virtual void releaseGLObjects(osg::State* state = 0) const;
103 * Build the techniques from the effect properties.
105 bool realizeTechniques(const SGReaderWriterXMLOptions* options = 0);
107 * Updaters that should be derefed when the effect is
108 * deleted. Updaters arrange to be run by listening on properties
111 struct Updater : public virtual SGReferenced
113 virtual ~Updater() {}
115 void addUpdater(Updater* data) { _extraData.push_back(data); }
116 // Callback that is added to the effect geode to initialize the
118 friend struct InitializeCallback;
119 struct InitializeCallback : public UpdateOnceCallback
121 void doUpdate(osg::Node* node, osg::NodeVisitor* nv);
124 std::vector<SGSharedPtr<Updater> > _extraData;
126 // Support for a cache of effects that inherit from this one, so
127 // Effect objects with the same parameters and techniques can be
132 Key(SGPropertyNode* unmerged_, const osgDB::FilePathList& paths_)
133 : unmerged(unmerged_), paths(paths_)
136 Key& operator=(const Key& rhs)
138 unmerged = rhs.unmerged;
142 SGPropertyNode_ptr unmerged;
143 osgDB::FilePathList paths;
146 bool operator()(const Key& lhs, const Key& rhs) const;
149 typedef std::tr1::unordered_map<Key, osg::ref_ptr<Effect>,
150 boost::hash<Key>, Key::EqualTo> Cache;
158 friend size_t hash_value(const Key& key);
159 friend Effect* makeEffect(SGPropertyNode* prop, bool realizeTechniques,
160 const SGReaderWriterXMLOptions* options);
163 // Automatic support for boost hash function
164 size_t hash_value(const Effect::Key&);
167 Effect* makeEffect(const std::string& name,
168 bool realizeTechniques,
169 const SGReaderWriterXMLOptions* options = 0);
171 Effect* makeEffect(SGPropertyNode* prop,
172 bool realizeTechniques,
173 const SGReaderWriterXMLOptions* options = 0);
175 bool makeParametersFromStateSet(SGPropertyNode* paramRoot,
176 const osg::StateSet* ss);
181 * The function that implements effect property tree inheritance.
183 void mergePropertyTrees(SGPropertyNode* resultNode,
184 const SGPropertyNode* left,
185 const SGPropertyNode* right);