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

  This program demonstrates the use of a mass-spring simulation
  to move 2 objects.  2 balls are connected by a spring, and
  move in response to the forces exerted by the spring.
  The spring constant, and the rest length of the spring, can
  be varied using the keyboard - 'a' & 's' keys change the
  spring constant, 'q' & 'w' keys change the rest length.

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

/*************** CODE OF INTEREST ******************************/
/* springConstant and restLength are parameters for the spring */
/* force formula.                                              */
/* point0 and point1 are the positions of the two masses       */
/* velocity0 and velocity1 are their current velocities        */
/* xform0 and xform1 are transformations for the two balls that*/
/* will be placed at the two point masses' positions           */
/*                                                             */
float springConstant = 1;
float restLength = 4;

Vector3 point0(-3, 0, 0), point1(3, 0, 0);
Vector3 velocity0(0,0,0), velocity1(0,0,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, 10);
    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(-3, 3, 0);
    drawString(str, p);
    sprintf(str, "restLength=%.1f\n", restLength);
    Vector3 p2(-3, 2.5, 0);
    drawString(str, p2);

    glutSwapBuffers();

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


/*************** CODE OF INTEREST ******************************/
/* Compute & apply the spring force to the two points.         */
/* The force is computed from F = -Kd.  d (displacement) is    */
/* computed from the distance between the two points (the      */
/* length of the vector between them).                         */
/* The motion assumes the two points have mass=1, hence        */
/* acceleration = force                                        */
/*                                                             */
void updateSpring(void)
    {
    Vector3 v = point1 - point0;
    float displacement = v.length() - restLength;
    v.normalize();
    Vector3 force = springConstant * displacement * v;
    Vector3 accel = force;
    velocity0 += accel * deltaTime();
    point0 += velocity0 * deltaTime();
    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);
    }


/*************** CODE OF INTEREST ******************************/
/* Apply the spring force to the two points, then use the point*/
/* values in the transformations for the graphical objects.    */
/*                                                             */
void idle(void)
    {
    beginFrame();
    if (active)
        updateSpring();
    xform0.setTranslation(point0);
    xform1.setTranslation(point1);
    glutPostRedisplay();
    }
