///////////////////////////////////////////////////////////////
// squareAnnulus2.cpp
// 
// This program draws a square annulus as a triangle strip with
// vertices specified using the pointer form glVertex3fv() and 
// colors using the pointer form glColor3fv().
// 
// Interaction:
// Press the space bar to toggle between wireframe and filled.
//
// Sumanta Guha
///////////////////////////////////////////////////////////////

#include <iostream>

#ifdef __APPLE__
#  include <GLUT/glut.h>
#else
#  include <GL/glut.h>
#endif

using namespace std;

// Globals.
static int isWire = 0; // Is wireframe?

// Vertex co-ordinate vectors.
static float vertices[8][3] = 
{
	{30.0, 30.0, 0.0}, 
	{10.0, 10.0, 0.0}, 
	{70.0, 30.0, 0.0}, 
	{90.0, 10.0, 0.0},
	{70.0, 70.0, 0.0},
	{90.0, 90.0, 0.0},
	{30.0, 70.0, 0.0},
	{10.0, 90.0, 0.0}
};

// Vertex color vectors.
static float colors[8][3] =  
{
	{0.0, 0.0, 0.0},
	{1.0, 0.0, 0.0},
	{0.0, 1.0, 0.0},
	{0.0, 0.0, 1.0},
	{1.0, 1.0, 0.0},
	{1.0, 0.0, 1.0},
	{0.0, 1.0, 1.0},
	{1.0, 0.0, 0.0}
};

// Drawing routine.
void drawScene(void)
{
   glClear(GL_COLOR_BUFFER_BIT);

   if (isWire) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

   // Draw square annulus.
   glBegin(GL_TRIANGLE_STRIP);
      for(int i = 0; i < 10; ++i)
	  {
         glColor3fv(colors[i%8]);
         glVertex3fv(vertices[i%8]);
	  }
   glEnd();

   glFlush();
}

// Initialization routine.
void setup(void) 
{
   glClearColor(1.0, 1.0, 1.0, 0.0); 
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
   glViewport(0, 0, (GLsizei)w, (GLsizei)h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
   switch(key) 
   {
      case ' ':
         if (isWire == 0) isWire = 1;
         else isWire = 0;
         glutPostRedisplay();
         break;	  
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}

// Routine to output interaction instructions to the C++ window.
void printInteraction(void)
{
   cout << "Interaction:" << endl;
   cout << "Press the space bar to toggle between wireframe and filled." << endl;
}

// Main routine.
int main(int argc, char **argv) 
{
   printInteraction();
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize(500, 500); 
   glutInitWindowPosition(100, 100);
   glutCreateWindow("squareAnnulus2.cpp");
   setup();
   glutDisplayFunc(drawScene); 
   glutReshapeFunc(resize);  
   glutKeyboardFunc(keyInput);
   glutMainLoop();

   return 0;   
}
