/*  Program:  teapot1.c  
    This program uses a simple menu that allows the user to decide if
    the teapot should be rotated or stopped.  It uses the function
    glutIdleFunc() to control what the computer does when it is not
    processing interrupts.
*/

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

static GLfloat spin = 0.0;
GLint toggle = 0;

static GLfloat light0_position[] = {2.0, 8.0, 2.0, 0.0};
//GLfloat mat_specular[]         = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[]        = {50.0};
GLfloat mat_amb_diff_color[]     = {0.5, 0.7, 0.5, 0.5};
//GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_ambient[] = {0.15, 0.15, 0.15};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};

void init () {
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_SMOOTH);   
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);  
}

void display (void)
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
 // glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 
  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff_color);
  glLoadIdentity();
  gluLookAt(2.0, 4.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  glPushMatrix();
  glRotatef(spin, 0.0, 1.0, 0.0);
  glScalef(2.0, 2.0, 2.0);
  glutSolidTeapot(1.0);
  glPopMatrix();
  glutSwapBuffers();
}



void reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode (GL_PROJECTION);	
  glLoadIdentity ();	/*  define the projection  */
  gluPerspective(45.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void spinDisplay()
{ spin += 5.0;
  if (spin >360.0) 
    spin = spin - 360.0;
  glutPostRedisplay();
}

#define SPIN	1
#define STOP	2
#define QUIT	3

void menu (int item)
{
 switch(item)  {
 case  SPIN:
      glutIdleFunc(spinDisplay);
      break;
 case  STOP:
      glutIdleFunc(NULL);
      break;
 case  QUIT:
      exit(0);
      break;
 }
}

int main(int argc, char** argv)
{
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition (0, 0);
  glutInitWindowSize(400, 400);
  glutCreateWindow ("Scaling Transformation");
  init ();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutCreateMenu(menu);
  glutAddMenuEntry("Spin the Picture", SPIN);
  glutAddMenuEntry("Stop Spinning", STOP);
  glutAddMenuEntry("Quit the Program", QUIT);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
//  glutMouseFunc(mouse);
  glutMainLoop();
  return 0;
    
}

