GL EXT texture integer

From OpenGL Wiki
Revision as of 01:55, 24 March 2011 by V-man (talk | contribs) (Nearest filter mode)
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.
http://opengl.org/registry/specs/EXT/texture_integer.txt
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.

These extensions are present on Gf 8 and up and possibly other GPUs.

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

You can use the texture with fixed pipeline if you want but that doesn't really make it useful.
These formats are more useful if you need absolute integer values returned from the sampler for your GLSL code.
GL_EXT_gpu_shader4 defines new samplers such as usampler2D, usampler3D, usamplerCube, usampler2DRect, etc.
Sampling from a fragment shader should work. Not sure if sampling from VS or GS is supported with these formats. Consult the IHVs manuals.
Example:

 //This is a FS
 uniform usampler2D Texture0;
 //------------
 varying vec2 TexCoord0;
 //------------
 void main()
 {
   unsigned int texel = texture2D(Texture0, TexCoord0).x;
   if(texel == 1000)
      gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
   else
      gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
 }