Cleaned up a bit:
#include <GLUT/glut.h>#include <string>#include <sstream>using namespace std;GLfloat ztrans;void drawString(GLint x, GLint y, const string& s) { glPushAttrib(GL_TRANSFORM_BIT | GL_CURRENT_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, 640, 0, 480, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glColor4f(1.0, 1.0, 1.0, 1.0); glRasterPos2i(x, y); for (string::iterator it = s.begin(); it != s.end(); ++it) { glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *it); } glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glPopAttrib();}void draw(){ glTranslatef(0.0, 0.0, ztrans); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glVertex3f(0.0, 0.0, -5.0); glVertex3f(1.0, 0.0, -5.0); glVertex3f(1.0, 1.0, -5.0); glVertex3f(0.0, 1.0, -5.0); glEnd(); stringstream ss("Keys are 'w', 'x' or ESC to exit, ztrans = "); ss << ztrans; drawString(10, 10, ss.str()); glFlush();}void init(){ glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(100.0, 1.0, 0.001, 10000.0); glMatrixMode(GL_MODELVIEW);}void handle_keyboard(unsigned char key, int, int){ switch (key) { case 'w': { ztrans += 0.1; glutPostRedisplay(); break; } case 'x': { ztrans -= 0.1; glutPostRedisplay(); break; } case 27: { exit(0); } }}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Weird Translations"); init(); glutKeyboardFunc(handle_keyboard); glutDisplayFunc(draw); glutMainLoop();}