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

float viewRotY = 0, viewRotX = 0;
int play=1, showNormals=0, specularity=0;

void drawEverything(void);
void initLight(void);
void drawWaveMesh(int columns,int rows,float timeOffset);
void drawWaveNormals(int columns,int rows,float timeOffset);
void normalizeVector(float *x,float *y,float *z);
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);
    glutCreateWindow("example");
    glutDisplayFunc(drawEverything);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialkey);
    glutIdleFunc(idle);
    glutMainLoop();
    return 0;
    }


void drawEverything(void)
    {
    static float t=0;
    
    glClearColor(0.0, 0.0, 0.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, -15.0);
    glRotatef(viewRotX, 1.0, 0.0, 0.0);
    glRotatef(viewRotY, 0.0, 1.0, 0.0);
    
    initLight();

    if (play)
        t = currentTime();
    
	glEnable(GL_LIGHTING);
    drawWaveMesh(32,32,t);
    glDisable(GL_LIGHTING);

    if (showNormals)
        drawWaveNormals(32,32,t);
    
    glutSwapBuffers();
    
    checkGLError("end-of-frame");
    }


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


void normalizeVector(float *x,float *y,float *z)
    {
    float length = sqrt((*x)*(*x) + (*y)*(*y) + (*z)*(*z));
    if (length > 0)
        {
        *x = *x / length;
        *y = *y / length;
        *z = *z / length;
        }
    }


void drawWaveMesh(int columns,int rows,float timeOffset)
    {
    int i,j;
    float x,y,z;
    float nx,ny,nz;
    GLfloat blue[4] = { 0, .4, 1, 1 };
    GLfloat white[4] = { 1, 1, 1, 1 };
    GLfloat black[4] = { 0,0,0,1 };
	glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);
    if (specularity)
        {
        glMaterialfv(GL_FRONT, GL_SPECULAR, white);
        glMaterialf(GL_FRONT, GL_SHININESS, 60.0);
        }
    else
        {
        glMaterialfv(GL_FRONT, GL_SPECULAR, black);
        glMaterialf(GL_FRONT, GL_SHININESS, 0.0);
        }
    for (j = 0; j < rows; j++)
        {
        glBegin(GL_TRIANGLE_STRIP);
        for (i = 0; i < columns; i++)
            {
            x = (((float)i) / columns) * 10.0 - 5.0;
            z = (((float)j) / rows) * 10.0 - 5.0;
            y = sin(x+timeOffset) * sin(z+timeOffset);
            nx = -cos(x+timeOffset) * sin(z+timeOffset);
            nz = -sin(x+timeOffset) * cos(z+timeOffset);
            ny = 1.0;
            normalizeVector(&nx, &ny, &nz);
            glNormal3f(nx, ny, nz);
            glVertex3f(x,y,z);

            z = (((float)j+1) / rows) * 10.0 - 5.0;
            y = sin(x+timeOffset) * sin(z+timeOffset);
            nx = -cos(x+timeOffset) * sin(z+timeOffset);
            nz = -sin(x+timeOffset) * cos(z+timeOffset);
            ny = 1.0;
            normalizeVector(&nx, &ny, &nz);
            glNormal3f(nx, ny, nz);
            glVertex3f(x,y,z);
            }
        glEnd();
        }
    }
   

void drawWaveNormals(int columns,int rows,float timeOffset)
    {
    int i,j;
    float x,y,z;
    float nx,ny,nz;
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    for (j = 0; j < rows; j++)
        {
        for (i = 0; i < columns; i++)
            {
            x = (((float)i) / columns) * 10.0 - 5.0;
            z = (((float)j) / rows) * 10.0 - 5.0;
            y = sin(x+timeOffset) * sin(z+timeOffset);
            nx = -cos(x+timeOffset) * sin(z+timeOffset);
            nz = -sin(x+timeOffset) * cos(z+timeOffset);
            ny = 1.0;
            normalizeVector(&nx, &ny, &nz);
            glVertex3f(x,y,z);
            glVertex3f(x+nx/2.0, y+ny/2.0, z+nz/2.0);
            }
        }
    glEnd();
    }


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 == ' ')
        play = !play;
    else if (k == 'n')
        showNormals = !showNormals;
    else if (k == 's')
        specularity = !specularity;
    }


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