2013-06-05 3 views
7

나는이 OpenGl goes too dark보다 상당히 똑같은 문제를 가지고 있지만 대답은 저에게는 효과적이지 않습니다. 나는 텍스처로 변환 표면에 이미지 감사를 표시하기 위해 노력하고있어 그 결과가 너무 지독한 어두운 :OpenGL 텍스처 형식 SDLSurface가 너무 어둡습니다.

enter image description here

후 OpenGL을

enter image description here

:

원래 왼쪽에는 원본이, 오른쪽에는 OpenGl img가 있습니다.

여기 내 코드입니다 :

void TexturedRect::draw(int scroll){ 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glBindTexture(GL_TEXTURE_2D, _texture); 

    glEnable(GL_TEXTURE_2D); 
    glBegin(GL_QUADS); //Begining the cube's drawing 

    int x = this->getX(); 
    int y = this->getY(); 
    int w = this->getWidth(); 
    int h = this->getHeight(); 
    int z = this->getZ(); 

    /* 
    (0,0) ------ (1,0) 
     |   | 
     |   | 
    (0,1) ------ (1,1) 
    */ 
    glTexCoord3i(0, 0, 1);glVertex3i(x + scroll,   y,  z); 
    glTexCoord3i(_tv, 0, 1);glVertex3i(x + w * _tv + scroll,  y,  z); 
    glTexCoord3i(_tv, _tu, 1);glVertex3i(x + w * _tv + scroll,  y + h * _tu, z); 
    glTexCoord3i(0, _tu, 1);glVertex3i(x + scroll,   y + h * _tu, z); 

    glEnd(); 
    glDisable(GL_TEXTURE_2D); 
} 

void TexturedRect::createTextureFromSurface() 
{ 
    SDL_Surface * surface = IMG_Load(filename.toStdString().c_str()); 
    // get the number of channels in the SDL surface 
    GLint nbOfColors = surface->format->BytesPerPixel; 
    GLenum textureFormat = 0; 

    switch (nbOfColors) { 
    case 1: 
     textureFormat = GL_ALPHA; 
     break; 
    case 3:  // no alpha channel 
     if (surface->format->Rmask == 0x000000ff) 
      textureFormat = GL_RGB; 
     else 
      textureFormat = GL_BGR; 
     break; 
    case 4:  // contains an alpha channel 
     if (surface->format->Rmask == 0x000000ff) 
      textureFormat = GL_RGBA; 
     else 
      textureFormat = GL_BGRA; 
     break; 
    default: 
     qDebug() << "Warning: the image is not truecolor..."; 
     break; 
    } 

    glEnable(GL_TEXTURE_2D); 
    // Have OpenGL generate a texture object handle for us 
    glGenTextures(1, &_texture); 

    // Bind the texture object 
    glBindTexture(GL_TEXTURE_2D, _texture); 


    // Edit the texture object's image data using the information SDL_Surface gives us 
    glTexImage2D(GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0, 
        textureFormat, GL_UNSIGNED_BYTE, surface->pixels); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 

} 
+1

다른 파일로 재생산 할 수 있습니까? –

+0

불행히도 예, 문제는 파일과 독립적이라고 보입니다 ... –

+0

glTexImage2D의 'internalFormat' 매개 변수에 대해'nbOfColors'를 전달하는 것이 이상하게 보입니다. – Brian

답변

5

당신은 아마이 쿼드를 그리기위한 비활성화 할 때 아직 활성화 된 코드에서 다른 설정 몇 가지 상태를 가지고있다.

glBindTexture (GL_TEXTURE_2D, _texture) 뒤에 다음을 넣으십시오. 드로우 코드에서 (그것은이 createTextureFromSurface 방법을 추첨 방식으로 수행하고 아니에요 것이 중요) :

glDisable(GL_BLEND); 
glDisable(GL_LIGHTING); 
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) 
glColor3f(1.0f, 1.0f, 1.0f); 

을이 당신이 상태가 문제의 원인이 된 알아 내기 위해 한 번에 하나씩 주석 수 있습니다 작동합니다. 이 쿼드를 그리기 위해 사용하지 않도록 설정 한 상태는이를 필요로하는 객체를 그릴 때 다시 활성화해야합니다.

+2

잘하셨습니다. GL_BLEND는 범인이었다! 여기 당신은 100rep입니다! –

+0

채광 조명이 도움이됩니다. – xqterry