---
deprecated: true
excerpt_separator: <br/>
category: technology
layout: post
title: 'Project TreeWars: How Anna got her Title Screen back'
date: '2011-07-24T08:00:00.000-04:00'
author: Anna Wiggins
tags:
- Programming
- OpenGL
- C++
- TreeWars
- GLSL
modified_time: '2013-10-22T11:19:51.474-04:00'
blogger_id: tag:blogger.com,1999:blog-4209116010564764361.post-5288894543073667200
blogger_orig_url: http://www.stringofbits.net/2011/07/project-treewars-how-anna-got-her-title.html
---

In my <a href="http://stringofbits.net/2011/07/17/project-treewars-opengl-vs-opengl/">last post</a>, I re-implemented all of my rendering code to take advantage of <em>Shaders</em>. 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.<br/><br/>But, at any rate, I've checked every function call I make against the ones used in the tutorial. They all match. Everything is <strong>exactly</strong> the sa...<br/><br/>Oh. Wait.<br/><br/>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:<br/><br/>[sourcecode lang="cpp" gutter="false"]<br/>static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };<br/>[/sourcecode]<br/><br/>And the equivalent line from my code:<br/><br/>[sourcecode lang="cpp" gutter="false"]<br/>GLfloat Renderer::rect_elements[] = {0, 1, 2, 3};<br/>[/sourcecode]<br/><br/>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 <strong>requires</strong> 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.<br/><br/>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:<br/><br/><a href="http://stringofbits.files.wordpress.com/2011/07/treewars_fixedtitle.png"><img src="http://stringofbits.files.wordpress.com/2011/07/treewars_fixedtitle.png" alt="" title="treewars_fixedtitle" width="450" height="349" class="alignnone size-full wp-image-499" /></a><br/><br/>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.<br/><br/>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.<br/><br/>So, my options are:<br/><br/><ol><br/><li>Figure out how to switch 'out' of the Shading pipeline properly and render text with FTGL, or</li><br/><li>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).</li><br/></ol><br/><br/>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.<br/><br/>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...