PDA

View Full Version : Rendering Image Texture to the Screen (Program closes unexpectedly after 5 seconds)



amorag59
12-03-2014, 09:03 PM
I posted on here recently asking how to load a bmp image to the screen, and that seemed to work fine. I load the file to a surface, assign the surface to a quad and so on. Before I go on, I'll state that I understand some of these functions are depreciated. The first program I made from the other post works on both machines this program will ever be run on, so I'll gradually get into the more updated way of doing things with shaders. So anyway, I run the code below, it goes for around 5 seconds then terminates with "status 3." What am I doing wrong?




//Section where we draw to the screen

//Clear
glClear(GL_COLOR_BUFFER_BIT);



glPushMatrix(); //Start phase

glOrtho(0,1280,800,0,-1,1); //Set the matrix



/* Draw */
//GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_QUADS, GL_TRIANGLES, GL_POLYGON

glColor4ub(255,255,255,255); //White color


char *image_path = "helloworld.bmp";


SDL_Surface *image = IMG_Load ( image_path );
if ( !image )
{
std::cout << "Error Cannot load image";
}

glEnable(GL_TEXTURE_2D);

//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLuint textures;
glGenTextures(1, &textures); //Number of textures stored in array name specified

glBindTexture(GL_TEXTURE_2D, textures);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Map the surface to the texture in video memory
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 1280, 800, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels); //GL_BITMAP
//SDL_FreeSurface ( image );

glBindTexture(GL_TEXTURE_2D, textures);

//Render texture quad
glBegin( GL_QUADS );
glTexCoord2f( 0.f, 0.f ); glVertex2f(0, 0);
glTexCoord2f( 1.f, 0.f ); glVertex2f(1280, 0);
glTexCoord2f( 1.f, 1.f ); glVertex2f(1280, 800);
glTexCoord2f( 0.f, 1.f ); glVertex2f(0, 800);
glEnd();

glDisable(GL_TEXTURE_2D);
glPopMatrix(); //End rendering phase
SDL_GL_SwapBuffers();//Draw to screen
SDL_Delay(3); //Hold rendered frame on screen for indicated amount of time

amorag59
12-04-2014, 05:50 AM
I fixed this problem by moving the image loading and texture initialization above the main loop. Seems the program was running out of memory when it was loading an image for every frame.