Introduction

Textures are usually a heavily used resource in a game, and theres alot of them. In this tutorial, we will compress the texture data to obtain maximum of 8:1 compression ratio.

Compression Schemes

There are a few compression schemes, the first one was created by S3, and the extension is hence called S3TC (S3 Texture Compression). It comes in a few flavours depending on the compression type and what the data is. Im going to concentrate on DXT1, DXT1A, DXT3 and DXT5 and all 4 are lossy and not suitable for normal maps (3Dc is used for normal maps).

  • DXT1 is the format that obtains the 8:1 compression ratio. It is RGB only and stores 16 input pixels into 64-bits of output consisting of two 16-bit RGB 5:6:5 color values and a 4×4 two bit lookup table.
  • DXT1A is the same format as DXT1, but with 1bit indicating either full transperancy, or full opacity.
  • DXT3 is RGBA and it stores 16 input pixels in 128bits, those 128bits are split in half, 64bits goes towards the alpha channel and the other 64bits going to the colour data. It is encoded the same way as DXT1, but obtains a 4:1 compresssion raio. DXT3 works best if there is sharp transitions in the alpha channel.
  • DXT5 uses a different compression scheme than the above, but the result is the same as DXT3 with the exception that its best suited for gradual transitions in the alpha channel.

Compressing the textures

You have two choices with the textures, you can either load the data pre-compressed and tell the GL that the data we're sending through is already compressed (like DDS), or we can let the GL compress the textures for us; I'll concentrate on the latter as the former is out of the scope of this tutorial.

Constants

There are 6 GLEnums that are important, and they come from 2 extensions; EXT_Texture_Compression_S3TC and ARB_Texture_Compression:

  1. GL_COMPRESSED_RGB_S3TC_DXT1_EXT
  2. GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  3. GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
  4. GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
  5. GL_COMPRESSED_RGB_ARB
  6. GL_COMPRESSED_RGBA_ARB

The latter two allow the GL to decide which method of compression is best suited, but the S3TC compression schemes have been very well adopted, and theres no reason to specify them specifically if you wanted to (providing the extensions are available).

Using the constants

The above constants are used as the arguments in the <internalformat> parameter of glTexImage2D, like so:

GL11.glTexImage2D(GL_TEXTURE_2D, 0, COMPRESSION_CONSTANT, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, myByteBuffer);

The above call specifies that you would like to compress mipmap level 0 of the currently bound texture using the COMPRESSION_CONSTANT compression scheme (replace it with any of the above constants).

Conclusion

Texture compression allows you to fit up to 8 times more textures in the same space, and thats a big plus whichever way you look at it. This one was easy wasn't?

 
lwjgl/tutorials/opengl/texturecompression.txt · Last modified: 2007/07/07 00:38 (external edit)
 
Recent changes RSS feed Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki