GL EXT texture integer

From OpenGL Wiki
Jump to: navigation, search

GL_EXT_texture_integer adds some additional internal formats. It is core in GL 3.0, so you can drop the postfix if GL 3.0 is supported.

This extension requires http://opengl.org/registry/specs/EXT/gpu_shader4.txt when you want to sample the texture from GLSL.

This extension is also core in GL 3.0.

Create The Texture

Here we are creating a 2D texture with GL_LUMINANCE16UI_EXT internal format. We won't be using mipmapping so that the values will be perfect. We will use NEAREST as the texture filtering mode.

 glGenTextures(1, &textureID);
 glBindTexture(GL_TEXTURE_2D, textureID);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16UI_EXT, width, height, 0, GL_LUMINANCE_INTEGER_EXT, GL_UNSIGNED_SHORT, pixels);

For cleanup

 glDeleteTextures(1, &textureID);

Sampling The Texture From GLSL

GL_EXT_gpu_shader4 defines new samplers such as usampler2D, usampler3D, usamplerCube, usampler2DRect, isampler2D, isampler3D, isamplerCube, isampler2DRect, etc.

For example, usampler2D would be for sampling an unsigned integer texture such as a GL_LUMINANCE16UI_EXT or GL_LUMINANCE32UI_EXT.

For example, isampler2D would be for sampling an signed integer texture such as a GL_LUMINANCE16I_EXT or GL_LUMINANCE32I_EXT.

Sampling from a fragment shader should work. Not sure if sampling from VS or GS is supported with these formats. Consult the IHVs manuals.

We will need GLSL 1.30.

Example:

  //VERTEX SHADER
  #version 130

  in vec3 InVertex;
  in vec2 InTexCoord0;

  uniform mat4 ProjectionModelviewMatrix;

  smooth out vec2 TexCoord0;

  void main()
  {
    gl_Position = ProjectionModelviewMatrix * vec4(InVertex, 1.0);
    TexCoord0 = InTexCoord0;
  }

Notice that the above is a GLSL 1.30 vertex shader, therefore we don't use varying. We use out.

Also, we are using the default interpolater mode : smooth.

  //FRAGMENT SHADER
  #version 130

  #extension GL_EXT_gpu_shader4 : enable    //Include support for this extension, which defines usampler2D

  uniform usampler2D Texture0;

  smooth in vec2 TexCoord0;

  void main()
  {
    uint texel = texture2D(Texture0, TexCoord0).x;
    if(texel == 1000U)
       FragColor = vec4(0.0, 0.0, 1.0, 1.0);
    else
       FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  }

The fragment shader defines its own output variable. Here we named it FragColor. This is required for GLSL 1.30.