/*****************************************************************
  spring1.cpp
  by Dave Pape
  4 April 2003

  This program extends spring0.cpp by adding another force -
  gravity - and keeping one of the points at a fixed location.
  The result is a pendulum motion of the other point.
  A drag force is also added, to cause the swinging point to
  eventually slow down & stop.

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

using namespace dms;

void createScene(Object& root);
void drawEverything(void);

void key(unsigned char k, int x, int y);
void specialkey(int k, int x, int y);
void idle(void);


PerspCamera camera;
Light light;
Object root;

bool active = false;

float springConstant = 100;
float restLength = 6;

Vector3 point0(-6, 4, 0), point1(0, 4, 0);
Vector3 velocity0(0,0,0), velocity1(0,0,0);
Vector3 gravity(0, -10, 0);

SimpleTransform xform0, xform1;


int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(512,512);
    glutCreateWindow(argv[0]);
    
    glutDisplayFunc(drawEverything);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialkey);
    glutIdleFunc(idle);
    
    camera.setPosition(0, 0, 16);
    light.setInfinitePosition(-5, 4, 1);
    createScene(root);
    
    glLineWidth(4);
    
    glutMainLoop();
    return 0;
    }


void createScene(Object& root)
    {
    QuadricObject *ball;
    ball = new QuadricObject;
    ball->setMaterial(*(new Material(Color::Yellow)));
    ball->setTransform(xform0);
    root.attach(*ball);
    ball = new QuadricObject;
    ball->setMaterial(*(new Material(Color::Blue)));
    ball->setTransform(xform1);
    root.attach(*ball);
    
    xform0.setTranslation(point0);
    xform1.setTranslation(point1);
    }


void drawEverything(void)
    {
    glClearColor(0.5, 0.7, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    
    camera.apply();
    
    light.apply();

    root.drawAll();
    
    Color::Red.apply();
    glBegin(GL_LINES);
     glVertex3fv(point0.vec);
     glVertex3fv(point1.vec);
    glEnd();
    
    Color::White.apply();
    char str[256];
    sprintf(str, "K=%.3f\n", springConstant);
    Vector3 p(-6, -3, 0);
    drawString(str, p);
    sprintf(str, "restLength=%.1f\n", restLength);
    Vector3 p2(-6, -3.5, 0);
    drawString(str, p2);

    glutSwapBuffers();

    checkGLError("end-of-frame");
    }


/*************** CODE OF INTEREST ******************************/
/* This code is mostly the same as in spring0.cpp, except that */
/* we also add a gravity force and a drag force to the spring  */
/* force that is applied to the points.                        */
/* The drag force is computed from the formula F = -Cv.        */
/*                                                             */
void updateSpring(void)
    {
    Vector3 v = point1 - point0;
    float displacement = v.length() - restLength;
    v.normalize();
    Vector3 force = springConstant * displacement * v;
    Vector3 accel = force + gravity - 0.25 * velocity0;
    velocity0 += accel * deltaTime();
    point0 += velocity0 * deltaTime();
    accel = -force + gravity - 0.25 * velocity1;
    velocity1 += accel * deltaTime();
    point1 += velocity1 * deltaTime();
    }


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
    else if (k == ' ')
        active = !active;
    else if (k == 'a')
        springConstant /= 1.1;
    else if (k == 's')
        springConstant *= 1.1;
    else if (k == 'q')
        restLength -= 0.1;
    else if (k == 'w')
        restLength += 0.1;
    }


void specialkey(int k, int x, int y)
    {
    if (k == GLUT_KEY_LEFT)
        camera.turn(3);
    else if (k == GLUT_KEY_RIGHT)
        camera.turn(-3);
    else if (k == GLUT_KEY_UP)
        camera.pitch(2);
    else if (k == GLUT_KEY_DOWN)
        camera.pitch(-2);
    else if (k == GLUT_KEY_HOME)
        camera.moveForward(0.25);
    else if (k == GLUT_KEY_END)
        camera.moveForward(-0.25);
    else if (k == GLUT_KEY_PAGE_UP)
        camera.zoom(-1);
    else if (k == GLUT_KEY_PAGE_DOWN)
        camera.zoom(1);
    }


void idle(void)
    {
    beginFrame();
    if (active)
        updateSpring();
    velocity1.set(0, 0, 0);
    point1.set(0, 4, 0);
    xform0.setTranslation(point0);
    xform1.setTranslation(point1);
    glutPostRedisplay();
    }
