shan
shan

Reputation: 1202

SDL_LoadBMP loads the bitmap upside down and mirrored

SDL_LoadBMP loads the bitmap upside down and mirrored in OpenGL. What may be the problem ?

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;
}

..This is how i draw a test quad..

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();

Upvotes: 2

Views: 1468

Answers (1)

datenwolf
datenwolf

Reputation: 162164

OpenGL always assumes the origin of the texture image to be in the "lower left".

Strictly speaking, OpenGL assumes the data of the texture to start at the origin, and to progress with increasing texture coordinates. So if you draw a texture with projected texture coordinates to the right and up, the image origin will be in the lower left.

There's no way to change this behaviour on the OpenGL side, so you must take the proper adjustments on the loaded image data, or use some shader trickery to flip the directions there. Most image loading libraries however allow you to select the point of origin you expect, so I'd set this to lower left. The library then juggles around the data internally.

Upvotes: 1

Related Questions