C++ 找不到.DDS纹理的fourCC格式

C++ 找不到.DDS纹理的fourCC格式,c++,opengl,textures,dds-format,fourcc,C++,Opengl,Textures,Dds Format,Fourcc,我正在尝试创建一个函数来加载.DDS格式的纹理文件。当我的程序试图检测文件的FourCC格式时,问题似乎就出现了。使用FOURCC_DXT1、FOURCC_DXT3和FOURCC_DXT5都不会返回true。我已经尝试了很多纹理,其中一些是DXT1根据我采取他们的来源,但问题仍然存在 GLuint loadDDS(const char * imagepath) { unsigned char header[124]; FILE *fp; // open texture

我正在尝试创建一个函数来加载.DDS格式的纹理文件。当我的程序试图检测文件的FourCC格式时,问题似乎就出现了。使用FOURCC_DXT1、FOURCC_DXT3和FOURCC_DXT5都不会返回true。我已经尝试了很多纹理,其中一些是DXT1根据我采取他们的来源,但问题仍然存在

GLuint loadDDS(const char * imagepath) {

    unsigned char header[124];

    FILE *fp;
    // open texture data
    fopen_s(&fp, imagepath, "rb");
    if (fp == NULL) return 0;

    std::cout << "file: " << fp << '\n';

    // get the surface desc 
    fread(&header, 124, 1, fp);

    // header data
    unsigned int height = *(unsigned int*)&(header[8]);
    unsigned int width = *(unsigned int*)&(header[12]);
    unsigned int linearSize = *(unsigned int*)&(header[16]);
    unsigned int mipMapCount = *(unsigned int*)&(header[24]);
    unsigned int fourCC = *(unsigned int*)&(header[80]);

    // allocate buffer
    data = (unsigned char*)malloc(width * height * 4);

    // file data
    unsigned char * buffer;
    unsigned int bufsize;

    // how big is it going to be including all mipmaps? 
    bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize;
    buffer = (unsigned char*)malloc(bufsize * sizeof(unsigned char));
    fread(buffer, 1, bufsize, fp);

    // close the file pointer 
    fclose(fp);

    unsigned int components = (fourCC == FOURCC_DXT1) ? 3 : 4;
    unsigned int format;

    // here is where the problem occurs. Switch returns the default case. 
    //At this point fourCC has the value of 4.
    switch (fourCC)
    {
    case FOURCC_DXT1:
        format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
        break;
    case FOURCC_DXT3:
        format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
        break;
    case FOURCC_DXT5:
        format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
        break;
    default:
        std::cout << "Error when trying to read header of texture. Could not find FOURCC component" << '\n';
        free(data);
        return 0;
    }
    // Create one OpenGL texture
    GLuint textureID;
    glGenTextures(1, &textureID);

    // "Bind" the newly created texture : all future texture functions will modify this texture
    glBindTexture(GL_TEXTURE_2D, textureID);

    unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
    unsigned int offset = 0;

    // load the mipmaps 
    for (unsigned int level = 0; level < mipMapCount && (width || height); ++level)
    {
        unsigned int size = ((width + 3) / 4)*((height + 3) / 4)*blockSize;
        glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height,
            0, size, data + offset);

        offset += size;
        width /= 2;
        height /= 2;
    }
    free(data);

    return textureID;
GLuint loadDDS(常量字符*imagepath){
无符号字符头[124];
文件*fp;
//打开纹理数据
fopen_s(&fp,imagepath,“rb”);
如果(fp==NULL)返回0;

std::cout懒得调试代码……由于这类问题在这里是离题的,所以不要将其作为答案处理(更像是注释,但作为注释,这既不可读也不可能)

以下是我用于将DDS加载到我的OpenGL引擎中的内容:

//------------------------------------------------------------------------------
//---DDS库版本:1.03----------------------------------------------------
//------------------------------------------------------------------------------
#ifndef\u dds\u h
#定义dds
//------------------------------------------------------------------------------
名称空间DDS{
//------------------------------------------------------------------------------
AnsiString_errorlog=“”;
常量DWORD DDS_MAGIC=0x205344444;
常量DWORD DDSD_CAPS=0x00000001;
常量DWORD DDSD_高度=0x00000002;
常量DWORD DDSD_宽度=0x00000004;
常数DWORD DDSD_节距=0x00000008;
常量DWORD DDSD_像素格式=0x00001000;
常量DWORD DDSD_MIPMAPCOUNT=0x00020000;
常量DWORD DDSD_LINEARSIZE=0x00080000;
常量DWORD DDSD_深度=0x00800000;
常量DWORD DDPF_ALPHAPIXELS=0x00000001;
常量DWORD DDPF_FOURCC=0x00000004;
常量DWORD DDPF_索引=0x00000020;
常量DWORD DDPF_RGB=0x00000040;
常量DWORD DDSCAPS_复数=0x00000008;
常量DWORD DDSCAPS_纹理=0x00001000;
常量DWORD DDSCAPS\u MIPMAP=0x00400000;
常数DWORD DDSCAPS2_立方体映射=0x00000200;
常数DWORD DDSCAPS2_立方映射_POSITIVEX=0x00000400;
常数DWORD DDSCAPS2_立方映射_negativeEx=0x00000800;
常数DWORD DDSCAPS2_立方体映射_POSITIVEY=0x00001000;
常数DWORD DDSCAPS2_立方映射_NEGATIVEY=0x00002000;
常数DWORD DDSCAPS2_立方体映射_正片=0x00004000;
常数DWORD DDSCAPS2_立方映射_NEGATIVEZ=0x00008000;
常量DWORD DDSCAPS2_体积=0x00200000;
常量DWORD D3DFMT_DXT1='1TXD';
常量DWORD D3DFMT_DXT2='2TXD';
常量DWORD D3DFMT_DXT3='3TXD';
常量DWORD D3DFMT_DXT4='4TXD';
常量DWORD D3DFMT_DXT5='5TXD';
//------------------------------------------------------------------------------
#定义PF_为_DXT1(PF)\
((pf.dwFlags和DDPF_FOURCC)和\
(pf.dwFourCC==D3DFMT_DXT1))
#定义PF_为_DXT3(PF)\
((pf.dwFlags和DDPF_FOURCC)和\
(pf.dwFourCC==D3DFMT_DXT3))
#定义PF_为_DXT5(PF)\
((pf.dwFlags和DDPF_FOURCC)和\
(pf.dwFourCC==D3DFMT_DXT5))
#定义PF_为_BGRA8(PF)\
((pf.dwFlags和DDPF_RGB)和\
(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==32)和\
(pf.DWRBITMAK==0xff0000)和\
(pf.dwGBitMask==0xff00)和\
(pf.dwBBitMask==0xff)和\
(pf.dwAlphaBitMask==0xff000000U))
#定义PF_为_RGBA8(PF)\
((pf.dwFlags和DDPF_RGB)和\
(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==32)和\
(pf.dwRBitMask==0xff)和\
(pf.dwGBitMask==0xff00)和\
(pf.dwBBitMask==0xff0000)和\
(pf.dwAlphaBitMask==0xff000000U))
#定义PF_为_BGR8(PF)\
((pf.dwFlags和DDPF_RGB)和\
!(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==24)和\
(pf.DWRBITMAK==0xff0000)和\
(pf.dwGBitMask==0xff00)和\
(pf.dwBBitMask==0xff))
#定义PF_为_RGB8(PF)\
((pf.dwFlags和DDPF_RGB)和\
!(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==24)和\
(pf.dwRBitMask==0xff)和\
(pf.dwGBitMask==0xff00)和\
(pf.dwBBitMask==0xff0000))
#定义PF_为_BGR5(PF)\
((pf.dwFlags和DDPF_RGB)和\
!(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==16)和\
(pf.DWRBITMAK==0x00007c00)和\
(pf.dwGBitMask==0x000003e0)和\
(pf.dwBBitMask==0x0000001f)和\
(pf.dwAlphaBitMask==0x00000000))
#定义PF_为_BGR5A1(PF)\
((pf.dwFlags和DDPF_RGB)和\
(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==16)和\
(pf.DWRBITMAK==0x00007c00)和\
(pf.dwGBitMask==0x000003e0)和\
(pf.dwBBitMask==0x0000001f)和\
(pf.DWA字母位掩码==0x00008000))
#定义PF_为_BGR565(PF)\
((pf.dwFlags和DDPF_RGB)和\
!(pf.dwFlags和DDPF_字母像素)和\
(pf.dwRGBBitCount==16)和\
(pf.DWRBITMAK==0x0000f800)和\
(pf.dwGBitMask==0x000007e0)和\
(pf.dwBBitMask==0x0000001f))
#定义PF_是_INDEX8(PF)\
((pf.dwFlags和DDPF_索引)和\
(pf.dwRGBBitCount==8))
//------------------------------------------------------------------------------
#布拉格语包(1)
联合DDS_头
{
结构
{
德沃德·德怀特;
DWORD dwSize;
德沃德旗;
德沃德·德赫特;
德沃德宽度;
DWORD dwPitchOrLinearSize;
德沃德深度;
DWORD-dwMipMapCount;
DWORD dwReserved1[11];
//像素格式
结构
{
DWORD dwSize;
德沃德旗;
DWORD dwFourCC;
DWORD dwRGBBitCount;
德沃德·德尔比特马斯克;
德沃德·德格比特面具;
DWORD dwBBitMask;
DWORD字母位掩码;
}像素格式;
//DDCAPS2
结构
{
DWORD dwCaps1;
德沃德