[osg-users] What wrong with my "SubloadCallback" derived class?

Robert Osfield robert.osfield at gmail.com
Thu Mar 24 01:43:21 PDT 2016


HI Zheng,

I am away from my dev machine so not in a position to look up the
implementation side for subload callback to double check your code.

In general though I wouldn't recommend having the draw traversal doing a
read from a device as this is very likely to cause a frame drop. The
standard way to tackle this type of problem is to have a back ground thread
read from the device copying the data an osg::Image, after copying calling
dirty() on the image.  This osg::Image would also be attached to the
osg::Texture in the scene graph, but there is no need to implement a
subload callback as the Texture::apply() checks the Image modifiedCount()
and automatically subloads for you.

The video plugins like ffmpeg, quicktime and directshow ones implement this
approach.

Robert.

On 23 March 2016 at 15:10, Zheng Li <908835930 at qq.com> wrote:

> 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=66611#66611
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20160324/463b1f9f/attachment-0002.htm>


More information about the osg-users mailing list