This post was saved / ported from a previous site migration. You may encounter missing images, dead links, and strange formatting. Sorry about that!

Project TreeWars: How Anna got her Title Screen back

Programming OpenGL C++ TreeWars GLSL

In my last post, I re-implemented all of my rendering code to take advantage of Shaders. After doing this, nothing rendered. Despite the fact that I was following a tutorial, more or less. I have been modifying it to fit my project, which has a lot of code around the rendering code already and is in C++ instead of C, and also modifying it to do something that will actually be useful for me down the line.

But, at any rate, I’ve checked every function call I make against the ones used in the tutorial. They all match. Everything is exactly the sa…

Oh. Wait.

One of the things you create when using shaders is an index buffer (also called an element buffer); a list of what order the vertices of your polygon should be drawn in. From the tutorial:

[sourcecode lang=“cpp” gutter=“false”]
static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };
[/sourcecode]

And the equivalent line from my code:

[sourcecode lang=“cpp” gutter=“false”]
GLfloat Renderer::rect_elements[] = {0, 1, 2, 3};
[/sourcecode]

I got so used to things being GLfloat type that I made my index buffer floats, even though that doesn’t make any sense (you can’t have vertex number 0.5, after all). Not only does it not make sense, OpenGL requires that the element buffer be composed of integers. Even better, if your element buffer is of the wrong type, OpenGL fails silently: no error message, no crash. The rendering simply doesn’t happen.

So, a couple hours of debugging, down to one simple line of code. I really wish GLSL had a way to report meaningful errors back to the program using it. At any rate, after I fix the line:



I’m back to where I was several days ago. But this time, I’m using shaders, which are both less deprecated and more flexible; I’ve set up a framework that will allow me to do more interesting things later on.

Now, on to the next challenge: rendering text. In SDL this was fairly easy; the SDL_ttf library made it pretty simple to render text to the screen. In OpenGL, however, rendering text is a bit trickier. There are a few libraries out there that do it (FTGL seemingly the best option), but they all use the fixed-pipeline functions. I’d even be willing to settle for that, and worry about ripping the code out later and putting in something more shader-friendly, except switching back and forth between Shaders and the fixed pipeline seems to be a bit tricky.

So, my options are:


  1. Figure out how to switch ‘out’ of the Shading pipeline properly and render text with FTGL, or

  2. Use freetype2 directly and implement my own font loading, render the text to a Framebuffer Object, then blit that to the backbuffer (the buffer that represents the next visual frame).



The first option might be easier in the short term, but the latter sounds more robust, all things considered. The problem is that stopping to get font rendering working without any deprecated functions could take quite a while (I’m not even sure on a good estimate for the time). So, I hack FTGL into working and move on.

Now, I’m ready to get things back to the way they looked before I decided GLSL was something that needed to happen. I just need to figure out the best way to draw a circle with GLSL…