//////////////////////////////////////////////////////////////
// tetrahedron.cpp
//
// This program draws a tetrahedron.
//
// Interaction:
// Press x, X, y, Y, z, Z to turn the tetrahedron.
//
// Sumanta Guha
//////////////////////////////////////////////////////////////

#include <iostream>

#ifdef __APPLE__
#  include <GLUT/glut.h>
#else
#  include <GL/glut.h>
#endif

using namespace std;

// Begin globals.
static float Xangle = 150.0, Yangle = 0.0, Zangle = 0.0; // Angles to rotate the tetrahedron.

// Vertex co-ordinate vectors for the tetrahedron.
static float vertices[] =  
{	
	 1.0,  1.0,  1.0, // V0
    -1.0,  1.0, -1.0, // V1
     1.0, -1.0, -1.0, // V2
	-1.0, -1.0,  1.0  // v3
};

// Vertex indices for the four trianglular faces.
static int triangleIndices[4][3] =
{
	{1, 2, 3}, // F0
	{0, 3, 2}, // F1
	{0, 1, 3}, // F2
	{0, 2, 1}  // F3
};
// End globals.

// Drawing routine.
void drawScene(void)
{
   int i;

   glClear(GL_COLOR_BUFFER_BIT);  

   glLoadIdentity();
   gluLookAt(0.0, 3.0, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

   glColor3f(0.0, 0.0, 0.0);
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

   // Rotate scene.
   glRotatef(Zangle, 0.0, 0.0, 1.0);
   glRotatef(Yangle, 0.0, 1.0, 0.0);
   glRotatef(Xangle, 1.0, 0.0, 0.0);

   // Draw tetrahedron.
   glBegin(GL_TRIANGLES);
      for(i = 0; i < 4; ++i)
	  {
         glArrayElement(triangleIndices[i][0]);
         glArrayElement(triangleIndices[i][1]);
         glArrayElement(triangleIndices[i][2]);
	  }
   glEnd();

   glutSwapBuffers();
}

// Initialization routine.
void setup(void) 
{
   glClearColor(1.0, 1.0, 1.0, 0.0); 

   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(3, GL_FLOAT, 0, vertices);
}

// OpenGL window reshape routine.
void resize (int w, int h)
{
   glViewport (0, 0, (GLsizei)w, (GLsizei)h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(60.0, (float)w/(float)h, 1.0, 50.0);
   glMatrixMode(GL_MODELVIEW);
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
   switch(key) 
   {
      case 27:
         exit(0);
         break;
      case 'x':
         Xangle += 5.0;
		 if (Xangle > 360.0) Xangle -= 360.0;
         glutPostRedisplay();
         break;
      case 'X':
         Xangle -= 5.0;
		 if (Xangle < 0.0) Xangle += 360.0;
         glutPostRedisplay();
         break;
      case 'y':
         Yangle += 5.0;
		 if (Yangle > 360.0) Yangle -= 360.0;
         glutPostRedisplay();
         break;
      case 'Y':
         Yangle -= 5.0;
		 if (Yangle < 0.0) Yangle += 360.0;
         glutPostRedisplay();
         break;
      case 'z':
         Zangle += 5.0;
		 if (Zangle > 360.0) Zangle -= 360.0;
         glutPostRedisplay();
         break;
      case 'Z':
         Zangle -= 5.0;
		 if (Zangle < 0.0) Zangle += 360.0;
         glutPostRedisplay();
         break;
      default:
         break;
   }
}
// Routine to output interaction instructions to the C++ window.
void printInteraction(void)
{
   cout << "Interaction:" << endl;
   cout << "Press x, X, y, Y, z, Z to turn the tetrahedron." << endl;
}

// Main routine.
int main(int argc, char **argv) 
{
   printInteraction();
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize(500, 500); 
   glutInitWindowPosition(100, 100);
   glutCreateWindow("tetrahedron.cpp");
   setup();
   glutDisplayFunc(drawScene); 
   glutReshapeFunc(resize);  
   glutKeyboardFunc(keyInput);
   glutMainLoop();

   return 0;   
}
