Difference between revisions of "Array Texture"

From OpenGL.org
Jump to: navigation, search
(de-pluralizing, per Wikipedia standard.)
m (moved Array Textures to Array Texture: de-pluralizing, per Wikipedia standard.)
(No difference)

Revision as of 16:36, 16 March 2010

An Array Texture is a Texture that contain a sequence of 1D or 2D textures of the same size. Array textures are similar to 3D Textures in that they have an effective "depth" component. However, filtering never takes place between the separate textures in the array.

Array texture are not usable from the fixed function pipeline. To use them, you have to use an assembly or GLSL shader. Therefore it is invalid to call:

glEnable(GL_TEXTURE_2D_ARRAY);

or

glDisable(GL_TEXTURE_2D_ARRAY);

and will result in the OpenGL error flag being set to GL_INVALID_ENUM.

To bind and initialize a texture object as an array texture in a typical case, you should write:


GLuint texture = 0;

//Set this to the preferred texture width.

GLuint width = 2;

//Set this to the preferred texture height.

GLuint height = 2;

//Set this to the preferred texture slice count.

GLuint sliceCount = 2;

//Read you texels here. In the current example, we have 2*2*2*4=32 texels.

GLfloat texels[32] = 

{

     //Texels for first image. 

     0.0,0.0,0.0,1.0,

     1.0,0.0,0.0,1.0,

     0.0,1.0,0.0,1.0,

     0.0,0.0,1.0,1.0,

     //Texels for second image.

     1.0,1.0,1.0,1.0,

     1.0,1.0,0.0,1.0,

     0.0,1.0,1.0,1.0,

     1.0,0.0,1.0,1.0

};

glGenTextures(1,&texture);

glBindTexture(GL_TEXTURE_2D_ARRAY,texture);

glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);

glTexImage3D(GL_TEXTURE_2D_ARRAY,0,GL_RBGA,width,height,sliceCount,0,GL_RGBA,GL_FLOAT,texels);

To declare a GLSL sampler for texture arrays:

uniform sampler2DArray textureName;

to read from the sampler, write:

vec4 color = texture2DArray(textureName,texCoord.xyz);

where texCoord.xy contains s,t for the 2D image texture coordinates and texCoord.z contains the array index of the texture. s and t are in the range of [0..1] (floating point), array index is in the range of [0..number of texture array slices-1] (floating point rounded to nearest integer).