#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>

// ogldemo.c
// Harriet Fell
// May 22, 2001
// based on 
// Example 1-3 : Double-Buffered Program: double.c
//		http://glprogramming.com/red/chapter01.html
// Example 2-5 : Line Stipple Patterns: lines.c
//		http://glprogramming.com/red/chapter02.html
// Example 2-6 : Polygon Stipple Patterns: polys.c
//		http://glprogramming.com/red/chapter02.html

#define drawOneLine(x1,y1,x2,y2)  glBegin(GL_LINES);  \
   glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();

// hfcolor: red,green, blue, a values between 0 to 1
struct hfcolor{
   float r,g,b,a;
};
// hfpoint: x, y, z - 3D float coordinates
struct hfpoint{
   float x,y,z;
};

static GLfloat spin = 0.0;

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}

void display(void)
{
	GLubyte fly[] = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60, 
      0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20, 
      0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
      0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22, 
      0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 
      0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
      0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 
      0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC, 
      0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
      0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0, 
      0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0, 
      0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
      0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08, 
      0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08, 
      0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};
      
   	glClear(GL_COLOR_BUFFER_BIT);
   	glPushMatrix();
   	glRotatef(spin, 0.0, 0.0, 1.0);
   
    glColor3f(0.0f, 1.0f, 1.0f);
    glPointSize(2.0f);
	glBegin(GL_POINTS);
      float k, h, v;      
      for( k=0; k<100; k++)
      {
         h=rand()%200 - 100;
         v=rand()%200 - 100;
         glVertex3f(h/2, v/2, 0.0);
      }  
    glEnd();
    
/* 
   glColor3f(1.0, 1.0, 1.0);

 	glBegin(GL_POLYGON);
      glVertex2f(-25.0, -25.0);
      glVertex2f(-25.0, 25.0);
      glVertex2f(0.0, 40.0);
      glVertex2f(25.0, 25.0);
      glVertex2f(25.0, -25.0);
    glEnd();
 */

 
/*
    glEnable (GL_POLYGON_STIPPLE);
    glPolygonStipple (fly);        
    glBegin(GL_POLYGON);
      glVertex2f(-25.0, -25.0);
      glVertex2f(-25.0, 25.0);
      glVertex2f(0.0, 40.0);
      glVertex2f(25.0, 25.0);
      glVertex2f(25.0, -25.0);
    glEnd();
    glDisable (GL_POLYGON_STIPPLE);
*/    
   
/* 
    glColor3f(1.0, 0.0, 1.0);   
    glBegin(GL_TRIANGLES);
      glVertex2f(-20.0, -20.0);
      glVertex2f(20.0, -20.0);
      glVertex2f(0.0, 20.0);
    glEnd();
 */

        
     
/* 
    glColor3f(0.0, 0.0, 1.0);   
    //glBegin(GL_TRIANGLE_FAN);
    glBegin(GL_TRIANGLE_STRIP);
      glVertex2f(60.0, -80.0);
      glVertex2f(80.0, -80.0);
      glVertex2f(60.0, -60.0);
      glColor3f(0.1, 0.1, 1.0);
      glVertex2f(80.0, -60.0);
      glColor3f(0.2, 0.2, 1.0);
      glVertex2f(60.0, -40.0);
      glColor3f(0.3, 0.3, 1.0);
      glVertex2f(80.0, -40.0);
      glColor3f(0.4, 0.4, 1.0);
      glVertex2f(60.0, -20.0);
      glColor3f(0.5, 0.5, 1.0);
      glVertex2f(80.0, -20.0);
      glColor3f(0.6, 0.6, 1.0);
      glVertex2f(60.0, 0.0);
      glColor3f(0.7, 0.7, 1.0);
      glVertex2f(80.0, 0.0);
      glColor3f(0.8, 0.8, 1.0);
      glVertex2f(60.0, 20.0);
      glColor3f(0.9, 0.9, 1.0);
      glVertex2f(80.0, 20.0); 
      glColor3f(1.0, 1.0, 1.0);
      glVertex2f(60.0, 40.0);
      glColor3f(1.0, 1.0, 0.9);
      glVertex2f(80.0, 40.0);
      glColor3f(1.0, 1.0, 0.8);
      glVertex2f(60.0, 60.0);
      glColor3f(1.0, 1.0, 0.7);
      glVertex2f(80.0, 60.0);
      glColor3f(1.0, 1.0, 0.6);
      glVertex2f(60.0, 80.0);
      glColor3f(1.0, 1.0, 0.5);
      glVertex2f(80.0, 80.0);
	glEnd();
 */

	
	
/* 
	glColor3f(0.8, 1.0, 0.5);
	drawOneLine(-80.0, -80.0, -80.0, 80.0);
	glEnable(GL_LINE_STIPPLE);
    glLineStipple(1,0x0F03);
    drawOneLine(-70.0, -80.0, -70.0, 80.0);
    glLineWidth(5.0);
    drawOneLine(-60.0, -80.0, -60.0, 80.0);
	glDisable(GL_LINE_STIPPLE);
	glLineWidth(1.0);
 */

	
	/* 
glColor3f(1.0, 0.0, 0.5);
	GLfloat V0[] = {-70.0, -85.0, 0.0};
	GLfloat V1[] = {0.0, -95.0, 0.0};
	GLfloat V2[] = {70.0, -85.0, 0.0};
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glBegin(GL_POLYGON);
    glEdgeFlag(GL_TRUE);
    glVertex3fv(V0);
    glEdgeFlag(GL_TRUE);
    glVertex3fv(V1);
    glEdgeFlag(GL_FALSE);
    glVertex3fv(V2);
	glEnd();
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 */



	
	glFlush();
    glPopMatrix();
    glutSwapBuffers();
}

void spinDisplay(void)
{
   spin = spin + 2.0;
   if (spin > 360.0)
     spin = spin - 360.0;
   glutPostRedisplay();
}

void reshape(int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
//  glMatrixMode(GL_MODELVIEW);
//  glLoadIdentity();
}


void mouse(int button, int state, int x, int y) 
{
   switch (button) {
     case GLUT_LEFT_BUTTON:
       if (state == GLUT_DOWN)
         glutIdleFunc(spinDisplay);
       break;
     case GLUT_RIGHT_BUTTON:
       if (state == GLUT_DOWN)
         glutIdleFunc(NULL);
       break;
     default:
       break;
   }
}

/* 
 * Request double buffer display mode.
 * Register mouse input callback functions
 */
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (400, 400); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape); 
   glutMouseFunc(mouse);
   glutMainLoop();
   return 0;
}
