/*
 *  sc.c
 */
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

static GLint fogMode;

/*  Initialize depth buffer, fog, light source, 
 *  material property, and lighting model.
 */
static void init(void)
{
   GLfloat position[] = { 0.0, 2.0, 0.0, 0.1 };

   glEnable(GL_DEPTH_TEST);

   glLightfv(GL_LIGHT0, GL_POSITION, position);
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   {
    GLfloat mat_diffuse[] = { 1.0, 1.0, 0.5, 1.0 };
    GLfloat mat_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
      glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);
      glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
   }
   glShadeModel (GL_SMOOTH);
}

void display(void)
{
    GLfloat sz = 0.75;

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //gluLookAt(10,10,10,0,0,0,0,1,0);
    gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
    glPushMatrix();
	glutSolidCube (1.0);
        glDisable(GL_LIGHTING);
        glColor3f(1.0,0.0,0.0);
        glBegin(GL_POLYGON);
            glVertex3f(-sz,2.0, -sz);
            glVertex3f( sz,2.0, -sz);
            glVertex3f( sz,2.0,  sz);
            glVertex3f(-sz,2.0,  sz);
        glEnd();
        glEnable(GL_LIGHTING);
    glPopMatrix();
   glFlush();
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= h)
      glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
         2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
   else
      glOrtho (-2.5*(GLfloat)w/(GLfloat)h,
         2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity ();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}


/*  Main Loop
 *  Open window with initial window size, title bar, 
 *  RGBA display mode, depth buffer, and handle input events.
 */
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(500, 500);
   glutCreateWindow(argv[0]);
   init();
   glutReshapeFunc (reshape);
   glutKeyboardFunc (keyboard);
   glutDisplayFunc (display);
   glutMainLoop();
   return 0;
}

