difficulty with mouse function


I am trying to create a function that will change the background color of my window with a right mouse click. the function is supposed to cycle through three different colors with each click and on every fourth click revert to the original background color. so far it only changes to the first color and does nothing on any successive clicks. I am very new to OpenGL so I apologize for my ignorance. here is my function:

static void onclick (int button, int state, int x, int y)
{
int n = 0;
float m = 0.2;

if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
	switch (n) {
	        case 0: {
		 glClearColor(0.0f, 0.0f, m, 0.1f);
		 redraw ();
		 n = 1;
		 break;
		}
		case 1: {
		 glClearColor(0.0f, m, 0.0f, 0.1f);
		 redraw ();
		 n = 2;
		 break;
		}
		case 2: {
		 glClearColor(m, 0.0f, 0.0f, 0.1f);
		 redraw ();
		 n = 3;
		 break;
		}
		case 3: {
		 glClearColor(0.0f, 0.0f, 0.0f, 0.1f);
		 redraw ();	
		 n = 0;				
		 break;
		}
	}
}

}

I think you have to make n a static variable or declare it outside of your mouse function. Each time your mouse function is entered it sets n=0;

static int n=0;

On each click, n remains constant. Try this code:


static int n = 0; //following glSpider post
static void onclick (int button, int state, int x, int y){

  float m = 0.2;
  
  if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
    n++;
    switch(n){
    case 0: 
      glClearColor(0.0f, 0.0f, m, 0.1f);
      redraw ();
      break;
    
    case 1: 
      glClearColor(0.0f, m, 0.0f, 0.1f);
      redraw ();
      break;
      
    case 2: 
      glClearColor(m, 0.0f, 0.0f, 0.1f);
      redraw ();
      break;
    
    case 3:       
      glClearColor(0.0f, 0.0f, 0.0f, 0.1f);
      n = 0;
      redraw ();
      break;
    }
  }
}