From 2dfb8f333b2e47882125624ead9f62147010cc6e Mon Sep 17 00:00:00 2001 From: Chris Laurel Date: Tue, 6 Mar 2001 21:57:53 +0000 Subject: [PATCH] Introduced support for compressed textures. --- src/render.cpp | 5 ++++- src/render.h | 1 + src/solarsys.cpp | 4 ++++ src/surface.h | 7 ++++--- src/texmanager.cpp | 14 +++++++++---- src/texmanager.h | 2 +- src/texture.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++-- src/texture.h | 8 +++++++- 8 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/render.cpp b/src/render.cpp index 89f846ea..5d3d9a1f 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -856,7 +856,10 @@ void Renderer::renderPlanet(const Body& body, if (surface.baseTexture != "") { if (!textureManager->find(surface.baseTexture, &tex)) - tex = textureManager->load(surface.baseTexture); + { + bool compress = ((surface.appearanceFlags & Surface::CompressBaseTexture) != 0); + tex = textureManager->load(surface.baseTexture, compress); + } } // If this renderer can support bump mapping then get the bump texture diff --git a/src/render.h b/src/render.h index 624bd73b..34d8845b 100644 --- a/src/render.h +++ b/src/render.h @@ -167,6 +167,7 @@ class Renderer int nSimultaneousTextures; bool useRegisterCombiners; bool useCubeMaps; + bool useCompressedTextures; public: friend bool operator<(const Renderer::RenderListEntry&, diff --git a/src/solarsys.cpp b/src/solarsys.cpp index accc1dca..a61e1899 100644 --- a/src/solarsys.cpp +++ b/src/solarsys.cpp @@ -174,12 +174,16 @@ static Body* CreatePlanet(PlanetarySystem* system, bool applyBumpMap = planetData->getString("BumpMap", surface.bumpTexture); bool blendTexture = false; planetData->getBoolean("BlendTexture", blendTexture); + bool compressTexture = false; + planetData->getBoolean("CompressTexture", compressTexture); if (blendTexture) surface.appearanceFlags |= Surface::BlendTexture; if (applyBaseTexture) surface.appearanceFlags |= Surface::ApplyBaseTexture; if (applyBumpMap) surface.appearanceFlags |= Surface::ApplyBumpMap; + if (compressTexture) + surface.appearanceFlags |= Surface::CompressBaseTexture; body->setSurface(surface); string mesh(""); diff --git a/src/surface.h b/src/surface.h index 60e1f0ab..8fa6b059 100644 --- a/src/surface.h +++ b/src/surface.h @@ -23,9 +23,10 @@ class Surface // Appearance flags enum { - BlendTexture = 0x1, - ApplyBaseTexture = 0x2, - ApplyBumpMap = 0x4, + BlendTexture = 0x1, + ApplyBaseTexture = 0x2, + ApplyBumpMap = 0x4, + CompressBaseTexture = 0x8, }; Color color; diff --git a/src/texmanager.cpp b/src/texmanager.cpp index 82c622fa..ce0c6bf9 100644 --- a/src/texmanager.cpp +++ b/src/texmanager.cpp @@ -25,12 +25,18 @@ bool TextureManager::find(const string& name, CTexture** tex) } -CTexture* TextureManager::load(const string& name) +CTexture* TextureManager::load(const string& name, bool compress) { DPRINTF("Loading texture: %s\n", name.c_str()); CTexture* tex = LoadTextureFromFile(baseDir + "\\" + name); + if (tex != NULL) - tex->bindName(true); + { + uint32 texFlags = CTexture::WrapTexture; + if (compress) + texFlags |= CTexture::CompressTexture; + tex->bindName(texFlags); + } addResource(name, (void*) tex); return tex; @@ -43,8 +49,8 @@ CTexture* TextureManager::loadBumpMap(const string& name) CTexture* tex = LoadTextureFromFile(baseDir + "\\" + name); if (tex != NULL) { - tex->normalMap(5.0f, true); - tex->bindName(); + tex->normalMap(2.5f, true); + tex->bindName(CTexture::WrapTexture); } addResource(name, static_cast(tex)); diff --git a/src/texmanager.h b/src/texmanager.h index 460c389f..ada4fca7 100644 --- a/src/texmanager.h +++ b/src/texmanager.h @@ -25,7 +25,7 @@ class TextureManager : public ResourceManager ~TextureManager(); bool find(const std::string& name, CTexture**); - CTexture* load(const std::string& name); + CTexture* load(const std::string& name, bool compress = false); CTexture* loadBumpMap(const std::string& name); }; diff --git a/src/texture.cpp b/src/texture.cpp index a898d159..49961299 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -48,6 +48,15 @@ typedef struct unsigned int colorsImportant; } BMPImageHeader; +static bool initialized = false; +static bool compressionSupported = false; + + +static void initTextureLoader() +{ + compressionSupported = ExtensionSupported("GL_ARB_texture_compression"); + initialized = true; +} CTexture::CTexture(int w, int h, int fmt) : @@ -58,6 +67,10 @@ CTexture::CTexture(int w, int h, int fmt) : cmap = NULL; cmapEntries = 0; + // Yuck . . . + if (!initialized) + initTextureLoader(); + switch (format) { case GL_RGB: @@ -97,8 +110,11 @@ CTexture::~CTexture() } -void CTexture::bindName(bool wrap) +void CTexture::bindName(uint32 flags) { + bool wrap = ((flags & WrapTexture) != 0); + bool compress = ((flags & CompressTexture) != 0) && compressionSupported; + if (pixels == NULL) return; @@ -110,8 +126,37 @@ void CTexture::bindName(bool wrap) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + + int internalFormat = components; + if (compress) + { + switch (format) + { + case GL_RGB: + case GL_BGR_EXT: + internalFormat = GL_COMPRESSED_RGB_ARB; + break; + case GL_RGBA: + internalFormat = GL_COMPRESSED_RGBA_ARB; + break; + case GL_ALPHA: + internalFormat = GL_COMPRESSED_ALPHA_ARB; + break; + case GL_LUMINANCE: + internalFormat = GL_COMPRESSED_LUMINANCE_ARB; + break; + case GL_LUMINANCE_ALPHA: + internalFormat = GL_COMPRESSED_LUMINANCE_ALPHA_ARB; + break; + case GL_INTENSITY: + internalFormat = GL_COMPRESSED_INTENSITY_ARB; + break; + } + glHint((GLenum) GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST); + } + gluBuild2DMipmaps(GL_TEXTURE_2D, - components, + internalFormat, width, height, format, GL_UNSIGNED_BYTE, diff --git a/src/texture.h b/src/texture.h index 0cdffcbf..828569f4 100644 --- a/src/texture.h +++ b/src/texture.h @@ -11,6 +11,7 @@ #define _TEXTURE_H_ #include +#include "basictypes.h" typedef void (*ProceduralTexEval)(float, float, float, unsigned char*); @@ -21,7 +22,12 @@ class CTexture CTexture(int w, int h, int fmt); ~CTexture(); - void bindName(bool wrap = false); + enum { + WrapTexture = 0x1, + CompressTexture = 0x2, + }; + + void bindName(uint32 flags = 0); unsigned int getName(); void normalMap(float scale, bool wrap);