[osg-users] Setting a monochrome 2d texture from byte array

Glenn Waldron gwaldron at gmail.com
Tue Nov 5 04:47:12 PST 2019


Sandy,
Try this instead. You probably want normalized [0..1] data on the GPU, not
[0..255] integers.

 img->setImage(w, h, 1, GL_R8, GL_RED, GL_UNSIGNED_BYTE, data,
osg::Image::USE_MALLOC_FREE);

Glenn Waldron / osgEarth


On Tue, Nov 5, 2019 at 2:14 AM Steve Hardy <osgforum at tevs.eu> wrote:

> 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
>
>
>
>
>
> _______________________________________________
> 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/20191105/0cff3cb6/attachment.html>


More information about the osg-users mailing list