Rômulo Cerqueira
Rômulo Cerqueira

Reputation: 189

Dynamic arrays as texture GLSL

I have worked with C++/OpenSceneGraph/GLSL integration and I need to handle dynamic array on shader.

My dynamic data array of vec3 was converted into 1D-texture to pass as uniform to fragment (I'm using GLSL 1.3), as follows:

osg::ref_ptr<osg::Image> image = new osg::Image; 
image->setImage(myVec3Array->size(), 1, 1, GL_RGBA8, GL_RGBA, GL_FLOAT, (unsigned char*) &myVec3Array[0], osg::Image::NO_DELETE);

// Pass the texture to GLSL as uniform
osg::StateSet* ss = scene->getOrCreateStateSet();
ss->addUniform( new osg::Uniform("vertexMap", texture) ); 

For now, I would like to retrieve my raw array of vec3 on fragment shader. How can I do this process? Does a texture2D function only return normalized values?

Upvotes: 0

Views: 554

Answers (1)

derhass
derhass

Reputation: 45322

Does a texture2D function only return normalized values?

No. It returns values depedning on the internal format of the texture.

image->setImage(myVec3Array->size(), 1, 1, GL_RGBA8, GL_RGBA, GL_FLOAT, (unsigned char*) &myVec3Array[0], osg::Image::NO_DELETE);
                                           ^^^^^^^^

GL_RGBA8 is an unsigned normalized integer format ("UNORM" for short). So the values in the texture are unsigned integers with 8 bit per channel, and [0,255] is mapped to [0,1] when sampling the texture.

If you want unnormalized floats, you must use some appropriate format, like GL_RGBA32F.

Upvotes: 1

Related Questions