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

View file

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

View file

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