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

**URL:** <https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792>\
**Category:** Using Cinder\
**Created:** [July 18, 2017, 4:46pm UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792 "2017-07-18T16:46:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)\
**Post date:** [July 18, 2017, 4:46pm UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792/1 "2017-07-18T16:46:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Craigson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/craigson/32/43_2.png) [@Craigson](https://discourse.libcinder.org/u/Craigson)\
**Post date:** [July 18, 2017, 4:56pm UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792/2 "2017-07-18T16:56:39Z")

</div>

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::color(Color::white());
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.

---

<div class="post-metadata">

**Author:** ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)\
**Post date:** [July 18, 2017, 5:11pm UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792/3 "2017-07-18T17:11:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 18, 2017, 9:33pm UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792/4 "2017-07-18T21:33:26Z")

</div>

The scoped classes follow the programming principle / idiom called “`RAII`” ([Resource Acquisition Is Initialization](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii)). 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](https://github.com/cinder/Cinder/blob/master/include/cinder/gl/scoped.h) to see how.

-Paul

---

<div class="post-metadata">

**Author:** ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)\
**Post date:** [July 19, 2017, 4:05am UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792/5 "2017-07-19T04:05:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)\
**Post date:** [July 19, 2017, 4:43am UTC](https://discourse.libcinder.org/t/difference-between-gl-glslprog-and-gl-scoped-glslprog/792/6 "2017-07-19T04:43:48Z")

</div>

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 😊
