Wednesday 23 July 2008

Was glut initialised?

I created small programs with freeglut and OpenGL that do useful stuff for me. But, when I tried to call its functions from other programs, I had lots of problems with glutInit.

The most common message I saw was
illegal glutInit() reinitialization attempt

Some of these problems I solved using the glutLeaveMainLoop. But not all of them. I needed to find out if glut was considered initialized or not. If only I could access the contents of the freeglut internal fgState.Initialised. But it is not possible without recompiling freeglut.

The solution I found is to call
int glut_time = glutGet(GLUT_ELAPSED_TIME);
if(glut_time > 0){
glutInit(&argc, argv);
}

This is the only way I found to know whether glut is initialised or not.

Leaving glutMainLoop without killing the application

I am working with OpenGL since a few months ago and enjoying it a lot. I was using glut to create my interfaces but it sometimes is quite limited.

A few days ago I was trying to stop and restart the glut main loop. After thinking and searching a lot, I found the function
void glutLeaveMainLoop(void)

It is available in freeglut, but not in glut. I wanted it to just leave the main loop, but its default behavior is... it does not just leave the main loop, it closes the application too.

To function as I expected, I must, after calling glutInit, call
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

I could not find it documented anywhere, so, here it is... Thanks for reading.