1
0
Fork 0

Canvas: default image element dimensions to texture size

This commit is contained in:
Thomas Geymayer 2012-08-09 21:58:55 +02:00
parent 83bbd9e45c
commit 288e7fa5ca
3 changed files with 55 additions and 14 deletions

View file

@ -74,9 +74,7 @@ namespace canvas
Image::Image(SGPropertyNode_ptr node): Image::Image(SGPropertyNode_ptr node):
Element(node, COLOR_FILL | BOUNDING_BOX), Element(node, COLOR_FILL | BOUNDING_BOX),
_texture(new osg::Texture2D), _texture(new osg::Texture2D),
_node_src_rect( node->getNode("source", 0, true) ), _node_src_rect( node->getNode("source", 0, true) )
_src_rect(0,0,0,0),
_region(0,0,0,0)
{ {
_geom = new osg::Geometry; _geom = new osg::Geometry;
_geom->setUseDisplayList(false); _geom->setUseDisplayList(false);
@ -139,17 +137,12 @@ namespace canvas
if( !_node_src_rect->getBoolValue("normalized", true) ) if( !_node_src_rect->getBoolValue("normalized", true) )
{ {
osg::Texture2D *texture = !_canvas.expired() const Rect<int>& tex_dim = getTextureDimensions();
? _canvas.lock()->getTexture()
: _texture.get();
int texWidth = texture->getTextureWidth(); u0 /= tex_dim.width();
int texHeight = texture->getTextureHeight(); u1 /= tex_dim.width();
v0 /= tex_dim.height();
u0 /= texWidth; v1 /= tex_dim.height();
u1 /= texWidth;
v0 /= texHeight;
v1 /= texHeight;
} }
(*_texCoords)[0].set(u0, v0); (*_texCoords)[0].set(u0, v0);
@ -171,6 +164,9 @@ namespace canvas
_geom->setCullCallback( _geom->setCullCallback(
canvas ? new CullCallback(canvas->getCameraCullCallback()) : 0 canvas ? new CullCallback(canvas->getCameraCullCallback()) : 0
); );
if( !_canvas.expired() )
setupDefaultDimensions();
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -188,6 +184,9 @@ namespace canvas
_texture->setImage(img); _texture->setImage(img);
_geom->getOrCreateStateSet() _geom->getOrCreateStateSet()
->setTextureAttributeAndModes(0, _texture); ->setTextureAttributeAndModes(0, _texture);
if( img )
setupDefaultDimensions();
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -298,4 +297,38 @@ namespace canvas
_colors->dirty(); _colors->dirty();
} }
//----------------------------------------------------------------------------
void Image::setupDefaultDimensions()
{
if( !_src_rect.width() || !_src_rect.height() )
{
const Rect<int>& tex_dim = getTextureDimensions();
_node_src_rect->setBoolValue("normalized", false);
_node_src_rect->setFloatValue("right", tex_dim.width());
_node_src_rect->setFloatValue("bottom", tex_dim.height());
}
if( !_region.width() || !_region.height() )
{
_node->setFloatValue("size[0]", _src_rect.width());
_node->setFloatValue("size[1]", _src_rect.height());
}
}
//----------------------------------------------------------------------------
Rect<int> Image::getTextureDimensions() const
{
osg::Texture2D *texture = !_canvas.expired()
? _canvas.lock()->getTexture()
: _texture.get();
return Rect<int>
(
0,0,
texture->getTextureWidth(),
texture->getTextureHeight()
);
}
} // namespace canvas } // namespace canvas

View file

@ -72,6 +72,9 @@ namespace canvas
void handleHit(float x, float y); void handleHit(float x, float y);
void setupDefaultDimensions();
Rect<int> getTextureDimensions() const;
osg::ref_ptr<osg::Texture2D> _texture; osg::ref_ptr<osg::Texture2D> _texture;
// TODO optionally forward events to canvas // TODO optionally forward events to canvas
CanvasWeakPtr _canvas; CanvasWeakPtr _canvas;

View file

@ -27,7 +27,12 @@ namespace canvas
class Rect class Rect
{ {
public: public:
Rect() {} Rect():
_x1(0),
_x2(0),
_y1(0),
_y2(0)
{}
Rect(T x, T y, T w, T h): Rect(T x, T y, T w, T h):
_x1(x), _x1(x),