【问题标题】:DDS texture transparency rendered black OpenglDDS 纹理透明度渲染为黑色 Opengl
【发布时间】:2013-02-16 17:17:28
【问题描述】:

我目前正在尝试在 Opengl 中渲染带纹理的对象。一切都很好,直到我想渲染一个透明的纹理。它没有显示对象透明,而是呈现为全黑。

加载纹理文件的方法是这样的:

//  structures for reading and information variables
char magic[4];
unsigned char header[124];
unsigned int width, height, linearSize, mipMapCount, fourCC;
unsigned char* dataBuffer;
unsigned int bufferSize;

fstream file(path, ios::in|ios::binary);

//  read magic and header
if (!file.read((char*)magic, sizeof(magic))){
    cerr<< "File " << path << " not found!"<<endl;
    return false;
}

if (magic[0]!='D' || magic[1]!='D' || magic[2]!='S' || magic[3]!=' '){
    cerr<< "File does not comply with dds file format!"<<endl;
    return false;
}

if (!file.read((char*)header, sizeof(header))){
    cerr<< "Not able to read file information!"<<endl;
    return false;
}

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

//  determine dataBuffer size
bufferSize = mipMapCount > 1 ? linearSize * 2 : linearSize;
dataBuffer = new unsigned char [bufferSize*2];

//  read data and close file
if (file.read((char*)dataBuffer, bufferSize/1.5))
    cout<<"Loading texture "<<path<<" successful"<<endl;
else{
    cerr<<"Data of file "<<path<<" corrupted"<<endl;
    return false;
}

file.close();

//  check pixel format
unsigned int format;

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:
    cerr << "Compression type not supported or corrupted!" << endl;
    return false;
}

glGenTextures(1, &ID);


glBindTexture(GL_TEXTURE_2D, ID);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);

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, dataBuffer + offset);

    offset += size;
    width  /= 2;
    height /= 2;
}

textureType = DDS_TEXTURE;

return true;

在片段着色器中,我只设置了 gl_FragColor = texture2D(myTextureSampler, UVcoords)

我希望有一个简单的解释,例如缺少一些代码。 在 openGL 初始化中我 glEnabled GL_Blend 并设置了一个混合函数。

有人知道我做错了什么吗?

【问题讨论】:

    标签: opengl textures alpha-transparency


    【解决方案1】:
    1. 确保混合函数是您想要完成的正确函数。对于你所描述的应该是 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    2. 您可能不应该在您的 openGL 初始化函数中设置混合函数,而应该将其包裹在您的绘图调用中,例如:

      glEnable(GL_BLEND)
      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
      
      //gl draw functions (glDrawArrays,glDrawElements,etc..)
      
      glDisable(GL_BLEND)
      
    3. 您是否在交换缓冲区之前清除了 2D 纹理绑定?即...

      glBindTexture(GL_TEXTURE_2D, 0);
      

    【讨论】:

    • 感谢您的建议。不幸的是,包括这些更改并没有产生任何影响。在我看来,加载 dds 或着色器可能有问题。如果我为 gl_FragColor 设置一个固定的 alpha 值,透明度就可以了。只有从纹理加载时才会出现问题。
    • 天啊!我解决了。这么笨。起初我只为图纸的一部分启用了 GL_BLEND,而不是需要透明的部分。然后这是正确的,但加载了错误的纹理。对不起,打扰你。并再次感谢您的帮助。我已经放弃了这个。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2018-09-17
    • 2010-11-21
    相关资源
    最近更新 更多