//#include <windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <math.h>
GLfloat d;

GLfloat p1x;
GLfloat p1y;
GLfloat p1z;

const float p1radius = 2.0;
const float p2radius = 1.0;

GLfloat p2x;
GLfloat p2y;
GLfloat p2z;

bool keys[256];   //put this in at the top of the program

void collision (void) {
d = sqrt(((p1x - p2x) * (p1x - p2x)) + ((p1y - p2y) * (p1y - p2y)) 
+ ((p1z - p2z) * (p1z - p2z)));
}

void pointz (void) {
glPushMatrix();
if (d <= p2radius + p1radius)
{
glColor3f(1, 0, 0);
}
else
{
glColor3f(0, 0, 1);
}
/*glBegin(GL_POINTS);
glVertex3f(p1x, p1y, p1z);
glEnd();*/
glTranslatef(p1x, p1y, p1z);
glutSolidSphere(p1radius,30,30);
glPopMatrix();

glPushMatrix();
glColor3f(0, 1, 0);
/*glBegin(GL_POINTS);
glVertex3f(p2x, p2y, p2z);
glEnd();*/
glTranslatef(p2x, p2y, p2z);
glutSolidSphere(p2radius,30,30);
glPopMatrix();
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
	glEnable(GL_DEPTH_TEST);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();  
	glTranslatef(0, 0, -20.0);
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glPointSize(5);
    
    if (keys['w']==1) {
      p1y = p1y + 0.001;
    }

    if (keys['s']==1) {
     p1y = p1y - 0.001;
    }

    if (keys['a']==1) {
     p1x = p1x - 0.001;
    }

    if (keys['d']==1) {
     p1x = p1x + 0.001;
    }

    if (keys['i']==1) {
     p2y = p2y + 0.001;
    }

    if (keys['k']==1) {
     p2y = p2y - 0.001;
    }

    if (keys['j']==1) {
     p2x = p2x - 0.001;
    }

    if (keys['l']==1) {
     p2x = p2x + 0.001;
    }

    collision();
    pointz();
    glutSwapBuffers();
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 400.0);
    glMatrixMode (GL_MODELVIEW);
}

/*
void keyboard (unsigned char key, int x, int y) {
if (key=='q') {
p1z = p1z - 0.1;
}

if (key=='z') {
p1z = p1z + 0.1;
}

if (key=='w') {
p1y = p1y + 0.1;
}

if (key=='s') {
p1y = p1y - 0.1;
}

if (key=='a') {
p1x = p1x - 0.1;
}

if (key=='d') {
p1x = p1x + 0.1;
}

if (key=='i') {
p2y = p2y + 0.1;
}

if (key=='k') {
p2y = p2y - 0.1;
}

if (key=='j') {
p2x = p2x - 0.1;
}

if (key=='l') {
p2x = p2x + 0.1;
}

if (key==27) { //27 is the ascii code for the ESC key
    exit (0); //end the program
}
}
*/


//here are the key up and key down functions
void keyboard(unsigned char key, int x, int y){
	keys[(int)key] = true;
}

void keyboardUp(unsigned char key, int x, int y){
	keys[(int)key] = false;
}
/*
void keyboardSpecial(int key, int x, int y){
}

void keyboardUpSpecial(int key, int x, int y){
}
*/

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutKeyboardFunc (keyboard);//the call for the keyboard 

	
    glutKeyboardUpFunc(keyboardUp);
    //glutSpecialFunc(keyboardSpecial);
    //glutSpecialUpFunc(keyboardUpSpecial);

    glutMainLoop ();
    return 0;
}