Tutorial no.2 - Texturecompression
Introduction
As you might know, S3 introduced a texturecompression method with it's older chipsets about 6 years ago.Although S3 doesn't exists any more (it's now part of VIA), the texture compression method is supportet by OpenGL and Direct3D and at this time is also supported by every gfx-chipset.
This texture compression is implemented under OpenGL with extensions ARB_texture_compression and EXT_texture_compression_s3tc and there are four different internal formats :

S3TC internal format Base internal format Compression ratio
GL_COMPRESSED_RGB_S3TC_DXT1_EXT RGB 6:1
GL_COMPRESSED_RGBA_S3TC_DXT1_EXT RGBA 8:1
GL_COMPRESSED_RGBA_S3TC_DXT3_EXT RGBA 4:1
GL_COMPRESSED_RGBA_S3TC_DXT5_EXT RGBA 4:1

As you can see, the the compression is fixed and depends on the chosen format.So if you use S3TC_DXT1, you'll waste less memory than with S3TC_DXT3, but your image will loke worse.


RGBA (uncompressed)

S3TC_DXT1 (8:1)

S3TC_DTX3 (4:1)

If you take a closer look at the two different images above, you'll notice that the texture compression format S3TC_DXT1 isn't well suited for images like the above one.You can clearly see the quality has suffered a lot as the sky doesn't look smooth any more.
But when you look at the egypt texture beneath you won't notice any quality differences between the compressed and uncompressed images.
So you'll have to decide for you yourself if you compress a texture or not.In my engine, I store my skyboxtextures in a different array as the walltextures, and I only compress the last ones, so that the skybox still looks very sharp.But if you really need to compress those ones to, then take the S3TC_DXT3 format as the qualityloss isn't that big.

Using the compressed textures
And now to the implementation of texture compression into your app.I use an altered version of the skybox program from the first tutorial :


procedure TForm1.LoadSkyBox(pComponents : Cardinal);
const
 SkyBoxName : array[0..5] of String = ('BK', 'A1', 'DN', 'UP', 'LF', 'RT');
var
 i   : Integer;
begin
ActivateRenderingContext(FDC, FRC);
for i := 0 to High(SkyBoxTexture) do
 begin
 if SkyBoxTexture[i] = nil then
  begin
  SkyBoxTexture[i] := TGLBmp.Create;
  SkyBoxTexture[i].LoadImage(SkyBoxName[i]+'.jpg');
  end;
 with SkyBoxTexture[i] do
  begin
   begin
   if TextureID > 0 then
    glDeleteTextures(1, @TextureID);
   glGenTextures(1, @TextureID);
   glBindTexture(GL_TEXTURE_2D, TextureID);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   GL_LINEAR_MIPMAP_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   gluBuild2DMipmaps(GL_TEXTURE_2D, pComponents, GetWidth, GetHeight,
                     GL_RGBA,GL_UNSIGNED_BYTE, GetData);
   end;
  end;
 end;
GenerateSkyBox(48, 48, 48);
DeActivateRenderingContext;
end;

As in the first tutorial we'll load the textures of our skybox in a loop to make things a bit easier.But now we don't use the texture generation methods of the TGLBmp class, as they aren't flexible enough for our needs.Instead we generate the textures ourself with OpenGL commands.
As we want to change our texture format during runtime, we only create and load the TGLBmp the first time this procedure gets called.After that we delete our texture if it already exists in memory (this happens always after the second call), get space for a new texture object and bind it.
Now we set our minification and magnification filter and set texture wrapping to GL_CLAMP for both coordinates to have seamless borders again.
After that we generate our mipmaps with the format given in the parameter pComponents.This parameter is changed via the buttons in the sample app.If you want your textures compressed in the S3TC_DXT1 format then you set pComoponents to GL_COMPRESSED_RGBA_S3TC_DXT1.
That's all for loading your textures compressed.The rest of the program is nothing else than a simple spinning cube.

The sample application
The app renders a simple cube.You can change the texture compression modes on the fly to see the difference.RGBA means no texture compression.


Download

Download the texture compression demo including the sourcecode