/* Luo Li
   Super Comp Program 2
*/

/*sample GL.cpp*/

#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "glaux.h"


//Universal Constant
float MAXV=300;
float MAXA=90;
float v,theta,dist,range;
float t,tstep,vx,vy,a,m;
float forcex,forcey, fstren;

static void Init (void)
{

  glClearColor(0.2, 0.2, 0.2, 1.0);
  glShadeModel( GL_FLAT  );

}

static void Reshape (int width, int height )
{
  glViewport( 0, 0, (GLint)width, (GLint)height);
  glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
  glOrtho( 0.0, MAXV, 0.0, MAXA, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
}
static void key_up()
{
  cout<<"AUX_UP\n";
}
static void key_down()
{
  cout<<"AUX_DOWN\n";
}
static void key_esc()
{
 auxQuit();
}

void DrawMyStuff()
{
	float xdif,ydif,radius;
 	float tempf,tsin,tcos;
  
 	float x=0;
 	float y=0;

 	while (y>=0)
 	 {
	  t+=tstep;
	  x=vx*t;
	  y=(vy*t)-(.5*a*(t*t));

//	  cout<<"Before- "<<vx<<" "<<vy<<" pos-"<<x<<" "<<y<<endl;
	  xdif=forcex-x;
	  ydif=forcey-y;
	  radius=(sqrt( (xdif*xdif) + (ydif*ydif) ) );
	  tempf= (fstren/radius);
	  tsin= (fabs) (sin(ydif/radius));
	  tcos= (fabs) (cos(xdif/radius));
	  if ((xdif>0) && (ydif>0)) { vx+= tempf*(tcos); vy+= tempf*(tsin); }
	  if ((xdif<0) && (ydif>0)) { vx+= (-1)*tempf*(tcos); vy+= tempf*(tsin); }
	  if ((xdif>0) && (ydif<0)) { vx+= tempf*(tcos); vy+= (-1)*tempf*(tsin); }
	  if ((xdif<0) && (ydif<0)) { vx+= (-1)*tempf*(tcos); vy+= (-1)*tempf*(tsin); }  
	//  cout<<"Executed!"<<endl;
	//  cout<<"After- "<<vx<<" "<<vy<<" position- "<<x<<" "<<y<<endl;
	 }

	float temp;
	temp= x-dist;
	temp= (fabs)(temp);
	float temp2;
        temp2 = (dist)/(range);
	glBegin( GL_POINTS );
	if (temp<range)
	 {
	  glColor3f( 1.0, 1.0, 1.0 );
	  glVertex3f(v,m,0.0);
//	  cout<<"plotted-1"<<endl;
	 } 
	  else 
	 {
	  if (temp<(range*temp2 ))
	   glColor3f( 0.0, 0.9, 0.4 );
	   else if (temp<(range*temp2*2))
	        glColor3f( 0.0, 0.6, 0.4 );
	        else  if (temp<(range*temp2*3))
	              glColor3f( 0.0, 0.3, 0.4 );
	              else glColor3f( 0.0, 0.0, 0.0 );
	  glVertex3f(v,m,0.0);
//	  cout<<"plotted-2"<<endl;
	 } 

	 glEnd();
	 glFlush();
	 auxSwapBuffers();
}

static void display()
{
 glClear ( GL_COLOR_BUFFER_BIT);
 //cout<<"Enter Distance to Targe-";
 //cin>>dist;
 //cout<<"Enter Range-";
 //cin>>range; 
// cout<<"Enter force strength-";
// cin>>fstren;

 for ( v=0; v<MAXV; v+=.5)
	{
	   for ( m=0; m<MAXA; m+=.5)
	   {
	        t=0; tstep=.1; a=32;
	        dist=600; range=20;
		forcex=200; forcey=200;

		fstren=0;

		theta= ( (m/180)*(M_PI) );
	 	vx=(fabs)(v*cos(theta));
	 	vy=(fabs)(v*sin(theta));
		DrawMyStuff();

	   } 
	}
 char temp;
 cin>> temp;
}

int main (int argc, char **argv)
{ 

 auxInitDisplayMode( AUX_RGBA);
 auxInitPosition( 50, 50, MAXV, MAXA);
 if (auxInitWindow("Projectile")==GL_FALSE)
   { 
     auxQuit();
   }
 Init();
 
 auxExposeFunc(Reshape);
 auxReshapeFunc(Reshape);
 auxKeyFunc( AUX_UP, key_up);
 auxKeyFunc( AUX_DOWN, key_down);
 auxMainLoop(display);

 return 0;

}

    

