/*
 * initial.c
 * draw and spin my initials, woohoo!
 * Bill Thibault 1/13/98
 */
#include <stdlib.h>
#include <GL/glut.h>

#include <math.h>

long frame = 0;
int winHeight = 0;


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

void initials ( int frame, int forPicking )
{
  static GLfloat points[8][3] = {
    {-4,2,0},
    {-2.5,-2,0},
    {-2,0,0},
    {-1.5,-2,0},
    {0,2,0},
    {2,-2,0},
    {4,2,0},
    {2,2,0}
  };

  glPushMatrix();
  glRotatef ( (GLfloat)frame * 3.0, 0, 0, 1 );
  glRotatef ( (GLfloat)frame, 1, 0, 0 );
  glRotatef ( (GLfloat)frame * 2.0, 0, 1, 0 );

  glLineWidth ( 10.0 );

  /* W */
  if ( forPicking ) {
	  glColor3f ( 1.0/255.0, 0.0, 0.0 );
  } else {
	glColor3f ( 1.0, 0, 0 );
  }
  glBegin ( GL_LINE_STRIP );
   glVertex3fv ( points[0] );
   glVertex3fv ( points[1] );
   glVertex3fv ( points[2] );
   glVertex3fv ( points[3] );
   glVertex3fv ( points[4] );
  glEnd();

  /* T */
 if ( forPicking ) {
	  glColor3f ( 2.0/255.0, 0.0, 0.0 );
  } else {
	glColor3f ( 0, 0, 1 );
  }
  glBegin ( GL_LINES );
   glVertex3fv ( points[4] );
   glVertex3fv ( points[6] );
   glVertex3fv ( points[5] );
   glVertex3fv ( points[7] );
  glEnd ();

  glPopMatrix();

}


void display(void)
{

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   /*
    * Draw the initials
    */

   initials (frame++, 0);

   glutSwapBuffers();

}


int testPick ( int x , int y )
{
	unsigned char pixel[256];
	int objNum;
	glPushAttrib ( GL_ALL_ATTRIB_BITS );
	glShadeModel(GL_FLAT);
	glDisable(GL_LIGHTING);
	glDisable(GL_DITHER);
	glClearColor(0.0, 0.0, 0.0, 1.0);

	initials ( frame, 1 );
	
	glReadBuffer(GL_BACK);

	glReadPixels(x,winHeight-y,1,1,GL_RGB,GL_UNSIGNED_BYTE,pixel);
	objNum = pixel[0];
	//objNum = 255-objNum;
	glPopAttrib();
	
	if ( objNum != 0 )
		return objNum;
	else
		return 0;
}

void reshape (int w, int h)
{
	winHeight = h;
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 50.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef (0.0, 0.0, -10.0);
}

void mouse ( int button, int state, int x, int y )
{
	int obj;
	if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {
		obj = testPick ( x, y );
		printf ( "picked %d\n", obj );
	}
}

void keyboard (unsigned char key, int x, int y)
{
   switch (key) {
      case 27: /* ESC */
         exit(0);
         break;
      default:
         break;
   }
}

void
timer ( int foo ) 
{
	glutPostRedisplay();
	glutTimerFunc ( 100, timer, 0 );
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
   glutInitWindowSize (300, 300); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   //glutIdleFunc(display); 
   glutMouseFunc ( mouse );
   glutTimerFunc(100, timer, 0);
   glutMainLoop();
   return 0;
}
