OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

What wrong with my "SubloadCallback" derived class?


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General [3rdparty]
View previous topic :: View next topic  
Author Message
pango
Newbie


Joined: 26 Feb 2016
Posts: 6

PostPosted: Wed Mar 23, 2016 2:58 pm    Post subject:
What wrong with my "SubloadCallback" derived class?
Reply with quote

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
Back to top
View user's profile Send private message
pango
Newbie


Joined: 26 Feb 2016
Posts: 6

PostPosted: Wed Mar 23, 2016 3:09 pm    Post subject:
Reply with quote

sorry,I think I should post this topic to "OpenSceneGraph Lists->General" catalogue,I had post a same one under "OpenSceneGraph Lists->General" catalogue,so please delete this one;
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General [3rdparty] All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts osgDB::ImagePaged don't have "Pa... tang General 0 Mon Nov 13, 2017 2:51 am View latest post
No new posts "Memory: Colors" game from ... kornerr General 3 Sat Oct 28, 2017 11:19 am View latest post
No new posts Make ObjOptionsStruct in obj plugin a... Ralf Habacker Submission 1 Fri Oct 20, 2017 8:31 am View latest post
No new posts osgconv "noTriStripPolygons"... KariR General 2 Tue Sep 12, 2017 1:50 pm View latest post
No new posts How to change "field of view&quo... gambr General 3 Mon Aug 07, 2017 1:37 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP