/* 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"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

//universal constants
 double xmax=1.5, xmin=-2.5, ymax=2.0, ymin=-2.0; //screen size
 double intv=.001; //pixels ploted
 double rali, imagi; //Julia set initial points
 int pressed=0; //Mantel of Julia
 int wid=400, hig=400; //screen size
//

static void Init (void)
//sets up the screen
{
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glShadeModel( GL_FLAT  );
}

static void Reshape (int width, int height )
//sets up the screen. How large and wide it is. 
{ 
  wid=width; hig=height;
  glViewport( 0, 0, (GLint)width, (GLint)height);
  glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
  glOrtho( xmin,xmax,ymin,ymax, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
}


static void color(int num)
//color defaults for number of iterations to escape
//depending on Mandel of Julia the color goes from green to red
{
 int temp;
 temp = num %10;
 switch (temp)
 {
  case 1: glColor3f(1*pressed, .1, .1 ); break;
  case 2: glColor3f(1*pressed, .2, .1 ); break;

  case 3: glColor3f(1*pressed, .3, .1); break;
  case 4: glColor3f(1*pressed, .4, .1); break;

  case 5: glColor3f(1*pressed, .5, .1); break;
  case 6: glColor3f(1*pressed, .6, .1); break;

  case 7: glColor3f(1*pressed ,.7,.1); break;
  case 8: glColor3f(1*pressed ,.8, .1); break;

  case 9: glColor3f(1*pressed,.9,.1); break;
  case 0: glColor3f(1*pressed,1,1); break;
 }
}
void DrawMyStuff()
//plotting the points
{

 float imag,ral,temp; 
 double i,t;
 double max, min;
 glBegin( GL_POINTS );
 float check;
 int iter;

 min=-3.0;//presets for max and min
 max=3.0;

 for (i=min; i<max; i+=intv)
   for (t=min; t<max; t+=intv)
       {
 	check=0;
	iter=0;
	imag=i;
	ral=t;
	if (pressed==0)
 	  {
 	  //decides if Julia of Mandel is being plotted
            rali=t;
  	    imagi=i;
	  }
        while ((check<4)&&(iter<100)) 
          {
 	//plots the points for the equation
	  temp=ral;
          ral= ( (ral*ral) - (imag*imag) + rali );
 	  imag= ( (imagi) + (2*temp*imag) );
 	  check= ( (ral*ral) + (imag*imag) );
	  iter++;
          }
  	//checks for bounding 
        if (check<4)  glColor3f( 0.0, 0.0, 0.0 );
        if (check>4)  color(iter);
        glVertex3f(t,i,0.0);
       }
 glEnd();
 
}
static void display()
//displays
{
 glClear ( GL_COLOR_BUFFER_BIT);
 DrawMyStuff();
 glFlush(); //pushes the drawn stuf
 auxSwapBuffers(); //replots second buffer 
}

void locate( int button, int state, int x, int y)
//mouse commands 
{
if (button==GLUT_LEFT_BUTTON)
   {
       if (state==GLUT_DOWN)
         {
	  // grab point in Mandel and plots Julia
           	cout<<"left button pressed"<<endl;
      		pressed=1;
		rali=xmin+(xmax-xmin)*x/wid;
      		imagi=ymax-(ymax-ymin)*y/hig;
  		cout<<"Real-"<<rali<<endl;
		cout<<"Imaginary-"<<imagi<<endl;
      		glutPostRedisplay();
    	 }
    } 
if (button==GLUT_RIGHT_BUTTON)
   {
       if  (state==GLUT_DOWN)
	{
	 // in Julia, returns to Mandel
           	cout<<"right button pressed"<<endl;
      		pressed=0;
      		glutPostRedisplay();
         }
    }
}
int main (int argc, char **argv)
//Display commands
{ 
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glEnable(GL_DEPTH_TEST);

 glutInitWindowSize(400, 400);
 glutInitWindowPosition(100,100);
 if (glutCreateWindow("Mantle Box")==GL_FALSE) auxQuit();
 Init();
 glutDisplayFunc(display);
 glutReshapeFunc(Reshape);
 glutMouseFunc(locate);
 glutMainLoop();

}

    
