Difference between gl::GlslProg and gl::Scoped GlslProg?

Can someone explain the Difference between gl::GlslProg and gl::ScopedGlslProg? What do “scoped states” do?!

I’m sure someone more knowledgable than me will add to this / correct me, but scoped states are exactly what they sound like, they only exist in the current scope.

When the gl::ScopedGlslProg goes out of scope, the shader is no longer bound. If you look at the following example:

gl::drawSolidCircle(vec2(50,50), 50); // will draw a white circle

    gl::ScopedColor scpColor(1.f,0.f,0.f); // create a scoped variable and set color to red
    gl::drawSolidCircle(vec2(100,50), 50); // will draw a red circle
    // 'scpColor' will go out of scope here, restoring the color to white.
gl::drawSolidCircle(vec2(150,50), 50); // will draw a white circle

By the time the third drawSolidCircle() method is called, the gl::ScopedColor is out of scope so is no longer applicable, the same would apply with a GlslProg that’s gone out of scope.

Thank you so much. It makes sense. I am new to cinder and even to c++ so
just wanted to confirm my understanding of various concepts of cinder.

The scoped classes follow the programming principle / idiom called “RAII” (Resource Acquisition Is Initialization). It helps to prevent situations where you enable or initialize something, but subsequently forget to disable or destroy it when no longer needed.

In the case of the draw color, the gl::ScopedColor will automatically store the current draw color, then set the color to the one specified. When it’s destroyed, the color is restored to the previous color, making sure that your OpenGL state is the same at the end of your function as it was at the beginning.

If possible, try to use the scoped classes as much as possible. Your application will be more stable and predictable that way. You can also create your own scoped classes, it’s a great technique for a lot of things. Just look at the Cinder source to see how.


Hey paul.houx thank you. This is very valuable information. Helps me
understand c++ better.

I am glad that you mentioned “programming idioms”. I would have never known
that there is something called “programming idioms”. Now I am increasing my
awareness about other such programming idioms :blush: