The proper way to save/restore OpenGL ES state
I know that to save/restore matrix state you use standard push/pop operations. I haven't been able to 开发者_如何学Cfind a decent convention for saving/restoring state such as when using glLineWidth() or glColor4ub().
It seems not really necessary as long as you set your line width or color up properly whenever you want to draw anything but I can see an example of where you are in your own drawing function, call some separate drawing function that changes the line width on you without your knowledge, and then when you go to continue drawing have it come out not as expected.
To fix that you can reset your line width after you call any function that you don't know what it does but that seems inelegant and I feel that there should be already be a solution to this but that I've missed it somehow.
For my own code I've been doing this at the beginning of every drawing function:
GLfloat savedLineWidth = 1.0f;
glGetFloatv(GL_LINE_WIDTH, &savedLineWidth);
glLineWidth(lineWidth);
And then restoring the state at the end of the drawing function like so:
glLineWidth(savedLineWidth);
That doesn't really help me though unless I assume that every drawing function that I don't know the insides of is nice and restores the state on its own.
Should I make that assumption? Or am I missing something?
If this was plain OpenGL (not ES variant) then you could push the attribute like:
glPushAttrib(GL_LINE_WIDTH);
glLineWidth(2.0f);
... etc ...
glPopAttrib();
Alas, in OpenGL ES this isn't available. Your approach of getting and setting is about as good as you'll get.
Just try to reduce the number of times you do a get as this is a very time consuming process. Unlike sending, getting isn't buffered.
Maybe your approach should be to just not worry. Whenever you want to set the line width then just set it. Line width shouldn't affect too much.
Tough call. Depends on what these other drawing functions are. Do you have the code? Maybe you could look?
精彩评论