#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
#include <math.h>

#define LINES 8
#define DIST 2.0


// Globals
double e, rley, rmov;
int lw;
int mouse_x, mouse_y;
double dt = .01;
double tmax = 10.0
float pt[LINES][(int)(10.0/.01 +1)][3];

//GLfloat color[3];

long col[LINES][3];


//float ***pt;

// Function Prototypes
void display();
void init();
void initcond();
void ramp(int i);
void calc();

void calc(void)
{
	int i, t, j;
	float x, y, z, point[3];
	
	for(i = 0; i < LINES; i++)
		ramp(i);
	
//	float pt[LINES][(int)(tmax/dt +1)][3];
		//pt[0][0] = {0.0, 50.0, 0.0};
		pt[0][0][0] =0.0;
		pt[0][0][1] =50.0;
		pt[0][0][2] =0.0;
		
		//pt[1][0] = {0.0f, -50.0f, 0.0f};
		pt[1][0][0] =0.0;
		pt[1][0][1] =-50.0;
		pt[1][0][2] =0.0;
		//pt[2][0] = {50.0, 0.0, 0.0};
		pt[2][0][0] =50.0;
		pt[2][0][1] =0.0;
		pt[2][0][2] =0.0; 
		//pt[3][0] = {-50.0, 0.0. 0.0};
		pt[3][0][0] =-50.0;
		pt[3][0][1] =0.0;
		pt[3][0][2] =0.0;
		//pt[4][0] = {0.0, 10.0, 0.0};
		pt[4][0][0] =0.0;
		pt[4][0][1] =10.0;
		pt[4][0][2] =0.0;
		//pt[5][0] = {0.0, -10.0, 0.0};
		pt[5][0][0] =0.0;
		pt[5][0][1] =-10.0;
		pt[5][0][2] =0.0;
		//pt[6][0] = {10.0, 0.0, 0.0};
		pt[6][0][0] = 10.0;
		pt[6][0][1] =0.0;
		pt[6][0][2] =0.0;
		//pt[7][0] = {-10.0, 0.0, 0.0};
		pt[7][0][0] =-10.0;
		pt[7][0][1] =0.0;
		pt[7][0][2] =0.0;
	
	for(t =1; t <= (int)(tmax/dt +1); t++){ 	
		for(j=0; j <= LINES -1; j++){
		
			x = pt[j][t-1][0];
			y = pt[j][t-1][1];
			z = pt[j][t-1][2];
	
			pt[j][t][0] = x + 10*(y - x)*dt;
			pt[j][t][1] = y + (rley*x - x*z -y)*dt;
			pt[j][t][2] = z + (x*y - 8*z/3)*dt;
			}
		}
	}
	
void display(void)
{
	int  t;
	//float pt[LINES][(int)(tmax/dt +1)][3];
	int j, k, i, time;
	float x, y, z, point[3];
	
	for(t =1; t <= (int)(tmax/dt +1); t++){ 

	
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	glLoadIdentity( );


	glTranslatef(0.0f, 0.0f, -150.0f);
	glRotatef(mouse_x, 1.0f, 0.0f, 0.0f);
	glRotatef(mouse_y, 0.0f, 0.0f, -1.0f);

	
	for(j=0; j < LINES; j++){
		glBegin(GL_LINES);
		glColor3f(col[j][0], col[j][1], col[j][2]);
		for(time = (int)(t/rmov); time <= t; time ++){
			glVertex3fv( pt[j][time-1]);
			glVertex3fv(pt[j][time]);
			}
		glEnd();
	   }
	 
	} 
	 
	glBegin(GL_LINES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(  0.0f,  0.0f,  -1.5*e*.1f );
		glVertex3f(  0.0f,  0.0f,  1.5*e*.1f );
		glVertex3f(  0.0f,  -1.5*e*.1f,  0.0f );
		glVertex3f(  0.0f,  1.5*e*.1f,  0.0f );
		glVertex3f(  -1.5*e*.1f,  0.0f,  0.0f );
		glVertex3f(  1.5*e*.1f,  0.0f,  0.0f );
	glEnd();	
	glColor3f(1.0f, 1.0f, 1.0f);
	glutWireCube(e*0.2);
	
	glBegin(GL_POINTS);
	glColor3f(0.0f, 1.0f, 0.0f);
	for(i = -4; i <=4; i ++){
		for(j = -4; j <= 4; j++){
			for(k = -4; k <= 4; k++){
				point[0] = (float)i/4.0*e*.1;
				point[1] = (float)j/4.0*e*.1;
				point[2] = (float)k/4.0*e*.1;
				glVertex3f( point[0], point[1], point[2]);
				}
			}
		}
		glEnd();		
	
	glutSwapBuffers( );

    glutPostRedisplay( );
	
	}
   
void init( )
	{
	//int i;//,j,k;
	//float point[3];
	//printf("allocating", LINES);
	
		
	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );   // Black background
	glClearDepth( 1.0f );                     // Depth buffer setup
		glEnable(GL_COLOR_MATERIAL);
	
	 glMatrixMode(GL_PROJECTION);
	glLoadIdentity( );
	gluPerspective(60.0, 1., 0.1, 4*e);
	//glOrtho(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity( );
   //glTranslatef(0.0f, 0.0f, 0.0f);
   
   
		
}

void ramp(int i) 
{
	long c;
	int ri, gi, bi;
	float r, g, b;
	//GLfloat(*color)[3] = {0,0,0};
	float m = (6.0* (float)i/ (float)LINES);

	if((m >= 0.0) && (m < 1.0)) {
	r = 1.0;
	g =m;
	b = 0.0;
	}
	else if((m >= 1.0) && (m < 2.0)){
	r = 2.0 -m;
	g = 1.0;
	b = 0.0;
	}
	else if((m >=2.0)&&(m < 3.0)){
	r = 0.0;
	g = 1.0;
	b = m - 2.0;
	}
	else if((m >= 3.0) &&(m < 4.0)){
	r = 0.0;
	g = 4.0 -m;
	b = 1.0;
	}
	else if((m >= 4.0) &&(m < 5.0)){
	r = m- 4,0;
	g = 0.0;
	b = 1.0;
	}
	else if((m >= 5.0) &&(m < 6.0)){
	r = 1.0;
	g = 1.0;
	b = 6.0 -m;
	}
	else {
		r = 1.0;
		g = 1.0;
		b = 1.0;
	}
	 col[i][0] = r;
	col[i][1] = g;
	col[i][2] = b;
	//GLfloat(*color)[3] = {r, g, b};
	
	}
	
	
	
void passiveMouse(int x, int y)
{
	mouse_x =0.5*mouse_x + 0.5*x;
	mouse_y = 0.5*mouse_y + 0.5*y;
}

 
int main(int argc, char** argv)
	{
		long xrot, yrot;
		int t, t0;
	
		
		mouse_x = 0; mouse_y = 0;
		
		e = 500.0;
		//dt = .01;
		//tmax = 10.0;
		rley = 28.0;
		rmov = 2;
		lw = 1;
		
		//float pt[LINES][(int)(tmax/dt +1)][3];
		 //  glClearColor(0.0, 0.0, 0.0, 0.0);
		glutInit(&argc, argv);
		glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
		glutInitWindowSize(400, 400);
		glutCreateWindow("vector field");	
		init( );
		calc( );
		
		glLineWidth(lw);
		
		glutPassiveMotionFunc(passiveMouse);
//		glutMotionFunc(passiveMouse); 


		glutDisplayFunc(display);
		
		
	
		glutMainLoop( );
		return 0;
}
		

