Hello,
I am trying to draw a 2D bitmap image to the screen (or defined space on the window) in Opengl context. I am using the SDL and SDL_Image libraries. I’m getting an unhandled win32 exception when I run the code posted below. I think I also need to draw the quad where the texture will go on the screen (buffer), but have not done that. The program didn’t return this error when I had GL_BITMAP in glTexImage2D instead of BL_UNSIGNED_BYTE, but in that situation the window was blank. What am I doing wrong in this code?
/* Draw */
SDL_Surface *surface;
char *image_path = "helloworld.bmp";
int x_pos = 720;
int y_pos = 450;
GLuint mTextureWidth = 720;
GLuint mTextureHeight = 450;
SDL_Surface *image = IMG_Load ( image_path );
if ( !image )
{
std::cout << "Error Cannot load image";
}
// Draws the image on the surface:
SDL_Rect rcDest = { x_pos, y_pos, 0, 0 };
SDL_BlitSurface ( image, NULL, surface, &rcDest );
// something like SDL_UpdateRect(surface, x_pos, y_pos, image->w, image->h); is missing here
SDL_FreeSurface ( image );
glEnable(GL_TEXTURE_2D);
GLuint textures;
glGenTextures(1, &textures); //Number of textures stored in array name specified
glBindTexture(GL_TEXTURE_2D, textures);
/* Map the surface to the texture in video memory */
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 720, 450, 0, GL_RGB, GL_UNSIGNED_BYTE, surface); //GL_BITMAP,
glBindTexture(GL_TEXTURE_2D, textures);
//Render texture quad
glBegin( GL_QUADS );
glTexCoord2f( 0.f, 0.f ); glVertex2f( 0.f, 0.f );
glTexCoord2f( 1.f, 0.f ); glVertex2f( mTextureWidth, 0.f );
glTexCoord2f( 1.f, 1.f ); glVertex2f( mTextureWidth, mTextureHeight );
glTexCoord2f( 0.f, 1.f ); glVertex2f( 0.f, mTextureHeight );
glEnd();
glDisable(GL_TEXTURE_2D);
//Do I need to create another GL_QUADS here to put the texture on the screen?
glPopMatrix(); //End rendering phase
//Draw to screen
SDL_GL_SwapBuffers();
SDL_Delay(10); //Hold rendered frame on screen for indicated amount of time