[osg-users] fixed size texture
Vitaliy Polyakov
poljak181 at yandex.ru
Tue Jul 28 08:43:18 PDT 2015
Hi,
I want to draw an icon using texture. My texture image is an image of 8x8 pixel size. It looks like quadrate without center (center is transparent). Border width of this quadrate is one pixel.
When I moving camera with the mouse - border size of icon changed. It can also disappear.
How can I provide permanent width of quadrate border (1 pixel, as in a texture)
My code to add icon on the scene.
Code:
osg::ref_ptr<osg::Node> createTexture()
{
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("8x8.png");
float width = image->s();
float height = image->t();
osg::Geometry* geometry = new osg::Geometry;
osg::Vec3Array* verts = new osg::Vec3Array(4);
(*verts)[0] = osg::Vec3(-width/2.0f, -height/2.0, 0.0f);
(*verts)[1] = osg::Vec3(width/2.0f, -height/2.0, 0.0f);
(*verts)[2] = osg::Vec3(width/2.0f, height/2.0, 0.0f);
(*verts)[3] = osg::Vec3(-width/2.0f,height/2.0, 0.0f);
geometry->setVertexArray( verts );
osg::Vec2Array* texcoords = new osg::Vec2Array(4);
(*texcoords)[0].set(0.0f, 0.0f);
(*texcoords)[1].set(width, 0.0f);
(*texcoords)[2].set(width, height);
(*texcoords)[3].set(0.0f, height);
geometry->setTexCoordArray(0, texcoords);
osg::Vec4Array* colors = new osg::Vec4Array(1);
(*colors)[0].set(1,1,1,1);
geometry->setColorArray( colors );
geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
geometry->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4));
osg::StateSet* stateSet = geometry->getOrCreateStateSet();
osg::TextureRectangle* texture = new osg::TextureRectangle( image );
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
stateSet->setAttributeAndModes( new osg::Depth(osg::Depth::ALWAYS,false), 1 );
osg::Geode* geode = new osg::Geode;
geode->addDrawable( geometry );
osg::AutoTransform* at = new osg::AutoTransform;
at->setAutoScaleToScreen( true );
at->setAutoRotateMode( osg::AutoTransform::ROTATE_TO_SCREEN );
at->addChild( geode );
return at;
}
Thank you![/code]
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=64511#64511
More information about the osg-users
mailing list