According to OpenGL documentation,
3.1 glutMainLoop
glutMainLoop enters the GLUT event loop.
Using
void glutMainLoop(void);
Description
glutMainLoop is part of the GLUT event loop. This procedure must be called no more than once in the GLUT program. After the call, this procedure will never return. It will call as necessary any callbacks that have been registered.
Therefore, whenever glutMainLoop () is called, it will never return. As a result, I could not free my memory after allocation. My problem: I need to load an image from a file, the solution of the book (Superbible 4th edition) is to place this boot file procedure inside the drawing function. However, I realized that this method is too expensive due to several opening and closing files. I recalled from my class Data structure when studying the B-tree, the cost of accessing external resources is significant, so I try to avoid as much as I can. So my alternative solution is to put this loading image procedure inside an installed scene function that is called only once. But now I am facing another problem, I cannot delete the memory due toglutMainLoop. What can I do in this situation? I am new to openGL, so I really don't know how to deal with this particular problem. Any idea would be greatly appreciated.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "Utility.h"
#include "TgaHeader.h"
#include "TgaImage.h"
#include <GL/glut.h>
using namespace std;
TgaImage* image = NULL;
void setupScene() {
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
image = loadTgAFile( "Fire.tga" );
}
void renderScene() {
glClear( GL_COLOR_BUFFER_BIT );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glRasterPos2i( 0, 0 );
if( image != NULL ) {
glDrawPixels(
image->header.width,
image->header.height,
image->format,
GL_UNSIGNED_BYTE,
image->pixels
);
}
glutSwapBuffers();
}
void resizeWindow( int w, int h ) {
if( h == 0 ) {
h = 1;
}
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0f, w, 0.0f, h );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
int main( int argc, char** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize( 512, 512 );
glutCreateWindow( "Image" );
glutReshapeFunc( resizeWindow );
glutDisplayFunc( renderScene );
setupScene();
glutMainLoop();
delete image;
}
Thank,