2013-02-16 281 views
0

我目前正在尝试渲染Opengl中的纹理对象。一切工作正常,直到我想渲染具有透明度的纹理。而不是显示对象透明,它只是呈现全黑色。DDS纹理透明呈现黑色Opengl

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

// 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并设置了一个混合函数。

有没有人有我做错了什么的想法?

回答

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纹理你交换缓冲之前绑定?即...

+0

感谢您的建议。不幸的是,包括这些变化并没有什么区别。在我看来,加载dds或着色器时可能会出现问题。如果我为gl_FragColor设置了一个固定的alpha值,透明效果。只有从纹理上加载某些东西时才会出错。 OMG! – 2013-02-25 22:05:02

+0

OMG!我解决了它。如此愚蠢。起初,我只为图纸的一部分启用了GL_BLEND,而不是那些需要透明的部分。然后这是正确的,但加载了错误的纹理。对不起,打扰你。并再次感谢您的帮助。我已经放弃了这一点。 :d – 2013-02-25 22:26:02