#include <input.h>

void handleKey(unsigned char key, int x, int y) {
    switch (key) {
    case 'a':
        rotateView( 0.3f, 0.0f, 1.0f, 0.0f);
        break;
    case 'd':
        rotateView(-0.3f, 0.0f, 1.0f, 0.0f);
        break;
    case 'q':
        rotateScene(2);
        break;
    case 'e':
        rotateScene(-2);
        break;
    case 'w':
        walk(-0.1f);
        break;
    case 's':
        walk( 0.1f);
        break;
    case 'x':
        exit(0);
        break;
    }
}

void walk(GLfloat r) {
    eye[0] += (eye[0] - cnt[0]) * r;
    eye[1] += (eye[1] - cnt[1]) * r;
    eye[2] += (eye[2] - cnt[2]) * r;
}

void rotateScene(int angle) {
    rot = (rot+angle)%360; 
}

void rotateView(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) {
    GLfloat pos[3];
    pos[0] = eye[0] - cnt[0];
    pos[1] = eye[1] - cnt[1];
    pos[2] = eye[2] - cnt[2];
    
    GLfloat newPos[3];

    GLfloat cosTheta = (GLfloat)cos(angle);
    GLfloat sinTheta = (GLfloat)sin(angle);

    newPos[0]  = (cosTheta + (1 - cosTheta) * x * x)        * pos[0];
    newPos[0] += ((1 - cosTheta) * x * y - z * sinTheta)    * pos[1];
    newPos[0] += ((1 - cosTheta) * x * z + y * sinTheta)    * pos[2];

    newPos[1]  = ((1 - cosTheta) * x * y + z * sinTheta)    * pos[0];
    newPos[1] += (cosTheta + (1 - cosTheta) * y * y)        * pos[1];
    newPos[1] += ((1 - cosTheta) * y * z - x * sinTheta)    * pos[2];

    newPos[2]  = ((1 - cosTheta) * x * z - y * sinTheta)    * pos[0];
    newPos[2] += ((1 - cosTheta) * y * z + x * sinTheta)    * pos[1];
    newPos[2] += (cosTheta + (1 - cosTheta) * z * z)        * pos[2];

    eye[0] = cnt[0] + newPos[0];
    eye[1] = cnt[1] + newPos[1];
    eye[2] = cnt[2] + newPos[2];
}

