<div dir="ltr">HI Zheng,<div><br></div><div>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. </div><div><br></div><div>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.</div><div><br></div><div>The video plugins like ffmpeg, quicktime and directshow ones implement this approach.</div><div><br></div><div>Robert. </div></div><div class="gmail_extra"><br><div class="gmail_quote">On 23 March 2016 at 15:10, Zheng Li <span dir="ltr"><<a href="mailto:908835930@qq.com" target="_blank">908835930@qq.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">Hi,<br>
<br>
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?<br>
<br>
void main()<br>
{<br>
// the class to update texture image dynamically;<br>
class UpdateTextureCallback : public osg::Texture2D::SubloadCallback<br>
{<br>
public:<br>
osg::ref_ptr<osg::Image> _image;<br>
SIZE m_szVideo;<br>
<br>
public:<br>
UpdateTextureCallback(int vwidth,int vheight)<br>
{<br>
m_szVideo.cx = vwidth;<br>
m_szVideo.cy = vheight;<br>
_image = osgDB::readImageFile("C:\\test.jpg");<br>
assert(_image->valid());<br>
_image->scaleImage(vwidth,vheight,_image->r());<br>
};<br>
<br>
virtual ~UpdateTextureCallback()<br>
{<br>
_image = NULL;<br>
};<br>
<br>
virtual void load(const osg::Texture2D& texture,osg::State& state) const<br>
{<br>
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,m_szVideo.cx,m_szVideo.cy,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,NULL);<br>
};<br>
<br>
virtual void subload(const osg::Texture2D& texture,osg::State& state) const<br>
{<br>
readImageFromDevice(_image);<br>
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,_image->s(),_image->t(),_image->getPixelFormat(),_image->getDataType(),_image->getDataPointer());<br>
};<br>
};<br>
<br>
const int VideoWidth = 3840;<br>
const int VideoHeight = 2160;<br>
osg::ref_ptr<osg::Texture2D> texVideo = new osg::Texture2D();<br>
texVideo->setResizeNonPowerOfTwoHint(false);<br>
texVideo->setTextureSize(VideoWidth,VideoHeight);<br>
texVideo->setInternalFormat(GL_RGBA);<br>
texVideo->setSubloadCallback(new UpdateTextureCallback(VideoWidth,VideoHeight));<br>
<br>
osg::ref_ptr<osg::Geode> quad = new osg::Geode();<br>
quad->addDrawable(createDrawable_Quad());<br>
quad->getOrCreateStateSet()->setTextureAttributeAndModes(0,texVideo.get());<br>
<br>
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();<br>
viewer->realize();<br>
viewer->getCamera()->setAllowEventFocus(false);<br>
viewer->getCamera()->setProjectionMatrixAsOrtho2D(0,1,0,1);<br>
viewer->getCamera()->setViewMatrix(osg::Matrix::identity());<br>
viewer->setSceneData( quad );<br>
viewer->run();<br>
}<br>
<br>
osg::Geometry * createDrawable_Quad(float alpha=1.0f)<br>
{<br>
osg::ref_ptr<osg::Vec2Array> vertices = new osg::Vec2Array;<br>
vertices->push_back( osg::Vec2(0,0) );<br>
vertices->push_back( osg::Vec2(0,1) );<br>
vertices->push_back( osg::Vec2(1,1) );<br>
vertices->push_back( osg::Vec2(1,0) );<br>
<br>
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;<br>
texcoords->push_back( osg::Vec2(0.0f, 0.0f) );<br>
texcoords->push_back( osg::Vec2(0.0f, 1.0f) );<br>
texcoords->push_back( osg::Vec2(1.0f, 1.0f) );<br>
texcoords->push_back( osg::Vec2(1.0f, 0.0f) );<br>
<br>
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();<br>
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,alpha));<br>
<br>
osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;<br>
quad->setVertexArray( vertices.get() );<br>
quad->setColorArray(colors.get());<br>
quad->setColorBinding( osg::Geometry::BIND_OVERALL );<br>
quad->setTexCoordArray( 0, texcoords.get() );<br>
quad->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4) );<br>
return quad.release();<br>
}<br>
<br>
<br>
Thank you!<br>
<br>
Cheers,<br>
Zheng<br>
<br>
</div></div><span class="">------------------<br>
Read this topic online here:<br>
</span><a href="http://forum.openscenegraph.org/viewtopic.php?p=66611#66611" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=66611#66611</a><br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</div></div></blockquote></div><br></div>