Displaying Text On the Scene

/* A modification of the shadow program of the box with rotating light source
which adds atext window for displaying diagnostics. */

#include <math.h>
#include <GL/glut.h>
#include <stdio.h>

typedef GLfloat point3[3];
GLfloat LightRadius=4;
GLfloat LightHeight=2;
GLfloat Lx=LightRadius,Ly=LightHeight,Lz=0;
int sub,cube;
double PI,PI2;
GLfloat m[16]={
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,0};

GLfloat t=0;
// List all vertices in array and reference later by index
point3 p[8]={
{0,0,0},
{0,0,1},
{1,0,1},
{1,0,0},
{0,1,0},
{0,1,1},
{1,1,1},
{1,1,0}
};

void drawPoint(int i,int shadow){
// Draw point i in color i
if(shadow)glColor3f(0,0,0);
else glColor3fv(p[i]);
glVertex3fv(p[i]);
}

void drawFace(int i1,int i2,int i3,int i4, int shadow){
// draw a face of the cube
glBegin(GL_POLYGON);
drawPoint(i1,shadow);
drawPoint(i2,shadow);
drawPoint(i3,shadow);
drawPoint(i4,shadow);
glEnd();
}
void myinit(void)
{
PI=atan(1)*4;
PI2=2*PI;
m[7]=-1/Ly;
/* attributes */
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
glColor3f(0.0, 0.0, 0.0); /* draw in black */
/* set up viewing */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w=.1;
glFrustum(-w,w,-w,w,.2,10);
}

void drawCube(int shadow)
{
drawFace(0,3,2,1,shadow); //bottom
drawFace(1,2,6,5,shadow); //front
drawFace(2,3,7,6,shadow); //right side
drawFace(0,1,5,4,shadow); //left side
drawFace(3,0,4,7,shadow); //back side
drawFace(4,5,6,7,shadow); //top
}

void display( void )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /*clear the window */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4,2,4,0,0,0,0,1,0);
drawCube(0);
glTranslatef(Lx,Ly,Lz); //translate back
glMultMatrixf(m);
glTranslatef(-Lx,-Ly,-Lz);
drawCube(1);
glFlush(); /* clear buffers */
glutSwapBuffers();
}

void printstring(char *s,int &x,int &y){
int i;
int len = strlen(s);
glRasterPos2i(x,y); // set the initial cursor position
for( i = 0; i < len; i++ ) // draw each character in string s
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, s[i]);
y-=20;
}

void subdisplay( void )
{
//glutSetWindow(sub);
// We don't want a perspective view of text!
int x=5,y=130;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 150, 0, 150);
glMatrixMode(GL_MODELVIEW);
glColor3f(1,0,0);
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
int i;
char s[100]="Hello, world!";
sprintf(s,"Height %4.1f",Ly);
printstring(s,x,y);
printstring("Hi again",x,y);
glFlush();
glutSwapBuffers();
glutSetWindow(cube); // make cube the current window
}

void idle()
{
t+=.1;
Lx=LightRadius*cos(t);
Ly=LightHeight;
Lz=LightRadius*sin(t);
m[7]=-1/Ly;
if(t>=PI2){
t-=PI2;
LightHeight+=.5;
}
glutPostRedisplay();
glutSetWindow(sub); // force redisplay of sub window:
glutPostRedisplay();

}

void main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500); /* 500 x 500 pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
cube=glutCreateWindow("Cube"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
glutIdleFunc(idle);
myinit(); /* set attributes */
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glEnable(GL_SMOOTH); /* Enable smooth shading and color interpolation */
// make subwindow in upper left
sub=glutCreateSubWindow(cube,0,0,150,150);
// This will be a text window -- we want colors, not lighting:
glDisable(GL_DEPTH_TEST);
glDisable(GL_SMOOTH); /*enable smooth shading */
glDisable(GL_LIGHTING); /* enable lighting */
glDisable(GL_LIGHT0); /* enable light 0 */
glutDisplayFunc(subdisplay);
glutMainLoop(); /* enter event loop */
}