/*
 * (C) Copyright Ian D. Romanick 2006
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>
#include <stdio.h>
#include "debug/glut.h"
#include "data.h"

cbk_sphere_data Sphere1( 5,  0, 0, -1,  0, 0, 1, 0, 0);
cbk_sphere_data Sphere2( 0,  5, 0,  0, -1, 0, 0, 1, 0);

int DetectCollision(cbk_sphere_data *l_Sphere1, cbk_sphere_data *l_Sphere2) {
	cbk_vector q(l_Sphere1->get_position() - l_Sphere2->get_position());
	cbk_vector r(l_Sphere1->get_velocity() - l_Sphere2->get_velocity());
	GLfloat d = 2;

    GLfloat D = (((q * r) * 2) * ((q * r) * 2)) - (((r * r) * 4) * ((q * q) * (d * d));

	return 0;
}

void init(void)
{
    glClearColor(0.3, 0.3, 0.3, 0.0);
    glShadeModel(GL_SMOOTH);

    /* This state never changes, so set it up once at initialization and be
     * done with it.
     */
    glEnable(GL_DEPTH_TEST);
}


/**
 * Display callback for GLUT.
 */
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//Camera Setup
    glLoadIdentity();
    gluLookAt( 0.0,  0.0, 10.0,     /* eye position */
	           0.0,  0.0,  0.0,     /* point the eye is looking at */
	           0.0,  1.0,  0.0);    /* "up" direction */
	glPushMatrix();

	Sphere1.draw_sphere();
	Sphere2.draw_sphere();

	//Display
	glutSwapBuffers();
}


void reshape(int width, int height)
{
    /* Set the view port and the perspective project transform.  We'll cover
     * this during week 3.
     */
    glViewport(0, 0, (GLsizei) width, (GLsizei) height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
}


void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
	case 27: // 27 is the ASCII code for escape.
		exit(0);
		break;
	}
}

void idle(void)
{
	static double t0 = -1.0;
	const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
	double dt = (t0 < 0.0) ? 0.0 : t - t0;

	t0 = t;

	Sphere1.update_sphere(dt);
	Sphere2.update_sphere(dt);

	glutPostRedisplay();
}

int main(int argc, char ** argv)
{
    /* Initialize GLUT.  Create our window.
     */
    glutInit(& argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutInitWindowSize(512, 384);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);

    /* Perform local program initialization.
     */
    init();

    /* Register our functions as GLUT callbacks.
     */
	glutIdleFunc(idle);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

	glDisable(GL_LIGHTING);

    /* Let 'er rip!
     */
    glutMainLoop();

    
    /* Since glutMainLoop never actually returns, this will never be reached.
     * However, the explicit return make the compiler happy.
     */
    return 0;
}
