Why glPushAttrib?

Why is calling glPushAttrib() important when rendering a display list font?
I commented it out and it works fine without it. I don’t see any purpose for it.

void PrintString(char *mes) {
glPushAttrib(GL_LIST_BIT);
glListBase(myfont_listbase);
glCallLists( strlen(mes), GL_UNSIGNED_BYTE, mes );
glPopAttrib();
}

This way you can make sure, that your font-rendering code does not get into conflict with any other code. Every change to a state, that it does, will be made undone, which results in very nice and clean code. This reduces the probability for errors in the code and headaches on your side.

Jan.

If protecting state change is important, then why isn’t every single GL command
surrounded by Push and Pop commands, like glPolygonMode or glShadeModel, etc…?

The point in this example is that with glPushAttrib the “PrintString” function is state-neutral. So you can call it from any point in your program without worrying about it changing some state that you might want preserved.

Consider for example some code that does draw using a display list and that relies on some particular list base. If you want to use the PrintString function inside this code you have to remember to always reset the list base after you call it. With Push/PopAttrib you don’t have to worry about it…