Stencil Buffers Are Rad

I started playing with the stencil buffer the other day and apparently it isn’t as mystical as I once thought. A couple of years ago, back when I was still learning how to use openGL and couldn’t upload data to a vertex buffer without failing miserably and copypasting code from everywhere, I tried to use the stencil buffer, and I failed miserably. So I gave up and tried learning about textures instead. A few days ago I got distracted and decided to try again and I totally figured them out. Turns out it’s actually really simple and I was just being bad. After a few hours of work, I had these rendering.

stencil7 stencil5 stencil3

Pretty simple to do but I think they look pretty cool. Next day I decided to take it up a notch. So I tried portal rendering in 3D. And it works. Not crazily complex, but cool for the time I put in.

And I managed that with (almost) no tutorials. At one stage things were drawing weird, e.g., the scenes in the portals were drawing outside the portals sometimes, and I just couldn’t figure out why, so I looked up some tutorials to see where I went wrong. Turns out glClear doesn’t work on buffers that have write masks set to zero (which I was doing while rendering). Protip: reset write masks after messing with them. It seems super obvious but it wasn’t at the time so keep that in mind.
But yeah, I’ve determined that stencil buffers can make things look really cool relatively easily. 10/10 recommend looking into them if you haven’t already. I will most certainly be messing with them more.


Hello, I’m Patrick Monaghan, a games programmer in training. The purpose of this blog is to track the progress of my various projects relating to game development and various experiments I decide to try.A little bit about myself, I'm fluent in c++. I'm familiar with a few other languages also. I have a working knowledge of blender and gimp, thus can pump out programmer art like it’s going out of style. I’m always willing to learn new things and I always welcome feedback if it means I can improve. Also, here's a youtube channel And my github And my soundcloud Also I'm @_manpat on twitter Finally here's a todo list that I'm putting here so I can't ignore it

Leave a Reply

Your email address will not be published. Required fields are marked *