#include "cs148.h"

struct point {
  int x,y;
};


void myDisplay(void) {

	glClear(GL_COLOR_BUFFER_BIT);

  // Fill my polygons
  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

  // Some variables to help me keep my primitives from
  // overlapping
  int xpos = 0;
  int width = 150;
  int height = 300; 
  int gap = 30;

  // A red triangle
  glColor3f(1.0,0.0,0.0);
	glBegin(GL_TRIANGLES);
    glVertex2i(xpos,0);
    glVertex2i(xpos+width,0);
    glVertex2i(xpos,height);
	glEnd();

  xpos += width+gap;

  // A blue triangle strip (shaped like a ??)
  glColor3f(0.0,0.0,1.0);
  glBegin(GL_TRIANGLE_STRIP);
    glVertex2i(xpos + width/2,height/2);
    glVertex2i(xpos +   width,height);
    glVertex2i(xpos +       0,height);
    glVertex2i(xpos +       0,0);
    glVertex2i(xpos +   width,0);
    glVertex2i(xpos +   width,height);
	glEnd();

  xpos += width+gap;

  // A green random polygon with 5 sides
  glColor3f(0.0,1.0,0.0);
  
  // Choose 6 random points
  int i;
  point mypoints[6];
  for(i=0; i<6; i++) {
    mypoints[i].x = CS148::RandInt(width)+xpos;
    mypoints[i].y = CS148::RandInt(height);
  } 

  // Draw the polygon
  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  glBegin(GL_POLYGON);
    for(i=0; i<6; i++) glVertex2i(mypoints[i].x,mypoints[i].y);
	glEnd();
  
  // Draw the same points we just drew, as a black line
  glLineWidth(3.0);
  glColor3f(0,0,0);
  glBegin(GL_LINE_LOOP);
    for(i=0; i<6; i++) glVertex2i(mypoints[i].x,mypoints[i].y);
	glEnd();

	glFlush();
}


void myInit(void) {
	glClearColor(1.0,1.0,1.0,0.0);
	glColor3f(0.0f,0.0f,0.0f);
	glPointSize(12.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}


void main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,150);
	glutCreateWindow("OpenGL");
	glutDisplayFunc(myDisplay);
	myInit();
	glutMainLoop();
}

