Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an issue here with converting an existing SDL_Surface into an OpenGL texture. The texture gets created, but it's a plain white texture, below is a screenshot of the result: Screenshot of Result

It seems to me that the SDL_Surface image data is not being properly cast onto the OpenGL texture. But I'm unsure why. Below is another screenshot, showing the image data of the "Loaded_Image", which is the existing SDL_Surface. Screenshot of Existing Image Data

I'm just a bit unsure where I'm going wrong. The code seems alright to me:

SDLib::Image SDLib::ConvertImage(SDL_Surface* surface)
    SDL_Surface *LoadedImage = NULL;
    GLuint tempTexture;
    Image tempImage;
    LoadedImage = surface;

    //Set Width/Height Of Image
    tempImage.width = LoadedImage->w;
    tempImage.height = LoadedImage->h;

    SDL_Surface *image = SDL_CreateRGBSurface(0, LoadedImage->w, LoadedImage->h, 16, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
    SDL_BlitSurface(LoadedImage, NULL, image, NULL);

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

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

    //Finally Set The TempTexture data for our OpenGl Texture to the Temporary Image Struct
    tempImage.texData = tempTexture;

    return tempImage;

The image itself it supposed to rendering out like this for reference:

Expected Output

share|improve this question
Check the return value of SDL_BlitSurface, here is its Doc – Maik Semder Feb 2 '12 at 17:51
It returns -1 :/, so an error somewhere is occurring. – dan369 Feb 2 '12 at 17:59
If you found the problem, you should answer your own question and mark it as the answer. – michael.bartnett Feb 2 '12 at 18:59
up vote 3 down vote accepted

I think you are using wrong color masks when you create your image surface. Try this for a GL_BGRA texture:

SDL_Surface *image = SDL_CreateRGBSurface(0, LoadedImage->w, LoadedImage->h, 16, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);

Though, a better solution for loading images would be to use something like this (from gpwiki):

// get the number of channels in the SDL surface
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4)     // contains an alpha channel
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGBA;
                texture_format = GL_BGRA;
} else if (nOfColors == 3)     // no alpha channel
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGB;
        texture_format = GL_BGR;
} else {
    printf("warning: the image is not truecolor..  this will probably break\n");
    // this error should not go unhandled

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

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

// Set the texture's stretching properties

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

That way you can even skip blitting your image to a surface and make a texture right away.

share|improve this answer
Make certain that you watch your pixel store alignments if uploading 3-component textures ( and also note that use of 1, 2, 3 or 4 for the internalFormat param of glTexImage2D is deprecated in modern core contexts. – Le Comte du Merde-fou Feb 2 '12 at 19:13
Thanks for the link and answer :) – dan369 Feb 2 '12 at 19:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.