0

我目前正在尝试在 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 并设置了一个混合功能。

有谁知道我做错了什么?

4

1 回答 1

2
  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 纹理绑定?IE ...

    glBindTexture(GL_TEXTURE_2D, 0);
    
于 2013-02-25T01:18:22.463 回答