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);
142 _root_group.reset( new canvas::Group(_node) );
146 SGRawValueMethods<Canvas, int>( *this, &Canvas::getSizeX,
152 SGRawValueMethods<Canvas, int>( *this, &Canvas::getSizeY,
158 SGRawValueMethods<Canvas, int>( *this, &Canvas::getViewWidth,
159 &Canvas::setViewWidth )
164 SGRawValueMethods<Canvas, int>( *this, &Canvas::getViewHeight,
165 &Canvas::setViewHeight )
170 SGRawValueMethods<Canvas, int>(*this, &Canvas::getStatus)
175 SGRawValueMethods<Canvas, const char*>(*this, &Canvas::getStatusMsg)
177 _node->addChangeListener(this);
179 canvas::linkColorNodes
189 //------------------------------------------------------------------------------
190 void Canvas::update(double delta_time_sec)
192 if( !_texture.serviceable() )
194 if( _status != STATUS_OK )
197 _texture.setSize(_size_x, _size_y);
198 _texture.useImageCoords(true);
199 _texture.useStencil(true);
200 _texture.allocRT(_camera_callback);
201 _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
202 _texture.getCamera()->addChild(_root_group->getMatrixTransform());
204 if( _texture.serviceable() )
206 setStatusFlags(STATUS_OK);
210 setStatusFlags(CREATE_FAILED);
215 _root_group->update(delta_time_sec);
217 if( _sampling_dirty )
219 _texture.setSampling(
220 _node->getBoolValue("mipmapping"),
221 _node->getIntValue("coverage-samples"),
222 _node->getIntValue("color-samples")
224 _sampling_dirty = false;
228 _texture.getCamera()->setClearColor
230 osg::Vec4( _color_background[0]->getFloatValue(),
231 _color_background[1]->getFloatValue(),
232 _color_background[2]->getFloatValue(),
233 _color_background[3]->getFloatValue() )
235 _color_dirty = false;
238 while( !_dirty_placements.empty() )
240 SGPropertyNode *node = _dirty_placements.back();
241 _dirty_placements.pop_back();
243 if( node->getIndex() >= static_cast<int>(_placements.size()) )
245 _placements.resize(node->getIndex() + 1);
247 // Remove maybe already existing placements
248 clearPlacements(node->getIndex());
250 // add new placements
251 _placements[node->getIndex()] = _texture.set_texture(
253 _texture.getTexture(),
259 //------------------------------------------------------------------------------
260 void Canvas::setSizeX(int sx)
266 // TODO resize if texture already allocated
269 setStatusFlags(MISSING_SIZE_X);
271 setStatusFlags(MISSING_SIZE_X, false);
273 // reset flag to allow creation with new size
274 setStatusFlags(CREATE_FAILED, false);
277 //------------------------------------------------------------------------------
278 int Canvas::getSizeX() const
283 //------------------------------------------------------------------------------
284 void Canvas::setSizeY(int sy)
290 // TODO resize if texture already allocated
293 setStatusFlags(MISSING_SIZE_Y);
295 setStatusFlags(MISSING_SIZE_Y, false);
297 // reset flag to allow creation with new size
298 setStatusFlags(CREATE_FAILED, false);
301 //------------------------------------------------------------------------------
302 int Canvas::getSizeY() const
307 //------------------------------------------------------------------------------
308 void Canvas::setViewWidth(int w)
310 if( _view_width == w )
314 _texture.setViewSize(_view_width, _view_height);
317 //------------------------------------------------------------------------------
318 int Canvas::getViewWidth() const
323 //------------------------------------------------------------------------------
324 void Canvas::setViewHeight(int h)
326 if( _view_height == h )
330 _texture.setViewSize(_view_width, _view_height);
333 //------------------------------------------------------------------------------
334 int Canvas::getViewHeight() const
339 //------------------------------------------------------------------------------
340 const char* Canvas::getStatusMsg() const
342 return _status_msg.c_str();
345 //------------------------------------------------------------------------------
346 void Canvas::childAdded( SGPropertyNode * parent,
347 SGPropertyNode * child )
349 if( parent != _node )
352 if( child->getNameString() == "placement" )
353 _dirty_placements.push_back(child);
355 static_cast<canvas::Element*>(_root_group.get())
356 ->childAdded(parent, child);
359 //------------------------------------------------------------------------------
360 void Canvas::childRemoved( SGPropertyNode * parent,
361 SGPropertyNode * child )
363 if( parent != _node )
366 if( child->getNameString() == "placement" )
367 clearPlacements(child->getIndex());
369 static_cast<canvas::Element*>(_root_group.get())
370 ->childRemoved(parent, child);
373 //----------------------------------------------------------------------------
374 void Canvas::valueChanged(SGPropertyNode* node)
376 if( node->getParent()->getParent() == _node )
378 if( !_color_background.empty()
379 && _color_background[0]->getParent() == node->getParent() )
383 else if( node->getParent()->getNameString() == "placement" )
385 // prevent double updates...
386 for( size_t i = 0; i < _dirty_placements.size(); ++i )
388 if( node->getParent() == _dirty_placements[i] )
392 _dirty_placements.push_back(node->getParent());
395 else if( node->getParent() == _node )
397 if( node->getNameString() == "mipmapping"
398 || node->getNameString() == "coverage-samples"
399 || node->getNameString() == "color-samples" )
400 _sampling_dirty = true;
403 _root_group->valueChanged(node);
406 //------------------------------------------------------------------------------
407 void Canvas::setStatusFlags(unsigned int flags, bool set)
414 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
415 _status_msg = "Missing size";
416 else if( _status & MISSING_SIZE_X )
417 _status_msg = "Missing size-x";
418 else if( _status & MISSING_SIZE_Y )
419 _status_msg = "Missing size-y";
420 else if( _status & CREATE_FAILED )
421 _status_msg = "Creating render target failed";
422 else if( _status == STATUS_OK && !_texture.serviceable() )
423 _status_msg = "Creation pending...";
428 //------------------------------------------------------------------------------
429 void Canvas::clearPlacements(int index)
431 Placements& placements = _placements.at(index);
432 while( !placements.empty() )
434 osg::ref_ptr<osg::Group> group = placements.back();
435 placements.pop_back();
437 assert( group->getNumParents() == 1 );
438 assert( group->getNumChildren() == 1 );
440 osg::Group *parent = group->getParent(0);
441 osg::Node *child = group->getChild(0);
443 parent->addChild(child);
444 group->removeChild(child);
445 parent->removeChild(group);
449 //------------------------------------------------------------------------------
450 void Canvas::clearPlacements()
452 for(size_t i = 0; i < _placements.size(); ++i)
457 //------------------------------------------------------------------------------
458 void Canvas::unbind()
463 _node->untie("size[0]");
464 _node->untie("size[1]");
465 _node->untie("view[0]");
466 _node->untie("view[1]");
467 _node->untie("status");
468 _node->untie("status-msg");
469 _node->removeChangeListener(this);