Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: How to get integer textures?

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5

    How to get integer textures?

    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!

  2. #2
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    429
    First, GL_LUMINANCE has been deprecated in GL 3. Use GL_RED instead.

    For your question, this thread might help you.

  3. #3
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    443
    first you create a "integer texture" with the correct internal format:

    Code cpp:
    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:
    Code glsl:
    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:
    Code glsl:
    uint myuintvalue = texture(myuinttexture, texcoord).r; /* to fetch only the first component */

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5
    Quote Originally Posted by Silence View Post
    First, GL_LUMINANCE has been deprecated in GL 3. Use GL_RED instead.

    For your question, this thread might help you.
    Thanks a lot!

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5
    Quote Originally Posted by john_connor View Post
    first you create a "integer texture" with the correct internal format:

    Code cpp:
    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:
    Code glsl:
    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:
    Code glsl:
    uint myuintvalue = texture(myuinttexture, texcoord).r; /* to fetch only the first component */
    Thank you very much!

    It WORKS!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •