# How to reset from an arbitrary (unknown) OpenGL state?

**URL:** <https://discourse.libcinder.org/t/how-to-reset-from-an-arbitrary-unknown-opengl-state/706>\
**Category:** Using Cinder\
**Created:** [May 30, 2017, 10:48am UTC](https://discourse.libcinder.org/t/how-to-reset-from-an-arbitrary-unknown-opengl-state/706 "2017-05-30T10:48:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 30, 2017, 10:48am UTC](https://discourse.libcinder.org/t/how-to-reset-from-an-arbitrary-unknown-opengl-state/706/1 "2017-05-30T10:48:28Z")

</div>

Hi there, I have some “black box” OpenGL code, which is very complicated and “has its way” with the OpenGL state. Is there some recipe to get Cinder (v0.9.0 on macOS) back into a known state after calling into my black box code, so it can render its own primitive types (for example using `gl::drawStrokedCube()` or `gl::drawLine()`)?

I think the problem comes from the VAO and shader program. If I reset it to null (using `gl::ScopedVao vao(nullptr)`), I get OpenGL errors (`GL_INVALID_OPERATION`). If I set the shader program to null (using `gl::ScopedGlslProg prog(nullptr)`), then the call to draw a stroked cube or line returns an error and does nothing (they expect a shader program to be bound, whereas it seems to replace the context’s default VAO using `getDefaultVao()`).

I’ve also tried explicitly setting the shader program this way, prior to drawing Cinder line primitives:

```auto
    gl::ScopedGlslProg prog(gl::context()->getStockShader(gl::ShaderDef().color()));

```

In either case, I get OpenGL errors. If I add my own checks for glGetError() after each line in the Cinder draw function, I see the error occurs in the call to `gl::enableVertexAttribArray(posLoc)` in `drawStrokedCube()` (it’s enabling the `posLoc` for `geom::Attrib::POSITION`, which is set to 0, which sounds like a reasonable value). With different combinations of state setting – I may get the OpenGL error in other places.

Can someone give me a “formula” for getting Cinder back in a valid state, so it can draw its own primitive types?

(By the way, everything works fine – it draws with no OpenGL errors – if I first draw an arbitrary Cinder `gl::Batch` with its own shader program, and then afterward call `gl::drawLine()` in the same scope).

Thanks,  
Glen.

---

<div class="post-metadata">

**Author:** ![andrewfb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/andrewfb/32/2_2.png) [@andrewfb](https://discourse.libcinder.org/u/andrewfb)\
**Post date:** [May 30, 2017, 3:14pm UTC](https://discourse.libcinder.org/t/how-to-reset-from-an-arbitrary-unknown-opengl-state/706/2 "2017-05-30T15:14:20Z")

</div>

Hi - the integration with AntTweakBar has the same challenge. You might start by looking at the implementation of `pushGlState()` and `popGlState()`:

> <https://github.com/cinder/Cinder/blob/release_v0.9.1/src/cinder/params/Params.cpp#L75>

In particular the `restoreInvalidated*()` family of calls in `popGlState()` might be useful.

---

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 31, 2017, 7:59am UTC](https://discourse.libcinder.org/t/how-to-reset-from-an-arbitrary-unknown-opengl-state/706/3 "2017-05-31T07:59:57Z")

</div>

Yes, that’s exactly what I was looking for. Simply pushing/popping the GlslProg, plus doing a `restoreInvalidatedVao()` after calling my external code were enough to do the trick (in my case). If I have problems with other state, I know I can push/pop those as well.

Thanks, Andrew, for the quick reply!

(Maybe these “heavy” push/pop methods (in Params.cpp, above) should be exposed as part of the Cinder API…?)
