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
24 #include <osgDB/ReaderWriter>
26 #include <simgear/props/props.hxx>
27 #include <simgear/scene/util/UpdateOnceCallback.hxx>
47 * Object to be initialized at some point after an effect -- and its
48 * containing effect geode -- are hooked into the scene graph. Some
49 * things, like manipulations of the global property tree, are are
50 * only safe in the update process.
53 class InitializeWhenAdded
56 InitializeWhenAdded() : _initialized(false) {};
57 virtual ~InitializeWhenAdded() {};
58 void initOnAdd(Effect* effect, SGPropertyNode* propRoot)
61 initOnAddImpl(effect, propRoot);
65 bool getInitialized() const { return _initialized; }
67 virtual void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot) = 0;
71 class Effect : public osg::Object
74 META_Object(simgear,Effect)
76 Effect(const Effect& rhs,
77 const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
78 osg::StateSet* getDefaultStateSet();
79 std::vector<osg::ref_ptr<Technique> > techniques;
80 SGPropertyNode_ptr root;
81 // Pointer to the parameters node, if it exists
82 SGPropertyNode_ptr parametersProp;
83 Technique* chooseTechnique(osg::RenderInfo* renderInfo);
84 virtual void resizeGLObjectBuffers(unsigned int maxSize);
85 virtual void releaseGLObjects(osg::State* state = 0) const;
87 * Build the techniques from the effect properties.
89 bool realizeTechniques(const osgDB::ReaderWriter::Options* options = 0);
91 * Updaters that should be derefed when the effect is
92 * deleted. Updaters arrange to be run by listening on properties
95 struct Updater : public virtual SGReferenced
99 void addUpdater(Updater* data) { _extraData.push_back(data); }
100 // Callback that is added to the effect geode to initialize the
102 friend struct InitializeCallback;
103 struct InitializeCallback : public UpdateOnceCallback
105 void doUpdate(osg::Node* node, osg::NodeVisitor* nv);
109 std::vector<SGSharedPtr<Updater> > _extraData;
112 Effect* makeEffect(const std::string& name,
113 bool realizeTechniques,
114 const osgDB::ReaderWriter::Options* options = 0);
116 Effect* makeEffect(SGPropertyNode* prop,
117 bool realizeTechniques,
118 const osgDB::ReaderWriter::Options* options = 0);
120 bool makeParametersFromStateSet(SGPropertyNode* paramRoot,
121 const osg::StateSet* ss);
126 * The function that implements effect property tree inheritance.
128 void mergePropertyTrees(SGPropertyNode* resultNode,
129 const SGPropertyNode* left,
130 const SGPropertyNode* right);