The Basics


Every texture is basically an image. The image may consist of 1D, 2D or, in more advanced rendering programs, 3D textures. The size of the textured image must be in the power of two as required by OpenGL, with the possibility of one or two pixels around its edges for color definition of polygons that are outside the texture image. A textured image is sometimes referred to as simply a "texture", of which the individual elements (a color from a texture applied to a pixel fragment in the frame buffer) that make up this "texture" is known as "texels" (which is short for texture elements).

A 1D texture has either width or height but not both. Such a 1D texture is only a pixel wide or high. An image with multiple 1D texture is basically a line of pixels. 1D textures are very simple (and quick) to render. There is a single OpenGL function for defining 1D textures, glTexImage1D, with 8 arguments for specification purposes :

void glTexImage1D(GLenum target, GLint level, GLint components, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
  • GLint border and GLsizei width

  • These secifies the actual size of the texture image. The border parameter as well as value controls the number of border pixels and can be a value of 0, 1 or 2.
  • GLenum target

  • This specifies which texture is to be defined and there is only one argument for 1D texture images - it must be GL_TEXTURE_1D.
  • GLint level

  • This specifies the level of detail of texture images, usually 0 but can be a different value for mipmapped textures.
  • GLint components

  • This specifies the number of color values to be used for each and every pixel. For RGB as well as RGBA texture images, the values of 3 and 4 are used respectively, while a value of 1 is what is used for color index textures.
  • GLenum format

  • This specifies the type of color values to expect. The color values are :

    GL_COLOR_INDEX - pixel values are color indices
    GL_LUMINANCE - pixel values are greyscale colors
    GL_LUMINANCE_ALPHA - pixel values are alpha and greyscale colors
    GL_RED - pixel values are red intensities
    GL_GREEN - pixel values are green intensities
    GL_BLUE - pixel values are blue intensities
    GL_ALPHA - pixel values are alpha intensities
    GL_RGB - pixel values are RGB colors; and
    GL_RGBA - pixel values are RGBA colors.

    We can also have red and blue component swapping in Windows and SGI OpenGL implementations, which are GL_BGR_EXT and GL_BGRA_EXT (in OpenGL 1.2, these are standard formats of GL_BGR and GL_BGRA).

    2D textures has both width as well as height and are more than a pixel wide or high (such as 256x256). They can be loaded from a Windows Bitmap (.BMP) file as well as other file types such as Zsoft Paintbrush (.PCX) or Truevision Targa (.TGA). A structure such as a building that require complex surface geometry can be very expensive in terms of processing time – instead, we can now use simpler geometry but achieve a higher degree of realism by using 2D textures in such a case. Other uses for 2D textures can be for things such as clouds. For the definition of 2D texture images, glTexImage2D is called. In addition to all the 1D texture image arguments above, an additional height argument (GLsizei height) is used and the value must be in the power of two.

    3D textures, also known as volume textures, require support by the hardware. These are used generally for scanning programs (like MRIs). The most important drawback of having 3D textures is the amount of memory 3D textures can take up – a simple 256x256x256 luminance-alpha 3D texture image rendered at 640x480 at 32bits with double buffering (where image to be displayed is assembled in memory then placed on the screen in a single update operation, rather than building the image primitive-by-primitive on the screen; primitives are 2D polygonal shapes) plus a z-buffer will gobble up 20MB [math : 256x256x256 * 1 byte (alpha=1 byte) + framebuffer @ 640x480 * 4 bytes (32bit=4 bytes) * 3 (double buffer + z-buffer = 3) = approx 20MB].