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 <Canvas/property_helper.hxx>
25 #include <osgText/Text>
29 //------------------------------------------------------------------------------
31 * Callback used to disable/enable rendering to the texture if it is not
34 class CameraCullCallback:
35 public osg::NodeCallback
44 * Enable rendering for the next frame
46 void enableRendering()
55 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
66 * This callback is installed on every placement of the canvas in the scene to
67 * only render the canvas if at least one placement is visible
69 class PlacementCullCallback:
70 public osg::NodeCallback
74 PlacementCullCallback(Canvas* canvas, CameraCullCallback* camera_cull):
76 _camera_cull( camera_cull )
82 CameraCullCallback *_camera_cull;
84 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
86 _camera_cull->enableRendering();
91 //------------------------------------------------------------------------------
98 _sampling_dirty(false),
102 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
104 CameraCullCallback *camera_callback = new CameraCullCallback;
105 _camera_callback = camera_callback;
106 _cull_callback = new PlacementCullCallback(this, camera_callback);
109 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 int Canvas::getStatus() const
124 //------------------------------------------------------------------------------
125 void Canvas::reset(SGPropertyNode* node)
132 "Canvas::reset() texture[" << node->getIndex() << "]"
138 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
145 SGRawValueMethods<Canvas, int>( *this, &Canvas::getSizeX,
151 SGRawValueMethods<Canvas, int>( *this, &Canvas::getSizeY,
157 SGRawValueMethods<Canvas, int>( *this, &Canvas::getViewWidth,
158 &Canvas::setViewWidth )
163 SGRawValueMethods<Canvas, int>( *this, &Canvas::getViewHeight,
164 &Canvas::setViewHeight )
169 SGRawValueMethods<Canvas, int>(*this, &Canvas::getStatus)
174 SGRawValueMethods<Canvas, const char*>(*this, &Canvas::getStatusMsg)
176 _node->addChangeListener(this);
178 canvas::linkColorNodes
188 //------------------------------------------------------------------------------
189 void Canvas::update(double delta_time_sec)
191 if( !_texture.serviceable() )
193 if( _status != STATUS_OK )
196 _texture.setSize(_size_x, _size_y);
197 _texture.useImageCoords(true);
198 _texture.useStencil(true);
199 _texture.allocRT(_camera_callback);
200 _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
202 for( size_t i = 0; i < _groups.size(); ++i )
203 _texture.getCamera()->addChild( _groups[i]->getMatrixTransform() );
205 if( _texture.serviceable() )
207 setStatusFlags(STATUS_OK);
211 setStatusFlags(CREATE_FAILED);
216 for( size_t i = 0; i < _groups.size(); ++i )
217 _groups[i]->update(delta_time_sec);
219 if( _sampling_dirty )
221 _texture.setSampling(
222 _node->getBoolValue("mipmapping"),
223 _node->getIntValue("coverage-samples"),
224 _node->getIntValue("color-samples")
226 _sampling_dirty = false;
230 _texture.getCamera()->setClearColor
232 osg::Vec4( _color_background[0]->getFloatValue(),
233 _color_background[1]->getFloatValue(),
234 _color_background[2]->getFloatValue(),
235 _color_background[3]->getFloatValue() )
237 _color_dirty = false;
240 while( !_dirty_placements.empty() )
242 SGPropertyNode *node = _dirty_placements.back();
243 _dirty_placements.pop_back();
245 if( node->getIndex() >= static_cast<int>(_placements.size()) )
247 _placements.resize(node->getIndex() + 1);
249 // Remove maybe already existing placements
250 clearPlacements(node->getIndex());
252 // add new placements
253 _placements[node->getIndex()] = _texture.set_texture(
255 _texture.getTexture(),
261 //------------------------------------------------------------------------------
262 void Canvas::setSizeX(int sx)
268 // TODO resize if texture already allocated
271 setStatusFlags(MISSING_SIZE_X);
273 setStatusFlags(MISSING_SIZE_X, false);
275 // reset flag to allow creation with new size
276 setStatusFlags(CREATE_FAILED, false);
279 //------------------------------------------------------------------------------
280 int Canvas::getSizeX() const
285 //------------------------------------------------------------------------------
286 void Canvas::setSizeY(int sy)
292 // TODO resize if texture already allocated
295 setStatusFlags(MISSING_SIZE_Y);
297 setStatusFlags(MISSING_SIZE_Y, false);
299 // reset flag to allow creation with new size
300 setStatusFlags(CREATE_FAILED, false);
303 //------------------------------------------------------------------------------
304 int Canvas::getSizeY() const
309 //------------------------------------------------------------------------------
310 void Canvas::setViewWidth(int w)
312 if( _view_width == w )
316 _texture.setViewSize(_view_width, _view_height);
319 //------------------------------------------------------------------------------
320 int Canvas::getViewWidth() const
325 //------------------------------------------------------------------------------
326 void Canvas::setViewHeight(int h)
328 if( _view_height == h )
332 _texture.setViewSize(_view_width, _view_height);
335 //------------------------------------------------------------------------------
336 int Canvas::getViewHeight() const
341 //------------------------------------------------------------------------------
342 const char* Canvas::getStatusMsg() const
344 return _status_msg.c_str();
347 //------------------------------------------------------------------------------
348 void Canvas::childAdded( SGPropertyNode * parent,
349 SGPropertyNode * child )
351 if( parent != _node )
354 if( child->getNameString() == "group" )
358 boost::shared_ptr<canvas::Group>(new canvas::Group(child))
360 if( _texture.serviceable() )
361 _texture.getCamera()->addChild( _groups.back()->getMatrixTransform() );
363 else if( child->getNameString() == "placement" )
365 _dirty_placements.push_back(child);
369 //------------------------------------------------------------------------------
370 void Canvas::childRemoved( SGPropertyNode * parent,
371 SGPropertyNode * child )
373 if( parent != _node )
376 if( child->getNameString() == "placement" )
377 clearPlacements(child->getIndex());
380 //----------------------------------------------------------------------------
381 void Canvas::valueChanged(SGPropertyNode * node)
383 if( node->getParent()->getParent() == _node )
385 if( !_color_background.empty()
386 && _color_background[0]->getParent() == node->getParent() )
390 else if( node->getParent()->getNameString() == "placement" )
392 // prevent double updates...
393 for( size_t i = 0; i < _dirty_placements.size(); ++i )
395 if( node->getParent() == _dirty_placements[i] )
399 _dirty_placements.push_back(node->getParent());
402 else if( node->getParent() == _node )
404 if( node->getNameString() == "mipmapping"
405 || node->getNameString() == "coverage-samples"
406 || node->getNameString() == "color-samples" )
407 _sampling_dirty = true;
411 //------------------------------------------------------------------------------
412 void Canvas::setStatusFlags(unsigned int flags, bool set)
419 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
420 _status_msg = "Missing size";
421 else if( _status & MISSING_SIZE_X )
422 _status_msg = "Missing size-x";
423 else if( _status & MISSING_SIZE_Y )
424 _status_msg = "Missing size-y";
425 else if( _status & CREATE_FAILED )
426 _status_msg = "Creating render target failed";
427 else if( _status == STATUS_OK && !_texture.serviceable() )
428 _status_msg = "Creation pending...";
433 //------------------------------------------------------------------------------
434 void Canvas::clearPlacements(int index)
436 Placements& placements = _placements.at(index);
437 while( !placements.empty() )
439 osg::ref_ptr<osg::Group> group = placements.back();
440 placements.pop_back();
442 assert( group->getNumParents() == 1 );
443 assert( group->getNumChildren() == 1 );
445 osg::Group *parent = group->getParent(0);
446 osg::Node *child = group->getChild(0);
448 parent->addChild(child);
449 group->removeChild(child);
450 parent->removeChild(group);
454 //------------------------------------------------------------------------------
455 void Canvas::clearPlacements()
457 for(size_t i = 0; i < _placements.size(); ++i)
462 //------------------------------------------------------------------------------
463 void Canvas::unbind()
468 _node->untie("size[0]");
469 _node->untie("size[1]");
470 _node->untie("view[0]");
471 _node->untie("view[1]");
472 _node->untie("status");
473 _node->untie("status-msg");
474 _node->removeChangeListener(this);