Estaba tratando de cargar imágenes en sdl + opengl (en linux), y el primer problema que habia tenido era que me tomaba ciertas imágenes correctamente (texturas de 32 bits), las otras me las mostraba distorsionadas. Lo solucioné utilizando el código que viene en los ejemplos de sdl, más precisamente el testgl.c.
El tema ahora es que no puedo hacer que me tome las transparencias de un png. Este es mi código.
GLuint loadTexture(SDL_Surface *surface)
{
GLuint texture;
int w, h;
SDL_Surface *image;
SDL_Rect area;
Uint32 saved_flags;
Uint8 saved_alpha;
//verifica si el ancho de la imagen es potencia de 2
if(powerof2(surface->w) == 0) printf("warning: image width is not a power of 2\n");
//verifica si el alto de la imagen es potencia de 2
if(powerof2(surface->h) == 0) printf("warning: image height is not a power of 2\n");
w = surface->w;
h = surface->h;
image = SDL_CreateRGBSurface(
SDL_SWSURFACE,
w, h,
32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);
if ( image == NULL ) {
return 0;
}
/* Save the alpha blending attributes */
saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
saved_alpha = surface->format->alpha;
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
SDL_SetAlpha(surface, 0, 0);
}
/* Copy the surface into the GL texture image */
area.x = 0;
area.y = 0;
area.w = surface->w;
area.h = surface->h;
SDL_BlitSurface(surface, &area, image, &area);
/* Restore the alpha blending attributes */
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
SDL_SetAlpha(surface, saved_flags, saved_alpha);
}
/* Create an OpenGL texture for the image */
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_RGBA,
w, h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels);
SDL_FreeSurface(image); /* No longer needed */
return texture;
}
Para la inicialización del opengl arme esta función:
int initgraphics()
{
if((SDL_Init( SDL_INIT_VIDEO )) < 0 )
{
printf("error, no inicia video");
return 0;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
if((screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_OPENGL)) < 0)
{
printf("error, no inicia modo gráfico");
return 0;
}
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable( GL_TEXTURE_2D );
/* This allows alpha blending of 2D textures with the scene */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
glOrtho( 0.0, (GLdouble)SCREEN_WIDTH, (GLdouble)SCREEN_HEIGHT, 0.0, 0.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
return 1;
}
Si alguien tiene alguna idea, bienvenida sea, aunque sea otro método diferente.