/*
	Example - simple rotations around the X-axis
	
	Assumptions are simple -- body is modeled as a set of small squares, and heat flows
	between squares based on the relative heats of the two squares and a diffusion
	function that includes a constant that models the heat conductivity of the bodies.
	
	The diffusion function mimics the error-diffusion approach of some standard halftoning
	techniques
*/
#include "glut.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

float vp = 6.0, wiggle = 0.0, t = 0.0;
GLUquadric *myQuad;

void myinit(void);
void square(void);
void display(void);
void reshape(int, int);
void animate(void);

void myinit(void)
{
	glEnable (GL_DEPTH_TEST);
	glClearColor(0.0, 0.0, 1.0, 1.0); // blue background
}

void display( void )
{
//	In this example, the indentation level shows the level of activity on the transformation stack
//	The basis for this example is the unit gluSphere; everything else is done by explicit transformations
	
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
		//	model the head
		glColor3f(0.4, 0.4, 0.4);	//	dark gray head
		glScalef(3.0, 1.0, 1.0);
		myQuad = gluNewQuadric();
		gluSphere(myQuad, 1.0, 10, 10);
	glPopMatrix();
	glPushMatrix();
		//	model the left eye
		glColor3f(0.0, 0.0, 0.0);	//	black eyes
		glTranslatef(1.0, -0.7, 0.7);
		glScalef(0.2, 0.2, 0.2);
		myQuad = gluNewQuadric();
		gluSphere(myQuad, 1.0, 10, 10);
	glPopMatrix();
	glPushMatrix();
		//	model the right eye
		glTranslatef(1.0, 0.7, 0.7);
		glScalef(0.2, 0.2, 0.2);
		myQuad = gluNewQuadric();
		gluSphere(myQuad, 1.0, 10, 10);
	glPopMatrix();
	glPushMatrix();
		//	model the left ear
		glColor3f(1.0, 0.6, 0.6);	//	pink ears
		glRotatef(-10.0*wiggle, 0.0, 0.0, 1.0);
		glTranslatef(-1.0, -1.0, 1.0);
		glRotatef(-45.0, 1.0, 0.0, 0.0);
		glTranslatef( 0.5, 0.0, 0.0);	// begin
		glRotatef(-10.0*wiggle, 0.0, 0.0, 1.0);
		glTranslatef(-0.5, 0.0, 0.0);	// end
		glScalef(0.5, 2.0, 0.5);
		myQuad = gluNewQuadric();
		gluSphere(myQuad, 1.0, 10, 10);
	glPopMatrix();
	glPushMatrix();
		//	model the right ear
		glColor3f(1.0, 0.6, 0.6);
		glTranslatef(-1.0, 1.0, 1.0);
		glRotatef(45.0, 1.0, 0.0, 0.0);
		glTranslatef( 0.5, 0.0, 0.0);	// begin
		glRotatef(10.0*wiggle, 0.0, 0.0, 1.0);
		glTranslatef(-0.5, 0.0, 0.0);	// end
		glScalef(0.5, 2.0, 0.5);
		myQuad = gluNewQuadric();
		gluSphere(myQuad, 1.0, 10, 10);
	glPopMatrix();
	glutSwapBuffers();
}

void reshape(int w,int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, (float)w/(float)h, 1.0, 300.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//           eye point     center of view       up
	gluLookAt(vp/2.0, vp/2.0, vp, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
}

void animate(void)
{
#define twopi 6.28318

	t += 0.1;
	if (t > twopi) t -= twopi;
	wiggle = cos(t);
	glutPostRedisplay();
}

void main(int argc, char** argv)
{
/* Standard GLUT initialization */
	    glutInit(&argc,argv);
	    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
	    glutInitWindowSize(500,500);
	    glutInitWindowPosition(50,50);
		glutCreateWindow("Bunny View");
	    glutDisplayFunc(display);
	    glutReshapeFunc(reshape);
	    glutIdleFunc(animate);
		
        myinit();
	    glutMainLoop(); /* enter event loop */
}