1 // Owner Drawn Gauge helper class
3 // Written by Harald JOHNSEN, started May 2005.
5 // Copyright (C) 2005 Harald JOHNSEN
7 // Ported to OSG by Tim Moore - Jun 2007
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
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.
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.
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.
33 #include <osg/Texture2D>
34 #include <osg/AlphaFunc>
35 #include <osg/BlendFunc>
38 #include <osg/NodeVisitor>
40 #include <osg/PolygonMode>
41 #include <osg/ShadeModel>
42 #include <osg/StateSet>
43 #include <osg/FrameBufferObject> // for GL_DEPTH_STENCIL_EXT on Windows
45 #include <osgDB/FileNameUtils>
47 #include <simgear/scene/material/EffectGeode.hxx>
48 #include <simgear/scene/util/RenderConstants.hxx>
50 #include <Main/globals.hxx>
51 #include <Viewer/renderer.hxx>
52 #include <Scenery/scenery.hxx>
53 #include "od_gauge.hxx"
57 //------------------------------------------------------------------------------
58 static void cbAddCamera(osg::Camera* cam)
60 globals->get_renderer()->addCamera(cam, false);
63 //------------------------------------------------------------------------------
64 static void cbRemoveCamera(osg::Camera* cam)
66 globals->get_renderer()->removeCamera(cam);
69 //------------------------------------------------------------------------------
70 FGODGauge::FGODGauge():
71 simgear::ODGauge(cbAddCamera, cbRemoveCamera)
76 //------------------------------------------------------------------------------
77 FGODGauge::~FGODGauge()
83 * Replace a texture in the airplane model with the gauge texture.
85 class ReplaceStaticTextureVisitor:
86 public osg::NodeVisitor
90 ReplaceStaticTextureVisitor( const char* name,
91 osg::Texture2D* new_texture ):
92 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
93 _tex_name( osgDB::getSimpleFileName(name) ),
94 _new_texture(new_texture)
97 ReplaceStaticTextureVisitor( const SGPropertyNode* placement,
98 osg::Texture2D* new_texture,
99 osg::NodeCallback* cull_callback = 0 ):
100 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
101 _tex_name( osgDB::getSimpleFileName(
102 placement->getStringValue("texture"))
104 _node_name( placement->getStringValue("node") ),
105 _parent_name( placement->getStringValue("parent") ),
106 _new_texture(new_texture),
107 _cull_callback(cull_callback)
109 if( _tex_name.empty()
110 && _node_name.empty()
111 && _parent_name.empty() )
116 "No filter criterion for replacing texture. "
117 " Every texture will be replaced!"
122 * Get a list of groups which have been inserted into the scene graph to
123 * replace the given texture
125 canvas::Placements& getPlacements()
130 virtual void apply(osg::Geode& node)
132 simgear::EffectGeode* eg = dynamic_cast<simgear::EffectGeode*>(&node);
136 osg::StateSet* ss = eg->getEffect()->getDefaultStateSet();
140 osg::Group *parent = node.getParent(0);
141 if( !_node_name.empty() && parent->getName() != _node_name )
144 if( !_parent_name.empty() )
146 // Traverse nodes upwards starting at the parent node (skip current
148 const osg::NodePath& np = getNodePath();
150 for( int i = static_cast<int>(np.size()) - 2; i >= 0; --i )
152 const osg::Node* path_segment = np[i];
153 const osg::Node* path_parent = path_segment->getParent(0);
155 // A node without a name is always the parent of the root node of
156 // the model just containing the file name
157 if( path_parent && path_parent->getName().empty() )
160 if( path_segment->getName() == _parent_name )
171 for( size_t unit = 0; unit < ss->getNumTextureAttributeLists(); ++unit )
173 osg::Texture2D* tex = dynamic_cast<osg::Texture2D*>
175 ss->getTextureAttribute(unit, osg::StateAttribute::TEXTURE)
178 if( !tex || !tex->getImage() || tex == _new_texture )
181 if( !_tex_name.empty() )
183 std::string tex_name = tex->getImage()->getFileName();
184 std::string tex_name_simple = osgDB::getSimpleFileName(tex_name);
185 if( !osgDB::equalCaseInsensitive(_tex_name, tex_name_simple) )
189 // insert a new group between the geode an it's parent which overrides
191 osg::ref_ptr<osg::Group> group = new osg::Group;
192 group->setName("canvas texture group");
194 parent->removeChild(eg);
195 parent->addChild(group);
198 group->setCullCallback(_cull_callback);
200 _placements.push_back(
201 canvas::PlacementPtr(new ObjectPlacement(group))
204 osg::StateSet* stateSet = group->getOrCreateStateSet();
205 stateSet->setTextureAttribute( unit, _new_texture,
206 osg::StateAttribute::OVERRIDE );
207 stateSet->setTextureMode( unit, GL_TEXTURE_2D,
208 osg::StateAttribute::ON );
214 "Replaced texture '" << _tex_name << "'"
215 << " for object '" << parent->getName() << "'"
216 << (!_parent_name.empty() ? " with parent '" + _parent_name + "'"
225 class ObjectPlacement:
226 public canvas::Placement
229 ObjectPlacement(osg::ref_ptr<osg::Group> group):
234 * Remove placement from the scene
236 virtual ~ObjectPlacement()
238 assert( _group->getNumChildren() == 1 );
239 osg::Node *child = _group->getChild(0);
241 if( _group->getNumParents() )
243 osg::Group *parent = _group->getParent(0);
244 parent->addChild(child);
245 parent->removeChild(_group);
248 _group->removeChild(child);
252 osg::ref_ptr<osg::Group> _group;
255 std::string _tex_name, ///<! Name of texture to be replaced
256 _node_name, ///<! Only replace if node name matches
257 _parent_name; ///<! Only replace if any parent node matches
258 /// given name (all the tree upwards)
259 osg::Texture2D *_new_texture;
260 osg::NodeCallback *_cull_callback;
262 canvas::Placements _placements;
265 //------------------------------------------------------------------------------
266 canvas::Placements FGODGauge::set_texture( const char* name,
267 osg::Texture2D* new_texture )
269 osg::Group* root = globals->get_scenery()->get_aircraft_branch();
270 ReplaceStaticTextureVisitor visitor(name, new_texture);
271 root->accept(visitor);
272 return visitor.getPlacements();
275 //------------------------------------------------------------------------------
276 canvas::Placements FGODGauge::set_texture( const SGPropertyNode* placement,
277 osg::Texture2D* new_texture,
278 osg::NodeCallback* cull_callback )
280 osg::Group* root = globals->get_scenery()->get_aircraft_branch();
281 ReplaceStaticTextureVisitor visitor(placement, new_texture, cull_callback);
282 root->accept(visitor);
283 return visitor.getPlacements();