#include "cs148.h"

#define FIELD_SIZE 3.0

void axis(double length) {
	glPushMatrix();
	glBegin(GL_LINES);
		glVertex3d(0,0,0); 
		glVertex3d(0,0,length); 
	glEnd();
	glTranslated(0,0, length-0.2);
	glutWireCone(0.04, 0.2, 12, 9);
	glPopMatrix();
}

void drawField() {
  	
	// draw the axes
	glColor3d(0, 0, 1.);
	axis(FIELD_SIZE);
	glPushMatrix();
	glRotated(90, 0, 1, 0);
	glColor3d(1., 0, 0);
	axis(FIELD_SIZE);
	glRotated(-90, 1, 0, 0);
	glColor3d(0, 1., 0);
	axis(FIELD_SIZE);
	glPopMatrix();
	glColor3d(0, 0, 0);
	
	// draw a cube showing boundaries of drawing space
	glPushMatrix();
	glTranslated(FIELD_SIZE/2.0, FIELD_SIZE/2.0, FIELD_SIZE/2.0);
	glutWireCube(FIELD_SIZE);
	glPopMatrix();

  glPointSize(8.0);

}

void display(void) {

	glClear(GL_COLOR_BUFFER_BIT);

  // Draw a nice field so we can see the axes
  drawField();

  // We'll talk more about this soon... it lets us cancel the transformation.
  glPushMatrix();

    // Red point: translation then scale
    glColor3d(1.0,0.0,0.0);  
    glTranslatef(1.0,0,0);
    glScalef(2.0,2.0,2.0);
    glBegin(GL_POINTS);
	    glVertex3d(1.0,1.0,1.0);
    glEnd();

  // Cancel the transformation (we'll talk about this more later)
  glPopMatrix();

  // We're going to demonstrate another way of un-doing transformations here...
  // glPushMatrix();
  
    // Green point: scale then translate
    glColor3d(0.0,1.0,0.0);  
    glScalef(2.0,2.0,2.0);
    glTranslatef(1.0,0,0);
    glBegin(GL_POINTS);
	    glVertex3d(1.0,1.0,1.0);
    glEnd();

  // glPopMatrix();
  // Manually undo the transformations...
  glTranslatef(-1.0,0,0);
  glScalef(0.5,0.5,0.5);

  glPushMatrix();

    // Blue point: rotate then translate 
    glColor3d(0,0,1); 
    glRotatef(90,0,0,1);
    glTranslatef(-1.0,0,0);
    glBegin(GL_POINTS);
	    glVertex3d(1.0,0.0,0.0);
    glEnd();

  glPopMatrix();
  
  glPushMatrix();

    // Purple point: translate then rotate
    glColor3d(1,0,1); 
    glTranslatef(-1.0,0,0);
    glRotatef(90,0,0,1);
    glBegin(GL_POINTS);
	    glVertex3d(1.0,0.0,0.0);
    glEnd();

  glPopMatrix();

	glutSwapBuffers();
}

void myInit(void) {

	glClearColor(1.0,1.0,1.0,0.0);
	glColor3f(0.0f,0.0f,0.0f);
	glPointSize(12.0);
	
}


void myReshape(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Set up a perspective camera
    //gluLookAt(3,3,3,0,0,0,-3,3,-3);
    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef (-1.5, -1.5, -6.0);
}


void main(int argc, char** argv) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,150);
	glutCreateWindow("Transformations");
	glutDisplayFunc(display);
  glutReshapeFunc(myReshape);
	myInit();
	glutMainLoop();
}

