/*
   Program:  teapot1.c
   This program is similar to teapot1.c except it has an improved menu
   that includes sub-menus to allow for speeding up or slowing down the
   rotation of the teapot.
/*

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

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

static GLfloat spin = 0.0;
float spinspeed = 5.0;
GLint toggle = 0;
void menu (int);

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 changespeed(int item)
{
 switch (item)
   {
    case 0: spinspeed += 2.0;
      break;

    case 1: spinspeed -= 2.0;
      //if (spinspeed < 0) spinspeed = 0;
      break;

    case 2: spinspeed = 5.0;
   }
}

void createMenu(void)
{
  int speedMenu = glutCreateMenu(changespeed);
  glutAddMenuEntry("Faster", 0);
  glutAddMenuEntry("Slower", 1);
  glutAddMenuEntry("Reset", 2);

  glutCreateMenu(menu);
  glutAddMenuEntry("Spin the Teapot", SPIN);
  glutAddSubMenu("Change the Speed", speedMenu);
  glutAddMenuEntry("Stop Spinning", STOP);
  glutAddMenuEntry("Quit the Program", QUIT);
  glutAttachMenu(GLUT_RIGHT_BUTTON);

}
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 += spinspeed;
  if (spin >360.0) 
    spin = spin - 360.0;
  glutPostRedisplay();
}



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);
  createMenu();
//  glutMouseFunc(mouse);
  glutMainLoop();
  return 0;
    
}


