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"
21 #include "mouse_event.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 )
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)
106 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
108 _camera_callback = new CameraCullCallback;
109 _cull_callback = new CullCallback(_camera_callback);
111 canvas::linkColorNodes
120 //------------------------------------------------------------------------------
126 //------------------------------------------------------------------------------
127 void Canvas::update(double delta_time_sec)
129 if( !_texture.serviceable() )
131 if( _status != STATUS_OK )
134 _texture.setSize(_size_x, _size_y);
135 _texture.useImageCoords(true);
136 _texture.useStencil(true);
137 _texture.allocRT(_camera_callback);
138 _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
139 _texture.getCamera()->addChild(_root_group->getMatrixTransform());
141 if( _texture.serviceable() )
143 setStatusFlags(STATUS_OK);
147 setStatusFlags(CREATE_FAILED);
152 _root_group->update(delta_time_sec);
154 if( _sampling_dirty )
156 _texture.setSampling(
157 _node->getBoolValue("mipmapping"),
158 _node->getIntValue("coverage-samples"),
159 _node->getIntValue("color-samples")
161 _sampling_dirty = false;
165 _texture.getCamera()->setClearColor
167 osg::Vec4( _color_background[0]->getFloatValue(),
168 _color_background[1]->getFloatValue(),
169 _color_background[2]->getFloatValue(),
170 _color_background[3]->getFloatValue() )
172 _color_dirty = false;
175 while( !_dirty_placements.empty() )
177 SGPropertyNode *node = _dirty_placements.back();
178 _dirty_placements.pop_back();
180 if( node->getIndex() >= static_cast<int>(_placements.size()) )
182 _placements.resize(node->getIndex() + 1);
184 // Remove possibly existing placements
185 _placements[ node->getIndex() ].clear();
187 // Get new placements
188 PlacementFactoryMap::const_iterator placement_factory =
189 _placement_factories.find( node->getStringValue("type", "object") );
190 if( placement_factory != _placement_factories.end() )
192 canvas::Placements& placements =
193 _placements[ node->getIndex() ] =
194 placement_factory->second
197 boost::shared_static_cast<Canvas>(_self.lock())
202 placements.empty() ? "No match" : "Ok"
206 node->setStringValue("status-msg", "Unknown placement type");
210 _camera_callback->enableRendering();
213 //------------------------------------------------------------------------------
214 void Canvas::setSizeX(int sx)
220 // TODO resize if texture already allocated
223 setStatusFlags(MISSING_SIZE_X);
225 setStatusFlags(MISSING_SIZE_X, false);
227 // reset flag to allow creation with new size
228 setStatusFlags(CREATE_FAILED, false);
231 //------------------------------------------------------------------------------
232 void Canvas::setSizeY(int sy)
238 // TODO resize if texture already allocated
241 setStatusFlags(MISSING_SIZE_Y);
243 setStatusFlags(MISSING_SIZE_Y, false);
245 // reset flag to allow creation with new size
246 setStatusFlags(CREATE_FAILED, false);
249 //------------------------------------------------------------------------------
250 void Canvas::setViewWidth(int w)
252 if( _view_width == w )
256 _texture.setViewSize(_view_width, _view_height);
259 //------------------------------------------------------------------------------
260 void Canvas::setViewHeight(int h)
262 if( _view_height == h )
266 _texture.setViewSize(_view_width, _view_height);
269 //------------------------------------------------------------------------------
270 bool Canvas::handleMouseEvent(const canvas::MouseEvent& event)
274 _mouse_dx = event.dx;
275 _mouse_dy = event.dy;
276 _mouse_button = event.button;
277 _mouse_state = event.state;
278 _mouse_mod = event.mod;
279 _mouse_scroll = event.scroll;
280 // Always set event type last because all listeners are attached to it
281 _mouse_event = event.type;
285 //------------------------------------------------------------------------------
286 void Canvas::childAdded( SGPropertyNode * parent,
287 SGPropertyNode * child )
289 if( parent != _node )
292 if( child->getNameString() == "placement" )
293 _dirty_placements.push_back(child);
295 static_cast<canvas::Element*>(_root_group.get())
296 ->childAdded(parent, child);
299 //------------------------------------------------------------------------------
300 void Canvas::childRemoved( SGPropertyNode * parent,
301 SGPropertyNode * child )
303 if( parent != _node )
306 if( child->getNameString() == "placement" )
307 _placements[ child->getIndex() ].clear();
309 static_cast<canvas::Element*>(_root_group.get())
310 ->childRemoved(parent, child);
313 //----------------------------------------------------------------------------
314 void Canvas::valueChanged(SGPropertyNode* node)
316 if( boost::starts_with(node->getNameString(), "status") )
319 if( node->getParent()->getParent() == _node )
321 if( !_color_background.empty()
322 && _color_background[0]->getParent() == node->getParent() )
326 else if( node->getParent()->getNameString() == "placement" )
328 // prevent double updates...
329 for( size_t i = 0; i < _dirty_placements.size(); ++i )
331 if( node->getParent() == _dirty_placements[i] )
335 _dirty_placements.push_back(node->getParent());
338 else if( node->getParent() == _node )
340 if( node->getNameString() == "mipmapping"
341 || node->getNameString() == "coverage-samples"
342 || node->getNameString() == "color-samples" )
343 _sampling_dirty = true;
344 else if( node->getNameString() == "render-always" )
345 _render_always = node->getBoolValue();
346 else if( node->getNameString() == "size" )
348 if( node->getIndex() == 0 )
349 setSizeX( node->getIntValue() );
350 else if( node->getIndex() == 1 )
351 setSizeY( node->getIntValue() );
353 else if( node->getNameString() == "view" )
355 if( node->getIndex() == 0 )
356 setViewWidth( node->getIntValue() );
357 else if( node->getIndex() == 1 )
358 setViewHeight( node->getIntValue() );
362 _root_group->valueChanged(node);
365 //------------------------------------------------------------------------------
366 osg::Texture2D* Canvas::getTexture() const
368 return _texture.getTexture();
371 //------------------------------------------------------------------------------
372 GLuint Canvas::getTexId() const
374 osg::Texture2D* tex = _texture.getTexture();
378 // osgViewer::Viewer::Contexts contexts;
379 // globals->get_renderer()->getViewer()->getContexts(contexts);
381 // if( contexts.empty() )
384 osg::Camera* guiCamera =
385 flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
387 osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
391 osg::Texture::TextureObject* tobj =
392 tex->getTextureObject( state->getContextID() );
399 //------------------------------------------------------------------------------
400 Canvas::CameraCullCallbackPtr Canvas::getCameraCullCallback() const
402 return _camera_callback;
405 //----------------------------------------------------------------------------
406 Canvas::CullCallbackPtr Canvas::getCullCallback() const
408 return _cull_callback;
411 //------------------------------------------------------------------------------
412 void Canvas::addPlacementFactory( const std::string& type,
413 canvas::PlacementFactory factory )
415 if( _placement_factories.find(type) != _placement_factories.end() )
420 "Canvas::addPlacementFactory: replace existing factor for type " << type
423 _placement_factories[type] = factory;
426 //------------------------------------------------------------------------------
427 void Canvas::setStatusFlags(unsigned int flags, bool set)
430 _status = _status | flags;
432 _status = _status & ~flags;
433 // TODO maybe extend simgear::PropertyObject to allow |=, &= etc.
435 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
436 _status_msg = "Missing size";
437 else if( _status & MISSING_SIZE_X )
438 _status_msg = "Missing size-x";
439 else if( _status & MISSING_SIZE_Y )
440 _status_msg = "Missing size-y";
441 else if( _status & CREATE_FAILED )
442 _status_msg = "Creating render target failed";
443 else if( _status == STATUS_OK && !_texture.serviceable() )
444 _status_msg = "Creation pending...";
449 //------------------------------------------------------------------------------
450 Canvas::PlacementFactoryMap Canvas::_placement_factories;