Sign in to follow this  
Jethro_T

SDL_Surface to OpenGL Texture

Recommended Posts

I noticed some strange behavior when converting SDL_Surface's to OpenGL Textures.

Any size of texture will work fine in .png or .tif format however when I try to use .jpg or .bmp files, if the image's dimensions are not a power of 2, the texture will come out really messed up. The color will be greyed and there will be rainbow lines horizontally through the texture. Also, the texture will be slanted and "twisted". Does anyone have any idea what's going on?

My code:

bool TextureManager::_buildTextureFromSurface(SDL_Surface* surface, Texture& texture) {
if (!surface)
return false;

if (SDL_LockSurface(surface) != 0)
return false;

if (!surface->format || !surface->pixels) {
SDL_UnlockSurface(surface);
return false;
}

if (surface->format->BitsPerPixel != 4) {
if (surface->format->BitsPerPixel != 3) {
SDL_UnlockSurface(surface);
return false;
}

//Convert surface to 4 BPP
}

glGenTextures(1, &texture._glTextureID);

glBindTexture(GL_TEXTURE_2D, texture._glTextureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

if (surface->format->Rmask == 0x00ff0000)
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
else
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);

SDL_UnlockSurface(surface);

_setValid(texture);

return true;
}




[Edited by - Jethro_T on December 10, 2010 5:19:10 PM]

Share this post


Link to post
Share on other sites
Hey guys. I removed the extra padding that SDL seems to insert when the SDL_Surface isn't a power of 2 (The pitch isn't simply width * BPP). Anyway, that did NOT fix the problem.

This leads me to a new question... How does OpenGL deal with this padding issue? It seems to be working okay but should I actually be specifying a new unpadded array of pixel data for OpenGL to create a texture from?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this