Your assert() to verify if dimensions are powers of two is wrong, it checks for odd sizes.
OpenGL’s default packing and unpacking alignment is 4 bytes ( glPixelStore ), which would cause problems if your textures were as small as 2x2… but I’ll assume that’s not the case.
That format is really a mess, very limited and without compression. Perhaps you could consider PNG using either libpng or SDL_image? Otherwise, feel free to post a screenshot of the distorsion.
I can’t see why my method should fail porting it into an SDL_Surface
properly. This code was taken from a tutorial, working fine. I can’t
deliver the picture right now, but i can deliver the code:
void MMESurface::loadSurfaceOpenGL() {
assert (surface != null);
// Check that the image’s width is a power of 2
assert((surface->w % 2) == 0);
// Also check if the height is a power of 2
assert((surface->h % 2) == 0);
// 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, 3, surface->w, surface->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
openGLed = true;
Log(“Surface loaded for OpenGL”);
}
MMESurface class definition:
class MMESurface {
public:
int getHeight();
int getWidth();
int getDepth();
bool isOpenGL();
void loadSurface(string file);
void loadSurface(string file, bool openGL);
SDL_Surface* getSurface();
GLuint getTexture();
MMESurface(SDL_Surface *surf);
MMESurface();
~MMESurface();
private:
SDL_Surface *surface;
GLuint texture;
bool openGLed; // loaded for OpenGL
};
and the drawing routine itself:
void MMESurface::loadSurfaceOpenGL() {
assert (surface != null);
// Check that the image’s width is a power of 2
assert((surface->w % 2) == 0);
// Also check if the height is a power of 2
assert((surface->h % 2) == 0);
// 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, 3, surface->w, surface->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
openGLed = true;
Log(“Surface loaded for OpenGL”);
}
Again, it works. But only with some surfaces it does not. And without
changing the code (but by resaving a BMP) it looks like to work.
Perhaps Windows and Mac have tiny bit different habbit of
saving/loading BMP files?
–
Stefan Hendriks