[osg-users] SubloadCallback on iOS: missing texture.

Alessandro Terenzi a.terenzi at gmail.com
Sat Apr 23 13:06:38 PDT 2011


Hi again, here's the code I'm using:

// my SubloadCallback:
ITexture2DSubloadCallback::ITexture2DSubloadCallback(osg::Texture2D*
texture)
{

const osg::Image* _image = texture->getImage();

// m_POT_width, m_POT_height are members of the subload callback

m_POT_width = (double)mathNextPowerOf2((unsigned int)_image->s()),
m_POT_height = (double)mathNextPowerOf2((unsigned int)_image->t()),

// set the texture to beleive it is of power of two size
texture->setTextureSize(m_POT_width, m_POT_height);

// set to linear to disable mipmap generation
texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);

}

void ITexture2DSubloadCallback::load(const osg::Texture2D& texture,
osg::State& state) const
{

 const osg::Image* _image = texture.getImage();

glTexImage2D(GL_TEXTURE_2D, 0,

osg::Image::computeNumComponents(_image->getInternalTextureFormat()),
                     (int)m_POT_width,
                     (int)m_POT_height,
                     0,
                     _image->getPixelFormat(),
                     _image->getDataType(),
                    0
                    );

state.checkGLErrors("ITexture2DSubloadCallback::load");

}

void ITexture2DSubloadCallback::subload(const osg::Texture2D& texture,
osg::State& state) const
{

const osg::Image* _image = texture.getImage();


glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,

                    _image->s(), _image->t(),
                    _image->getPixelFormat(),
                    _image->getDataType(),
                    _image->data()
                   );

state.checkGLErrors("ITexture2DSubloadCallback::subload");

}

// and here's how I create the textured quad that is attached to the scene's
root...

float width = 200;
float height = 200;

osg::ref_ptr<osg::Geode> geode;
osg::ref_ptr<osg::Geometry> geom;

// create geometry...
geode = new osg::Geode;
geom = new osg::Geometry;

osg::Vec3 top_left(0, height, 0);
osg::Vec3 bottom_left(0, 0, 0);
osg::Vec3 bottom_right(width, 0, 0);
osg::Vec3 top_right(width, height, 0);

// vertices...
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(4);
(*vertices)[0] = top_left;
(*vertices)[1] = bottom_left;
(*vertices)[2] = bottom_right;
(*vertices)[3] = top_right;
geom->setVertexArray(vertices.get());

// texcoords...
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(4);

(*texcoords)[0].set(0.0f,1.0f);
(*texcoords)[1].set(0.0f,0.0f);
(*texcoords)[2].set(1.0f,0.0f);
(*texcoords)[3].set(1.0f,1.0f);
geom->setTexCoordArray(0, texcoords.get());

// normals...
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array(1);
(*normals)[0].set(0.0f, 0.0f, 1.0f);
geom->setNormalArray(normals.get());
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);

// colors...
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
(*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);   // this is the color that will
be displayed if the texture won't be displayed
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
geode->addDrawable(geom.get());

// texture...
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP );
texture->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP );
texture->setDataVariance(osg::Object::DYNAMIC);

osg::ref_ptr<osg::Image> image = osgDB::readImageFile("Icon.png"); // 57x57
image (NPOT)...
if(image.valid())
    texture->setImage(image.get());

*osg::ref_ptr<ITexture2DSubloadCallback> t2dscb = new
ITexture2DSubloadCallback(texture);
texture->setSubloadCallback(t2dscb.get());
*
// stateset...
osg::StateSet* stateset = geom->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF |
osg::StateAttribute::PROTECTED);
stateset->setTextureAttributeAndModes(0, texture.get(),
osg::StateAttribute::ON);

m_root->addChild(geode.get());

That's all. Hope you can help. Thanks!
Alessandro
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20110423/e1f18397/attachment-0003.htm>


More information about the osg-users mailing list