]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/Effect.hxx
Effects in models working for transparent materials and chrome animation
[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
23 #include <osg/Object>
24 #include <osgDB/ReaderWriter>
25
26 #include <simgear/props/props.hxx>
27 #include <simgear/scene/util/UpdateOnceCallback.hxx>
28
29 namespace osg
30 {
31 class Drawable;
32 class StateSet;
33 class RenderInfo;
34 }
35
36 namespace osgUtil
37 {
38 class CullVisitor;
39 }
40
41 namespace simgear
42 {
43 class Technique;
44 class Effect;
45
46 /**
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.
51  */
52
53 class InitializeWhenAdded
54 {
55 public:
56     InitializeWhenAdded() : _initialized(false) {};
57     virtual ~InitializeWhenAdded() {};
58     void initOnAdd(Effect* effect, SGPropertyNode* propRoot)
59     {
60         if (!_initialized) {
61             initOnAddImpl(effect, propRoot);
62             _initialized = true;
63         }
64     }
65     bool getInitialized() const { return _initialized; }
66 private:
67     virtual void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot) = 0;
68     bool _initialized;
69 };
70
71 class Effect : public osg::Object
72 {
73 public:
74     META_Object(simgear,Effect)
75     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;
86     /**
87      * Build the techniques from the effect properties.
88      */
89     bool realizeTechniques(const osgDB::ReaderWriter::Options* options = 0);
90     /**
91      * Updaters that should be derefed when the effect is
92      * deleted. Updaters arrange to be run by listening on properties
93      * or something.
94      */
95     struct Updater : public virtual SGReferenced
96     {
97         virtual ~Updater() {}
98     };
99     void addUpdater(Updater* data) { _extraData.push_back(data); }
100     // Callback that is added to the effect geode to initialize the
101     // effect.
102     friend struct InitializeCallback;
103     struct InitializeCallback : public UpdateOnceCallback
104     {
105         void doUpdate(osg::Node* node, osg::NodeVisitor* nv);
106     };
107     
108 protected:
109     std::vector<SGSharedPtr<Updater> > _extraData;
110     ~Effect();
111 };
112 Effect* makeEffect(const std::string& name,
113                    bool realizeTechniques,
114                    const osgDB::ReaderWriter::Options* options = 0);
115
116 Effect* makeEffect(SGPropertyNode* prop,
117                    bool realizeTechniques,
118                    const osgDB::ReaderWriter::Options* options = 0);
119
120 bool makeParametersFromStateSet(SGPropertyNode* paramRoot,
121                                 const osg::StateSet* ss);
122
123 namespace effect
124 {
125 /**
126  * The function that implements effect property tree inheritance.
127  */
128 void mergePropertyTrees(SGPropertyNode* resultNode,
129                         const SGPropertyNode* left,
130                         const SGPropertyNode* right);
131 }
132 }
133 #endif