[osg-users] Setting a monochrome 2d texture from byte array
Steve Hardy
osgforum at tevs.eu
Mon Nov 4 16:06:54 PST 2019
Hi,
I am trying to render a monochrome camera image to a 2d texture, but without success unless I first convert it to BGR.
After reading
http://forum.openscenegraph.org/viewtopic.php?t=16236
I got closer, but still no cigar as the result is a boring uniform black. Here is some code:
Code:
void VSGeode::setImageParameters(bool linear)
{
if (getNumDrawables() < 1)
throw OSGWrapException("No drawables");
quad = dynamic_cast<osg::Geometry*>(getDrawable(0));
if (!quad)
throw OSGWrapException("No geometry");
osg::StateSet* state = quad->getStateSet();
if (!state)
throw OSGWrapException("No state");
tex = state->getTextureAttribute(0, osg::StateAttribute::TEXTURE)->asTexture();
if (!tex)
throw OSGWrapException("No texture");
tex->setFilter(osg::Texture::MIN_FILTER,linear ? osg::Texture::LINEAR : osg::Texture::NEAREST);
tex->setFilter(osg::Texture::MAG_FILTER,linear ? osg::Texture::LINEAR : osg::Texture::NEAREST); // NEAREST for pixelated look, LINEAR for smooth.
img = tex->getImage(0);
}
void VSGeode::setImageBGR(int w, int h, unsigned char * data)
{
img->setImage(w, h, 1, 3, GL_BGR, GL_UNSIGNED_BYTE, data, osg::Image::USE_MALLOC_FREE);
quad->dirtyDisplayList();
}
void VSGeode::setImageMono8(int w, int h, unsigned char * data)
{
img->setImage(w, h, 1, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, data, osg::Image::USE_MALLOC_FREE); // black
quad->dirtyDisplayList();
}
SetImageBGR() works fine, but as mentioned, setImageMono8() does not.
What happens is that the image comes in from a hi-res GigE camera, and it is a monochrome byte array. Because this is already fairly CPU intensive, I wish to avoid another copy to expand into a BGR array. But for the life of me I can't work out a magic combo of parameters that actually works. The data really is there, since I can fake it and just call setImageBGR() and it comes up with something, albeit like a Degas painting.
So what is the most efficient way of getting a grey-scale image in a texture, from a 2D byte array in memory?
Thank you!
Cheers,
Steve
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=76871#76871
More information about the osg-users
mailing list