How to load DDS files for volume texture?

Hi there,
I’m trying to load DDS files for volume textures in OpenGL, but it doesn’t work…

Here’s the function which loads a DDS volume texture.

DDS_IMAGE_DATA_3D* LoadDDSTexture3D(const char *file_name)
{
	DDS_IMAGE_DATA_3D *pDDSImageData;
	DDSURFACEDESC2 ddsd;
	char file_code[4];
	FILE *pFile;
	int factor;
	int buffer;
	
	pFile = fopen(file_name, "rb");
	
	if (pFile == NULL)
	{
		printf("ERROR: %s was not found.
\a", file_name);
		exit(1);
        }
	
	fread(file_code, 1, 4, pFile);
	
	if (strncmp(file_code, "DDS ", 4) != 0)
	{
		printf("ERROR: %s is not a DDS file.
\a", file_name);
		exit(1);
	}
	
	fread(&ddsd, sizeof (ddsd), 1, pFile);
	
	pDDSImageData = (DDS_IMAGE_DATA_3D*) malloc(sizeof (DDS_IMAGE_DATA_3D));
	
	memset(pDDSImageData, 0, sizeof (DDS_IMAGE_DATA_3D));
	
	switch (ddsd.ddpfPixelFormat.dwFourCC)
	{
	case FOURCC_DXT1:
		pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
		factor = 2;
		break;
	
	case FOURCC_DXT3:
		pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
		factor = 4;
		break;
	
	case FOURCC_DXT5:
		pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
		factor = 4;
		break;
	}
	
	if (ddsd.dwMipMapCount > 1)
	{
		buffer = ddsd.dwLinearSize * factor;
	}
	else
	{
		buffer = ddsd.dwLinearSize;
	}
	
	pDDSImageData->pixels = (unsigned char*) malloc(buffer * sizeof (unsigned char));
	
	fread(pDDSImageData->pixels, 1, buffer, pFile);

        fclose(pFile);

	ddsd.dwFlags = DDSD_CAPS
				 | DDSD_PIXELFORMAT
				 | DDSCAPS_COMPLEX
				 | DDSCAPS2_VOLUME
				 | DDSD_WIDTH
				 | DDSD_HEIGHT
				 | DDSD_DEPTH;

	pDDSImageData->flags      = ddsd.dwFlags;	
	pDDSImageData->width      = ddsd.dwWidth;
	pDDSImageData->height     = ddsd.dwHeight;
	pDDSImageData->depth      = ddsd.dwDepth;
	pDDSImageData->numMipMaps = ddsd.dwMipMapCount;
	
	if (ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1)
	{
		pDDSImageData->components = 3;
	}
	else
	{
		pDDSImageData->components = 4;
	}
	
	return pDDSImageData;
}

Does anyone know what the problem is? Or any sample code available?

Thanks.

I have not checked your code, but have you checked your code against the Humus frame work?

http://esprit.campus.luth.se/~humus/

Goto the OpenGL section then download the framework. I think he handles 3D DDS files

Why not use DevIL?

http://openil.sourceforge.net

PS: File formats are offtopic for this forum