PDA

View Full Version : Problem applying texture using opengl+sdl (solved)



zalzane
04-16-2011, 03:32 PM
SOLVED: the texture that was being loaded turned out to be corrupted

So I'm using opengl with sdl and am having a problem where a white square is showing up instead of a square with the texture bound to it.

Most of this code is pieced together from multiple tutorials, so please point out if something is terribly wrong.

Init function:

glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)640/(GLfloat)480,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

GLuint useTexture;
SDL_Surface* surface;
if ( (surface = loadImg(path)) ) {

// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &useTexture );

// Bind the texture object
glBindTexture( GL_TEXTURE_2D, useTexture );



// 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_RGB, GL_UNSIGNED_BYTE, surface->pixels );

// 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 );
}
else {
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}


render function:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);

glBindTexture(GL_TEXTURE_2D, useTexture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();


SDL_GL_SwapBuffers();