acdemiralp / gl

Header-only C++17 wrapper for OpenGL 4.6 Core Profile.
Boost Software License 1.0
158 stars 11 forks source link

State tracker. #4

Open acdemiralp opened 7 years ago

acdemiralp commented 7 years ago

State leaking is a common problem in OpenGL. Many free functions set a global state which often has to be unset at the end of render calls. Find a way to "track" this state and unset it on scope exits, perhaps similar to a std::lock_guard.

Example:

{
  gl::state_guard sg;

  gl::set_depth_test_enabled(false);
  gl::set_depth_function(GL_EQUAL);
  gl::set_front_face(GL_CCW);
  gl::set_cull_face(GL_BACK);
} 
// Depth test, depth function, front face and cull face are reverted to the defaults here.
acdemiralp commented 6 years ago

Two implementation options:

Since this functionality is auxiliary and the library is intended to be a thin wrapper, I would rather not pollute the existing global state functions with state tracking code, and go with the first approach.