PDA

View Full Version : SDL/OpenGL image doesnt display correctly :S



OhSnapEpicPwnage
04-24-2011, 03:26 PM
This is my code here:

#include <SDL/SDL.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

SDL_Surface *screen;
SDL_Event event;

GLuint texture;
SDL_Surface *img;

int main(int argc,char* argv[])
{
int start;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("OpenGL is the shiznit!",NULL);
screen = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_OPENGL);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);

img = SDL_LoadBMP("lol.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,img->w,img->h,GL_RGB,GL_UNSIGNED_BYTE,img->pixels);

while(true)
{
start = SDL_GetTicks();

if(SDL_PollEvent(&amp;event))
{
if(event.type == SDL_QUIT)
{
return false;
}
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
return false;
}
}
}
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex2f(100.0f,100.0f);
glTexCoord2i(0,1); glVertex2f(100.0f + img->w,100.0f);
glTexCoord2i(1,1); glVertex2f(100.0f + img->w,100.0f + img->h);
glTexCoord2i(1,0); glVertex2f(100.0f,100.0f + img->h);
glEnd();

SDL_GL_SwapBuffers();
}

SDL_Quit();
return 0;
}

This is what i see after compiling and running:http://localhostr.com/file/4j5ae66/see.PNG
And this is the original image:http://localhostr.com/file/ByVsR7W/oi.PNG

I have no idea what im suppose to do :S

mhagain
04-24-2011, 03:41 PM
Firstly you need to use GL_BGR instead of GL_RGB for format (the second GL_RGB) in your gluBuild2DMipmaps call. That'll get the colours right. Also read this for some interesting notes on GL_RGB/GL_BGR: http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads and this for some notes on gluBuild2DMipmaps: http://www.opengl.org/wiki/Common_Mistakes#gluBuild2DMipmaps

Secondly your glTexCoord calls don't match with your glVertex calls. Have another look at them, check out the second pair for example. You're adding width to x in glVertex but using 1 for your t parameter in glTexCoord. Use something like this instead:

glTexCoord2f(0,0); glVertex2f(100.0f,100.0f);
glTexCoord2f(1,0); glVertex2f(100.0f + img->w,100.0f);
glTexCoord2f(1,1); glVertex2f(100.0f + img->w,100.0f + img->h);
glTexCoord2f(0,1); glVertex2f(100.0f,100.0f + img->h);
Note that I switched the order of s and t around in your glTexCoord calls to match your width and height additions in glVertex. Note also that I changed them to glTexCoord2f - that'll save you some pain in the future.

I see as well that you're not using glGenTextures anywhere; you really need a call to that before you load the texture. Also your SDL_GL_SetAttribute is probably having no effect as I believe that you need to call it before SDL_SetVideoMode.

OhSnapEpicPwnage
04-24-2011, 03:48 PM
Thanks man.
Its working flawlessly now.