Sign in to follow this  
qbic

OpenGL .DDS Fileformat

Recommended Posts

I'm trying to write a img->.dds file converter using SDL and OpenGL. (Could't find anyone for linux) I've been revese engereered it until now sence i only can find text's about how o load them. I simply wonder if anywony can points me to a .dds reference or something. I esepecially wanna how the header looks like, couse i think i've gotten the hang of how the compression is made (compressing it using S3TC extension.

Share this post


Link to post
Share on other sites
you get the sourcecode for a .dds-reader with nvidias Cg toolkit IIRC.

edit: yep, just checked... check out examples\runtime_CgFX\nv_dds.c/h in the root of the Cg install.

Share this post


Link to post
Share on other sites
Headers :

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// DirectDraw's structures

//
//
//
struct DDCOLORKEY
{
DWORD dwColorSpaceLowValue; // low boundary of color space that is to
// be treated as Color Key, inclusive
DWORD dwColorSpaceHighValue; // high boundary of color space that is
// to be treated as Color Key, inclusive
};



//
//DDPIXELFORMAT
//
struct DDPIXELFORMAT
{
DWORD dwSize; // size of structure
DWORD dwFlags; // pixel format flags
DWORD dwFourCC; // (FOURCC code)
union
{
DWORD dwRGBBitCount; // how many bits per pixel
DWORD dwYUVBitCount; // how many bits per pixel
DWORD dwZBufferBitDepth; // how many total bits/pixel in z buffer (including any stencil bits)
DWORD dwAlphaBitDepth; // how many bits for alpha channels
DWORD dwLuminanceBitCount; // how many bits per pixel
DWORD dwBumpBitCount; // how many bits per "buxel", total
DWORD dwPrivateFormatBitCount;// Bits per pixel of private driver formats. Only valid in texture
// format list and if DDPF_D3DFORMAT is set
};
union
{
DWORD dwRBitMask; // mask for red bit
DWORD dwYBitMask; // mask for Y bits
DWORD dwStencilBitDepth; // how many stencil bits (note: dwZBufferBitDepth-dwStencilBitDepth is total Z-only bits)
DWORD dwLuminanceBitMask; // mask for luminance bits
DWORD dwBumpDuBitMask; // mask for bump map U delta bits
DWORD dwOperations; // DDPF_D3DFORMAT Operations
};
union
{
DWORD dwGBitMask; // mask for green bits
DWORD dwUBitMask; // mask for U bits
DWORD dwZBitMask; // mask for Z bits
DWORD dwBumpDvBitMask; // mask for bump map V delta bits
struct
{
WORD wFlipMSTypes; // Multisample methods supported via flip for this D3DFORMAT
WORD wBltMSTypes; // Multisample methods supported via blt for this D3DFORMAT
} MultiSampleCaps;

};
union
{
DWORD dwBBitMask; // mask for blue bits
DWORD dwVBitMask; // mask for V bits
DWORD dwStencilBitMask; // mask for stencil bits
DWORD dwBumpLuminanceBitMask; // mask for luminance in bump map
};
union
{
DWORD dwRGBAlphaBitMask; // mask for alpha channel
DWORD dwYUVAlphaBitMask; // mask for alpha channel
DWORD dwLuminanceAlphaBitMask;// mask for alpha channel
DWORD dwRGBZBitMask; // mask for Z channel
DWORD dwYUVZBitMask; // mask for Z channel
};
};



//
// DDSCAPS2
//
struct DDSCAPS2
{
DWORD dwCaps; // capabilities of surface wanted
DWORD dwCaps2;
DWORD dwCaps3;
union
{
DWORD dwCaps4;
DWORD dwVolumeDepth;
};
};



//
// DDSURFACEDESC2
//
struct DDSURFACEDESC2
{
DWORD dwSize; // size of the DDSURFACEDESC structure
DWORD dwFlags; // determines what fields are valid
DWORD dwHeight; // height of surface to be created
DWORD dwWidth; // width of input surface
union
{
LONG lPitch; // distance to start of next line (return value only)
DWORD dwLinearSize; // Formless late-allocated optimized surface size
};
union
{
DWORD dwBackBufferCount; // number of back buffers requested
DWORD dwDepth; // the depth if this is a volume texture
};
union
{
DWORD dwMipMapCount; // number of mip-map levels requestde
// dwZBufferBitDepth removed, use ddpfPixelFormat one instead
DWORD dwRefreshRate; // refresh rate (used when display mode is described)
DWORD dwSrcVBHandle; // The source used in VB::Optimize
};
DWORD dwAlphaBitDepth; // depth of alpha buffer requested
DWORD dwReserved; // reserved
LPVOID lpSurface; // pointer to the associated surface memory
union
{
DDCOLORKEY ddckCKDestOverlay; // color key for destination overlay use
DWORD dwEmptyFaceColor; // Physical color for empty cubemap faces
};
DDCOLORKEY ddckCKDestBlt; // color key for destination blt use
DDCOLORKEY ddckCKSrcOverlay; // color key for source overlay use
DDCOLORKEY ddckCKSrcBlt; // color key for source blt use
union
{
DDPIXELFORMAT ddpfPixelFormat; // pixel format description of the surface
DWORD dwFVF; // vertex format description of vertex buffers
};
DDSCAPS2 ddsCaps; // direct draw surface capabilities
DWORD dwTextureStage; // stage in multitexture cascade
};



