| 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 | * Used to remember the located groups that require modification |
| 71 | */ |
| 72 | typedef struct { |
| 73 | osg::ref_ptr<osg::Group> parent; |
| 74 | osg::ref_ptr<osg::Geode> node; |
| 75 | unsigned int unit; |
| 76 | } GroupListItem; |
| 77 | |
| 78 | /** |
| 79 | * Replace a texture in the airplane model with the gauge texture. |
| 80 | */ |
| 81 | class ReplaceStaticTextureVisitor: |
| 82 | |
| 83 | public osg::NodeVisitor |
| 84 | { |
| 85 | public: |
| 86 | |
| 87 | typedef osg::ref_ptr<osg::Group> GroupPtr; |
| 88 | typedef osg::ref_ptr<osg::Material> MaterialPtr; |
| 89 | |
| 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), |
| 95 | _cull_callback(0) |
| 96 | {} |
| 97 | |
| 98 | ReplaceStaticTextureVisitor( SGPropertyNode* placement, |
| 99 | osg::Texture2D* new_texture, |
| 100 | osg::NodeCallback* cull_callback = 0, |
| 101 | const simgear::canvas::CanvasWeakPtr& canvas = |
| 102 | simgear::canvas::CanvasWeakPtr() ): |
| 103 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
| 104 | _tex_name( osgDB::getSimpleFileName( |
| 105 | placement->getStringValue("texture")) |
| 106 | ), |
| 107 | _node_name( placement->getStringValue("node") ), |
| 108 | _parent_name( placement->getStringValue("parent") ), |
| 109 | _node(placement), |
| 110 | _new_texture(new_texture), |
| 111 | _cull_callback(cull_callback), |
| 112 | _canvas(canvas) |
| 113 | { |
| 114 | if( _tex_name.empty() |
| 115 | && _node_name.empty() |
| 116 | && _parent_name.empty() ) |
| 117 | SG_LOG |
| 118 | ( |
| 119 | SG_GL, |
| 120 | SG_WARN, |
| 121 | "No filter criterion for replacing texture. " |
| 122 | " Every texture will be replaced!" |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get a list of groups which have been inserted into the scene graph to |
| 128 | * replace the given texture |
| 129 | */ |
| 130 | simgear::canvas::Placements& getPlacements() |
| 131 | { |
| 132 | return _placements; |
| 133 | } |
| 134 | |
| 135 | virtual void apply(osg::Geode& node) |
| 136 | { |
| 137 | simgear::EffectGeode* effectGeode = dynamic_cast<simgear::EffectGeode*>(&node); |
| 138 | if( !effectGeode ) |
| 139 | return; |
| 140 | simgear::Effect* eff = effectGeode->getEffect(); |
| 141 | if (!eff) |
| 142 | return; |
| 143 | |
| 144 | // Assume that the parent node to the EffectGeode contains the object |
| 145 | // name. This is true for AC3D and glTF models. |
| 146 | osg::Group *parent = node.getParent(0); |
| 147 | if( !_node_name.empty() && getNodeName(*parent) != _node_name ) |
| 148 | return; |
| 149 | |
| 150 | if( !_parent_name.empty() ) |
| 151 | { |
| 152 | // Traverse nodes upwards starting at the parent node (skip current |
| 153 | // node) |
| 154 | const osg::NodePath& np = getNodePath(); |
| 155 | bool found = false; |
| 156 | for( int i = static_cast<int>(np.size()) - 2; i >= 0; --i ) |
| 157 | { |
| 158 | const osg::Node* path_segment = np[i]; |
| 159 | const osg::Node* path_parent = path_segment->getParent(0); |
| 160 | |
| 161 | // A node without a name is always the parent of the root node of |
| 162 | // the model just containing the file name |
| 163 | if( path_parent && path_parent->getName().empty() ) |
| 164 | return; |
| 165 | |
| 166 | if( path_segment->getName() == _parent_name ) |
| 167 | { |
| 168 | found = true; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if( !found ) |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (_tex_name.empty()) { |
| 178 | // No texture name was provided, so replace texture unit 0 by default |
| 179 | groups_to_modify.push_back({parent, &node, 0}); |
| 180 | } else { |
| 181 | // A texture name was provided, so attempt to find the corresponding |
| 182 | // texture unit. |
| 183 | // XXX: This only works for AC3D models and Effects with a |
| 184 | // fixed-function pipeline technique. |
| 185 | osg::StateSet* ss = eff->getDefaultStateSet(); |
| 186 | if (!ss) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | for (unsigned int unit = 0; unit < ss->getNumTextureAttributeLists(); ++unit) { |
| 191 | osg::Texture2D* tex = dynamic_cast<osg::Texture2D*>( |
| 192 | ss->getTextureAttribute(unit, osg::StateAttribute::TEXTURE)); |
| 193 | |
| 194 | if (!tex || !tex->getImage() || tex == _new_texture) { |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | std::string tex_name = tex->getImage()->getFileName(); |
| 199 | std::string tex_name_simple = osgDB::getSimpleFileName(tex_name); |
| 200 | if (!osgDB::equalCaseInsensitive(_tex_name, tex_name_simple)) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | groups_to_modify.push_back({ parent, &node, unit }); |
| 205 | return; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | /* |
| 210 | * this section of code used to be in the apply method above, however to work this requires modification of the scenegraph nodes |
| 211 | * that are currently iterating, so instead the apply method will locate the groups to be modified and when finished then the |
| 212 | * nodes can actually be modified safely. Initially found thanks to the debug RTL in MSVC2015 throwing an exception. |
| 213 | * should be called immediately after the visitor to ensure that the groups are still valid and that nothing else has modified these groups. |
| 214 | */ |
| 215 | void modify_groups() |
| 216 | { |
| 217 | for (auto g : groups_to_modify) { |
| 218 | // insert a new group between the geode an it's parent which overrides |
| 219 | // the texture |
| 220 | GroupPtr group = new osg::Group; |
| 221 | group->setName("canvas texture group"); |
| 222 | group->addChild(g.node); |
| 223 | g.parent->removeChild(g.node); |
| 224 | g.parent->addChild(group); |
| 225 | |
| 226 | if (_cull_callback) |
| 227 | group->setCullCallback(_cull_callback); |
| 228 | |
| 229 | group->getOrCreateStateSet()->setTextureAttributeAndModes( |
| 230 | g.unit, |
| 231 | _new_texture, |
| 232 | osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| 233 | |
| 234 | _placements.push_back(simgear::canvas::PlacementPtr( |
| 235 | new simgear::canvas::ObjectPlacement(_node, group, _canvas) |
| 236 | )); |
| 237 | |
| 238 | SG_LOG |
| 239 | ( |
| 240 | SG_GL, |
| 241 | SG_INFO, |
| 242 | "Replaced texture '" << _tex_name << "'" |
| 243 | << " for object '" << g.parent->getName() << "'" |
| 244 | << (!_parent_name.empty() ? " with parent '" + _parent_name + "'" |
| 245 | : "") |
| 246 | ); |
| 247 | } |
| 248 | groups_to_modify.clear(); |
| 249 | } |
| 250 | |
| 251 | protected: |
| 252 | |
| 253 | std::string _tex_name, ///<! Name of texture to be replaced |
| 254 | _node_name, ///<! Only replace if node name matches |
| 255 | _parent_name; ///<! Only replace if any parent node matches |
| 256 | /// given name (all the tree upwards) |
| 257 | |
| 258 | SGPropertyNode_ptr _node; |
| 259 | osg::Texture2D *_new_texture; |
| 260 | osg::NodeCallback *_cull_callback; |
| 261 | typedef std::vector<GroupListItem> GroupList; |
| 262 | GroupList groups_to_modify; |
| 263 | simgear::canvas::CanvasWeakPtr _canvas; |
| 264 | simgear::canvas::Placements _placements; |
| 265 | |
| 266 | const std::string& getNodeName(const osg::Node& node) const |
| 267 | { |
| 268 | if( !node.getName().empty() ) |
| 269 | return node.getName(); |
| 270 | |
| 271 | // Special handling for pick animation which clears the name of the object |
| 272 | // and instead sets the name of a parent group with one or two groups |
| 273 | // attached (one for normal rendering and one for the picking highlight). |
| 274 | osg::Group const* parent = node.getParent(0); |
| 275 | if( parent->getName() == "pick render group" ) |
| 276 | return parent->getParent(0)->getName(); |
| 277 | |
| 278 | return node.getName(); |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | //------------------------------------------------------------------------------ |
| 283 | simgear::canvas::Placements |
| 284 | FGODGauge::set_texture( osg::Node* branch, |
| 285 | const char * name, |
| 286 | osg::Texture2D* new_texture ) |
| 287 | { |
| 288 | ReplaceStaticTextureVisitor visitor(name, new_texture); |
| 289 | branch->accept(visitor); |
| 290 | visitor.modify_groups(); |
| 291 | return visitor.getPlacements(); |
| 292 | } |
| 293 | |
| 294 | //------------------------------------------------------------------------------ |
| 295 | simgear::canvas::Placements |
| 296 | FGODGauge::set_aircraft_texture( const char* name, |
| 297 | osg::Texture2D* new_texture ) |
| 298 | { |
| 299 | return set_texture |
| 300 | ( |
| 301 | globals->get_scenery()->get_aircraft_branch(), |
| 302 | name, |
| 303 | new_texture |
| 304 | ); |
| 305 | } |
| 306 | |
| 307 | //------------------------------------------------------------------------------ |
| 308 | simgear::canvas::Placements |
| 309 | FGODGauge::set_texture( osg::Node* branch, |
| 310 | SGPropertyNode* placement, |
| 311 | osg::Texture2D* new_texture, |
| 312 | osg::NodeCallback* cull_callback, |
| 313 | const simgear::canvas::CanvasWeakPtr& canvas ) |
| 314 | { |
| 315 | ReplaceStaticTextureVisitor visitor( placement, |
| 316 | new_texture, |
| 317 | cull_callback, |
| 318 | canvas ); |
| 319 | branch->accept(visitor); |
| 320 | visitor.modify_groups(); |
| 321 | return visitor.getPlacements(); |
| 322 | } |
| 323 | |
| 324 | //------------------------------------------------------------------------------ |
| 325 | simgear::canvas::Placements |
| 326 | FGODGauge::set_aircraft_texture( SGPropertyNode* placement, |
| 327 | osg::Texture2D* new_texture, |
| 328 | osg::NodeCallback* cull_callback, |
| 329 | const simgear::canvas::CanvasWeakPtr& canvas ) |
| 330 | { |
| 331 | return set_texture |
| 332 | ( |
| 333 | globals->get_scenery()->get_aircraft_branch(), |
| 334 | placement, |
| 335 | new_texture, |
| 336 | cull_callback, |
| 337 | canvas |
| 338 | ); |
| 339 | } |