[osg-users] [3rdparty] What wrong with my "SubloadCallback" derived class?
Zheng Li
908835930 at qq.com
Wed Mar 23 07:58:08 PDT 2016
Hi,
I wrote a "SubloadCallback" derived class and attach it to a texture2D object to update image dynamically,but unfortunately the texture image can not be updated,the texture always show a piece of white;below is my code,what wrong happened to it?
void main()
{
// the class to update texture image dynamically;
class UpdateTextureCallback : public osg::Texture2D::SubloadCallback
{
public:
osg::ref_ptr<osg::Image> _image;
SIZE m_szVideo;
public:
UpdateTextureCallback(int vwidth,int vheight)
{
m_szVideo.cx = vwidth;
m_szVideo.cy = vheight;
_image = osgDB::readImageFile("C:\\test.jpg");
assert(_image->valid());
_image->scaleImage(vwidth,vheight,_image->r());
};
virtual ~UpdateTextureCallback()
{
_image = NULL;
};
virtual void load(const osg::Texture2D& texture,osg::State& state) const
{
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,m_szVideo.cx,m_szVideo.cy,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,NULL);
};
virtual void subload(const osg::Texture2D& texture,osg::State& state) const
{
readImageFromDevice(_image);
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,_image->s(),_image->t(),_image->getPixelFormat(),_image->getDataType(),_image->getDataPointer());
};
};
const int VideoWidth = 3840;
const int VideoHeight = 2160;
osg::ref_ptr<osg::Texture2D> texVideo = new osg::Texture2D();
texVideo->setResizeNonPowerOfTwoHint(false);
texVideo->setTextureSize(VideoWidth,VideoHeight);
texVideo->setInternalFormat(GL_RGBA);
texVideo->setSubloadCallback(new UpdateTextureCallback(VideoWidth,VideoHeight));
osg::ref_ptr<osg::Geode> quad = new osg::Geode();
quad->addDrawable(createDrawable_Quad());
quad->getOrCreateStateSet()->setTextureAttributeAndModes(0,texVideo.get());
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->realize();
viewer->getCamera()->setAllowEventFocus(false);
viewer->getCamera()->setProjectionMatrixAsOrtho2D(0,1,0,1);
viewer->getCamera()->setViewMatrix(osg::Matrix::identity());
viewer->setSceneData( quad );
viewer->run();
}
osg::Geometry * createDrawable_Quad(float alpha=1.0f)
{
osg::ref_ptr<osg::Vec2Array> vertices = new osg::Vec2Array;
vertices->push_back( osg::Vec2(0,0) );
vertices->push_back( osg::Vec2(0,1) );
vertices->push_back( osg::Vec2(1,1) );
vertices->push_back( osg::Vec2(1,0) );
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
texcoords->push_back( osg::Vec2(0.0f, 0.0f) );
texcoords->push_back( osg::Vec2(0.0f, 1.0f) );
texcoords->push_back( osg::Vec2(1.0f, 1.0f) );
texcoords->push_back( osg::Vec2(1.0f, 0.0f) );
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,alpha));
osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
quad->setVertexArray( vertices.get() );
quad->setColorArray(colors.get());
quad->setColorBinding( osg::Geometry::BIND_OVERALL );
quad->setTexCoordArray( 0, texcoords.get() );
quad->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4) );
return quad.release();
}
Thank you!
Cheers,
Zheng
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=66609#66609
More information about the osg-users
mailing list