Far out brussel sprout

Project Page Next project post Post History

Boy, have I been dealing with a headache the past week. I'm still no closer to completing the last part of The Dark Room's distortion engine, thanks to an annoying bug that's taken me quite sometime to resolve.

It's funny, the last couple of posts have been about bug fixing. And here we are, bug fixing again!

Here's the low down. I use the graphics card to do my viewport drawing, and I've been using a well-known library to achieve this. And in my draw engine code, I've been using custom frame buffer objects to draw into.

In the draw pipeline, parts of the draw need to reuse the currently drawn image, for example, to apply a post-effect, like a blur. To do this, programmers often use the 'ping-pong' method, whereby you alternate between render buffer textures; using one to render into, swap, then rendering the next step while using the previous step's rendered image as a texture in the new render step. You can switch back and forth as many times as you need. It's a really handy way to do post effects in draw engines.

Up until now, my engine only needed to do a few things. Occasionally the issue would rear its ugly head, but mostly, with a bit of fumbling about, I could get it to look like it was working correctly. Until I added another shader to the draw pipeline, and then the draw engine stopped working. I wanted this shader for the tracking interface, so I had to stop as well, and spend time debugging the issue.

I searched for days and days and no matter what I came across, or tried changing in code, nothing solved the issue. Then, I kind of did a bit of a Hail Mary and commented out a function, for no reason other than as a last ditch effort to try something. And to my surprise, the draw started to work again. So I knew my issue was related to this function.

Turns out I had been using a setting for the graphics library incorrectly the whole time. I misunderstood what the setting was meant to do. In fact, the setting wasn't needed at all. So I disabled it and viola, it all works as expected!

Oddly enough, I had read someone having a similar issue online, and while theirs was related, it wasn't the same setting. So their trick didn't work for me. But looking back, the issue was in the same kind of place.

Despite the lost time, it's a relief to have fixed this. Now my draw pipeline works in the expected way. I can 'ping-pong' as many times as needed, with the correct render now showing in the viewport.

The shader I've been working on is a greyscale shader. An example of it working in my engine is below, using the luminance method to calculate the grey levels.

The black and white shader working in The Dark Room viewport

In the image above, you can see my draw pipeline working using the 'ping-pong' draw technique. You can see the selected clip is greyscaled, while other parts, like the world axis, are still in colour.

For more information on what shaders are, here's a Wikipedia article on shaders in OpenGL.