1 // Canvas with 2D rendering api
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "canvas_mgr.hxx"
21 #include <Cockpit/od_gauge.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Scripting/NasalModelData.hxx>
24 #include <Viewer/CameraGroup.hxx>
26 #include <simgear/canvas/Canvas.hxx>
28 namespace sc = simgear::canvas;
30 //------------------------------------------------------------------------------
31 static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
32 sc::CanvasPtr canvas )
34 int module_id = placement->getIntValue("module-id", -1);
36 return sc::Placements();
38 FGNasalModelData* model_data =
39 FGNasalModelData::getByModuleId( static_cast<unsigned int>(module_id) );
42 return sc::Placements();
44 if( !model_data->getNode() )
45 return sc::Placements();
47 return FGODGauge::set_texture
49 model_data->getNode(),
52 canvas->getCullCallback(),
57 //------------------------------------------------------------------------------
58 CanvasMgr::CanvasMgr():
59 simgear::canvas::CanvasMgr( fgGetNode("/canvas/by-index", true) ),
63 &CanvasMgr::handleModelReinit,
64 fgGetNode("/sim/signals/model-reinit", true)
70 //----------------------------------------------------------------------------
71 void CanvasMgr::init()
73 _gui_camera = flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
74 assert(_gui_camera.valid());
76 sc::Canvas::addPlacementFactory
81 &FGODGauge::set_aircraft_texture,
83 boost::bind(&sc::Canvas::getTexture, _2),
84 boost::bind(&sc::Canvas::getCullCallback, _2),
88 sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
90 simgear::canvas::CanvasMgr::init();
93 //----------------------------------------------------------------------------
94 void CanvasMgr::shutdown()
96 simgear::canvas::CanvasMgr::shutdown();
98 sc::Canvas::removePlacementFactory("object");
99 sc::Canvas::removePlacementFactory("scenery-object");
104 //------------------------------------------------------------------------------
106 CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
111 osg::Texture2D* tex = canvas->getTexture();
115 // osgViewer::Viewer::Contexts contexts;
116 // globals->get_renderer()->getViewer()->getContexts(contexts);
118 // if( contexts.empty() )
121 osg::ref_ptr<osg::Camera> guiCamera;
122 if( !_gui_camera.lock(guiCamera) )
125 osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
129 osg::Texture::TextureObject* tobj =
130 tex->getTextureObject( state->getContextID() );
137 //----------------------------------------------------------------------------
138 void CanvasMgr::handleModelReinit(SGPropertyNode*)
140 for(size_t i = 0; i < _elements.size(); ++i)
141 static_cast<sc::Canvas*>(_elements[i].get())->reloadPlacements("object");