Load Dds
Load Dds
Because a lot of crappy, weird DDS file loader files were found online. The
resources are actually VERY VERY limited.
Written in C, can very easily port to C++ through casting mallocs (ensure your
imports are correct), goto can be replaced.
https://www.gamedev.net/forums/topic/637377-loading-dds-textures-in-opengl-black-
texture-showing/
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
^ Two examples of terrible code
https://gist.github.com/Hydroque/d1a8a46853dea160dc86aa48618be6f9
^ My first look and clean up 'get it working'
https://ideone.com/WoGThC
^ Improvement details
File Structure:
Section Length
///////////////////
FILECODE 4
HEADER 124
HEADER_DX10* 20 (https://msdn.microsoft.com/en-us/library/bb943983(v=vs.85).aspx)
PIXELS fseek(f, 0, SEEK_END); (ftell(f) - 128) - (fourCC == "DX10" ? 17 or
20 : 0)
* the link tells you that this section isn't written unless its a DX10 file
unsigned int w;
unsigned int h;
GLuint tid = 0;
// open the DDS file for binary reading and get file size
FILE* f;
if((f = fopen(path, "rb")) == 0)
return 0;
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
// allocate new unsigned char space with 4 (file code) + 124 (header size) bytes
// read in 128 bytes from the file
header = malloc(128);
fread(header, 1, 128, f);
// extract height, width, and amount of mipmaps - yes it is stored height then
width
height = (header[12]) | (header[13] << 8) | (header[14] << 16) | (header[15]
<< 24);
width = (header[16]) | (header[17] << 8) | (header[18] << 16) | (header[19]
<< 24);
mipMapCount = (header[28]) | (header[29] << 8) | (header[30] << 16) |
(header[31] << 24);
// figure out what format to use for what fourCC file type it is
// block size is about physical chunk storage of compressed data in file
(important)
if(header[84] == 'D') {
switch(header[87]) {
case '1': // DXT1
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
blockSize = 8;
break;
case '3': // DXT3
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
blockSize = 16;
break;
case '5': // DXT5
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
blockSize = 16;
break;
case '0': // DX10
// unsupported, else will error
// as it adds sizeof(struct DDS_HEADER_DXT10) between
pixels
// so, buffer = malloc((file_size - 128) - sizeof(struct
DDS_HEADER_DXT10));
default: goto exit;
}
} else // BC4U/BC4S/ATI2/BC55/R8G8_B8G8/G8R8_G8B8/UYVY-packed/YUY2-packed
unsupported
goto exit;
// easy macro to get out quick and uniform (minus like 15 lines of bulk)
exit:
free(buffer);
free(header);
fclose(f);
return tid;
}