/* Lorenz Mask in GLUT, gkf 3jan2K */ 
#include <stdlib.h>
#include <stdio.h>
#include <GLUT/glut.h>
#include <math.h>
/***********************************************************/
/* assuming the window has an 800 x 400 pixels */
float xo = 400 , yo = 200 ,             /* screen origin */
      ux = 8 , uy =-8 ,             /*  screenunits */
      xx = 5, yy = -1 , zz = 0.,    /* world particle */ 
      ee = .05,            /* epsilon eye shear */ 
      ns = 200,             /* nose displacement */
      dd = .01;            /* delta stepsize  */
/***********************************************************/
void plotAdot(float xx, float yy, float red, float green, float blue){
     glColor3f(red,green,blue);
     glBegin(GL_POINTS); glVertex2f(xx,yy); 
     glEnd();
}      
/***********************************************************/
void display(){       
  {static first=1; if(first)        /* first time clear the slate */ 
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.,0.,0.,0.);       /* to black */
    first =0;}
    glDrawBuffer(GL_FRONT);
  { /* draw the dots */
    float xleft = xo - ns, xriht = xo + ns;
    plotAdot(xleft  + (xx - ee*zz)*ux, yo - yy*uy, 1.,0.,1. ) ;
    plotAdot(xriht  + (xx + ee*zz)*ux, yo - yy*uy, 0.,1.,1. ) ; } 

  {  /*update a dot */     
    float xn,yn,zn;  /*local variables */
    xn = xx + (10*yy - 10*xx)*dd;
    yn = yy + (28*xx - xx*zz - yy)*dd;
    zn = zz + (xx*yy - 8*zz/3)*dd;
    xx = xn; yy = yn; zz = zn; }
glFlush();
  /*  usleep(NAP);  */   
}
/***********************************************************/
void keyboard(unsigned char key, int x, int y){
  switch(key){ 
     case  27: fprintf(stderr," Thanks for using GLUT ! \n"); exit(0); break;
     case 'w': {glClear(GL_COLOR_BUFFER_BIT);
                glClearColor(0.,0.,0.,0.);
                glutPostRedisplay();
                break;};
   }
}
/***********************************************************/
void idle(void){ glutPostRedisplay(); } 
/***********************************************************/
int main(int argc, char **argv){ 
  glutInitWindowSize(800, 400);        
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); 
  glutCreateWindow("<< Sierpinski in GLUT >>");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);
  glutIdleFunc(idle); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,800,0,400,-10.0,10.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}
