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

  This program creates a slightly more complex system of springs
  than in spring0/spring1/spring2.cpp.  It creates four points
  in a row, and links them with three springs.
  It shows how we can construct a system of pointmasses & springs,
  place them into a couple arrays, and then have a generic update
  loop that doesn't need to know how many pointmasses or springs
  there are, or how they're connected.

*****************************************************************/
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <dms/dms.h>
#include <vector>
#include "PointMass.h"
#include "Spring.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;
bool windActive = false;

vector<PointMass> points;
vector<Spring> springs;
Vector3 gravity(0, -10, 0);
Vector3 wind(5, 0, 0);

SimpleTransform xform0, xform1, xform2, xform3;


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

/*************** CODE OF INTEREST ******************************/
/* Create all the PointMasses & Springs, and stick them into   */
/* the two Vectors.                                            */
/*                                                             */
    points.push_back(PointMass(Vector3(0, 4, 0)));
    points.push_back(PointMass(Vector3(-2, 4, 0)));
    points.push_back(PointMass(Vector3(-4, 4, 0)));
    points.push_back(PointMass(Vector3(-5, 4, 0), 0.25));
    points[0].freezePosition();
    for (int i=0; i < points.size(); i++)
        points[i].setDrag(0.2);
    springs.push_back(Spring(points[0], points[1], 1000));
    springs.push_back(Spring(points[1], points[2], 1000));
    springs.push_back(Spring(points[2], points[3], 1000));
    
    glLineWidth(4);
    
    glutMainLoop();
    return 0;
    }


void createScene(Object& root)
    {
    QuadricObject *ball;
    ball = new QuadricObject;
    ball->makeSphere(0.5);
    ball->setMaterial(*(new Material(Color::Yellow)));
    ball->setTransform(xform0);
    root.attach(*ball);
    ball = new QuadricObject;
    ball->makeSphere(0.5);
    ball->setMaterial(*(new Material(Color::Blue)));
    ball->setTransform(xform1);
    root.attach(*ball);
    ball = new QuadricObject;
    ball->makeSphere(0.5);
    ball->setMaterial(*(new Material(Color::Red)));
    ball->setTransform(xform2);
    root.attach(*ball);
    ball = new QuadricObject;
    ball->makeSphere(0.25);
    ball->setMaterial(*(new Material(Color::Black)));
    ball->setTransform(xform3);
    root.attach(*ball);
    }


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();
    for (int i=0; i < springs.size(); i++)
        springs[i].drawLine();
    
    Color::White.apply();
    char str[256];
    sprintf(str, "K=%.3f\n", springs[0].springConstant());
    Vector3 p(-6, -3, 0);
    drawString(str, p);
    sprintf(str, "restLength=%.1f\n", springs[0].restLength());
    Vector3 p2(-6, -3.5, 0);
    drawString(str, p2);

    glutSwapBuffers();

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


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
    else if (k == ' ')
        active = !active;
    else if (k == 13)
        windActive = !windActive;
    else if (k == 'a')
        springs[0].setSpringConstant(springs[0].springConstant() / 1.1);
    else if (k == 's')
        springs[0].setSpringConstant(springs[0].springConstant() * 1.1);
    else if (k == 'q')
        springs[0].setRestLength(springs[0].restLength() - 0.1);
    else if (k == 'w')
        springs[0].setRestLength(springs[0].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 ******************************/
/* Run a generic update loop on the system.  The update loop   */
/* just operates on the two vectors, without caring how they   */
/* were created.                                               */
/*                                                             */
void idle(void)
    {
    int i;
    beginFrame();
    if (active)
        {
        for (i=0; i < points.size(); i++)
            {
            points[i].clearForces();
            points[i].applyForce(gravity);
            if (windActive)
                points[i].applyForce(wind);
            }
        for (i=0; i < springs.size(); i++)
            springs[i].apply();
        for (i=0; i < points.size(); i++)
            points[i].update(deltaTime());
        }
/*************** CODE OF INTEREST ******************************/
/* This part, which takes the data from the mass-spring system */
/* and applies it to the graphical objects, is not so generic. */
/* To create a more general purpose approach, we could make    */
/* another vector which lists the SimpleTransform objects that */
/* are associated with the PointMasses.                        */
/*                                                             */
    xform0.setTranslation(points[0].position());
    xform1.setTranslation(points[1].position());
    xform2.setTranslation(points[2].position());
    xform3.setTranslation(points[3].position());
    glutPostRedisplay();
    }