#define FOURCC_DXT1 MAKEFOURCC('D', 'X', 'T', '1')
#define FOURCC_DXT3 MAKEFOURCC('D', 'X', 'T', '3')
#define FOURCC_DXT5 MAKEFOURCC('D', 'X', 'T', '5')





Function :

//
// Charge une texture depuis un fichier
//
// filename : nom du fichier .dds
//
bool cTexture::LoadDDS(const char *filename)
{
DDSURFACEDESC2 ddsd;
char filecode[4];
int factor;
int bufferSize;
int format;
FILE *file=NULL;

file = fopen(filename, "rb");

// Verify the file is a true .dds file
fread(&filecode, 1, 4, file);
if( strncmp( filecode, "DDS ", 4 ) != 0 )
{
DebugManager->Print(Error, __FUNCTION__, "\"%s\" is not a valid .dds file !", filename);
return false;
}


// Get the surface descriptor
fread(&ddsd, 1, sizeof(ddsd), file);


//
// Type de compression
switch(ddsd.ddpfPixelFormat.dwFourCC)
{
case FOURCC_DXT1:
// DXT1's compression ratio is 8:1
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
factor = 2;
break;

case FOURCC_DXT3:
// DXT3's compression ratio is 4:1
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
factor = 4;
break;

case FOURCC_DXT5:
// DXT5's compression ratio is 4:1
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
factor = 4;
break;

default:
DebugManager->Print(Todo, __FUNCTION__, "The file \"%s\" doesn't appear to be compressed using DXT1, DXT3, or DXT5!", filename);
fclose(file);
return false;
break;
}

//
// Taille du buffer
//
if(!ddsd.dwLinearSize)
{
DebugManager->Print(Error, __FUNCTION__, "ddsd.dwLinearSize is 0 !");
fclose(file);
return false;
}
if (ddsd.dwMipMapCount > 1) bufferSize = ddsd.dwLinearSize * factor;
else bufferSize = ddsd.dwLinearSize;
ImageData = new unsigned char[bufferSize];
memset(ImageData, 0, bufferSize);
int ret = 0;
ret = fread(ImageData, 1, bufferSize, file);
fclose(file);

// Sauvegarde les infos
Width = ddsd.dwWidth;
Height = ddsd.dwHeight;
if(ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1 ) PixelFormat = PF_RGB;
else PixelFormat = PF_RGBA;


//
// Generation de la texture
glGenTextures(1, (GLuint *)&GLid);
glBindTexture(GL_TEXTURE_2D, GLid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );



// Chargement des mipmaps
int nSize = 0;
int nOffset = 0;
int nHeight = Height;
int nWidth = Width;
int nNumMipMaps = ddsd.dwMipMapCount;
int nBlockSize = format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ? 8 : 16;

for( unsigned long i = 0; i < ddsd.dwMipMapCount; ++i )
{
if( nWidth == 0 ) nWidth = 1;
if( nHeight == 0 ) nHeight = 1;

nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize;

glCompressedTexImage2DARB( GL_TEXTURE_2D,
i,
format,
nWidth,
nHeight,
0,
nSize,
ImageData + nOffset );

nOffset += nSize;

// Moitie de la taille pour le prochain mipmap...
nWidth /= 2;
nHeight /= 2;
}


return true;
}




If You have questions, You know where to ask :)

Share this post


Link to post
Share on other sites
A sweet! sence i'm using linux i didn't have access to the DirectDraw headers, that's exacly what i wanted!

That MSDN resource looked pretty good to, gonna have a good look at that aswell.

Thank you gues very much!

Share this post


Link to post
Share on other sites
MB use DevIL (openil.sf.net). It supports DXTn compression (yes, it was not mentioned that MSDN documentation doesn't contain the description of most popular compression algo family - DXTn, which is used in almostly all DDS (I know only one exception --- Doom 3), so you should look it, for example, in http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_compression_s3tc.txt)

DevIL loads DDS without using GL hardware support and allows saving in DDS.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this