title: Change background color after collision detection?
body: <pre><code>#include &lt;stdio.h&gt; // this library is for standard input and output
  #include "glut.h"// this library is for glut the OpenGL Utility Toolkit
  #include &lt;math.h&gt;
  
  float squareX = 162.0f;
  float squareY = 0.0f;
  float squareZ = 0.0f;
  
  // background color
  void drawBackground() {
      float width = 400;
      float height = 100;
      // color up
      glBegin(GL_POLYGON);
      glColor3f(0.0, 0.0, 1.0);
      glVertex2f(width, height);
      glVertex2f(width, width);
      glVertex2f(0, width);
      glVertex2f(0, height);
      glVertex2f(width, height);
      glEnd();
      // color down
      glBegin(GL_POLYGON);
      glColor3f(0.0, 1.0, 0.0);
      glVertex2f(width, height);
      glVertex2f(width, 0);
      glVertex2f(0, 0);
      glVertex2f(0, height);
      glVertex2f(width, height);
      glEnd();
  }
  
  void drawShape(void) {
      float width = 58.0f;
      float height = 40.0f;
      glTranslatef(squareX, squareY, squareZ);
      glBegin(GL_POLYGON);
      glColor3f(1.0, 0.0, 0.0);
      glVertex2f(0, 0);
      glVertex2f(width, 0);
      glVertex2f(width, height);
      glVertex2f(0, height);
      glVertex2f(0, 0);
      glEnd();
  }
  
  // called when the window is resized
  void handleResize(int w, int h) {
      glViewport(0, 0, w, h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
  }
  
  void drawScene() {
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      drawBackground();
      glPushMatrix();
      drawShape();
      glPushMatrix();
      glFlush();
      glutSwapBuffers();
      glutPostRedisplay();
  }
  
  int state = 1;
  
  // make the square go up
  void update(int value) {
      // 1 : move up
      if (state == 1) {
          squareY += 1.0f;
          if (squareY &gt; 400.0) {
              state = 2;
              squareX = 0.0f;
              squareY = 180.0f;
          }
      }
      // 2 : move right
      else if (state == 2) {
          squareX += 1.0f;
          if (squareX &gt; 400.0) {
              state = 3;
              squareX = 180.0f;
              squareY = 400.0f;
          }
      }
      // 3 : move down
      else if (state == 3) {
          squareY -= 1.0f;
          if (squareY &lt; 0.0) {
              state = 0;
          }
      }
      glutPostRedisplay();
      glutTimerFunc(25, update, 0);
  }
  
  int main(int argc, char** argv) {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(400, 400);
      glutCreateWindow("Moving Square");
      glutDisplayFunc(drawScene);
      glutReshapeFunc(handleResize);
      glutTimerFunc(25, update, 0);
      glutMainLoop();
      return(0);
  }
  </code></pre>
  
  <p>The code above moves a square from bottom to top, left to right, and top to bottom. The background color is blue, but how do I change the background color after each collision detection? The color order I am trying to achieve after each collision detection is blue, red, and green. I've added a comment to where the background is.</p>