#include <GL/glut.h>
#include <math.h>

GLsizei wh = 800, ww = 800;
float left_theta = 0.0, right_theta = 0.0;
int page_num = 0, old_page_num = 0;
int turning = 0;                 /*flag to know page is turning*/
/***sandi globals***/
float spositionx = 20, spositiony = 0; /*initial position of cone on page1*/
float scenter = 0, sbalance = 0;  /*for the changing point of balance
				   on page 1*/
float stheta = 18;         /*amount to rotate board on page1*/  
int slevel = 1;           /*level of expertise for page1 */
GLubyte stexture_image[3*32];
GLfloat sgenparams[] = {1.0, 1.0, 1.0, 0.0};

/*** seema globals ***/

float p_rock_theta = 0.0, p_rock_vert = 0.0;
float p_wood_theta = 0.0, p_wood_theta2 = 0.0, p_wood_vert = 0.0;
int p_level = 1;

/*** dave globals ***/
int dir=0, grow=0;                        /*book upright or flat & spring dir*/
float sprangle=15, springthick=0.7, springpos, springt, vinit = 70;  /*spring consts*/
int dlevel, dtype=1;                   /* begin/adv and which choice */
int dchoice, bounce=0;               /*ligth/heavy obj, # of bounces*/
float startpt;                     /*where bouncing object starts*/
int cancycle=0;


void sandipage(void);
void sandilight(void);
void sandimouse(int, int, int, int);
void sandimouse2(int, int);
void sanditext(void);
void sanditexture(void);
void sandimake_texture(void);
void unlight(void);
void untexture(void);
void spoly(int, int, int, int);

void seema_page();
void seema_light();
void seema_text();

void mover();

void davemouse(int, int, int, int);
void davelight();
void davepage(void);
void davetext();
void spring(int, int, int);             /* coils, diameter, ground */
void davespring(void);                  /* moving spring */
void davebounce();                      /* drop things on spring */
void davethrow();                       /* use spring to throw stuff */
void davemenu1(int);
void davemenu2(int);


void init(void)
{ 
  glClearColor(1.0, 0.0, 0.0, 0.0); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45, ww/wh, 10.0, 900.0);  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}


/* function for drawing page */

void page(float x, float y)
{
  glVertex3f(x, y, 0.0);
  glVertex3f(x+300.0, y, 0.0);
  glVertex3f(x+300.0, y+400.0, 0.0);
  glVertex3f(x, y+400.0, 0.0);
}

void display(void)
{ 
  int i;
  char cover[] = "Physics for Kids";
  char cover2[] = "by";
  char cover3[] = "Sandi, Seema, and Dave";
  char help1[] = "Welcome to our interactive physics book for children!";
  char help2[] = "Use the LEFT and RIGHT arrow keys to advance the pages forwards and backwards.";
  char help3[] = "Each page will have its own instructions on the left side.";
  char help4[] = "To quit, use the RIGHT mouse button.";
  char back[] = "The End";
  char page1[] = "Page 1";
  char page2[] = "Page 2";
  char page3[] = "Page 3";


  glLoadIdentity(); 
  gluLookAt(0.0, 0.0, 800.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glColor3f(1.0, 1.0, 1.0);
  glRasterPos3f(-290.0, -250.0, 1.0);
  for (i=0; i < strlen(help1); i++)
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, help1[i]);
  glRasterPos3f(-290.0, -270.0, 1.0);
  for (i=0; i < strlen(help2); i++)
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, help2[i]);
  glRasterPos3f(-290.0, -290.0, 1.0);
  for (i=0; i < strlen(help3); i++)
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, help3[i]);
  glRasterPos3f(-290.0, -310.0, 1.0);
  for (i=0; i < strlen(help4); i++)
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, help4[i]);


  /* draw left page if page = 0, i.e. cover page */
  
  if (page_num!=0)
  {
    glPushMatrix();
    glRotatef(left_theta, 0.0, 1.0, 0.0);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_QUADS);
      page(-300.0, -200.0);
    glEnd();
    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
      page(-300.0, -200.0);
    glEnd();
    
    if (page_num==4)
    {
      if(!turning)
	{
	  glRasterPos3f(-190.0, 10.0, 1.0);
	  for (i=0; i < strlen(back); i++)
	    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, back[i]);
	}
      old_page_num = 4;
    }
    glPopMatrix();
  }

  /* draw right page if book not closed shut */

  if (page_num!= 4)
  {
    glPushMatrix();
    glRotatef(-right_theta, 0.0, 1.0, 0.0);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
      page(0.0, -200.0);
    glEnd(); 
    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
      page(0.0, -200.0);
    glEnd();

    if (page_num==0 && !turning)
    {
      glRasterPos3f(90.0, 10.0, 1.0);
      for (i=0; i < strlen(cover); i++)
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, cover[i]);
       glRasterPos3f(142.0, -20.0, 1.0);
      for (i=0; i < strlen(cover2); i++)
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, cover2[i]);
      glRasterPos3f(75.0, -50.0, 1.0);
      for (i=0; i < strlen(cover3); i++)
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, cover3[i]);

      glPushMatrix();

      glTranslatef(120, 17, 22);          /* move to final position */
      glRotatef(60, 0, 1, 0);             /* make elipse */
      glRotatef(dir, 0, 0, 1);            /* rotate it */

      glTranslatef(30, 0, 0);             /* set orbit of 30*/

      glColor3f(0.0, 1.0, 0.0);
      glutSolidSphere(3, 15, 15);

      glTranslatef(-60, 0, 0);

      glColor3f(0.0, 1.0, 1.0);
      glutSolidSphere(3, 15, 15);
      glPopMatrix();

      glPushMatrix();
      glTranslatef(120, 17, 22);          /* move to final position */
      glRotatef(-60, 0, 1, 0);             /* make elipse */
      
      glRotatef(dir+90, 0, 0, 1);            /* rotate it */

      glTranslatef(30, 0, 0);             /* set orbit of 30*/

      glColor3f(1.0, 1.0, 0.0);
      glutSolidSphere(3, 15, 15);
    
      glPopMatrix();

      dir +=9;


    }


    /* if page=1, draw Sandi's page */
    if (page_num==1)
    {
      if (!turning)
      {
	glRasterPos3f(250.0, 185.0, 1.0);
	for (i=0; i < strlen(page1); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, page1[i]);
      }
      glutSetMenu(1);
      glutAttachMenu(GLUT_LEFT_BUTTON);
      glutDetachMenu(GLUT_MIDDLE_BUTTON);
      old_page_num = 1;
      sanditext();
      sandilight(); 
      sandipage();
    }

    /* if page=2, draw Seema's page */
    if (page_num==2)
    {
      if (!turning)
      {
	glRasterPos3f(250.0, 185.0, 1.0);
	for (i=0; i < strlen(page2); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, page2[i]);
      }
      
      glutDetachMenu(GLUT_LEFT_BUTTON);
      glutDetachMenu(GLUT_MIDDLE_BUTTON);
      glutSetMenu(2);
      glutAttachMenu(GLUT_LEFT_BUTTON);
      glutSetMenu(3);
      glutAttachMenu(GLUT_MIDDLE_BUTTON);
      old_page_num=2;
      seema_text();
      seema_light();
      seema_page();
    }


    /* if page=3, draw Dave's page */
    if (page_num==3)
    {
      if (!turning)
      {
	glRasterPos3f(250.0, 185.0, 1.0);
	for (i=0; i < strlen(page3); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, page3[i]);
      }
      if(old_page_num != 3)
	{
	  glutDetachMenu(GLUT_MIDDLE_BUTTON);
	  glutDetachMenu(GLUT_LEFT_BUTTON);
	  glutSetMenu(4);
	  glutAttachMenu(GLUT_LEFT_BUTTON);
	  old_page_num = 3;
	}
      davelight();
      davetext();
      davepage();

    }
    glPopMatrix();
  }
  glFlush();
  glutSwapBuffers();
} 


void keys(int key, int x, int y)
{

  /* right arrow key turns pages forward until back cover displayed */
/* if back cover displayed, next depression of this key displays front cover */

  if (key == GLUT_KEY_RIGHT)
  {
    glutDetachMenu(GLUT_LEFT_BUTTON);
    glutDetachMenu(GLUT_MIDDLE_BUTTON);

    turning = 1;
    unlight();

    while (right_theta < 190.0)
    {
      display(); 
      unlight();
      right_theta += 10.0;
    };
    page_num = (page_num + 1) % 5;
    right_theta = 0.0;
    turning = 0;
    display();
  }

  /* left arrow key turns pages back until front cover displayed */
  /* if front cover displayed, next depression of this key does nothing */

  if (key == GLUT_KEY_LEFT)
  {
    glutDetachMenu(GLUT_LEFT_BUTTON);
    glutDetachMenu(GLUT_MIDDLE_BUTTON);

    turning = 1;
    if (page_num > 0)
    {
      unlight();
      while (left_theta < 190.0)
      {
	display();
	unlight();
	left_theta += 10.0;
      };
      page_num = page_num - 1;
      left_theta = 0.0;
      turning = 0;
      display();
    }
  }
}

void mouse(int button, int state, int x, int y)
{
  if(page_num==1)
    {
      sandimouse(button, state, x, y);
    }
  else if(page_num==3)
    davemouse(button, state, x, y);
}

/*placing the cone and effects to the board*/
void sandimouse2(int x, int y)
{
  spositionx = x-400-150-50;   /*convert window to obj coords*/
  
  /*don't let cone go past board edges*/
  if(spositionx > 125)
    spositionx = 125;
  if(spositionx < -125)
    spositionx = -124;
  
  stheta = asin(45/(125 + fabs(spositionx)));
  stheta = (180*stheta)/ 3.14159;    /*convert to radians*/
  /*give 2 pixels of freedom for place of balance*/
  if((spositionx == sbalance) || 
     (spositionx > (sbalance-5) && spositionx < (sbalance+5)))
    {
      stheta = 0;
    }
  else if(spositionx < 0 && spositionx >sbalance)
    {
      stheta = asin(45/(125+spositionx));
      stheta = (180*stheta)/3.14159;
    }
  else if(spositionx < sbalance)
    {
      stheta = -stheta;
    }

  unlight();
  display();
}

void sandimouse(int button, int state, int x, int y)
{
  /*middle button places the cone*/
  if(button==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
    {
      sandimouse2(x, y);
    }
}


void davemouse(int button, int state, int x, int y)
{
  if ((dtype==2) && ((bounce==0)||(bounce==4)))  /*only for bouncing option */
    {
    if((button==GLUT_MIDDLE_BUTTON) && (x>400)&&(x<452)&&(y<350))
      dchoice = 1;
    else if((button==GLUT_MIDDLE_BUTTON) && (x<490) && (y<350))
      dchoice = 2;
    else 
      dchoice = 0;
    }
}

void sandipage(void)
{
  GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat mat_shininess[] = { 100.0 };
  GLfloat mat_diffuse[] = {1.0, 0.0, 1.0, 1.0 };
  GLfloat mat_specular2[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat mat_shininess2[] = { 50.0 };
  GLfloat mat_diffuse2[] = {1.0, 0.0, 0.0, 1.0 };
  GLfloat mat_specular3[] = { 1.0, 0.0, 0.5, 1.0 };
  GLfloat mat_shininess3[] = { 20.0 };
  GLfloat mat_diffuse3[] = {0.7, 0.4, 0.6, 1.0 };
  

  char weight1[] = "1";
  char weight2[] = "2";

  if(turning)
    {
      glScalef(1.0,1.0,0.1);
    }

  sanditexture();
  
  /*give the table its properties*/
  glPushMatrix();
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse3);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular3);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess3);
  glPopMatrix();

  /*draw the table*/
  glPushMatrix();
  glTranslatef(150, -100, 10);
  glRotatef(10, 1.0, 0.0, 0.0);
  glScalef(7, .2, 1);
  glutSolidCube(40);
  glPopMatrix();

  untexture();

  /*give the base its properties*/  
  glPushMatrix();
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  glPopMatrix();

  /*draw the base*/
  glPushMatrix();
  glTranslatef(150,-100,10);      /*place on lower rt page*/      
  glTranslatef(spositionx, spositiony, 10);  /*amount to move from mouse*/
  glRotatef(-90, 1.0, 0.0, 0.0);  /*for correct orientation of cone*/
  glutSolidCone(25, 45, 50, 50);
  glPopMatrix();

  /*give the bar its properties*/
  sanditexture();

  glPushMatrix();
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse2);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular2);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess2);
  glPopMatrix();

  /*draw the bar*/
  glPushMatrix();
  glTranslatef(150, -100, 10);   /*place on lower rt page*/
  glTranslatef(0, 50, 10);       /*to position above cone*/
  /*when the cone is moved, need to rotate board about the point
    where the cone is now.  So, translate that point to orgin, 
    rotate as needed, and translate back*/
  glTranslatef(spositionx, 0 , 0); 
  glRotatef(stheta, 0.0, 0.0, 1.0);
  glTranslatef(-spositionx, 0, 0);
  glScalef(5,.2,1);      /*change a cube into a board*/
  glutSolidCube(50);
  glPopMatrix();

  untexture();
  
  /*level 2: weights added*/
  if(slevel==2 || slevel ==3)
    {
      /*same explanations as for the board*/
      glPushMatrix();
      glTranslatef(150, -100, 10);
      glTranslatef(spositionx, 0.0, 0.0);
      glRotatef(stheta, 0.0 , 0.0, 1.0);
      glTranslatef(100, 60, 10);   /*position on board*/
      glTranslatef(-spositionx, 0.0, 0.0);
      glRasterPos3f(-5.0, 0.0, 25.0);
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, weight1[0]);
      glutSolidCube(20);
      glPopMatrix();

      glPushMatrix();
      glTranslatef(150, -100, 10);
      glTranslatef(spositionx, 0.0, 0.0);
      glRotatef(stheta, 0.0, 0.0, 1.0);
      glTranslatef(-100, 65, 10);  /*position on board*/
      glTranslatef(-spositionx, 0.0, 0.0);
      glRasterPos3f(-3.0, 0.0, 35.0);
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, weight2[0]);
      glutSolidCube(30);
      glPopMatrix();
    }

}


void sanditext(void)
{
  int i;
  char text1[] = "BALANCE";
  char text2[] = "Can you balance this board?";
  char text7[] = "Now there are weights on the board.";
  char text8[] = "The larger one weighs 2 kg and the";
  char text9[] = "smaller weighs 1kg.";
  char text10[] = "How does this change the balance?";
  char text11[] = "For the board to balance, this must be true: ";
  char text12[] = "d1*w1 = d2*w2";
  char text13[] = "where d1 and d2 are the distance from";
  char text14[] = "the cone, and w1 and w2 are the weights";
  char text15[] = "of the sides.";
  char text3[] = "Click the MIDDLE mouse button on";
  char text4[] = "the spot for the cone to balance the beam.";
  char text5[] = "Use the menu on the LEFT button to";
  char text6[] = "choose different levels.";
  if(!turning)
    {

      glRasterPos3f(-195, 165, 1.0);
      for (i=0; i < strlen(text1); i++)
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text1[i]);
      
      glRasterPos3f(-290, 110, 1.0);
      for (i=0; i < strlen(text2); i++)
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text2[i]);
      
      /*      glRasterPos3f(-290, 95, 1.0);
      for (i=0; i < strlen(text5); i++)
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text5[i]);

      glRasterPos3f(-290, 80, 1.0);
	  for (i=0; i < strlen(text6); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text6[i]);*/
      
	  /*help info at bottom of page*/
       glPushAttrib(GL_ALL_ATTRIB_BITS);
       glColor3f(0.0, 0.5, 0.0);
       glRasterPos3f(-290, -100, 1.0);
       for (i=0; i < strlen(text3); i++)
	 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text3[i]);
       glRasterPos3f(-290, -115, 1.0);
       for (i=0; i < strlen(text4); i++)
	 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text4[i]);
       glRasterPos3f(-290, -135, 1.0);
       for (i=0; i < strlen(text5); i++)
	 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text5[i]);
       glRasterPos3f(-290, -150, 1.0);
       for (i=0; i < strlen(text6); i++)
	 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text6[i]);
       
       glPopAttrib();	  


       if(slevel == 2 || slevel ==3)
	{
	  glRasterPos3f(-290, 70, 1.0);
	  for (i=0; i < strlen(text7); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text7[i]);
	  
	  glRasterPos3f(-290, 55, 1.0);
	  for (i=0; i < strlen(text8); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text8[i]);

	  glRasterPos3f(-290, 40, 1.0);
	  for (i=0; i < strlen(text9); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text9[i]);

	  glRasterPos3f(-290, 25, 1.0);
	  for (i=0; i < strlen(text10); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text10[i]);
	}

      if(slevel == 3)
	{
	  glRasterPos3f(-290, 0, 1.0);
	  for (i=0; i < strlen(text11); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text11[i]);
	  
	  glRasterPos3f(-290, -15, 1.0);
	  for (i=0; i < strlen(text12); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text12[i]);

	  glRasterPos3f(-290, -30, 1.0);
	  for (i=0; i < strlen(text13); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text13[i]);

	  glRasterPos3f(-290, -45, 1.0);
	  for (i=0; i < strlen(text14); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text14[i]);

	   glRasterPos3f(-290, -60, 1.0);
	  for (i=0; i < strlen(text15); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text15[i]);
	}
    }
      
}

void sandilight(void)
{
  GLfloat light_position[] = { 1.0, 0.0, 1.0, 0.0 };
  GLfloat light_spec[] = {1.0, 1.0, 1.0, 1.0 }; 
  GLfloat light_diff[] = {1.0, 1.0, 1.0, 1.0 };
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glLightfv(GL_LIGHT0, GL_SPECULAR, light_spec);
  /* glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diff);*/


  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
 
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
}

void sandimake_texture(void)
{
   int j;
    
   for (j = 0; j < 32; j++) 
     {
       stexture_image[3*j] = (j<=4) ? 255 : 0;
       stexture_image[3*j+1] = (j>4) ? 205 : 0;
       stexture_image[3*j+2] = 1;
     }
}

void sanditexture(void)
{
  sandimake_texture();
  
  glPushMatrix();
  
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexImage1D(GL_TEXTURE_1D, 0, 3, 32, 0,
	       GL_RGB, GL_UNSIGNED_BYTE, stexture_image);
  
  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  glTexGenfv(GL_S, GL_OBJECT_PLANE, sgenparams);
  
  glPopMatrix();

  glEnable(GL_TEXTURE_GEN_S);
  glEnable(GL_TEXTURE_1D);
}

void sandimenu(int id)
{
  if(id==1)
    {
      slevel=1;
      sbalance = 0;
      scenter = 150;
      sandimouse2(620,400);   /*reposition the balance*/
      unlight();
      display();
    }
  if(id==2)
    {
      slevel=2;
      sbalance = -42;
      scenter = 150-42;      
      sandimouse2(620,400);  /*reposition the balance*/
      unlight();
      display();
    }
  if(id==3) 
  {
    slevel = 3;
    sbalance = -42;
    scenter = 150-42;
    sandimouse2(620,400);
    unlight();
    display();
  }
  
}

void seema_menu(int id)
{
  if (id==1) p_level = 1;
  if (id==2) p_level = 2;
  if (id==3) p_level = 3;
  unlight();
  display();
}
  

void seema_light()
{
   
  glEnable(GL_SMOOTH);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
}

void seema_page()
{
  GLfloat light_position[] = {0.0, 0.0, 1.0, 1.0 };
  GLfloat light_position2[] = { -3.0, 1.0, 1.0, 0.0 };
  GLfloat light_position3[] = { -5.0, 1.0, 3.0, 0.0 };

  GLfloat glass_amb[] = { 0.15, 0.15, 0.15, 0.20 };
  GLfloat glass_diff[] = { 0.15, 0.15, 0.15, 0.20 };
  GLfloat glass_spec[] = {1.0, 1.0, 1.0, 0.20 };
  GLfloat glass_emission[] = { 0.15, 0.15, 0.15, 0.20 };
  GLfloat glass_shini[] = { 100.0 };

  GLfloat water_amb[] = { 0.2, 0.2, 0.2, 0.50};
  GLfloat water_diff[] = { 0.0, 0.2, 1.0, 0.50};
  GLfloat water_spec[] = { 1.0, 1.0, 1.0, 0.50};
  GLfloat water_emission[] = { 0.0, 0.3, 1.0, 0.50 };
  GLfloat water_shini[] = { 15.0 };

  GLfloat wood_amb[] = { 0.2, 0.2, 0.2, 1.0};
  GLfloat wood_diff[] = { 0.5, 0.0, 0.0, 1.0 };
  GLfloat wood_spec[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat wood_emission[] = { 0.0, 0.0, 0.0, 1.0 }; 

  GLfloat rock_diff[] = { 0.8, 0.8, 0.8, 1.0 };

  GLUquadricObj *glass, *water_cyl, *water_disk_top, *water_disk_bottom;

  if(turning)
  {
    glScalef(1.0,1.0,0.1);
  }

  glMaterialfv(GL_FRONT, GL_AMBIENT, wood_amb);
  glMaterialfv(GL_FRONT, GL_SPECULAR, wood_spec);
  glMaterialfv(GL_FRONT, GL_SHININESS, water_shini);

  glPushMatrix();
  glLightfv(GL_LIGHT0, GL_POSITION, light_position2);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, wood_diff);
  glTranslatef(140.0, -70.0, 50.0);
  if (p_wood_theta2 == 90.0) glRotatef(-40.0, 1.0, 0.0, 0.0);
  glRotatef(-p_wood_theta2, 0.0, 0.0, 1.0);
  glTranslatef(-140.0, 70.0, -50.0);
  glTranslatef(0.0, -p_wood_vert, 0.0);
  glTranslatef(230.0, 0.0, 230.0);
  glRotatef(p_wood_theta, 0.0, 0.0, 1.0);
  glTranslatef(-230.0, 0.0, -230.0);
  glTranslatef(230, 90, 50);
  glRotatef(40.0, 1.0, 0.0, 0.0);
  glScalef(6.0, 1.0, 2.5);
  glutSolidCube(10);
  glPopMatrix();

  glPushMatrix();
  glLightfv(GL_LIGHT0, GL_POSITION, light_position3);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, rock_diff);
  glTranslatef(0.0, -p_rock_vert, 0.0);
  glTranslatef(50.0, 0.0, 50.0);
  glRotatef(-p_rock_theta, 0.0, 0.0, 1.0);
  glTranslatef(-50.0, 0.0, -50.0);
  glTranslatef(50.0, 90.0, 50.0);
  glutSolidSphere(25.0, 8.0, 8.0);
  glPopMatrix();

  glPushMatrix();
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

  /* rotate gluCylinder upright and translate */
  /* coord sys +z dir = glu -y dir */
  /* coord sys +y dir = glu +z dir */ 

  glMaterialfv(GL_FRONT, GL_AMBIENT, water_amb);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, water_diff);
  glMaterialfv(GL_FRONT, GL_SPECULAR, water_spec);
  glMaterialfv(GL_FRONT, GL_EMISSION, water_emission);
  glMaterialfv(GL_FRONT, GL_SHININESS, water_shini);

  glEnable(GL_BLEND);
  glDepthMask(GL_FALSE);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glRotatef(-90.0, 1.0, 0.0, 0.0);
  glTranslatef(140.0, -50.0, -150.0);
 
  water_cyl = gluNewQuadric();
  gluQuadricDrawStyle(water_cyl, GLU_FILL);
  gluCylinder(water_cyl, 49.0, 49.0, 75.0, 20.0, 20.0); 
  water_disk_bottom = gluNewQuadric();
  gluQuadricDrawStyle(water_disk_bottom, GLU_FILL);
  gluDisk(water_disk_bottom, 0.0, 49.0, 20.0, 20.0);
  glPushMatrix();
  glTranslatef(0.0, 0.0, 75.0);
  water_disk_top = gluNewQuadric();
  gluQuadricDrawStyle(water_disk_top, GLU_FILL);
  gluDisk(water_disk_top, 0.0, 49.0, 20.0, 20.0);
  glPopMatrix();
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  glMaterialfv(GL_FRONT, GL_AMBIENT, glass_amb);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, glass_diff);
  glMaterialfv(GL_FRONT, GL_SPECULAR, glass_spec);
  glMaterialfv(GL_FRONT, GL_EMISSION, glass_emission);
  glMaterialfv(GL_FRONT, GL_SHININESS, glass_shini);

  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glass = gluNewQuadric();
  gluQuadricDrawStyle(glass, GLU_FILL);
  gluCylinder(glass, 50.0, 50.0, 125.0, 20.0, 20.0);

  glDepthMask(GL_TRUE);
  glDisable(GL_BLEND);
  glPopMatrix();
  glMaterialfv(GL_FRONT, GL_EMISSION, wood_emission);
}

void seema_text()
{
  int i;
  char text1[] = "BUOYANCY";
  char text2[] = "What makes an object float?";
  char text3[] = "Will the rock and wood float or sink?";

  char text4[] = "An object will float if it weighs less";
  char text5[] = "than the water it displaces (or pushes ";
  char text6[] = "out of the way).  It will sink if it weighs";
  char text7[] = "more than the displaced water.";  

  char text8[] = "Water pushes up on an object with a";
  char text9[] = "buoyant force Fb.";
  char text10[] = "Fb = weight of water displaced by object";
  char text11[] = "mg = weight of object";
  char text12[] = "If (Fb > mg)  ==> object will float.";
  char text13[] = "If (Fb < mg)  ==> object will sink.";
  char text14[] = "Use the menu on the MIDDLE BUTTON to choose";
  char text15[] = "which object to drop in the water.";
  char text16[] = "Use menu on the LEFT BUTTON to select a"; 
  char text17[] = "difficulty level.";

  if(!turning)
  {
    glRasterPos3f(-190, 175, 1.0);
    for (i=0; i < strlen(text1); i++)
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text1[i]);
    glRasterPos3f(-290, 150, 1.0);
    for (i=0; i < strlen(text2); i++)
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text2[i]);
    glRasterPos3f(-290, 135, 1.0);
    for (i=0; i < strlen(text3); i++)
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text3[i]);
      
    if (p_level==2 || p_level==3)  
    {
      glRasterPos3f(-290, 105, 1.0);
	  for (i=0; i < strlen(text4); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text4[i]);
      glRasterPos3f(-290, 90, 1.0);
	  for (i=0; i < strlen(text5); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text5[i]);
      glRasterPos3f(-290, 75, 1.0);
	  for (i=0; i < strlen(text6); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text6[i]);
      glRasterPos3f(-290, 60, 1.0);
	  for (i=0; i < strlen(text7); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text7[i]);
    }

    if (p_level==3)
    {
      glRasterPos3f(-290, 30, 1.0);
	  for (i=0; i < strlen(text8); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text8[i]);
      glRasterPos3f(-290, 15, 1.0);
	  for (i=0; i < strlen(text9); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text9[i]);
      glRasterPos3f(-290, 0, 1.0);
	  for (i=0; i < strlen(text10); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text10[i]);
      glRasterPos3f(-290, -15, 1.0);
	  for (i=0; i < strlen(text11); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text11[i]);
      glRasterPos3f(-290, -35, 1.0);
	  for (i=0; i < strlen(text12); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text12[i]);
      glRasterPos3f(-290, -50, 1.0);
	  for (i=0; i < strlen(text13); i++)
	    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text13[i]);
    }
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glColor3f(0.0, 0.5, 0.0);
    glRasterPos3f(-290, -100, 1.0);
	for (i=0; i < strlen(text14); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text14[i]);
    glRasterPos3f(-290, -115, 1.0);
	for (i=0; i < strlen(text15); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text15[i]);
    glRasterPos3f(-290, -135, 1.0);
	for (i=0; i < strlen(text16); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text16[i]);
    glRasterPos3f(-290, -150, 1.0);
	for (i=0; i < strlen(text17); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text17[i]);
     
    glPopAttrib();
   
  } 
}



void seema_drop_menu(int id)
{
  if (id==1) 
  {
    do
    {
      unlight();
      p_rock_theta += 10.0;
      display();
    } while (p_rock_theta!= 90.0);
  
    if (p_rock_theta==90.0)
    {
      do
      {
	unlight();
	p_rock_vert += 25.0;
	display();
      } while (p_rock_vert!= 125.0);
    }
    p_rock_theta = 0.0;
    p_rock_vert = 0.0;

  }
  
  if (id==2)
  {
    do
    {
      unlight();
      p_wood_theta += 10.0;
      display();
    } while (p_wood_theta!= 90.0);

    if (p_wood_theta==90.0)
    {
      do
      {
	unlight();
	p_wood_vert += 35.0;
	display();
      } while (p_wood_vert!=70.0);
    }

    if (p_wood_vert == 70.0)
    {
      do
      {
	unlight();
	p_wood_theta2 += 30.0;
	display();
      } while (p_wood_theta2!=90.0);
    }
     
    p_wood_theta = 0.0;
    p_wood_vert = 0.0;
    p_wood_theta2 = 0.0;

  }
}


void davepage(void)
{
   if(turning)
    {
      glScalef(1.0,1.0,0.1);
    }


  glTranslatef(140.0, 0.0, 50.0);
  glRotatef(90.0, 1.0, 0.0, 0.0);
  glScalef(.9, .9, .9);
  if(dtype==1)                          /*learn about the spring */
    davespring();
  
  else if (dtype==2)                    /*learn about dropping weights */
    {
      if (bounce<4)                     /*bounce 4 times*/
	davebounce();
      else
	if (dchoice !=0)                /*restart*/
	  {
	    vinit = 0;
	    davebounce();
	  }
	else
	  {
	    vinit = 100;
	    bounce=0;
	  }
    }
  /*  else if (dtype==3) */            /*learn about throwing things*/
  /*    davethrow();*/
}


void davetext()
{
  int i;
  char text1[] = "SPRINGS";

  char text2[] = "A spring is a special object that is able";
  char text3[] = "to store energy by being pressed or pulled.";

  char text4[] = "When released, the spring creates a force.";
  char text5[] = "The harder it is pressed or pulled the";
  char text6[] = "more force it has to give.";

  char text7[] = "The force of the spring is proportional";
  char text8[] = "to the displacement of the spring";
  char text9[] = "from it's normal unstretched position.";
  char text10[] = "This force also depends on the";
  char text11[] = "material the spring is made of.";
  char text12[] = "              F = kx";

  char text13[] = "Use the LEFT button to choose the example";
  char text30[] = "or learning level.";


  char text14[] = "The spring can be used to move objects.";
  char text15[] = "The lighter the object, the less force";
  char text16[] = "it has to push the object away.";

  char text17[] = "Force is proportional to the displacement"; 
  char text18[] = "of the spring and the mass of the object.";
  char text19[] = "              F = kx  = ma ";
  char text20[] = "Changing kinetic to potential energy gives";
  char text21[] = "            1/2 mv^2   = mgh ";
  char text22[] = "So, the final velocity of the object is";
  char text23[] = "              v = sqrt(2gh)";
  char text24[] = "The general equation of motion is:";
  char text25[] = "            x = vt + 1/2 gt^2 ";

  char text26[] = "Click the MIDDLE button on an object to";
  char text27[] = "drop it on the spring.  The globe is light,";
  char text28[] = "and the block is heavy.  After it stops,"; 
  char text29[] = "click on the screen to restart.";

  if (dtype == 1)           /* spring choice */
    {
    if(!turning)
      {
	glRasterPos3f(-190, 175, 1.0);
	for (i=0; i < strlen(text1); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text1[i]);
	
	if (dlevel == 1)            /*beginner */
	  {
	    glRasterPos3f(-290, 150, 1.0);
	    for (i=0; i < strlen(text2); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text2[i]);
	    glRasterPos3f(-290, 135, 1.0);
	    for (i=0; i < strlen(text3); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text3[i]);
	    glRasterPos3f(-290, 105, 1.0);
	    for (i=0; i < strlen(text4); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text4[i]);
	    glRasterPos3f(-290, 90, 1.0);
	    for (i=0; i < strlen(text5); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text5[i]);
	    glRasterPos3f(-290, 75, 1.0);
	    for (i=0; i < strlen(text6); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text6[i]);
	  }
	
	if (dlevel==2)
	  {
	    glRasterPos3f(-290, 30, 1.0);
	    for (i=0; i < strlen(text7); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text7[i]);
	    glRasterPos3f(-290, 15, 1.0);
	    for (i=0; i < strlen(text8); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text8[i]);
	    glRasterPos3f(-290, 0, 1.0);
	    for (i=0; i < strlen(text9); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text9[i]);
	    glRasterPos3f(-290, -25, 1.0);
	    for (i=0; i < strlen(text10); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text10[i]);
	    glRasterPos3f(-290, -40, 1.0);
	    for (i=0; i < strlen(text11); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text11[i]);
	    glRasterPos3f(-290, -55, 1.0);
	    for (i=0; i < strlen(text12); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text12[i]);
	  }
	glRasterPos3f(-290, -160, 1.0);
	for (i=0; i < strlen(text13); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text13[i]);
	glRasterPos3f(-290, -175, 1.0);
	for (i=0; i < strlen(text30); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text30[i]);
      }
    }

  else if (dtype == 2)                   /* bouncing demo */
    {
    if(!turning)
      {
	glRasterPos3f(-190, 175, 1.0);
	for (i=0; i < strlen(text1); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text1[i]);
	
	if (dlevel == 1)
	  {
	    glRasterPos3f(-290, 135, 1.0);
	    for (i=0; i < strlen(text14); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text14[i]);
	    glRasterPos3f(-290, 120, 1.0);
	    for (i=0; i < strlen(text15); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text15[i]);
	    glRasterPos3f(-290, 105, 1.0);
	    for (i=0; i < strlen(text16); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text16[i]);
	  }
	else if (dlevel==2)  
	  {
	    glRasterPos3f(-290, 90, 1.0);
	    for (i=0; i < strlen(text17); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text17[i]);
	    glRasterPos3f(-290, 75, 1.0);
	    for (i=0; i < strlen(text18); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text18[i]);
	    glRasterPos3f(-290, 60, 1.0);
	    for (i=0; i < strlen(text19); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text19[i]);
	    glRasterPos3f(-290, 35, 1.0);
	    for (i=0; i < strlen(text20); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text20[i]);
	    glRasterPos3f(-290, 20, 1.0);
	    for (i=0; i < strlen(text21); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text21[i]);
	    glRasterPos3f(-290, 0, 1.0);
	    for (i=0; i < strlen(text22); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text22[i]);
	    glRasterPos3f(-290, -15, 1.0);
	    for (i=0; i < strlen(text23); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text23[i]);
	    glRasterPos3f(-290, -40, 1.0);
	    for (i=0; i < strlen(text24); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text24[i]);
	    glRasterPos3f(-290, -55, 1.0);
	    for (i=0; i < strlen(text25); i++)
	      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text25[i]);
	  }

	glRasterPos3f(-290, -125, 1.0);
	for (i=0; i < strlen(text26); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text26[i]);
	glRasterPos3f(-290, -140, 1.0);
	for (i=0; i < strlen(text27); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text27[i]);
	glRasterPos3f(-290, -155, 1.0);
	for (i=0; i < strlen(text28); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text28[i]);
	glRasterPos3f(-290, -170, 1.0);
	for (i=0; i < strlen(text29); i++)
	  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text29[i]);
	
      }
    }
}


void davemenu1(int choice)
{
  switch (choice)
    {
    case 1:                           /* spring choice */
      dtype=1;
      dlevel=0;
      unlight();
      display();
      break;
    case 2:                           /* bouncing choice */
      dtype=2;
      dlevel=0;
      springpos=0.0;
      unlight();
      display();
      break;
      /*    case 3:
      dtype=3;
      davethrow();
      break;
    case -1:
      exit(1);
      break; */
    default:
      dtype=0;
      break;
    }
}

void davemenu2(int choice)
{
  dlevel = choice;
}

void davelight()
{
  GLfloat light_position[] = { 5.0, 10.0, 1.0, 0.0 };

  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
 
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
}

               /* generic function to draw a spring */
void spring(int springlength, int springrad, int depth)
{
  int i;
  float finangle = sprangle*2*3.14159/360.0;

                                         /*material properties of spring*/
  GLfloat mat_specular[] = { 1.0, 1.0, 0.7, 1.0 };
  GLfloat mat_shininess[] = { 80.0 };
  GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1.0 };
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

  glTranslatef(0, 0, depth);                 /*move down to right level*/
  

  for (i=0; i<springlength; i++)                /*place coils correctly*/
    {
      glTranslatef(0, 0, -2*springrad*fsin(finangle));     /*left angle*/

      glPushMatrix();
      glRotatef(sprangle, 0, 1, 0);
      glutSolidTorus(springthick, springrad, 3, 15);
      glPopMatrix();

      glTranslatef(0, 0, -2*springrad*fsin(finangle));     /*right angle*/

      glPushMatrix();
      glRotatef(-sprangle, 0, 1, 0);
      glutSolidTorus(springthick, springrad, 3, 15);
      glPopMatrix();

    }
  if (grow !=2)
    startpt = 2*springrad*fsin(finangle)*springlength;
}


 
void davespring(void)                  /*contract/extend spring*/
{
  int springlength = 5;

  if( grow == 0)
    {
      if (sprangle < 10)
	{
	  sprangle +=2.0;
	}
      else
	grow = 1;
    }
  if (grow == 1)
    {
      if (sprangle > springthick*springlength-2)
	{
	  sprangle -=2.0;
	}
      else 
	grow = 0;
    }
  else
    grow=0;

  spring(springlength, 20, 50);

}

/* show that different weights bounce differently */
void davebounce()
{
                                           /* wall and floor */
  GLfloat dwall_specular[] = { 1.0, 1.0, 0.7, 1.0 };
  GLfloat dwall_shininess[] = { 20.0 };
  GLfloat dwall_diffuse[] = {0.0, 0.0, 0.7, 1.0 };

                                           /* red block */
  GLfloat heavy_specular[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat heavy_shininess[] = { 20.0 };
  GLfloat heavy_diffuse[] = {1.0, 0.0, 0.0, 1.0 };

                                           /*golden sphere- emit light */
  GLfloat light_specular[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat light_shininess[] = { 100.0 };
  GLfloat light_diffuse[] = {0.9, 0.9, 0.0, 1.0 };
                                           /* turns off light emission */
  GLfloat no_emission[] = {0.0, 0.0, 0.0, 1.0 };

  int springlength = 4;
  
  glPushMatrix();

  if ((bounce < 4) && (dchoice !=0))
    {
    if (grow == 0)                             /* in the shrinking phase */
      {
	if (sprangle > springthick*springlength)
	  {
	    sprangle -=2.0;
	  }
	else 
	  grow = 1;
      }
    if(grow == 1)                              /* in the growing phase */
      {
	if (sprangle < 10)
	  {
	    sprangle +=2.0;
	  }
	else
	  {
	    grow = 2;
	    if (dchoice==1)
	      vinit = vinit/1.3;
	    else if (dchoice==2)
	      vinit = vinit/1.7;

	    springt=0.0;                       /*sets for next parabola*/
	  }
      }

    if(grow == 2 )                             /*parabolic motion */
      {
	if(springpos >= 0.0)
	  {	                               /* x = vo*t - 1/2*g*t^2 */
	    springpos = (vinit*springt) - (.5*9.81*(springt*springt));
	    springt+=.25;
	    if (springpos < 0.0)        /* if it goes below the ground */
	      springpos = -1.0;             /* stop it */
	  }
	else
	  {
	  grow=0;
	  springpos=0.0;
	  bounce++;
	  }
      }
    }
    spring(springlength, 15, 100);

  glPopMatrix();

  glPushMatrix();

    glMaterialfv(GL_FRONT, GL_DIFFUSE, dwall_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, dwall_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, dwall_shininess);
 
    glTranslatef(-100, 0, 50);
    glScalef(1.0, 1.0, 2.0);
    glutSolidCube(100.0);

    glTranslatef(100, 0, 50);
    glScalef(3.0, 1.0, 0.5);
    glutSolidCube(100.0);


  glPopMatrix();
  
  glPushMatrix();                        /* red cube */

    glMaterialfv(GL_FRONT, GL_DIFFUSE, heavy_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, heavy_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, heavy_shininess);
 
    glTranslatef(-100.0, 0.0, -60.0);
    if (dchoice==2)
      {
	glTranslatef(95.0, 0.0, 100.0);

	if (grow==2)
	  glTranslatef(0, 0, -springpos);
	else
	    glTranslatef(0, 0, 46-(2*startpt));
      }

    glutSolidCube(20.0);
  glPopMatrix();

  glPushMatrix();                        /* golden sphere */

    glMaterialfv(GL_FRONT, GL_DIFFUSE, light_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, light_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, light_shininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, light_diffuse);

    glTranslatef(-130.0, 0.0, -60.0);
    if (dchoice==1)
      {
	glTranslatef(125.0, 0.0, 100.0);
	if (grow==2)
	  glTranslatef(0,0,-springpos);
	else
	  glTranslatef(0,0, 49-(2*startpt));
      }
    glutSolidSphere(10.0, 15, 15);
  glPopMatrix();

  glMaterialfv(GL_FRONT, GL_EMISSION, no_emission);

}




void unlight(void)
{
  glDisable(GL_LIGHTING);
  glDisable(GL_LIGHT0);
}

void untexture(void)
{
  glDisable(GL_TEXTURE_GEN_S);
  glDisable(GL_TEXTURE_1D);
}


void mover()                              /*idle subroutine calls display*/
{                                         /*only for dave's page */
  if (((page_num == 3)||(page_num==0)) && (!turning))
    {
      unlight();
      display();
    }
}

void quit(int id)
{
  exit(1);
}


void main(int argc, char **argv)
{
  int submenu1;

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
  glutInitWindowSize(800, 800);
  glutInitWindowPosition(0,0);
  glutCreateWindow(" prog4 ");   
  init();
  glutDisplayFunc(display);
  glutSpecialFunc(keys);
  glutMouseFunc(mouse);
  glutIdleFunc(mover);

  glutCreateMenu(sandimenu);
  glutAddMenuEntry("Beginner", 1);
  glutAddMenuEntry("Intermediate", 2);
  glutAddMenuEntry("Advanced",3);
  glutAttachMenu(GLUT_LEFT_BUTTON);

  glutCreateMenu(seema_menu);
  glutAddMenuEntry("Beginner", 1);
  glutAddMenuEntry("Intermediate", 2);
  glutAddMenuEntry("Advanced",3);
  glutAttachMenu(GLUT_LEFT_BUTTON);

  glutCreateMenu(seema_drop_menu);
  glutAddMenuEntry("Rock", 1);
  glutAddMenuEntry("Wood", 2);
  glutAttachMenu(GLUT_MIDDLE_BUTTON);

  /*  submenu1 = glutCreateMenu(davemenu2);
  glutAddMenuEntry("Beginner", 1);
  glutAddMenuEntry("Advanced", 0);*/
  
  glutCreateMenu(davemenu1);          /* first submenu */
  glutAddMenuEntry("Spring", 1);
  glutAddMenuEntry("Bounce", 2);
  /*  glutAddSubMenu("Change Level", submenu1);*/
  glutAddMenuEntry("Change Level", 0);
  glutChangeToSubMenu(3, "Change Level", 5);

  glutCreateMenu(davemenu2);        
  glutAddMenuEntry("Beginner", 1);
  glutAddMenuEntry("Advanced", 2);

  glutAttachMenu(GLUT_LEFT_BUTTON);

  glutCreateMenu(quit);
  glutAddMenuEntry("Quit",1);
  glutAttachMenu(GLUT_RIGHT_BUTTON);

  glEnable(GL_DEPTH_TEST);
  glutMainLoop();
}









