我按照本教程在OpenGL中实现了DXT压缩纹理的加载:
问题是,当宽度和高度相同时(总是2的幂),它工作正常,但是当它们不是时,它只显示黑色(1024 * 512,256 * 512等不起作用.. )。可能是什么问题?
我正在使用OpenGL 3.3而我的显卡是AMD Radeon HD 7610M(笔记本,但还不错)。此外,glGetError()没有显示错误(三重检查)。
我发布我的代码版本只是为了确定(区别在于我从打包文件格式中读取纹理,这不是问题,因为我从这些加载所有模型并且一切都很好):< / p>
void Texture::loadDDS(unsigned char* data)
{
unsigned int height = *(unsigned int*)&(data[12]);
unsigned int width = *(unsigned int*)&(data[16]);
unsigned int mipMapCount = *(unsigned int*)&(data[28]);
unsigned int fourCC = *(unsigned int*)&(data[84]);
unsigned int format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
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;
}
glGenTextures(1, &_id);
glBindTexture(GL_TEXTURE_2D, _id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
unsigned int offset = 124 + 4;
for(unsigned int level = 0; level < mipMapCount && (width || height); ++level)
{
unsigned int size = ((width + 3) / 4) * ((height + 3) / 4) * blockSize;
//if(size == 0) break; // neccessary?
glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height, 0, size, &data[offset]);
offset += size;
width /= 2;
height /= 2;
}
glBindTexture(GL_TEXTURE_2D, 0);
}
答案 0 :(得分:2)
我想你的问题是由于以下几行:
width /= 2;
height /= 2;
这会导致错误的0(零)值。
您应该尝试用以下内容替换它们:
width = MAX(1, width/2);
height = ...