简体   繁体   中英

Creating an Environment Stack in OpenGL

I'd like to create an abstraction in OpenGL of the environment settings(blending, stenciling, depth, etc.) that works like the matrix stack. Push onto the stack, make any changes you need, draw your objects, then pop the stack and go back to the prior settings.

For example, currently you might have drawing code like this:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
//Draw operations
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND); 

But with an environment stack it would look like this:

glPushEnv();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
//Draw operations
glPopEnv(); 

As I see it there are only 2 ways to do this:

  1. Create my own 'flavor' of each environment setting function and call that. It will in turn update the current EnvStack data structure and call the OpenGL environment function.
  2. Alter the OpenGL environment functions to point to environment functions, which will again update the current EnvStack data structure and call the original OpenGL environment functions. 环境函数,这将再次更新当前的EnvStack数据结构并调用原始OpenGL环境函数。

So option 1 is obviously much simpler. But I run into a problem if I'm using other peoples code in that I don't necessarily know what changes it's making to the environement and therefor my data structure would be out of sync. And since the whole point is to have a simple method of ensuring the environment settings are correct, this is not cool.

So my question is, in this context, how do I change the functions that the OpenGL environment functions point to?

OpenGL already contains this functionality. You want glPushAttrib(GL_ALL_ATTRIB_BITS); and glPopAttrib(); . See http://opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/pushattrib.html for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM