1 // The canvas for rendering with the 2d 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.
20 #include "elements/group.hxx"
22 #include <Canvas/MouseEvent.hxx>
23 #include <Canvas/property_helper.hxx>
24 #include <Main/globals.hxx>
25 #include <Viewer/CameraGroup.hxx>
26 #include <Viewer/renderer.hxx>
28 #include <simgear/scene/util/RenderConstants.hxx>
32 #include <osgText/Text>
33 #include <osgViewer/Viewer>
35 #include <boost/algorithm/string/predicate.hpp>
38 //----------------------------------------------------------------------------
39 Canvas::CameraCullCallback::CameraCullCallback():
46 //----------------------------------------------------------------------------
47 void Canvas::CameraCullCallback::enableRendering()
52 //----------------------------------------------------------------------------
53 void Canvas::CameraCullCallback::operator()( osg::Node* node,
54 osg::NodeVisitor* nv )
56 if( !_render && nv->getTraversalNumber() != _render_frame )
62 _render_frame = nv->getTraversalNumber();
65 //----------------------------------------------------------------------------
66 Canvas::CullCallback::CullCallback(CameraCullCallback* camera_cull):
67 _camera_cull( camera_cull )
72 //----------------------------------------------------------------------------
73 void Canvas::CullCallback::operator()( osg::Node* node,
74 osg::NodeVisitor* nv )
76 if( (nv->getTraversalMask() & simgear::MODEL_BIT) && _camera_cull.valid() )
77 _camera_cull->enableRendering();
82 //------------------------------------------------------------------------------
83 Canvas::Canvas(SGPropertyNode* node):
84 PropertyBasedElement(node),
89 _status(node, "status"),
90 _status_msg(node, "status-msg"),
91 _mouse_x(node, "mouse/x"),
92 _mouse_y(node, "mouse/y"),
93 _mouse_dx(node, "mouse/dx"),
94 _mouse_dy(node, "mouse/dy"),
95 _mouse_button(node, "mouse/button"),
96 _mouse_state(node, "mouse/state"),
97 _mouse_mod(node, "mouse/mod"),
98 _mouse_scroll(node, "mouse/scroll"),
99 _mouse_event(node, "mouse/event"),
100 _sampling_dirty(false),
102 _root_group( new canvas::Group(node) ),
103 _render_always(false)
105 // Remove automatically created property listener as we forward them on our
107 _root_group->removeListener();
110 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
112 _camera_callback = new CameraCullCallback;
113 _cull_callback = new CullCallback(_camera_callback);
115 canvas::linkColorNodes
124 //------------------------------------------------------------------------------
130 //------------------------------------------------------------------------------
131 void Canvas::update(double delta_time_sec)
133 if( !_texture.serviceable() )
135 if( _status != STATUS_OK )
138 _texture.setSize(_size_x, _size_y);
139 _texture.useImageCoords(true);
140 _texture.useStencil(true);
141 _texture.allocRT(_camera_callback);
142 _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
143 _texture.getCamera()->addChild(_root_group->getMatrixTransform());
145 if( _texture.serviceable() )
147 setStatusFlags(STATUS_OK);
151 setStatusFlags(CREATE_FAILED);
156 _root_group->update(delta_time_sec);
158 if( _sampling_dirty )
160 _texture.setSampling(
161 _node->getBoolValue("mipmapping"),
162 _node->getIntValue("coverage-samples"),
163 _node->getIntValue("color-samples")
165 _sampling_dirty = false;
169 _texture.getCamera()->setClearColor
171 osg::Vec4( _color_background[0]->getFloatValue(),
172 _color_background[1]->getFloatValue(),
173 _color_background[2]->getFloatValue(),
174 _color_background[3]->getFloatValue() )
176 _color_dirty = false;
179 while( !_dirty_placements.empty() )
181 SGPropertyNode *node = _dirty_placements.back();
182 _dirty_placements.pop_back();
184 if( node->getIndex() >= static_cast<int>(_placements.size()) )
186 _placements.resize(node->getIndex() + 1);
188 // Remove possibly existing placements
189 _placements[ node->getIndex() ].clear();
191 // Get new placements
192 PlacementFactoryMap::const_iterator placement_factory =
193 _placement_factories.find( node->getStringValue("type", "object") );
194 if( placement_factory != _placement_factories.end() )
196 canvas::Placements& placements =
197 _placements[ node->getIndex() ] =
198 placement_factory->second
201 boost::static_pointer_cast<Canvas>(_self.lock())
206 placements.empty() ? "No match" : "Ok"
210 node->setStringValue("status-msg", "Unknown placement type");
214 _camera_callback->enableRendering();
217 //------------------------------------------------------------------------------
218 void Canvas::setSizeX(int sx)
224 // TODO resize if texture already allocated
227 setStatusFlags(MISSING_SIZE_X);
229 setStatusFlags(MISSING_SIZE_X, false);
231 // reset flag to allow creation with new size
232 setStatusFlags(CREATE_FAILED, false);
235 //------------------------------------------------------------------------------
236 void Canvas::setSizeY(int sy)
242 // TODO resize if texture already allocated
245 setStatusFlags(MISSING_SIZE_Y);
247 setStatusFlags(MISSING_SIZE_Y, false);
249 // reset flag to allow creation with new size
250 setStatusFlags(CREATE_FAILED, false);
253 //------------------------------------------------------------------------------
254 void Canvas::setViewWidth(int w)
256 if( _view_width == w )
260 _texture.setViewSize(_view_width, _view_height);
263 //------------------------------------------------------------------------------
264 void Canvas::setViewHeight(int h)
266 if( _view_height == h )
270 _texture.setViewSize(_view_width, _view_height);
273 //------------------------------------------------------------------------------
274 bool Canvas::handleMouseEvent(const canvas::MouseEvent& event)
278 _mouse_dx = event.dx;
279 _mouse_dy = event.dy;
280 _mouse_button = event.button;
281 _mouse_state = event.state;
282 _mouse_mod = event.mod;
283 _mouse_scroll = event.scroll;
284 // Always set event type last because all listeners are attached to it
285 _mouse_event = event.type;
287 return _root_group->handleMouseEvent(event);
290 //------------------------------------------------------------------------------
291 void Canvas::childAdded( SGPropertyNode * parent,
292 SGPropertyNode * child )
294 if( parent != _node )
297 if( child->getNameString() == "placement" )
298 _dirty_placements.push_back(child);
300 static_cast<canvas::Element*>(_root_group.get())
301 ->childAdded(parent, child);
304 //------------------------------------------------------------------------------
305 void Canvas::childRemoved( SGPropertyNode * parent,
306 SGPropertyNode * child )
308 if( parent != _node )
311 if( child->getNameString() == "placement" )
312 _placements[ child->getIndex() ].clear();
314 static_cast<canvas::Element*>(_root_group.get())
315 ->childRemoved(parent, child);
318 //----------------------------------------------------------------------------
319 void Canvas::valueChanged(SGPropertyNode* node)
321 if( boost::starts_with(node->getNameString(), "status") )
325 if( node->getParent()->getParent() == _node )
327 if( !_color_background.empty()
328 && _color_background[0]->getParent() == node->getParent() )
332 else if( node->getParent()->getNameString() == "placement" )
334 // prevent double updates...
335 for( size_t i = 0; i < _dirty_placements.size(); ++i )
337 if( node->getParent() == _dirty_placements[i] )
341 _dirty_placements.push_back(node->getParent());
346 else if( node->getParent() == _node )
348 if( node->getNameString() == "mipmapping"
349 || node->getNameString() == "coverage-samples"
350 || node->getNameString() == "color-samples" )
351 _sampling_dirty = true;
352 else if( node->getNameString() == "render-always" )
353 _render_always = node->getBoolValue();
354 else if( node->getNameString() == "size" )
356 if( node->getIndex() == 0 )
357 setSizeX( node->getIntValue() );
358 else if( node->getIndex() == 1 )
359 setSizeY( node->getIntValue() );
361 else if( node->getNameString() == "view" )
363 if( node->getIndex() == 0 )
364 setViewWidth( node->getIntValue() );
365 else if( node->getIndex() == 1 )
366 setViewHeight( node->getIntValue() );
375 _root_group->valueChanged(node);
378 //------------------------------------------------------------------------------
379 osg::Texture2D* Canvas::getTexture() const
381 return _texture.getTexture();
384 //------------------------------------------------------------------------------
385 GLuint Canvas::getTexId() const
387 osg::Texture2D* tex = _texture.getTexture();
391 // osgViewer::Viewer::Contexts contexts;
392 // globals->get_renderer()->getViewer()->getContexts(contexts);
394 // if( contexts.empty() )
397 osg::Camera* guiCamera =
398 flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
400 osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
404 osg::Texture::TextureObject* tobj =
405 tex->getTextureObject( state->getContextID() );
412 //------------------------------------------------------------------------------
413 Canvas::CameraCullCallbackPtr Canvas::getCameraCullCallback() const
415 return _camera_callback;
418 //----------------------------------------------------------------------------
419 Canvas::CullCallbackPtr Canvas::getCullCallback() const
421 return _cull_callback;
424 //------------------------------------------------------------------------------
425 void Canvas::addPlacementFactory( const std::string& type,
426 canvas::PlacementFactory factory )
428 if( _placement_factories.find(type) != _placement_factories.end() )
433 "Canvas::addPlacementFactory: replace existing factor for type " << type
436 _placement_factories[type] = factory;
439 //------------------------------------------------------------------------------
440 void Canvas::setStatusFlags(unsigned int flags, bool set)
443 _status = _status | flags;
445 _status = _status & ~flags;
446 // TODO maybe extend simgear::PropertyObject to allow |=, &= etc.
448 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
449 _status_msg = "Missing size";
450 else if( _status & MISSING_SIZE_X )
451 _status_msg = "Missing size-x";
452 else if( _status & MISSING_SIZE_Y )
453 _status_msg = "Missing size-y";
454 else if( _status & CREATE_FAILED )
455 _status_msg = "Creating render target failed";
456 else if( _status == STATUS_OK && !_texture.serviceable() )
457 _status_msg = "Creation pending...";
462 //------------------------------------------------------------------------------
463 Canvas::PlacementFactoryMap Canvas::_placement_factories;