OK, I'm new at OpenGL + SDL but here is what I have.. Loads all? formats SDL_image supports except I can't get .xcf to work and don't have a .lbm to test with.
//called earlier..
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//load texture
SDL_Surface* tex = IMG_Load(file.c_str());
if (tex == 0) {
std::cout << "Could not load " << file << std::endl;
return false;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//nearest works but linear is best when scaled?
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
width = tex->w;
height = tex->h;
//IMG_is* doesn't seem to work right, esp for TGA, so use extension instead..
std::string ext = file.substr(file.length() - 4);
bool isBMP = (ext.compare(".bmp") == 0) || (ext.compare(".BMP") == 0);
bool isPNG = (ext.compare(".png") == 0) || (ext.compare(".PNG") == 0);
bool isTGA = (ext.compare(".tga") == 0) || (ext.compare(".TGA") == 0);
bool isTIF = ((ext.compare(".tif") == 0) || (ext.compare(".TIF") == 0) ||
(ext.compare("tiff") == 0) || (ext.compare("TIFF") == 0));
//default is RGBA but bmp and tga use BGR/A
GLenum format = GL_RGBA;
if(isBMP || isTGA)
format = (tex->format->BytesPerPixel == 4 ? GL_BGRA : GL_BGR);
//every image except png and bmp need to be converted
if (!(isPNG || isBMP || isTGA || isTIF)) {
SDL_Surface* fixedSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_BlitSurface(tex, 0, fixedSurface, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, fixedSurface->pixels);
SDL_FreeSurface(fixedSurface);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, tex->pixels);
}
SDL_FreeSurface(tex);
list = glGenLists(1);
glNewList(list, GL_COMPILE);
GLint vertices[] = {
0,0, 0,0,
0,1, 0,height,
1,1, width,height,
1,0, width,0
};
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_2D, texture);
glTexCoordPointer(2, GL_INT, 4*sizeof(GLint), &vertices[0]);
glVertexPointer(2, GL_INT, 4*sizeof(GLint), &vertices[2]);
glDrawArrays(GL_POLYGON, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glEndList();
And then to draw I set the color to opaque white (doesn't affect transparency?) then just call the list..
glColor4f(1,1,1,1);
glCallList(list);
And of course, any help for my code would be much appreciated too! :)