I used glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, iWidth, iHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pData) sending the texture data to the graphic card,
and in FS, wrote “FragColor = texture(texture1, TexCoord);” to get the texture color. The texture color is normalized to [0,1].
My problem is: how to get the original integer texture color value, like [0,255]?
Due to my texture data could be GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT or GL_UNSIGNED_INT, and I want to write some code below in FS:
int iValue = texture(texture1, TexCoord);
if(iValue == 100)
{
…
}
I found some one says that there must be #extension GL_EXT_gpu_shader4 : enable FS, and use GL_LUMINANCE_INTEGER_EXT parameter in the “glTexImage2D” function.
However, it doesn’t work well.
Is there something wrong? Please help me, thank you very much!
First, GL_LUMINANCE has been deprecated in GL 3. Use GL_RED instead.
For your question, this thread might help you.
first you create a “integer texture” with the correct internal format:
vector<unsigned int> texturedata(width * height, 12345);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, texturedata.data());
select GL_NEAREST as texture parameter
then you need a suitable sampler variable:
uniform usampler2D myuinttexture; /* note the "u" indicating unsigned integer type */
the glsl “texture(…)” function fetches a 4 components, even if your texture has only 1, so:
uint myuintvalue = texture(myuinttexture, texcoord).r; /* to fetch only the first component */
1 Like
[QUOTE=Silence;1287961]First, GL_LUMINANCE has been deprecated in GL 3. Use GL_RED instead.
For your question, this thread might help you.[/QUOTE]
Thanks a lot!
[QUOTE=john_connor;1287964]first you create a “integer texture” with the correct internal format:
vector<unsigned int> texturedata(width * height, 12345);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, texturedata.data());
select GL_NEAREST as texture parameter
then you need a suitable sampler variable:
uniform usampler2D myuinttexture; /* note the "u" indicating unsigned integer type */
the glsl “texture(…)” function fetches a 4 components, even if your texture has only 1, so:
uint myuintvalue = texture(myuinttexture, texcoord).r; /* to fetch only the first component */
[/QUOTE]
Thank you very much!
It WORKS!