PDA

View Full Version : How to get integer textures?



sc20184
07-23-2017, 08:46 PM
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!

Silence
07-23-2017, 10:50 PM
First, GL_LUMINANCE has been deprecated in GL 3. Use GL_RED instead.

For your question, this thread (https://www.opengl.org/discussion_boards/showthread.php/164724-using-unsigned-byte-in-Shader) might help you.

john_connor
07-24-2017, 01:51 AM
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 (https://www.khronos.org/opengl/wiki/Sampler_(GLSL)#Sampler_types):
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 */

sc20184
07-24-2017, 05:54 AM
First, GL_LUMINANCE has been deprecated in GL 3. Use GL_RED instead.

For your question, this thread (https://www.opengl.org/discussion_boards/showthread.php/164724-using-unsigned-byte-in-Shader) might help you.

Thanks a lot!

sc20184
07-24-2017, 06:00 AM
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 (https://www.khronos.org/opengl/wiki/Sampler_(GLSL)#Sampler_types):
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 */

Thank you very much!

It WORKS!