/****************************************************************************
 * From Hill, Figure 2.10                                                   *
 * Modified for triangles                                                   *
 *  Linkage flags:                                                          *
 *   -lX11 -lGL -lGLU -lglut -lXmu -L/usr/X11R6/lib                         *
 ****************************************************************************/
// Libraries on linux systems
#include <X11/X.h> // X libraries
#include <GL/gl.h> // Basic OpenGL Libraries 
#include <GL/glut.h> // GL Utility Toolkit

/****************************************************************************
 * Package globals into a single global object                              *
 ****************************************************************************/
class TriTry
{
public:

  // Constructor for these objects is empty
  TriTry() { };

  // init --- initialize
  //    Can't do this in the constructor because glut will not yet have been 
  //     initialized
  void init()
  {
    //<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
    glutInitWindowSize(640,480);          // set window size
    glutInitWindowPosition(100, 150);     // set window position on screen
    glutCreateWindow("try"); // open the screen window
    glClearColor(1.0,1.0,1.0,0.0);        // set white background color
    glColor3f(0.0f, 0.0f, 0.0f);          // set the drawing color 
    glPointSize(4.0);		          // a `dot' is 4 by 4 pixels
    glMatrixMode(GL_PROJECTION);       
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 480.0);
  }
  //triangle function
void serpent(int n=5, int tx=320, int ty=48, int lx=64, int ly=432, 
	     int rx=574, int ry=432)
{
	if(n==0)
	{
		glVertex2i(tx,ty);
		glVertex2i(lx,ly);
		glVertex2i(lx,ly);
		glVertex2i(rx,ry);
		glVertex2i(rx,ry);
		glVertex2i(tx,ty);
	}
	else
	{
		n--;
		serpent(n,(tx+lx)/2,(ty+ly)/2,lx,ly,(lx+rx)/2,ly);
		serpent(n,tx,ty,(tx+lx)/2,(ty+ly)/2,(tx+rx)/2,(ty+ry)/2);
		serpent(n,(tx+rx)/2,(ty+ry)/2,(lx+rx)/2,ly,rx,ry);
	}
}

  // reDisplay method updates display of dots
  void reDisplay(void)
  {
    //<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>

	glClear(GL_COLOR_BUFFER_BIT);         // clear the screen 
    	glBegin(GL_LINES);
    
        serpent(6);
    	glEnd();	
   	glFlush();		                  // send all output to display 
  }

  // private

};

// This is the display object
TriTry triangle;

// This is the wrapper for the reDisplay callback function
void wrapDisplay(void) { triangle.reDisplay(); }

//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv)
{
  glutInit(&argc, argv);            // initialize the toolkit
  triangle.init();                      // initialize the display
  glutDisplayFunc(wrapDisplay);     // register redraw function
  glutMainLoop(); 		    // go into a perpetual loop
}

