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"
24 #include <osgText/Text>
28 //#include <Main/globals.hxx>
29 //#include <Viewer/renderer.hxx>
31 //------------------------------------------------------------------------------
33 * Callback used to disable/enable rendering to the texture if it is not
36 class CameraCullCallback:
37 public osg::NodeCallback
46 * Enable rendering for the next frame
48 void enableRendering()
57 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
68 * This callback is installed on every placement of the canvas in the scene to
69 * only render the canvas if at least one placement is visible
71 class PlacementCullCallback:
72 public osg::NodeCallback
76 PlacementCullCallback(Canvas* canvas, CameraCullCallback* camera_cull):
78 _camera_cull( camera_cull )
84 CameraCullCallback *_camera_cull;
86 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
88 _camera_cull->enableRendering();
93 //------------------------------------------------------------------------------
101 _sampling_dirty(false)
103 setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y);
105 CameraCullCallback *camera_callback = new CameraCullCallback;
106 _camera_callback = camera_callback;
107 _cull_callback = new PlacementCullCallback(this, camera_callback);
110 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
117 int Canvas::getStatus() const
122 //------------------------------------------------------------------------------
123 void Canvas::reset(SGPropertyNode* node)
127 _node->untie("size[0]");
128 _node->untie("size[1]");
129 _node->untie("view[0]");
130 _node->untie("view[1]");
131 _node->untie("status");
132 _node->untie("status-msg");
133 _node->removeChangeListener(this);
137 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);
180 //------------------------------------------------------------------------------
181 void Canvas::update(double delta_time_sec)
183 if( !_texture.serviceable() )
185 if( _status != STATUS_OK )
188 _texture.setSize(_size_x, _size_y);
189 _texture.useImageCoords(true);
190 _texture.useStencil(true);
191 _texture.allocRT(_camera_callback);
192 _texture.getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 1.0f));
194 for( size_t i = 0; i < _groups.size(); ++i )
195 _texture.getCamera()->addChild( _groups[i]->getMatrixTransform() );
197 if( _texture.serviceable() )
199 setStatusFlags(STATUS_OK);
203 setStatusFlags(CREATE_FAILED);
208 for( size_t i = 0; i < _groups.size(); ++i )
209 _groups[i]->update(delta_time_sec);
211 if( _sampling_dirty )
213 _texture.setSampling(
214 _node->getBoolValue("mipmapping"),
215 _node->getIntValue("coverage-samples"),
216 _node->getIntValue("color-samples")
218 _sampling_dirty = false;
221 while( !_dirty_placements.empty() )
223 SGPropertyNode *node = _dirty_placements.back();
224 _dirty_placements.pop_back();
226 if( node->getIndex() >= static_cast<int>(_placements.size()) )
228 _placements.resize(node->getIndex() + 1);
230 // Remove maybe already existing placements
231 clearPlacements(node->getIndex());
233 // add new placements
234 _placements[node->getIndex()] = _texture.set_texture(
236 _texture.getTexture(),
242 //------------------------------------------------------------------------------
243 void Canvas::setSizeX(int sx)
249 // TODO resize if texture already allocated
252 setStatusFlags(MISSING_SIZE_X);
254 setStatusFlags(MISSING_SIZE_X, false);
256 // reset flag to allow creation with new size
257 setStatusFlags(CREATE_FAILED, false);
260 //------------------------------------------------------------------------------
261 int Canvas::getSizeX() const
266 //------------------------------------------------------------------------------
267 void Canvas::setSizeY(int sy)
273 // TODO resize if texture already allocated
276 setStatusFlags(MISSING_SIZE_Y);
278 setStatusFlags(MISSING_SIZE_Y, false);
280 // reset flag to allow creation with new size
281 setStatusFlags(CREATE_FAILED, false);
284 //------------------------------------------------------------------------------
285 int Canvas::getSizeY() const
290 //------------------------------------------------------------------------------
291 void Canvas::setViewWidth(int w)
293 if( _view_width == w )
297 _texture.setViewSize(_view_width, _view_height);
300 //------------------------------------------------------------------------------
301 int Canvas::getViewWidth() const
306 //------------------------------------------------------------------------------
307 void Canvas::setViewHeight(int h)
309 if( _view_height == h )
313 _texture.setViewSize(_view_width, _view_height);
316 //------------------------------------------------------------------------------
317 int Canvas::getViewHeight() const
322 //------------------------------------------------------------------------------
323 const char* Canvas::getStatusMsg() const
325 return _status_msg.c_str();
328 //------------------------------------------------------------------------------
329 void Canvas::childAdded( SGPropertyNode * parent,
330 SGPropertyNode * child )
332 if( parent != _node )
335 if( child->getNameString() == "group" )
339 boost::shared_ptr<canvas::Group>(new canvas::Group(child))
341 if( _texture.serviceable() )
342 _texture.getCamera()->addChild( _groups.back()->getMatrixTransform() );
344 else if( child->getNameString() == "placement" )
346 _dirty_placements.push_back(child);
349 // std::cout << "Canvas::childAdded: " << child->getPath() << std::endl;
352 //------------------------------------------------------------------------------
353 void Canvas::childRemoved( SGPropertyNode * parent,
354 SGPropertyNode * child )
356 if( parent != _node )
359 if( child->getNameString() == "placement" )
360 clearPlacements(child->getIndex());
362 std::cout << "Canvas::childRemoved: " << child->getPath() << std::endl;
365 //----------------------------------------------------------------------------
366 void Canvas::valueChanged(SGPropertyNode * node)
368 if( node->getParent()->getParent() == _node
369 && node->getParent()->getNameString() == "placement" )
371 // prevent double updates...
372 for( size_t i = 0; i < _dirty_placements.size(); ++i )
374 if( node->getParent() == _dirty_placements[i] )
378 _dirty_placements.push_back(node->getParent());
380 else if( node->getParent() == _node )
382 if( node->getNameString() == "mipmapping"
383 || node->getNameString() == "coverage-samples"
384 || node->getNameString() == "color-samples" )
385 _sampling_dirty = true;
389 //------------------------------------------------------------------------------
390 void Canvas::setStatusFlags(unsigned int flags, bool set)
397 if( (_status & MISSING_SIZE_X) && (_status & MISSING_SIZE_Y) )
398 _status_msg = "Missing size";
399 else if( _status & MISSING_SIZE_X )
400 _status_msg = "Missing size-x";
401 else if( _status & MISSING_SIZE_Y )
402 _status_msg = "Missing size-y";
403 else if( _status & CREATE_FAILED )
404 _status_msg = "Creating render target failed";
405 else if( _status == STATUS_OK && !_texture.serviceable() )
406 _status_msg = "Creation pending...";
411 //------------------------------------------------------------------------------
412 void Canvas::clearPlacements(int index)
414 Placements& placements = _placements.at(index);
415 while( !placements.empty() )
417 osg::ref_ptr<osg::Group> group = placements.back();
418 placements.pop_back();
420 assert( group->getNumParents() == 1 );
421 assert( group->getNumChildren() == 1 );
423 osg::Group *parent = group->getParent(0);
424 osg::Node *child = group->getChild(0);
426 parent->addChild(child);
427 group->removeChild(child);
428 parent->removeChild(group);