/* zteapot.c, Utah teapot for demonstrating Zbuffer and culling */

#include <GLUT/glut.h> // Needs editing for other systems
#include <math.h>

long count = 0;

void display()
{
	// Clear frame buffer, Z buffer and stencil buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // <- ***
	
	count++;
	
	// Draw the shape
	glRotatef(0.2, 1,1,1);
	glScalef(-1,1,1); // Flip teapot (or cull fronts)
	glutSolidTeapot(1);
	glScalef(-1,1,1); // Flip back
	
	glutSwapBuffers();
}

void reshape(GLsizei w, GLsizei h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1, 1, -1, 1, 1, 20); // left, right, bottom, top, near, far
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 0, 2.5, 0, 0, 0, 0, 1, 0); // camera, look-at-point, up-vector
}

void timer(int i)
{
	printf("%d fps\n", count/5);
	count = 0;
	glutTimerFunc(5000, timer, i);
}

int main(int argc, char **argv)
{
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); // <- ***
	glutInit(&argc, argv);
	glutCreateWindow("teapot");
	glutDisplayFunc(display);
	glutIdleFunc(display); // Max speed
	glutTimerFunc(5000, timer, 43);
	glutReshapeFunc(reshape);
	
	glClearColor(0.7, 0.9, 0.8, 1);
	glEnable(GL_CULL_FACE); // Comment/uncomment this
	glEnable(GL_DEPTH_TEST); // Comment/uncomment this
	
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	
	glutMainLoop();
}

