View Full Version : Why glPushAttrib?

05-07-2005, 12:24 PM
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) {
glCallLists( strlen(mes), GL_UNSIGNED_BYTE, mes );

05-07-2005, 02:04 PM
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.


05-08-2005, 05:15 AM
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..?

05-08-2005, 07:55 AM
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...