1 // An image on the canvas
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.
19 #include "CanvasImage.hxx"
21 #include <osgDB/ReadFile>
23 #include <Canvas/canvas.hxx>
24 #include <Canvas/canvas_mgr.hxx>
25 #include <Canvas/property_helper.hxx>
27 #include <osg/Geometry>
28 #include <osg/PrimitiveSet>
30 #include <Main/globals.hxx>
31 #include <Main/fg_props.hxx>
33 #include <boost/algorithm/string/predicate.hpp>
36 * Callback to enable/disable rendering of canvas displayed inside windows or
40 public osg::Drawable::CullCallback
43 CullCallback(Canvas::CameraCullCallback* camera_cull);
46 Canvas::CameraCullCallbackWeakPtr _camera_cull;
48 virtual bool cull( osg::NodeVisitor* nv,
49 osg::Drawable* drawable,
50 osg::RenderInfo* renderInfo ) const;
53 //------------------------------------------------------------------------------
54 CullCallback::CullCallback(Canvas::CameraCullCallback* camera_cull):
55 _camera_cull( camera_cull )
60 //------------------------------------------------------------------------------
61 bool CullCallback::cull( osg::NodeVisitor* nv,
62 osg::Drawable* drawable,
63 osg::RenderInfo* renderInfo ) const
65 if( _camera_cull.valid() )
66 _camera_cull->enableRendering();
68 // TODO check if window/image should be culled
74 //----------------------------------------------------------------------------
75 Image::Image(SGPropertyNode_ptr node, const Style& parent_style):
76 Element(node, parent_style, BOUNDING_BOX),
77 _texture(new osg::Texture2D),
78 _node_src_rect( node->getNode("source", 0, true) )
80 _geom = new osg::Geometry;
81 _geom->setUseDisplayList(false);
83 osg::StateSet *stateSet = _geom->getOrCreateStateSet();
84 stateSet->setTextureAttributeAndModes(0, _texture.get());
85 stateSet->setDataVariance(osg::Object::STATIC);
87 // allocate arrays for the image
88 _vertices = new osg::Vec3Array(4);
89 _vertices->setDataVariance(osg::Object::STATIC);
90 _geom->setVertexArray(_vertices);
92 _texCoords = new osg::Vec2Array(4);
93 _texCoords->setDataVariance(osg::Object::STATIC);
94 _geom->setTexCoordArray(0, _texCoords);
96 _colors = new osg::Vec4Array(4);
97 _colors->setDataVariance(osg::Object::STATIC);
98 _geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
99 _geom->setColorArray(_colors);
101 osg::DrawArrays* prim = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
102 prim->set(osg::PrimitiveSet::QUADS, 0, 4);
103 prim->setDataVariance(osg::Object::STATIC);
104 _geom->addPrimitiveSet(prim);
108 addStyle("fill", &Image::setFill, this);
109 setFill("#ffffff"); // TODO how should we handle default values?
114 //----------------------------------------------------------------------------
120 //----------------------------------------------------------------------------
121 void Image::update(double dt)
125 if( _attributes_dirty & DEST_SIZE )
127 (*_vertices)[0].set(_region.l(), _region.t(), 0);
128 (*_vertices)[1].set(_region.r(), _region.t(), 0);
129 (*_vertices)[2].set(_region.r(), _region.b(), 0);
130 (*_vertices)[3].set(_region.l(), _region.b(), 0);
133 _attributes_dirty &= ~DEST_SIZE;
137 if( _attributes_dirty & SRC_RECT )
139 double u0 = _src_rect.l(),
144 if( !_node_src_rect->getBoolValue("normalized", true) )
146 const Rect<int>& tex_dim = getTextureDimensions();
148 u0 /= tex_dim.width();
149 u1 /= tex_dim.width();
150 v0 /= tex_dim.height();
151 v1 /= tex_dim.height();
154 (*_texCoords)[0].set(u0, v0);
155 (*_texCoords)[1].set(u1, v0);
156 (*_texCoords)[2].set(u1, v1);
157 (*_texCoords)[3].set(u0, v1);
160 _attributes_dirty &= ~SRC_RECT;
164 //----------------------------------------------------------------------------
165 void Image::setCanvas(CanvasPtr canvas)
168 _geom->getOrCreateStateSet()
169 ->setTextureAttribute(0, canvas ? canvas->getTexture() : 0);
170 _geom->setCullCallback(
171 canvas ? new CullCallback(canvas->getCameraCullCallback()) : 0
174 if( !_canvas.expired() )
175 setupDefaultDimensions();
178 //----------------------------------------------------------------------------
179 CanvasWeakPtr Image::getCanvas() const
184 //----------------------------------------------------------------------------
185 void Image::setImage(osg::Image *img)
188 setCanvas( CanvasPtr() );
190 _texture->setImage(img);
191 _geom->getOrCreateStateSet()
192 ->setTextureAttributeAndModes(0, _texture);
195 setupDefaultDimensions();
198 //----------------------------------------------------------------------------
199 void Image::setFill(const std::string& fill)
201 osg::Vec4 color = parseColor(fill);
202 for( int i = 0; i < 4; ++i )
203 (*_colors)[i] = color;
207 //----------------------------------------------------------------------------
208 const Rect<float>& Image::getRegion() const
213 //----------------------------------------------------------------------------
214 void Image::childChanged(SGPropertyNode* child)
216 const std::string& name = child->getNameString();
218 if( child->getParent() == _node_src_rect )
220 _attributes_dirty |= SRC_RECT;
223 _src_rect.setLeft( child->getFloatValue() );
224 else if( name == "right" )
225 _src_rect.setRight( child->getFloatValue() );
226 else if( name == "top" )
227 _src_rect.setTop( child->getFloatValue() );
228 else if( name == "bottom" )
229 _src_rect.setBottom( child->getFloatValue() );
233 else if( child->getParent() != _node )
238 _region.setX( child->getFloatValue() );
239 _attributes_dirty |= DEST_SIZE;
241 else if( name == "y" )
243 _region.setY( child->getFloatValue() );
244 _attributes_dirty |= DEST_SIZE;
246 else if( name == "size" )
248 if( child->getIndex() == 0 )
249 _region.setWidth( child->getFloatValue() );
251 _region.setHeight( child->getFloatValue() );
253 _attributes_dirty |= DEST_SIZE;
255 else if( name == "file" )
257 static const std::string CANVAS_PROTOCOL = "canvas://";
258 const std::string& path = child->getStringValue();
260 if( boost::starts_with(path, CANVAS_PROTOCOL) )
262 CanvasMgr* canvas_mgr =
263 dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
266 SG_LOG(SG_GL, SG_ALERT, "canvas::Image: Failed to get CanvasMgr");
270 const SGPropertyNode* canvas_node =
271 canvas_mgr->getPropertyRoot()
273 ->getNode( path.substr(CANVAS_PROTOCOL.size()) );
276 SG_LOG(SG_GL, SG_ALERT, "canvas::Image: No such canvas: " << path);
280 // TODO add support for other means of addressing canvases (eg. by
282 CanvasPtr canvas = canvas_mgr->getCanvas( canvas_node->getIndex() );
285 SG_LOG(SG_GL, SG_ALERT, "canvas::Image: Invalid canvas: " << path);
293 SGPath tpath = globals->resolve_ressource_path(path);
294 if( tpath.isNull() || !tpath.exists() )
296 SG_LOG(SG_GL, SG_ALERT, "canvas::Image: No such image: " << path);
300 setImage( osgDB::readImageFile(tpath.c_str()) );
305 //----------------------------------------------------------------------------
306 void Image::setupDefaultDimensions()
308 if( !_src_rect.width() || !_src_rect.height() )
310 const Rect<int>& tex_dim = getTextureDimensions();
312 _node_src_rect->setBoolValue("normalized", false);
313 _node_src_rect->setFloatValue("right", tex_dim.width());
314 _node_src_rect->setFloatValue("bottom", tex_dim.height());
317 if( !_region.width() || !_region.height() )
319 _node->setFloatValue("size[0]", _src_rect.width());
320 _node->setFloatValue("size[1]", _src_rect.height());
324 //----------------------------------------------------------------------------
325 Rect<int> Image::getTextureDimensions() const
327 osg::Texture2D *texture = !_canvas.expired()
328 ? _canvas.lock()->getTexture()
337 texture->getTextureWidth(),
338 texture->getTextureHeight()
342 } // namespace canvas