/*
 * (C) Copyright Ian D. Romanick 2006
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>
#include "debug/glut.h"

GLfloat VertexArray[] = 
{ 
//   VERTEX COUNT
	   5,

//   VERTEX POSITION           VERTEX COLOR
//    X ,   Y ,  Z ,         R ,  G,   B ,  A
	-2.0, -2.0, 0.0,		1.0, 0.0, 0.0, 1.0,
	-3.0,  3.0, 0.0,		0.0, 1.0, 0.0, 1.0,
	 3.0, -3.0, 0.0,		0.0, 0.0, 1.0, 1.0,

	 2.0,  2.0, 0.0,		0.0, 0.0, 0.0, 1.0,

	 3.0,  3.0, 0.0,		1.0, 1.0, 1.0, 1.0
};

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


/**
 * Display callback for GLUT.
 */
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    /* Set the viewing transform.  We'll cover this during week 2.
     */
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 5.0,  /* eye position */
	          0.0, 0.0, 0.0,  /* point the eye is looking at */
	          0.0, 1.0, 0.0); /* "up" direction */

    /* Draw the defined triangle(s).
     */

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);

	glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 7, &VertexArray[1]);
	glColorPointer(4, GL_FLOAT, sizeof(GLfloat) * 7, &VertexArray[4]);

	glDrawArrays(GL_TRIANGLE_STRIP, 0, VertexArray[0]);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

    /* Since the display is singled buffered, flush all of the commands down
     * the pipeline so that the results can be seen.
     */
    glFlush();
}


void reshape(int width, int height)
{
    /* Set the view port and the perspective project transform.  We'll cover
     * this during week 2.
     */
    glViewport(0, 0, (GLsizei) width, (GLsizei) height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
}


void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
		case 27: //27 is the ASCII code for escape.
			exit(0);
			break;

		//Change the color when the user hits the proper keys
		case '+':
			VertexArray[6] += 0.05;
			if (VertexArray[6] > 1) { VertexArray[6] = 1; }
			VertexArray[11] += 0.05;
			if (VertexArray[11] > 1) { VertexArray[11] = 1; }
			VertexArray[19] += 0.05;
			if (VertexArray[19] > 1) { VertexArray[19] = 1; }

			break;
		case '-':
			VertexArray[6] -= 0.05;
			if (VertexArray[6] < 0) { VertexArray[6] = 0; }
			VertexArray[11] -= 0.05;
			if (VertexArray[11] < 0) { VertexArray[11] = 0; }
			VertexArray[19] -= 0.05;
			if (VertexArray[19] < 0) { VertexArray[19] = 0; }

			break;
    }
	
	glutPostRedisplay(); 
}


int main(int argc, char ** argv)
{
    /* Initialize GLUT.  Create our window.
     */
    glutInit(& argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(512, 384);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);


    /* Perform local program initialization.
     */
    init();
    

    /* Register our functions as GLUT callbacks.
     */
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);


    /* Let 'er rip!
     */
    glutMainLoop();

    
    /* Since glutMainLoop never actually returns, this will never be reached.
     * However, the explicit return make the compiler happy.
     */
    return 0;
}
