#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;

#define SPOT_DIRECTION  1
#define SPOT_CUTOFF     2
#define SPOT_EXPONENT   3

GLfloat lightPos[4] = { 0, 4, 10, 1 };
GLfloat spotDirection[] = { 0, 0, -1 };
GLfloat spotCutoff = 30;
GLfloat spotExponent = 0.0;

int mouseMode = SPOT_DIRECTION;

void drawEverything(void);
void defineLight(void);
void drawSpotlight(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);
void mousebutton(int button, int state, int x, int y);
void mousemotion(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);
    glutMouseFunc(mousebutton);
    glutMotionFunc(mousemotion);
    glutIdleFunc(idle);
    glutMainLoop();
    return 0;
    }


void drawEverything(void)
    {
    glClearColor(0.5, 0.8, 1.0, 0.0);
    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, -25.0);
    glRotatef(viewRotX, 1.0, 0.0, 0.0);
    glRotatef(viewRotY, 0.0, 1.0, 0.0);
    
    defineLight();
    glEnable(GL_LIGHTING);

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

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


void defineLight(void)
    {
    GLfloat white[4] = { 1, 1, 1, 1 };
     glEnable(GL_LIGHT0);
     glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, spotCutoff);
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, spotExponent);
    drawSpotlight();
    }


void drawSpotlight(void)
    {
    glColor3f(1.0, 1.0, 0.0);
    glPointSize(12.0);
    glBegin(GL_POINTS);
     glVertex3fv(lightPos);
    glEnd();
    glBegin(GL_LINES);
     glVertex3fv(lightPos);
     glVertex3f(spotDirection[0]*100, spotDirection[1]*100, spotDirection[2]*100);
    glEnd();
    }


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);
    }


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;
    }


void mousebutton(int button, int state, int x, int y)
    {
    if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
        mouseMode = SPOT_DIRECTION;
    else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN))
        mouseMode = SPOT_CUTOFF;
    else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
        mouseMode = SPOT_EXPONENT;
    }


void mousemotion(int x, int y)
    {
    if (mouseMode == SPOT_DIRECTION)
        {
        spotDirection[0] = (float)x / glutGet(GLUT_WINDOW_WIDTH) * 2.0 - 1.0;
        spotDirection[1] = 1.0 - (float)y / glutGet(GLUT_WINDOW_HEIGHT) * 2.0;
        }
    else if (mouseMode == SPOT_CUTOFF)
        {
        spotCutoff = (float)x / glutGet(GLUT_WINDOW_WIDTH) * 90.0;
        if (spotCutoff < 0)
            spotCutoff = 0;
        else if (spotCutoff > 90)
            spotCutoff = 90;
        }
    else if (mouseMode == SPOT_EXPONENT)
        {
        spotExponent = (float)x / glutGet(GLUT_WINDOW_WIDTH) * 10.0;
        if (spotExponent < 0)
            spotExponent = 0;
        }
    printf("direction = %.3f %.3f %.3f\t cutoff = %f\t exponent = %f\n",
           spotDirection[0], spotDirection[1], spotDirection[2],
           spotCutoff, spotExponent);
    }
