]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/Effect.hxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / material / Effect.hxx
1 // Copyright (C) 2008 - 2009  Tim Moore timoore@redhat.com
2 //
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.
7 //
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.
12 //
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.
16
17 #ifndef SIMGEAR_EFFECT_HXX
18 #define SIMGEAR_EFFECT_HXX 1
19
20 #include <vector>
21 #include <string>
22 #include <boost/tr1/unordered_map.hpp>
23
24 #include <boost/functional/hash.hpp>
25
26 #include <osg/Object>
27 #include <osg/observer_ptr>
28 #include <osgDB/ReaderWriter>
29
30 #include <simgear/props/props.hxx>
31 #include <simgear/scene/util/UpdateOnceCallback.hxx>
32
33 namespace osg
34 {
35 class Drawable;
36 class StateSet;
37 class RenderInfo;
38 }
39
40 namespace osgUtil
41 {
42 class CullVisitor;
43 }
44
45 namespace simgear
46 {
47 class Technique;
48 class Effect;
49 class SGReaderWriterOptions;
50
51 /**
52  * Object to be initialized at some point after an effect -- and its
53  * containing effect geode -- are hooked into the scene graph. Some
54  * things, like manipulations of the global property tree, are are
55  * only safe in the update process.
56  */
57
58 class InitializeWhenAdded
59 {
60 public:
61     InitializeWhenAdded() : _initialized(false) {};
62     virtual ~InitializeWhenAdded() {};
63     void initOnAdd(Effect* effect, SGPropertyNode* propRoot)
64     {
65         if (!_initialized) {
66             initOnAddImpl(effect, propRoot);
67             _initialized = true;
68         }
69     }
70     bool getInitialized() const { return _initialized; }
71 private:
72     virtual void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot) = 0;
73     bool _initialized;
74 };
75
76 class Effect : public osg::Object
77 {
78 public:
79     META_Object(simgear,Effect)
80     Effect();
81     Effect(const Effect& rhs,
82            const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
83     osg::StateSet* getDefaultStateSet();
84
85     // Define what needs to be generated for this effect
86     enum Generator
87     {
88         NORMAL,
89         TANGENT,
90         BINORMAL
91     };
92     void setGenerator(Generator what, int where) { generator[what] = where; }
93     int getGenerator(Generator what) const;  // Returns -1 if generator should not be used
94     std::map<Generator, int> generator;  // What is generated into which attribute location
95
96     std::vector<osg::ref_ptr<Technique> > techniques;
97     SGPropertyNode_ptr root;
98     // Pointer to the parameters node, if it exists
99     SGPropertyNode_ptr parametersProp;
100     Technique* chooseTechnique(osg::RenderInfo* renderInfo);
101     virtual void resizeGLObjectBuffers(unsigned int maxSize);
102     virtual void releaseGLObjects(osg::State* state = 0) const;
103     /**
104      * Build the techniques from the effect properties.
105      */
106     bool realizeTechniques(const SGReaderWriterOptions* options = 0);
107     /**
108      * Updaters that should be derefed when the effect is
109      * deleted. Updaters arrange to be run by listening on properties
110      * or something.
111      */
112     struct Updater : public virtual SGReferenced
113     {
114         virtual ~Updater() {}
115     };
116     void addUpdater(Updater* data) { _extraData.push_back(data); }
117     // Callback that is added to the effect geode to initialize the
118     // effect.
119     friend struct InitializeCallback;
120     struct InitializeCallback : public UpdateOnceCallback
121     {
122         void doUpdate(osg::Node* node, osg::NodeVisitor* nv);
123     };
124 protected:
125     std::vector<SGSharedPtr<Updater> > _extraData;
126     ~Effect();
127     // Support for a cache of effects that inherit from this one, so
128     // Effect objects with the same parameters and techniques can be
129     // shared.
130     struct Key
131     {
132         Key() {}
133         Key(SGPropertyNode* unmerged_, const osgDB::FilePathList& paths_)
134             : unmerged(unmerged_), paths(paths_)
135         {
136         }
137         Key& operator=(const Key& rhs)
138         {
139             unmerged = rhs.unmerged;
140             paths = rhs.paths;
141             return *this;
142         }
143         SGPropertyNode_ptr unmerged;
144         osgDB::FilePathList paths;
145         struct EqualTo
146         {
147             bool operator()(const Key& lhs, const Key& rhs) const;
148         };
149     };
150     typedef std::tr1::unordered_map<Key, osg::observer_ptr<Effect>,
151                                     boost::hash<Key>, Key::EqualTo> Cache;
152     Cache* getCache()
153     {
154         if (!_cache)
155             _cache = new Cache;
156         return _cache;
157     }
158     Cache* _cache;
159     friend size_t hash_value(const Key& key);
160     friend Effect* makeEffect(SGPropertyNode* prop, bool realizeTechniques,
161                               const SGReaderWriterOptions* options);
162     bool _isRealized;
163 };
164 // Automatic support for boost hash function
165 size_t hash_value(const Effect::Key&);
166
167
168 Effect* makeEffect(const std::string& name,
169                    bool realizeTechniques,
170                    const SGReaderWriterOptions* options);
171
172 Effect* makeEffect(SGPropertyNode* prop,
173                    bool realizeTechniques,
174                    const SGReaderWriterOptions* options);
175
176 bool makeParametersFromStateSet(SGPropertyNode* paramRoot,
177                                 const osg::StateSet* ss);
178
179 void clearEffectCache();
180
181 namespace effect
182 {
183 /**
184  * The function that implements effect property tree inheritance.
185  */
186 void mergePropertyTrees(SGPropertyNode* resultNode,
187                         const SGPropertyNode* left,
188                         const SGPropertyNode* right);
189 }
190 }
191 #endif