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

  This program is the same as spring1.cpp, except that it uses
  the PointMass & Spring classes to handle all the calculations
  on the points.

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

PointMass point0(Vector3(-6, 4, 0));
PointMass point1(Vector3(0, 4, 0));
Spring spring(point0, point1, 100);
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);
    point0.setDrag(0.25);
    point1.freezePosition();
    
    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);
    }


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.position().vec);
     glVertex3fv(point1.position().vec);
    glEnd();
    
    Color::White.apply();
    char str[256];
    sprintf(str, "K=%.3f\n", spring.springConstant());
    Vector3 p(-6, -3, 0);
    drawString(str, p);
    sprintf(str, "restLength=%.1f\n", spring.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 == 'a')
        spring.setSpringConstant(spring.springConstant() / 1.1);
    else if (k == 's')
        spring.setSpringConstant(spring.springConstant() * 1.1);
    else if (k == 'q')
        spring.setRestLength(spring.restLength() - 0.1);
    else if (k == 'w')
        spring.setRestLength(spring.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 ******************************/
/* Call the various PointMass & Spring functions to perform the*/
/* simulation.                                                 */
/* On each frame, we want to add up all the forces acting on   */
/* each point, and then use them to move the point.            */
/* This consists of clearing the forces on each point, having  */
/* the Spring object apply its force, adding the gravity force,*/
/* and then calling update() for each point to move it.        */
/*                                                             */
void idle(void)
    {
    beginFrame();
    if (active)
        {
        point0.clearForces();
        point1.clearForces();
        spring.apply();
        point0.applyForce(gravity);
        point1.applyForce(gravity);
        point0.update(deltaTime());
        point1.update(deltaTime());
        }
    xform0.setTranslation(point0.position());
    xform1.setTranslation(point1.position());
    glutPostRedisplay();
    }
