Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I remember messing with the DDS loader in glew, I don't think the header information there is correct. I never could get it to work correctly.</p> <p>The best way would be to use the header contstucts that are in DDraw.h here's what I was able to use for DXT1,3,and 5, which if I remember correctly are the only ones that can work in OpenGL.</p> <pre><code> struct DDS_IMAGE_DATA { GLsizei width; GLsizei height; GLint components; GLenum format; int numMipMaps; GLubyte *pixels; }; DDS_IMAGE_DATA* CImage::loadDDSTextureFile( const char *filename ) { DDS_IMAGE_DATA *pDDSImageData; DDSURFACEDESC2 ddsd; char filecode[4]; FILE *pFile; int factor; int bufferSize; // Open the file pFile = fopen( filename, "rb" ); if( pFile == NULL ) { #if DEBUG char str[255]; printf( str, "loadDDSTextureFile couldn't find, or failed to load \"%s\"", filename ); #endif return NULL; } // Verify the file is a true .dds file fread( filecode, 1, 4, pFile ); if( strncmp( filecode, "DDS ", 4 ) != 0 ) { #if DEBUG char str[255]; printf( str, "The file \"%s\" doesn't appear to be a valid .dds file!", filename ); #endif fclose( pFile ); return NULL; } // Get the surface descriptor fread( &amp;ddsd, sizeof(ddsd), 1, pFile ); pDDSImageData = (DDS_IMAGE_DATA*) malloc(sizeof(DDS_IMAGE_DATA)); memset( pDDSImageData, 0, sizeof(DDS_IMAGE_DATA) ); // // This .dds loader supports the loading of compressed formats DXT1, DXT3 // and DXT5. // switch( ddsd.ddpfPixelFormat.dwFourCC ) { case FOURCC_DXT1: // DXT1's compression ratio is 8:1 pDDSImageData-&gt;format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; factor = 2; break; case FOURCC_DXT3: // DXT3's compression ratio is 4:1 pDDSImageData-&gt;format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; factor = 4; break; case FOURCC_DXT5: // DXT5's compression ratio is 4:1 pDDSImageData-&gt;format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; factor = 4; break; default: #if DEBUG char str[255]; printf( str, "The file \"%s\" doesn't appear to be compressed " "using DXT1, DXT3, or DXT5!", filename ); #endif return NULL; } // // How big will the buffer need to be to load all of the pixel data // including mip-maps? // if( ddsd.dwLinearSize == 0 ) { #if DEBUG printf("dwLinearSize is 0!"); #endif } if( ddsd.dwMipMapCount &gt; 1 ) bufferSize = ddsd.dwLinearSize * factor; else bufferSize = ddsd.dwLinearSize; pDDSImageData-&gt;pixels = (unsigned char*)malloc(bufferSize * sizeof(unsigned char)); fread( pDDSImageData-&gt;pixels, 1, bufferSize, pFile ); // Close the file fclose( pFile ); pDDSImageData-&gt;width = ddsd.dwWidth; pDDSImageData-&gt;height = ddsd.dwHeight; pDDSImageData-&gt;numMipMaps = ddsd.dwMipMapCount; if( ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1 ) pDDSImageData-&gt;components = 3; else pDDSImageData-&gt;components = 4; return pDDSImageData; } void CImage::loadDDS(const char * szFilename, tTexture &amp; texData) { DDS_IMAGE_DATA *pDDSImageData = loadDDSTextureFile(szFilename); if( pDDSImageData != NULL ) { texData.m_nHeight = pDDSImageData-&gt;height; texData.m_nWidth = pDDSImageData-&gt;width; texData.m_nHeight = pDDSImageData-&gt;height; texData.m_eFormat = pDDSImageData-&gt;format; int nHeight = pDDSImageData-&gt;height; int nWidth = pDDSImageData-&gt;width; int nNumMipMaps = pDDSImageData-&gt;numMipMaps; int nBlockSize; if( pDDSImageData-&gt;format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ) nBlockSize = 8; else nBlockSize = 16; //glGenTextures( 1, &amp;g_compressedTextureID ); //glBindTexture( GL_TEXTURE_2D, g_compressedTextureID ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); int nSize; int nOffset = 0; // Load the mip-map levels for( int i = 0; i &lt; nNumMipMaps; ++i ) { if( nWidth == 0 ) nWidth = 1; if( nHeight == 0 ) nHeight = 1; nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize; glCompressedTexImage2DARB( GL_TEXTURE_2D, i, pDDSImageData-&gt;format, nWidth, nHeight, 0, nSize, pDDSImageData-&gt;pixels + nOffset ); nOffset += nSize; // Half the image size for the next mip-map level... nWidth = (nWidth / 2); nHeight = (nHeight / 2); } } if( pDDSImageData != NULL ) { if( pDDSImageData-&gt;pixels != NULL ) free( pDDSImageData-&gt;pixels ); free( pDDSImageData ); } } </code></pre> <p>This particular bit of code makes a few assumptions of the DDS file we are trying to load, first that is is a compressed file, either DXT1,3, or 5, and that the DDS file has pre-generated mipmaps saved in it, hence we don't have to generate them ourselves.</p> <p>I hope this was able to help you it took me some time to get it working correctly myself. If there's anything in this code snippet that seems unclear, let me know and I'll help you further.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload