2013-02-16 5 views
0

현재 텍스처링 된 객체를 Opengl에서 렌더링하려고합니다. 투명도가있는 텍스처를 렌더링하고 싶을 때까지 모든 것이 잘 동작했습니다. 객체를 투명하게 표시하는 대신 전체 검정색으로 렌더링했습니다.DDS 텍스처 투명도가 검은 색으로 렌더링 됨 Opengl

난 그냥 gl_FragColor = texture2D 설정 프래그먼트 쉐이더에서
// 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; 

(myTextureSampler, UVcoords)

내가 쉽게 설명 등이 희망 :

텍스처 파일을로드 FO 방법이있다 일부 코드가 누락되었습니다. 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 텍스처를 삭제하고 있습니까? 즉 ... 당신의 제안을

    glBindTexture(GL_TEXTURE_2D, 0); 
    
+0

감사합니다. 불행히도 이러한 변화를 포함하여 차이가 나지 않았습니다. dds 또는 shader를로드하는 데 문제가있는 것 같습니다. gl_FragColor 고정 알파 값을 설정하면 투명성이 작동합니다. 텍스처에서로드 될 때만 잘못 될 수 있습니다. –

+0

OMG! 나는 그것을 해결했다. 너무 바보. 처음에는 GL_BLEND를 도면의 한 부분에만 사용하도록 설정했으나 투명해야 할 필요는 없습니다. 그렇다면 이것은 정확했지만 잘못된 질감이로드되었습니다. 널 귀찮게해서 미안해. 그리고 당신의 도움에 다시 한번 감사드립니다. 나는 이미 이것을 포기했다. :디 –

관련 문제