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/property_helper.hxx>
23 #include <Main/globals.hxx>
24 #include <Viewer/renderer.hxx>
28 #include <osgText/Text>
29 #include <osgViewer/Viewer>
33 //------------------------------------------------------------------------------
35 * Callback used to disable/enable rendering to the texture if it is not
38 class CameraCullCallback:
39 public osg::NodeCallback
48 * Enable rendering for the next frame
50 void enableRendering()
59 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
70 * This callback is installed on every placement of the canvas in the scene to
71 * only render the canvas if at least one placement is visible
73 class PlacementCullCallback:
74 public osg::NodeCallback
78 PlacementCullCallback(Canvas* canvas, CameraCullCallback* camera_cull):
80 _camera_cull( camera_cull )
86 CameraCullCallback *_camera_cull;
88 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
90 _camera_cull->enableRendering();
95 //------------------------------------------------------------------------------
102 _sampling_dirty(false),
105 _render_always(false)
107 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
109 _camera_callback = new CameraCullCallback;
110 _cull_callback = new PlacementCullCallback(this, _camera_callback);
113 //------------------------------------------------------------------------------
122 //------------------------------------------------------------------------------
123 int Canvas::getStatus() const
128 //------------------------------------------------------------------------------
129 void Canvas::reset(SGPropertyNode* node)
136 "Canvas::reset() texture[" << node->getIndex() << "]"
142 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
146 _root_group.reset( new canvas::Group(_node) );
150 SGRawValueMethods<Canvas, int>( *this, &Canvas::getSizeX,
156 SGRawValueMethods<Canvas, int>( *this, &Canvas::getSizeY,
162 SGRawValueMethods<Canvas, int>( *this, &Canvas::getViewWidth,
163 &Canvas::setViewWidth )
168 SGRawValueMethods<Canvas, int>( *this, &Canvas::getViewHeight,
169 &Canvas::setViewHeight )
174 SGRawValueMethods<Canvas, int>(*this, &Canvas::getStatus)
179 SGRawValueMethods<Canvas, const char*>(*this, &Canvas::getStatusMsg)
181 _node->addChangeListener(this);
183 canvas::linkColorNodes
193 //------------------------------------------------------------------------------
194 void Canvas::update(double delta_time_sec)
196 if( !_texture.serviceable() )
198 if( _status != STATUS_OK )
201 _texture.setSize(_size_x, _size_y);
202 _texture.useImageCoords(true);
203 _texture.useStencil(true);
204 _texture.allocRT(_camera_callback);
205 _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
206 _texture.getCamera()->addChild(_root_group->getMatrixTransform());
208 if( _texture.serviceable() )
210 setStatusFlags(STATUS_OK);
214 setStatusFlags(CREATE_FAILED);
219 _root_group->update(delta_time_sec);
221 if( _sampling_dirty )
223 _texture.setSampling(
224 _node->getBoolValue("mipmapping"),
225 _node->getIntValue("coverage-samples"),
226 _node->getIntValue("color-samples")
228 _sampling_dirty = false;
232 _texture.getCamera()->setClearColor
234 osg::Vec4( _color_background[0]->getFloatValue(),
235 _color_background[1]->getFloatValue(),
236 _color_background[2]->getFloatValue(),
237 _color_background[3]->getFloatValue() )
239 _color_dirty = false;
242 while( !_dirty_placements.empty() )
244 SGPropertyNode *node = _dirty_placements.back();
245 _dirty_placements.pop_back();
247 if( node->getIndex() >= static_cast<int>(_placements.size()) )
249 _placements.resize(node->getIndex() + 1);
251 // Remove maybe already existing placements
252 clearPlacements(node->getIndex());
254 // add new placements
255 _placements[node->getIndex()] = _texture.set_texture(
257 _texture.getTexture(),
263 _camera_callback->enableRendering();
266 //------------------------------------------------------------------------------
267 void Canvas::setSizeX(int sx)
273 // TODO resize if texture already allocated
276 setStatusFlags(MISSING_SIZE_X);
278 setStatusFlags(MISSING_SIZE_X, false);
280 // reset flag to allow creation with new size
281 setStatusFlags(CREATE_FAILED, false);
284 //------------------------------------------------------------------------------
285 int Canvas::getSizeX() const
290 //------------------------------------------------------------------------------
291 void Canvas::setSizeY(int sy)
297 // TODO resize if texture already allocated
300 setStatusFlags(MISSING_SIZE_Y);
302 setStatusFlags(MISSING_SIZE_Y, false);
304 // reset flag to allow creation with new size
305 setStatusFlags(CREATE_FAILED, false);
308 //------------------------------------------------------------------------------
309 int Canvas::getSizeY() const
314 //------------------------------------------------------------------------------
315 void Canvas::setViewWidth(int w)
317 if( _view_width == w )
321 _texture.setViewSize(_view_width, _view_height);
324 //------------------------------------------------------------------------------
325 int Canvas::getViewWidth() const
330 //------------------------------------------------------------------------------
331 void Canvas::setViewHeight(int h)
333 if( _view_height == h )
337 _texture.setViewSize(_view_width, _view_height);
340 //------------------------------------------------------------------------------
341 int Canvas::getViewHeight() const
346 //------------------------------------------------------------------------------
347 const char* Canvas::getStatusMsg() const
349 return _status_msg.c_str();
352 //------------------------------------------------------------------------------
353 void Canvas::childAdded( SGPropertyNode * parent,
354 SGPropertyNode * child )
356 if( parent != _node )
359 if( child->getNameString() == "placement" )
360 _dirty_placements.push_back(child);
362 static_cast<canvas::Element*>(_root_group.get())
363 ->childAdded(parent, child);
366 //------------------------------------------------------------------------------
367 void Canvas::childRemoved( SGPropertyNode * parent,
368 SGPropertyNode * child )
370 if( parent != _node )
373 if( child->getNameString() == "placement" )
374 clearPlacements(child->getIndex());
376 static_cast<canvas::Element*>(_root_group.get())
377 ->childRemoved(parent, child);
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;
408 else if( node->getNameString() == "render-always" )
409 _render_always = node->getBoolValue();
412 _root_group->valueChanged(node);
415 //------------------------------------------------------------------------------
416 GLuint Canvas::getTexId() const
418 osg::Texture2D* tex = _texture.getTexture();
422 osgViewer::Viewer::Contexts contexts;
423 globals->get_renderer()->getViewer()->getContexts(contexts);
425 if( contexts.empty() )
428 osg::State* state = contexts[0]->getState();
432 osg::Texture::TextureObject* tobj =
433 tex->getTextureObject( state->getContextID() );
440 //------------------------------------------------------------------------------
441 void Canvas::setStatusFlags(unsigned int flags, bool set)
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 void Canvas::clearPlacements(int index)
465 Placements& placements = _placements.at(index);
466 while( !placements.empty() )
468 osg::ref_ptr<osg::Group> group = placements.back();
469 placements.pop_back();
471 assert( group->getNumChildren() == 1 );
472 osg::Node *child = group->getChild(0);
474 if( group->getNumParents() )
476 osg::Group *parent = group->getParent(0);
477 parent->addChild(child);
478 parent->removeChild(group);
481 group->removeChild(child);
485 //------------------------------------------------------------------------------
486 void Canvas::clearPlacements()
488 for(size_t i = 0; i < _placements.size(); ++i)
493 //------------------------------------------------------------------------------
494 void Canvas::unbind()
499 _node->untie("size[0]");
500 _node->untie("size[1]");
501 _node->untie("view[0]");
502 _node->untie("view[1]");
503 _node->untie("status");
504 _node->untie("status-msg");
505 _node->removeChangeListener(this);