#include "cs148.h"

struct point {
  int x,y;
};


int g_one_triangle[6];
int g_triangle_strip[12];
int g_random_polygon[12];


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);

  // 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
  g_one_triangle[0] = xpos; g_one_triangle[1] = 0;
  g_one_triangle[2] = xpos+width; g_one_triangle[3] = 0;
  g_one_triangle[4] = xpos; g_one_triangle[5] = height;

  xpos += width+gap;

  // A blue triangle strip that makes a rectangle
  g_triangle_strip[0]  = xpos + width/2; g_triangle_strip[1] = height/2;
  g_triangle_strip[2]  = xpos + width;   g_triangle_strip[3] = height;
  g_triangle_strip[4]  = xpos + 0;       g_triangle_strip[5] = height;
  g_triangle_strip[6]  = xpos + 0;       g_triangle_strip[7] = 0;
  g_triangle_strip[8]  = xpos + width;   g_triangle_strip[9] = 0;
  g_triangle_strip[10] = xpos + width;   g_triangle_strip[11] = height;

  xpos += width+gap;

  // A green random polygon with 5 sides
 
  // Choose 6 random points
  int i;
  for(i=0; i<12; i+=2) {
    g_random_polygon[i]   = CS148::RandInt(width)+xpos;
    g_random_polygon[i+1] = CS148::RandInt(height);
  } 

}


void myDisplay(void) {

	glClear(GL_COLOR_BUFFER_BIT);
  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  glEnableClientState(GL_VERTEX_ARRAY);
  glLineWidth(3.0);
  
  // A red triangle
  glColor3f(1.0f,0,0);
  glVertexPointer(2,GL_INT,0,g_one_triangle);
  glDrawArrays(GL_TRIANGLES,0,3);

  // A blue triangle strip (shaped like a rectangle)
  glColor3f(0,0,1.0f);
  glVertexPointer(2,GL_INT,0,g_triangle_strip);
  glDrawArrays(GL_TRIANGLE_STRIP,0,6);

  // A green random polygon with 5 sides
  glColor3f(0.0,1.0,0.0);
  
  // Choose 6 random points
  int i;
  int width = 150;
  int height = 300;
  int xpos = 450;

  for(i=0; i<12; i+=2) {
    g_random_polygon[i]   = CS148::RandInt(width)+xpos;
    g_random_polygon[i+1] = CS148::RandInt(height);
  }
  
  glVertexPointer(2,GL_INT,0,g_random_polygon);
  glDrawArrays(GL_POLYGON,0,6);

  // Draw the same points we just drew, as a black line
  glColor3f(0,0,0);
  glDrawArrays(GL_LINE_LOOP,0,6);
  
  glDisableClientState(GL_VERTEX_ARRAY);

	glFlush();
}


void main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,150);
	glutCreateWindow("Vertex Array Demo");
	glutDisplayFunc(myDisplay);
	myInit();
	glutMainLoop();
}

