0
\$\begingroup\$

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

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    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

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

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;
    else
                texture_format = GL_BGRA;
} else if (nOfColors == 3)     // no alpha channel
{
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGB;
    else
        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
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// 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.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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