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

/* The Red Shape Program */
/* This program display a red 3D shape using the glut shape library */

#include <GLUT/glut.h>

int ShapeChoice;
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
display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  switch (ShapeChoice) {
     case 1: glutSolidTeapot(1.0); break;
     case 2: glutWireTeapot(1.0); break;
     case 3: glutSolidSphere(1.0,10,10); break;
     case 4: glutWireSphere(1.0,10,10);
     }
  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 shape. */
  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 shape position to be asthetic angle. */
  /* Does not effect shape */
  glTranslatef(0.0, 0.0, -1.0);
  glRotatef(60, 1.0, 0.0, 0.0);
  glRotatef(-20, 0.0, 0.0, 1.0);
}

void HandleMenu(int Choice) {
  ShapeChoice = Choice;
  glutPostRedisplay();
}

void InitMenus(void) {
  glutCreateMenu(HandleMenu);
  glutAddMenuEntry("Solid Teapot",1);
  glutAddMenuEntry("Wire Teapot", 2);
  glutAddMenuEntry("Solid Sphere", 3);
  glutAddMenuEntry("Wire Sphere", 4);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
  ShapeChoice = 3;
  }

int
main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("3D lighted shape");
  glutDisplayFunc(display);
  InitMenus();
  init();
  glutMainLoop();
  return 0;         
}
