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

float viewRotY = 0, viewRotX = 0;

#define TOPO_ROWS 128
#define TOPO_COLS 128

typedef struct { float r,g,b; } color_t;
color_t * colors;
float * elevation;
float heightScale = 1;


void readTopoData(char * elevFile, char *colorFile);
void drawEverything(void);
void defineLight(void);
void drawTopoMesh(void);
void computeNormal(GLfloat v0[3], GLfloat v1[3], GLfloat v2[3],
                   GLfloat normal[3]);
void checkGLError(char *);
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);
    if (argc < 3)
        {
        fprintf(stderr,"Usage; %s elevation-file color-file\n",argv[0]);
        exit(1);
        }
    readTopoData(argv[1],argv[2]);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(512,512);
    glutCreateWindow("example");
    glutDisplayFunc(drawEverything);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialkey);
    glutMainLoop();
    return 0;
    }


void readTopoData(char * elevFile, char *colorFile)
    {
    int fd;
    
    elevation = (float *) malloc(TOPO_ROWS * TOPO_COLS * sizeof(float));
    colors = (color_t *) malloc(TOPO_ROWS * TOPO_COLS * sizeof(color_t));
    
    fd = open(elevFile, O_RDONLY);
    if (fd < 0)
        {
        perror(elevFile);
        exit(1);
        }
    read(fd,elevation,TOPO_ROWS * TOPO_COLS * sizeof(float));
    close(fd);
    
    fd = open(colorFile, O_RDONLY);
    if (fd < 0)
        {
        perror(colorFile);
        exit(1);
        }
    read(fd,colors,TOPO_ROWS * TOPO_COLS * sizeof(color_t));
    close(fd);
    }


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, -15.0);
    glRotatef(viewRotX, 1.0, 0.0, 0.0);
    glRotatef(viewRotY, 0.0, 1.0, 0.0);
    
    glEnable(GL_LIGHTING);
    defineLight();
    drawTopoMesh();
    
    glutSwapBuffers();
    
    checkGLError("end-of-frame");
    }


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


void drawTopoMesh(void)
    {
    int i,j, index;
    GLfloat v0[3], v1[3], v2[3], v3[3], norm[3];
    GLfloat black[4] = { 0, 0, 0, 1 };
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glMaterialfv(GL_FRONT, GL_SPECULAR, black);
    for (j = 0; j < TOPO_ROWS-1; j++)
        {
        glBegin(GL_TRIANGLE_STRIP);
        for (i = 0; i < TOPO_COLS-1; i++)
            {
          /* Vertex (i,j) */
            v0[0] = (((float)i) / TOPO_COLS) * 10.0 - 5.0;
            v0[1] = elevation[i + j * TOPO_COLS] * heightScale;
            v0[2] = (((float)j) / TOPO_ROWS) * 10.0 - 5.0;
          /* Vertex (i,j+1) */
            v1[0] = (((float)i) / TOPO_COLS) * 10.0 - 5.0;
            v1[1] = elevation[i + (j+1) * TOPO_COLS] * heightScale;
            v1[2] = (((float)j+1) / TOPO_ROWS) * 10.0 - 5.0;
          /* Vertex (i+1,j) */
            v2[0] = (((float)(i+1)) / TOPO_COLS) * 10.0 - 5.0;
            v2[1] = elevation[i+1 + j * TOPO_COLS] * heightScale;
            v2[2] = (((float)j) / TOPO_ROWS) * 10.0 - 5.0;
            computeNormal(v0, v1, v2, norm);
            glNormal3fv(norm); 
            index = i + j * TOPO_COLS;
            glColor3f(colors[index].r, colors[index].g, colors[index].b); 
            glVertex3fv(v0);
            
          /* Vertex (i+1,j+1) */
            v3[0] = (((float)(i+1)) / TOPO_COLS) * 10.0 - 5.0;
            v3[1] = elevation[i+1 + (j+1) * TOPO_COLS] * heightScale;
            v3[2] = (((float)(j+1)) / TOPO_ROWS) * 10.0 - 5.0;
            computeNormal(v1, v3, v2, norm);
            glNormal3fv(norm);
            index = i + (j+1) * TOPO_COLS;
            glColor3f(colors[index].r, colors[index].g, colors[index].b); 
            glVertex3fv(v1);
            }
        glEnd();
        }
    glDisable(GL_COLOR_MATERIAL);
    }


/* computeNormal - takes 3 vertex positions as input (v0, v1, v2), and
    returns a unit length normal vector for the polygon (normal) */
void computeNormal(GLfloat v0[3], GLfloat v1[3], GLfloat v2[3],
                   GLfloat normal[3])
    {
    GLfloat A[3], B[3], product[3], length;
    A[0] = v2[0] - v1[0];
    A[1] = v2[1] - v1[1];
    A[2] = v2[2] - v1[2];
    B[0] = v0[0] - v1[0];
    B[1] = v0[1] - v1[1];
    B[2] = v0[2] - v1[2];
    product[0] = A[1] * B[2] - A[2] * B[1];
    product[1] = A[2] * B[0] - A[0] * B[2];
    product[2] = A[0] * B[1] - A[1] * B[0];
    length = sqrt(product[0]*product[0] + product[1]*product[1] +
                  product[2]*product[2]);
    if (length == 0)    /* If there's a problem, */
        {               /* return an arbitrary result */
        normal[0] = 0;
        normal[1] = 1;
        normal[2] = 0;
        }
    else
        {
        normal[0] = product[0] / length;
        normal[1] = product[1] / length;
        normal[2] = product[2] / length;
        }
    }


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


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
    else if (k == '1')
        heightScale *= 1.1;
    else if (k == '2')
        heightScale /= 1.1;
    glutPostRedisplay();
    }


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