diff --git a/src/osg/Image.cpp b/src/osg/Image.cpp index b79137037..6602319cc 100644 --- a/src/osg/Image.cpp +++ b/src/osg/Image.cpp @@ -1251,6 +1251,15 @@ bool Image::isImageTranslucent() const offset = 3; delta = 4; break; + case(GL_RGB): + return false; + case(GL_BGR): + return false; + case(GL_COMPRESSED_RGB_S3TC_DXT1_EXT): + case(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT): + case(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT): + case(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT): + return dxtc_tool::CompressedImageTranslucent(_s, _t, _pixelFormat, _data); default: return false; } diff --git a/src/osg/dxtctool.cpp b/src/osg/dxtctool.cpp index ac011b434..ac333dd94 100644 --- a/src/osg/dxtctool.cpp +++ b/src/osg/dxtctool.cpp @@ -154,7 +154,70 @@ void dxtc_pixels::VFlip_DXT5() const } } +// +// Structure of a DXT-1 compressed texture block +// see page "Opaque and 1-Bit Alpha Textures (Direct3D 9)" on http://msdn.microsoft.com +// url at time of writing http://msdn.microsoft.com/en-us/library/bb147243(v=VS.85).aspx +// +struct DXT1TexelsBlock +{ + unsigned short color_0; // colors at their + unsigned short color_1; // extreme + unsigned int texels4x4; // interpolated colors (2 bits per texel) +}; +bool CompressedImageTranslucent(size_t width, size_t height, GLenum format, void * imageData) +{ + // OSG_NOTICE<<"CompressedImageTranslucent("<(imageData); + + // Only do the check on the first mipmap level, and stop when we + // see the first alpha texel + int i = (width*height)/16; + bool foundAlpha = false; + while ((!foundAlpha) && (i>0)) + { + // See if this block might contain transparent texels + if (texelsBlock->color_0<=texelsBlock->color_1) + { + // Scan the texels block for the '11' bit pattern that + // indicates a transparent texel + int j = 0; + while ((!foundAlpha) && (j < 32)) + { + // Check for the '11' bit pattern on this texel + if ( ((texelsBlock->texels4x4 >> j) & 0x03) == 0x03) + { + // Texture is using the 1-bit alpha encoding, so we + return true; + } + + // Next texel + j += 2; + } + } + + // Next block + --i; + ++texelsBlock; + } + return false; + } + case(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT): + return true; + case(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT): + return true; + default: + break; + } + + return false; +} } // namespace dxtc_tool diff --git a/src/osg/dxtctool.h b/src/osg/dxtctool.h index d421db6e2..9251064ac 100644 --- a/src/osg/dxtctool.h +++ b/src/osg/dxtctool.h @@ -79,6 +79,7 @@ namespace dxtc_tool { // C-like function wrappers bool VerticalFlip(size_t Width, size_t Height, GLenum Format, void * pPixels); +bool CompressedImageTranslucent(size_t Width, size_t Height, GLenum Format, void * pPixels); // Class holding reference to DXTC image pixels