/* Copyright (c) Mark J. Kilgard, 1997. *//* 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. *//* This program was requested by Patrick Earl; hopefully someone else   will write the equivalent Direct3D immediate mode program. */#include <string.h>#include <stdio.h>#include <stdarg.h>#include <GL/glut.h>GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */voidstroke_output(GLfloat Scale, GLfloat x, GLfloat y, GLfloat z, char *format,...){  va_list args;  char buffer[200], *p;  va_start(args, format);  vsprintf(buffer, format, args);  va_end(args);  glPushMatrix();  glTranslatef(x, y, z);  glScalef(0.005*Scale, 0.005*Scale, 0.005*Scale);  for (p = buffer; *p; p++)    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);  glPopMatrix();}voiddrawAxes(void) {  glDisable(GL_LIGHTING);   glBegin(GL_LINES);   glColor3f(1.0, 1.0, 1.0);   glVertex3f(-2.0, 0.0, 0.0);   glVertex3f( 2.0, 0.0, 0.0);   glVertex3f( 0.0, -2.0, 0.0);   glVertex3f( 0.0,  2.0, 0.0);   glVertex3f( 0.0, 0.0, -2.0);   glVertex3f( 0.0, 0.0, 2.0);   glEnd();      stroke_output(0.5, 2.0, 0.0, 0.0, "X");   stroke_output(0.5, 0.0, 2.0, 0.0, "Y");   stroke_output(0.5, 0.0, 0.0, 2.0, "Z");}voiddrawBox(void){  glEnable(GL_LIGHTING);    glBegin(GL_QUADS);    glNormal3f(0.0,0.0,-1.0); /*Front side 1 2 3 4*/    glVertex3f( 1.0,  1.0,  1.0); glVertex3f( 1.0, -1.0,  1.0);    glVertex3f(-1.0, -1.0,  1.0); glVertex3f(-1.0,  1.0,  1.0);    glNormal3f(-1.0, 0.0, 0.0); /*Left side 4 5 6 3*/    glVertex3f(-1.0,  1.0,  1.0); glVertex3f(-1.0, 1.0, -1.0);    glVertex3f(-1.0, -1.0, -1.0); glVertex3f(-1.0,-1.0,  1.0);      glNormal3f(1.0, 0.0, 0.0); /*Right side 8 1 2 7*/    glVertex3f( 1.0,  1.0, -1.0); glVertex3f( 1.0,  1.0,  1.0);    glVertex3f( 1.0, -1.0,  1.0); glVertex3f( 1.0, -1.0, -1.0);    glNormal3f(0.0, 0.0, -1.0); /*Back side 5 6 7 8*/    glVertex3f(-1.0,  1.0, -1.0); glVertex3f(-1.0, -1.0, -1.0);    glVertex3f( 1.0, -1.0, -1.0); glVertex3f( 1.0,  1.0, -1.0);    glNormal3f(0.0, 1.0, 0.0); /*Top side 8 5 4 1*/    glVertex3f( 1.0,  1.0, -1.0); glVertex3f(-1.0,  1.0, -1.0);    glVertex3f(-1.0,  1.0,  1.0); glVertex3f( 1.0,  1.0,  1.0);    glNormal3f(0.0, -1.0, 0.0); /*Bottom side 2 3 6 7*/    glVertex3f( 1.0, -1.0,  1.0); glVertex3f(-1.0, -1.0,  1.0);    glVertex3f(-1.0, -1.0, -1.0); glVertex3f( 1.0, -1.0, -1.0);;    glEnd();}voiddisplay(void){  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  drawAxes();  drawBox();  glutSwapBuffers();}voidinit(void){  /* Enable a single OpenGL light. */  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);  glLightfv(GL_LIGHT0, GL_POSITION, light_position);  glEnable(GL_LIGHT0);  glEnable(GL_LIGHTING);  /* Use depth buffering for hidden surface elimination. */  glEnable(GL_DEPTH_TEST);  /* Setup the _view of the cube. */  glMatrixMode(GL_PROJECTION);  gluPerspective( /* field of view in degree */ 40.0,    /* aspect ratio */ 1.0,    /* Z near */ 1.0, /* Z far */ 10.0);  glMatrixMode(GL_MODELVIEW);  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */    0.0, 0.0, 0.0,      /* center is at (0,0,0) */    0.0, 1.0, 0.);      /* up is in positi_ve Y direction */  /* Adjust cube position to be asthetic angle. */  glTranslatef(0.0, 0.0, -1.0);  glRotatef(60, 1.0, 0.0, 0.0);  glRotatef(-20, 0.0, 0.0, 1.0);}intmain(int argc, char **argv){  glutInit(&argc, argv);  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  glutCreateWindow("red 3D lighted cube");  glutDisplayFunc(display);  init();  glutMainLoop();  return 0;             /* ANSI C requires main to return int. */}