EXAMPLE OF USING GLUT FONTS

GLUT provides both 3-dimensional and 2-dimensional fonts:
-- Stroke Fonts and Bitmap Fonts (respectively).
GLUT Stroke Fonts are subject to transformations, and are illustrated below.
Note: these fonts are relative large (about 120 units at highest point, and descending 33 units at lowest). Chen Li suggests that you either scale it down between 0.001 to 0.01, or you place your eye/camera position far enough.
In the demo below, a message (default: "GLUT means OpenGL") is displayed and it slowly rotates.
-- The window size is 2000 units square.
-- An orthographic projection is used.
-- A popup menu can change the message and the font used.
After the message is stroked, it is transformed as follows (in this order, by right multiplication):
glTranslatef(-750,0,0) -- move center of message to origin
glRotatef(angle, 0,0,1) -- rotate message
glTranslatef(1000,1000,0) -- move rotated message to center of screen
See if you can locate these transformations in the program.
Here is the c program. See below for the commented (html) version.





/* Copyright (c) Mark J. Kilgard, 1994. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */


#include <string.h >
#include <GL/glut.h >


// Following are the two types of stroke fonts in GLUT:

void *fonts[] =
{GLUT_STROKE_ROMAN, GLUT_STROKE_MONO_ROMAN};


// GLUT_STROKE_ROMAN has variable width:

void *font = GLUT_STROKE_ROMAN;

char defaultMessage[] = "GLUT means OpenGL.";
char *message = defaultMessage;

int angle = 0;

void
selectFont(int newfont)
{
  font = fonts[newfont];
  glutPostRedisplay();
}

void
selectMessage(int msg)
{
  switch (msg) {
  case 1:
    message = "abcdefghijklmnop";
    break;
  case 2:
    message = "ABCDEFGHIJKLMNOP";
    break;
  }
}


void
tick(void)
{
  angle -= 2;
  glutPostRedisplay();
}



void
display(void)
{
  int len, i;

  glClear(GL_COLOR_BUFFER_BIT);
    // Question: Why do we Push and Pop Matrix?
  glPushMatrix();
  glRotatef(angle, 0.0, 0.0, 1.0);
  glTranslatef(-750, 0, 0);
  len = (int) strlen(message);
  for (i = 0; i < len; i++) {
    glutStrokeCharacter(font, message[i]);
  }
  glPopMatrix();
  glutSwapBuffers();
}


int
main(int argc, char **argv)
{
  int i, submenu;

  glutInit(&argc, argv);
  for (i = 1; i < argc; i++) {
    if (!strcmp(argv[i], "-mono")) {
      font = GLUT_STROKE_MONO_ROMAN;
    }
  }
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(600, 600);
  glutCreateWindow("anti-aliased stroke font");
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, 2000, 0, 2000);
  glMatrixMode(GL_MODELVIEW);
  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glLineWidth(3.0);
  glTranslatef(1000, 1000, 0); 
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glColor3f(1.0, 1.0, 1.0);
  glutDisplayFunc(display);
  glutIdleFunc(tick); 
  submenu = glutCreateMenu(selectMessage);
  glutAddMenuEntry("abc", 1);
  glutAddMenuEntry("ABC", 2);
  glutCreateMenu(selectFont);
  glutAddMenuEntry("Roman", 0);
  glutAddMenuEntry("Mono Roman", 1);
  glutAddSubMenu("Messages", submenu);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}