SDL_image & OpenGL Problem

Posted by Dylan on Game Development See other posts from Game Development or by Dylan
Published on 2012-11-02T01:03:10Z Indexed on 2012/11/02 5:25 UTC
Read the original article Hit count: 325

Filed under:
|
|
|

i've been following tutorials online to load textures using SDL and display them on a opengl quad. but ive been getting weird results that no one else on the internet seems to be getting...

so when i render the texture in opengl i get something like this. http://www.kiddiescissors.com/after.png

when the original .bmp file is this: http://www.kiddiescissors.com/before.bmp

ive tried other images too, so its not that this particular image is corrupt. it seems like my rgb channels are all jumbled or something. im pulling my hair out at this point.

heres the relevant code from my init() function

if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
    return 1;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);

SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50, (GLfloat)WINDOW_WIDTH/WINDOW_HEIGHT, 1, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND); 

heres the code that is called when my main player object (the one with which this sprite is associated) is initialized

texture = 0;
SDL_Surface* surface = IMG_Load("i.bmp");
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, surface->w, surface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(surface);

and then heres the relevant code from my display function

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor4f(1, 1, 1, 1);
    glPushMatrix();
        glBindTexture(GL_TEXTURE_2D, texture);
        glTranslatef(getCenter().x, getCenter().y, 0);
        glRotatef(getAngle()*(180/M_PI), 0, 0, 1);
        glTranslatef(-getCenter().x, -getCenter().y, 0);
        glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex3f(getTopLeft().x, getTopLeft().y, 0);
            glTexCoord2f(0, 1);
            glVertex3f(getTopLeft().x, getTopLeft().y + size.y, 0);
            glTexCoord2f(1, 1);
            glVertex3f(getTopLeft().x + size.x, getTopLeft().y + size.y, 0);
            glTexCoord2f(1, 0);
            glVertex3f(getTopLeft().x + size.x, getTopLeft().y, 0);
        glEnd();
    glPopMatrix();

let me know if i left out anything important... or if you need more info from me.

thanks a ton,

-Dylan

© Game Development or respective owner

Related posts about c++

Related posts about opengl