SDL_LoadBMP上下颠倒并镜像了位图

时间:2012-07-16 05:53:00

标签: opengl sdl textures

SDL_LoadBMP将位图颠倒加载并在OpenGL中镜像。可能是什么问题?

unsigned int loadTexture (const char * fileName)
{
    unsigned int texId;
    GLenum format;
    GLint colorCount;

    SDL_Surface * image = SDL_LoadBMP (fileName);

    colorCount = image->format->BytesPerPixel;
    if (colorCount == 4)
    {
         if (image->format->Rmask == 0x000000ff)
             format = GL_RGBA;
         else
             format = GL_BGRA;
    }
    else if (colorCount == 3)
    {
         if (image->format->Rmask == 0x000000ff)
             format = GL_RGB;
         else
             format = GL_BGR;
    }

    glGenTextures(1, &texId);
    glBindTexture(GL_TEXTURE_2D, texId);

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

    glTexImage2D(GL_TEXTURE_2D, 0, colorCount, image->w, image->h, 0, format, GL_UNSIGNED_BYTE, image->pixels);

    SDL_FreeSurface(image);
    return texId;
}

..这是我绘制测试四元组的方式..

glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex3f(-1.0, -1.0, 0.0);
    glTexCoord2i(1, 0); glVertex3f( 1.0, -1.0, 0.0);
    glTexCoord2i(1, 1); glVertex3f( 1.0,  1.0, 0.0);
    glTexCoord2i(0, 1); glVertex3f(-1.0,  1.0, 0.0);
glEnd();

1 个答案:

答案 0 :(得分:1)

OpenGL始终假定纹理图像的原点位于“左下角”。

严格地说,OpenGL假定纹理的数据从原点开始,并随着纹理坐标的增加而前进。因此,如果您向右和向上绘制具有投影纹理坐标的纹理,则图像原点将位于左下方。

无法在OpenGL端更改此行为,因此您必须对加载的图像数据进行适当调整,或使用某些着色器技巧来翻转那里的方向。但是,大多数图像加载库允许您选择所需的原点,因此我将其设置为左下角。然后,图书馆在内部处理数据。