#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>

GLfloat viewRotY = 0, viewRotX = 0;
GLfloat cameraDistance = 15;


void drawEverything(void);
void defineLight(void);
void drawFloorMesh(int columns,int rows);
void drawPedestal(void);
void drawTeapot(void);
void drawBall(void);
float currentTime(void);
void checkGLError(char *);
void idle(void);
void key(unsigned char k, int x, int y);
void specialkey(int k, int x, int y);



int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(512, 512);
    glutCreateWindow("example");
    glutDisplayFunc(drawEverything);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialkey);
    glutIdleFunc(idle);
    glutMainLoop();
    return 0;
    }


void drawEverything(void)
    {
    GLfloat sky[] = { 0.5, 0.8, 1.0, 0.0 };
    glClearColor(sky[0], sky[1], sky[2], sky[3]);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50.0, 1.0, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
    
    glTranslatef(0.0, 0.0, -cameraDistance);
    glRotatef(viewRotX, 1.0, 0.0, 0.0);
    glRotatef(viewRotY, 0.0, 1.0, 0.0);
    
    defineLight();
    glEnable(GL_LIGHTING);

    glEnable(GL_FOG);
    glFogi(GL_FOG_MODE, GL_LINEAR);
    glFogfv(GL_FOG_COLOR, sky);
    glFogf(GL_FOG_START, 5.0);
    glFogf(GL_FOG_END, 40.0);

    drawFloorMesh(32,32);
    drawTeapot();
    drawPedestal();
    drawBall();

    glDisable(GL_LIGHTING);
        
    glutSwapBuffers();
    
    checkGLError("end-of-frame");
    }


void defineLight(void)
    {
    GLfloat white[4] = { 1, 1, 1, 1 };
    GLfloat lightPos[4] = { -5, 4, 1, 0 };
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    }


void drawFloorMesh(int columns,int rows)
    {
    int i,j;
    GLfloat x,z;
    GLfloat green[4] = { 0, 0.8, 0.1, 1 };
    GLfloat black[4] = { 0, 0, 0, 1 };
    glMaterialfv(GL_FRONT, GL_AMBIENT, green);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
    glMaterialfv(GL_FRONT, GL_SPECULAR, black);
    glNormal3f(0.0, 1.0, 0.0);
    for (j = 0; j < rows; j++)
        {
        glBegin(GL_TRIANGLE_STRIP);
        for (i = 0; i < columns; i++)
            {
            x = (((float)i) / columns) * 40.0 - 20.0;
            z = (((float)j) / rows) * 40.0 - 20.0;
            glVertex3f(x, -2.0, z);
            z = (((float)j+1) / rows) * 40.0 - 20.0;
            glVertex3f(x, -2.0, z);
            }
        glEnd();
        }
    }


void drawTeapot(void)
    {
    GLfloat red[4] = { 0.8, 0, 0, 1 };
    GLfloat white[4] = { 1, 1, 1, 1 };
    glMaterialfv(GL_FRONT, GL_AMBIENT, red);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
    glMaterialfv(GL_FRONT, GL_SPECULAR, white);
    glMaterialf(GL_FRONT, GL_SHININESS, 90.0);
    glPushMatrix();
     glTranslatef(2.0, 4.0, -1.0);
     glRotatef(currentTime() * 30.0, 0.0, 1.0, 0.0);
     glutSolidTeapot(2.0);
    glPopMatrix();
    }


void drawPedestal(void)
    {
    static GLUquadricObj * quadric = NULL;
    GLfloat grey[4] = { 0.6, 0.6, 0.6, 1 };
    GLfloat white[4] = { 1, 1, 1, 1 };
    if (!quadric)
        quadric = gluNewQuadric();
    glMaterialfv(GL_FRONT, GL_AMBIENT, grey);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, grey);
    glMaterialfv(GL_FRONT, GL_SPECULAR, white);
    glMaterialf(GL_FRONT, GL_SHININESS, 30.0);
    glPushMatrix();
     glTranslatef(2.0, -2.0, -1.0);
     glRotatef(-90.0, 1.0, 0.0, 0.0);
     gluCylinder(quadric, 4.0, 4.0, 4.0, 16, 1);
     glTranslatef(0.0, 0.0, 4.0);
     gluDisk(quadric, 0.0, 4.0, 16, 1);
    glPopMatrix();
    }


void drawBall(void)
    {
    GLfloat cyan[4] = { 0, 1, 1, 1 };
    GLfloat black[4] = { 0, 0, 0, 1 };
    glMaterialfv(GL_FRONT, GL_AMBIENT, cyan);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
    glMaterialfv(GL_FRONT, GL_SPECULAR, black);
    glPushMatrix();
     glTranslatef(-10.0, fabs(sin(currentTime())) * 3.0, -15.0);
     glutSolidSphere(2.0, 16, 8);
    glPopMatrix();
    }


float currentTime(void)
    {
    static struct timeval startTime;
    static int firstCall=1;
    struct timeval t;
    if (firstCall)
        {
        firstCall = 0;
        gettimeofday(&startTime, NULL);
        }
    gettimeofday(&t,NULL);
    return (t.tv_sec-startTime.tv_sec) +
           (t.tv_usec - startTime.tv_usec) / 1000000.0;
    }


void checkGLError(char *prefix)
    {
    GLenum err = glGetError();
    if (err != GL_NO_ERROR)
        printf("%s GL error '%s'\n",prefix,gluErrorString(err));
    }


void idle(void)
    {
    glutPostRedisplay();
    }


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
    else if (k == '-')
        cameraDistance += 1;
    else if (k == '=')
        cameraDistance -= 1;
    }


void specialkey(int k, int x, int y)
    {
    if (k == GLUT_KEY_LEFT)
        viewRotY += 3;
    else if (k == GLUT_KEY_RIGHT)
        viewRotY -= 3;
    else if (k == GLUT_KEY_UP)
        viewRotX += 3;
    else if (k == GLUT_KEY_DOWN)
        viewRotX -= 3;
    }
