1 // Canvas with 2D rendering api
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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.
18 
19 #include "canvas_mgr.hxx"
20 
21 #include <boost/bind.hpp>
22 
23 #include <Cockpit/od_gauge.hxx>
24 #include <Main/fg_props.hxx>
25 #include <Scripting/NasalModelData.hxx>
26 #include <Viewer/CameraGroup.hxx>
27 
28 #include <simgear/canvas/Canvas.hxx>
29 
30 namespace sc = simgear::canvas;
31 
32 //------------------------------------------------------------------------------
addSceneObjectPlacement(SGPropertyNode * placement,sc::CanvasPtr canvas)33 static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
34                                                sc::CanvasPtr canvas )
35 {
36   int module_id = placement->getIntValue("module-id", -1);
37   if( module_id < 0 )
38     return sc::Placements();
39 
40   FGNasalModelData* model_data =
41     FGNasalModelData::getByModuleId( static_cast<unsigned int>(module_id) );
42 
43   if( !model_data )
44     return sc::Placements();
45 
46   if( !model_data->getNode() )
47     return sc::Placements();
48 
49   return FGODGauge::set_texture
50   (
51     model_data->getNode(),
52     placement,
53     canvas->getTexture(),
54     canvas->getCullCallback(),
55     canvas
56   );
57 }
58 
59 //------------------------------------------------------------------------------
CanvasMgr()60 CanvasMgr::CanvasMgr():
61   simgear::canvas::CanvasMgr( fgGetNode("/canvas/by-index", true) ),
62   _cb_model_reinit
63   (
64     this,
65     &CanvasMgr::handleModelReinit,
66     fgGetNode("/sim/signals/model-reinit", true)
67   )
68 {
69 
70 }
71 
72 //----------------------------------------------------------------------------
init()73 void CanvasMgr::init()
74 {
75   _gui_camera = flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
76     if (_gui_camera.valid()) {
77         // add our two placement factories
78         sc::Canvas::addPlacementFactory
79         (
80           "object",
81           boost::bind
82           (
83             &FGODGauge::set_aircraft_texture,
84             _1,
85             boost::bind(&sc::Canvas::getTexture, _2),
86             boost::bind(&sc::Canvas::getCullCallback, _2),
87             _2
88           )
89         );
90 
91         sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
92     }
93 
94   simgear::canvas::CanvasMgr::init();
95 }
96 
97 //----------------------------------------------------------------------------
shutdown()98 void CanvasMgr::shutdown()
99 {
100   simgear::canvas::CanvasMgr::shutdown();
101 
102   sc::Canvas::removePlacementFactory("object");
103   sc::Canvas::removePlacementFactory("scenery-object");
104 
105     _gui_camera = 0;
106 }
107 
108 //------------------------------------------------------------------------------
109 unsigned int
getCanvasTexId(const simgear::canvas::CanvasPtr & canvas) const110 CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
111 {
112   if( !canvas )
113     return 0;
114 
115   osg::Texture2D* tex = canvas->getTexture();
116   if( !tex )
117     return 0;
118 
119 //  osgViewer::Viewer::Contexts contexts;
120 //  globals->get_renderer()->getViewer()->getContexts(contexts);
121 //
122 //  if( contexts.empty() )
123 //    return 0;
124 
125   osg::ref_ptr<osg::Camera> guiCamera;
126   if( !_gui_camera.lock(guiCamera) )
127     return 0;
128 
129   osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
130   if( !state )
131     return 0;
132 
133   osg::Texture::TextureObject* tobj =
134     tex->getTextureObject( state->getContextID() );
135   if( !tobj )
136     return 0;
137 
138   return tobj->_id;
139 }
140 
141 //----------------------------------------------------------------------------
handleModelReinit(SGPropertyNode *)142 void CanvasMgr::handleModelReinit(SGPropertyNode*)
143 {
144     for (size_t i = 0; i < _elements.size(); ++i)
145     {
146         sc::Canvas* element = static_cast<sc::Canvas*>(_elements[i].get());
147         if (element)
148             element->reloadPlacements("object");
149     }
150 }
151 
152 
153 // Register the subsystem.
154 SGSubsystemMgr::Registrant<CanvasMgr> registrantCanvasMgr(
155     SGSubsystemMgr::DISPLAY);
156