#include "cs148.h"

float cuberotx=0.0,cuberoty=45.0;

int winw,winh;
int blending_enabled = 1;

float camera_x=0.0, camera_z=3.0;

float quad_z = 1.0;

void lights(void);

void display(void)
{

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  if (blending_enabled) glEnable(GL_BLEND);
  else glDisable(GL_BLEND);
  
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    
  // Turn on the lights
  lights();

  glPushMatrix();

  // Rotate the cube if the user asked us to
  glRotatef(cuberotx,1,0,0);
  glRotatef(cuberoty,0,1,0);

  // The blue cube
  glColor3f(0.5, 1.0, 1.0);
  glutSolidCube(1.0);
  glPopMatrix();

  // The yellow cube    
  glPushMatrix();
  glColor3f(0.8, 0.8, 0.2);
  glRotatef(45,0,1,0);
  glTranslatef(0.3,0.1,0.8);
  glutSolidCube(0.8);
  glPopMatrix();

  // The transparent quad
  glDepthMask(GL_FALSE);
  glColor4f(0.8,0.2,0.2,0.3);
  glBegin(GL_QUADS);
  glVertex3f(-1.0, 0.0  , quad_z);
  glVertex3f(-1.0, -1.0 , quad_z);
  glVertex3f(1.0 , -1.0 , quad_z);
  glVertex3f(1.0 , 0.0  , quad_z);
  glEnd();
  glDepthMask(GL_TRUE);
  
  glutSwapBuffers();
}


void reshape (int w, int h) {

  winw = w; winh = h;
  glViewport (0, 0, (GLsizei) w, (GLsizei) h); 

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  
  // Set up a perspective projection
  gluPerspective(45.0,1.0,1.5,20.0);
  
  // Back to modelview mode
  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();          

  // Place the camera (really this translates everything else by
  // the opposite of (camera_x,0,camera_z) )
  gluLookAt(camera_x, 0.0, camera_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 
}


void keyboard(unsigned char key, int x, int y)
{

  switch (key) {
    case 'b':
      blending_enabled = 1 - blending_enabled;
      break; 
    case 'a':
      cuberotx+=5.0;
      break;
    case 'z':
      cuberotx-=5.0;
      break;
    case 's':
      cuberoty+=5.0;
      break;
    case 'x':
      cuberoty-=5.0;
      break;    
    case 'd':
      quad_z-=0.1;
      break;
    case 'c':
      quad_z+=0.1;
      break;
  }
  glutPostRedisplay();
}


void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);
   glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
   glEnable(GL_COLOR_MATERIAL);
   glEnable(GL_DEPTH_TEST);  
}


// Turn the light on
void lights(void)
{

  // The position of the light
  GLfloat position[] =  {3.0, 3.0, 3.0, 1.0};

  GLfloat color[] = {0.5,0.5,0.5,1};
  GLfloat acolor[] = {0.4,0.4,0.4,1};
  
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  
  glLightfv(GL_LIGHT0, GL_POSITION, position);
  glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 80.0);
  glLightfv(GL_LIGHT0, GL_AMBIENT, acolor);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, color);
  glLightfv(GL_LIGHT0, GL_SPECULAR, color);

}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
   glutInitWindowSize(500, 500); 
   glutInitWindowPosition(100, 100);
   glutCreateWindow(argv[0]);
   glutKeyboardFunc(keyboard);
   init();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}

