]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/od_gauge.cxx
883a17178036aabfa0a814506dca636056fd7cb2
[flightgear.git] / src / Cockpit / od_gauge.cxx
1 // Owner Drawn Gauge helper class
2 //
3 // Written by Harald JOHNSEN, started May 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN
6 //
7 // Ported to OSG by Tim Moore - Jun 2007
8 //
9 // Heavily modified to be usable for the 2d Canvas by Thomas Geymayer - April 2012
10 // Supports now multisampling/mipmapping, usage of the stencil buffer and placing
11 // the texture in the scene by certain filter criteria
12 //
13 // This program is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU General Public License as
15 // published by the Free Software Foundation; either version 2 of the
16 // License, or (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26 //
27 //
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include <osg/Texture2D>
34 #include <osg/AlphaFunc>
35 #include <osg/BlendFunc>
36 #include <osg/Camera>
37 #include <osg/Geode>
38 #include <osg/NodeVisitor>
39 #include <osg/Matrix>
40 #include <osg/PolygonMode>
41 #include <osg/ShadeModel>
42 #include <osg/StateSet>
43 #include <osg/FrameBufferObject> // for GL_DEPTH_STENCIL_EXT on Windows
44
45 #include <osgDB/FileNameUtils>
46
47 #include <simgear/canvas/CanvasObjectPlacement.hxx>
48 #include <simgear/scene/material/EffectGeode.hxx>
49 #include <simgear/scene/util/RenderConstants.hxx>
50
51 #include <Main/globals.hxx>
52 #include <Scenery/scenery.hxx>
53 #include "od_gauge.hxx"
54
55 #include <cassert>
56
57 //------------------------------------------------------------------------------
58 FGODGauge::FGODGauge()
59 {
60
61 }
62
63 //------------------------------------------------------------------------------
64 FGODGauge::~FGODGauge()
65 {
66
67 }
68
69 /**
70  * Replace a texture in the airplane model with the gauge texture.
71  */
72 class ReplaceStaticTextureVisitor:
73   public osg::NodeVisitor
74 {
75   public:
76
77     typedef osg::ref_ptr<osg::Group> GroupPtr;
78     typedef osg::ref_ptr<osg::Material> MaterialPtr;
79
80     ReplaceStaticTextureVisitor( const char* name,
81                                  osg::Texture2D* new_texture ):
82         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
83         _tex_name( osgDB::getSimpleFileName(name) ),
84         _new_texture(new_texture),
85         _cull_callback(0)
86     {}
87
88     ReplaceStaticTextureVisitor( SGPropertyNode* placement,
89                                  osg::Texture2D* new_texture,
90                                  osg::NodeCallback* cull_callback = 0,
91                                  const simgear::canvas::CanvasWeakPtr& canvas =
92                                    simgear::canvas::CanvasWeakPtr() ):
93         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
94         _tex_name( osgDB::getSimpleFileName(
95           placement->getStringValue("texture"))
96         ),
97         _node_name( placement->getStringValue("node") ),
98         _parent_name( placement->getStringValue("parent") ),
99         _node(placement),
100         _new_texture(new_texture),
101         _cull_callback(cull_callback),
102         _canvas(canvas)
103     {
104       if(    _tex_name.empty()
105           && _node_name.empty()
106           && _parent_name.empty() )
107         SG_LOG
108         (
109           SG_GL,
110           SG_WARN,
111           "No filter criterion for replacing texture. "
112           " Every texture will be replaced!"
113         );
114     }
115
116     /**
117      * Get a list of groups which have been inserted into the scene graph to
118      * replace the given texture
119      */
120     simgear::canvas::Placements& getPlacements()
121     {
122       return _placements;
123     }
124
125     virtual void apply(osg::Geode& node)
126     {
127       simgear::EffectGeode* eg = dynamic_cast<simgear::EffectGeode*>(&node);
128       if( !eg )
129         return;
130
131       osg::StateSet* ss = eg->getEffect()->getDefaultStateSet();
132       if( !ss )
133         return;
134
135       osg::Group *parent = node.getParent(0);
136       if( !_node_name.empty() && getNodeName(*parent) != _node_name )
137         return;
138
139       if( !_parent_name.empty() )
140       {
141         // Traverse nodes upwards starting at the parent node (skip current
142         // node)
143         const osg::NodePath& np = getNodePath();
144         bool found = false;
145         for( int i = static_cast<int>(np.size()) - 2; i >= 0; --i )
146         {
147           const osg::Node* path_segment = np[i];
148           const osg::Node* path_parent = path_segment->getParent(0);
149
150           // A node without a name is always the parent of the root node of
151           // the model just containing the file name
152           if( path_parent && path_parent->getName().empty() )
153             return;
154
155           if( path_segment->getName() == _parent_name )
156           {
157             found = true;
158             break;
159           }
160         }
161
162         if( !found )
163           return;
164       }
165
166       for( size_t unit = 0; unit < ss->getNumTextureAttributeLists(); ++unit )
167       {
168         osg::Texture2D* tex = dynamic_cast<osg::Texture2D*>
169         (
170           ss->getTextureAttribute(unit, osg::StateAttribute::TEXTURE)
171         );
172
173         if( !tex || !tex->getImage() || tex == _new_texture )
174           continue;
175
176         if( !_tex_name.empty() )
177         {
178           std::string tex_name = tex->getImage()->getFileName();
179           std::string tex_name_simple = osgDB::getSimpleFileName(tex_name);
180           if( !osgDB::equalCaseInsensitive(_tex_name, tex_name_simple) )
181             continue;
182         }
183
184         // insert a new group between the geode an it's parent which overrides
185         // the texture
186         GroupPtr group = new osg::Group;
187         group->setName("canvas texture group");
188         group->addChild(eg);
189         parent->removeChild(eg);
190         parent->addChild(group);
191
192         if( _cull_callback )
193           group->setCullCallback(_cull_callback);
194
195         osg::StateSet* stateSet = group->getOrCreateStateSet();
196         stateSet->setTextureAttribute( unit, _new_texture,
197                                              osg::StateAttribute::OVERRIDE );
198         stateSet->setTextureMode( unit, GL_TEXTURE_2D,
199                                         osg::StateAttribute::ON );
200
201         _placements.push_back( simgear::canvas::PlacementPtr(
202           new simgear::canvas::ObjectPlacement(_node, group, _canvas)
203         ));
204
205         SG_LOG
206         (
207           SG_GL,
208           SG_INFO,
209              "Replaced texture '" << _tex_name << "'"
210           << " for object '" << parent->getName() << "'"
211           << (!_parent_name.empty() ? " with parent '" + _parent_name + "'"
212                                     : "")
213         );
214         return;
215       }
216     }
217
218   protected:
219
220     std::string _tex_name,      ///<! Name of texture to be replaced
221                 _node_name,     ///<! Only replace if node name matches
222                 _parent_name;   ///<! Only replace if any parent node matches
223                                 ///   given name (all the tree upwards)
224
225     SGPropertyNode_ptr  _node;
226     osg::Texture2D     *_new_texture;
227     osg::NodeCallback  *_cull_callback;
228
229     simgear::canvas::CanvasWeakPtr  _canvas;
230     simgear::canvas::Placements     _placements;
231
232     const std::string& getNodeName(const osg::Node& node) const
233     {
234       if( !node.getName().empty() )
235         return node.getName();
236
237       // Special handling for pick animation which clears the name of the object
238       // and instead sets the name of a parent group with one or two groups
239       // attached (one for normal rendering and one for the picking highlight).
240       osg::Group const* parent = node.getParent(0);
241       if( parent->getName() == "pick render group" )
242         return parent->getParent(0)->getName();
243
244       return node.getName();
245     }
246 };
247
248 //------------------------------------------------------------------------------
249 simgear::canvas::Placements
250 FGODGauge::set_texture( osg::Node* branch,
251                         const char * name,
252                         osg::Texture2D* new_texture )
253 {
254   ReplaceStaticTextureVisitor visitor(name, new_texture);
255   branch->accept(visitor);
256   return visitor.getPlacements();
257 }
258
259 //------------------------------------------------------------------------------
260 simgear::canvas::Placements
261 FGODGauge::set_aircraft_texture( const char* name,
262                                  osg::Texture2D* new_texture )
263 {
264   return set_texture
265   (
266     globals->get_scenery()->get_aircraft_branch(),
267     name,
268     new_texture
269   );
270 }
271
272 //------------------------------------------------------------------------------
273 simgear::canvas::Placements
274 FGODGauge::set_texture( osg::Node* branch,
275                         SGPropertyNode* placement,
276                         osg::Texture2D* new_texture,
277                         osg::NodeCallback* cull_callback,
278                         const simgear::canvas::CanvasWeakPtr& canvas )
279 {
280   ReplaceStaticTextureVisitor visitor( placement,
281                                        new_texture,
282                                        cull_callback,
283                                        canvas );
284   branch->accept(visitor);
285   return visitor.getPlacements();
286 }
287
288 //------------------------------------------------------------------------------
289 simgear::canvas::Placements
290 FGODGauge::set_aircraft_texture( SGPropertyNode* placement,
291                                  osg::Texture2D* new_texture,
292                                  osg::NodeCallback* cull_callback,
293                                  const simgear::canvas::CanvasWeakPtr& canvas )
294 {
295   return set_texture
296   (
297     globals->get_scenery()->get_aircraft_branch(),
298     placement,
299     new_texture,
300     cull_callback,
301     canvas
302   );
303 }