Discussion:
[osg-users] Image containing floats
ivar out
2018-09-20 09:31:45 UTC
Permalink
Hi,

I'm trying to generate my own lookup table containing floats and store it in an image, and apply it to a texture to send it to the shader. I am having issues with getting the image to store floats though. Is there any way to do so, or am I trying something impossible?

cheers,
Ivar

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74953#74953
David Heitbrink
2018-09-20 21:07:49 UTC
Permalink
yes this is doable. You need to set the internal format to something like: GL_RGBA32F

and the source type to float on your texture.

You might look at the OSG forest example, and its use of osg::TextureBuffer. Basically with the forest example, its doing instancing where the transforms are loaded into a texture, and the data is used in the vertex shader.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74954#74954
ivar out
2018-09-24 08:02:21 UTC
Permalink
Thanks David! I got it to work now.

Here my function creating a 2D texture from a 2-Dimensional vector, in case it's of any use to anyone.


Code:

osg::Texture2D* createTextureLUT2D(std::vector<std::vector<float> >& data)
{
int rows = data.size();
int cols = data[0].size();

//create image containing one channel of float, we use the RED channel
osg::Image* image = new osg::Image;
image->allocateImage(cols, rows, 1, GL_RED, GL_FLOAT);
image->setInternalTextureFormat(GL_R32F);

//set the data to the image; the image stores successive columns rather than succesive rows
float* dataPtr = (float*)image->data();
for (int b = 0; b < cols; b++)
for (int a = 0; a < rows; a++)
*dataPtr++ = data[a][b];

//create the texture and set its image
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(image);
texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);

return texture.release();
}




cheers, Ivar

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74963#74963

Loading...