January 22, 2014

OpenGL - Writing text

Making text on OpenGL is a lot more complicated than I would have thought..  And I still don't really understand everything in the code below.  Or if I'm following the right conventions or something.

Sigh.  Being a complete beginner is rough.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glext.h>
using namespace std;

int width = 500;
int height = 500;

void writeText(string message) {
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 0);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();
  glTranslatef(1.5, 1, 0);

  glLoadIdentity();
  glScalef(.0005,.0005,0);
 
  glColor3f(.1176,.562,1);
  for (size_t i = 0; i < message.length(); i++) {
 glutStrokeCharacter(GLUT_STROKE_ROMAN, message[i]);
  }
}

void display()
{
  glClear(GL_COLOR_BUFFER_BIT);

  string message = "Sup World";
  writeText(message);
  glFlush();
}


///////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitWindowSize(width, height);
  glutCreateWindow("text");
  glutDisplayFunc(display);
  glutMainLoop();
}

Sup: