Since glPushAttrib/glPopAttrib are deprecated, what is the new way to save attributes like GL_DEPTH_FUNC?
Asked Answered
G

1

12

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?

Gyral answered 11/1, 2014 at 18:54 Comment(3)
Wrap your state management. If instead of calling glDepthFunc (...) directly, you write a layer on top of it, you can always know the state without having to query it.Evasion
Probably such layer already exists in the driver, in order to avoid to do a round trip to the GPU (if you do several calls in a row setting the same depth/cull mode). Apart from that, you could use C++ and RAII. class DepthFuncSetter { public: DepthFuncSetter(GLenum f) { glGetIntegerv(GL_DEPTH_FUNC, &m_oldSetting); glDepthFunc(f); } ~DepthFuncSetter() { glDepthFunc(m_oldSetting); } private: GLenum m_oldSetting; };Pasol
I am using C, so sadly I can't use RAII. I guess I could write some sort of handler in that vein, but I was hoping there is something built-in.Gyral
L
6

I think this forum post on opengl.org pretty much sums it up: http://www.opengl.org/discussion_boards/showthread.php/173957-glPushAttrib-depreciated-replace-by

Basically OpenGL state is expected to be handled by the client, because with the move to shaders most of the attributes will be stored in your program anyway. It's part of the push to remove the old fixed-function pipeline from OpenGL.

Larrisa answered 20/3, 2014 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.