I'm trying to write modern OpenGL, but have hit something that bugs me.
I have this bit of code:
glUseProgram(skybox_program->id);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->id);
glUniform1i(get_program_uniform(skybox_program, "cubemap_tex"), 1);
//From here
GLint save_cull_mode, save_depth_mode;
glGetIntegerv(GL_CULL_FACE_MODE, &save_cull_mode);
glGetIntegerv(GL_DEPTH_FUNC, &save_depth_mode);
glCullFace(GL_FRONT);
glDepthFunc(GL_LEQUAL);
//To here
glUniformMatrix4fv(get_program_uniform(skybox_program, "camera"), 1, GL_FALSE, cam_rot.mat);
glBindVertexArray(skybox_vao);
glDrawArrays(GL_TRIANGLES, 0, 6 * 2 * 3);
//And these next 2 lines
glCullFace(save_cull_mode);
glDepthFunc(save_depth_mode);
glUseProgram(0);
This code, as you probably realized, draws a skybox. Part of this is disabling culling (so we can see the box while inside it) and changing the depth func (basically an optimization, part of which is in the shader and part here). I want to save and restore these values, but because I can't use gl[Push/Pop]Attrib
, I have to do it myself. This isn't a big deal with just two attributes, but if I used more this would quickly become a huge pain (and, as I understand, slow, since glGet*
isn't very fast). I read that the push/pop functions were deprecated because they were mostly used to handle FFP state, but this obviously isn't FFP state and yet the push/pop functions would be a good fit to use. What should I use instead? Or do I have to just deal with it?
glDepthFunc (...)
directly, you write a layer on top of it, you can always know the state without having to query it. – Evasionclass DepthFuncSetter { public: DepthFuncSetter(GLenum f) { glGetIntegerv(GL_DEPTH_FUNC, &m_oldSetting); glDepthFunc(f); } ~DepthFuncSetter() { glDepthFunc(m_oldSetting); } private: GLenum m_oldSetting; };
– Pasol