[HELP]change color using keyboard function

I want to change a shape color using keyboard key like F1 F2 F3, so when i press F1, the shape wil change to red as i set to this color. Im using switch statement, but I still cant able to change the color. It is an openGL programming so i don't i have posted correctly here, move me to proper section if im wrong. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <glut.h>

GLsizei winWidth = 400, winHeight = 400;
float red = 1.0f,blue = 1.0f,green = 1.0f;

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

	glMatrixMode (GL_PROJECTION);
	gluOrtho2D (0.0, winWidth, 0.0, winHeight);
}

void displayFcn (void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
	glColor3f (1.0, 0.0, 0.0);
	glBegin (GL_POLYGON);
		glVertex2i (50,100);
		glVertex2i (50,50);
		glVertex2i (200,50);
		glVertex2i (200,100);
	glEnd ();

	glFlush ( ); 
}


void processNormalKeys(unsigned char key, int x, int y) {

	if (key == 27)
		exit(0);
}

void processSpecialKeys(int key,int x,int y)
{
          switch (key)
		  {
           case GLUT_KEY_F1:
			     red = 1.0;
				 green = 0.0;
				 blue = 0.0;
				 break;
		   case GLUT_KEY_F2:
			     red = 0.0;
				 green = 1.0;
				 blue = 0.0;
				 break;
		   case GLUT_KEY_F3:
			     red = 0.0;
				 green = 0.0;
				 blue = 1.0;
				 break;
		   default:
			  break;

	}
}

void main (int argc, char** argv)
{
	glutInit (&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition (350, 100);
	glutInitWindowSize (winWidth, winHeight);
	glutCreateWindow ("Rectangle Program");

	glutKeyboardFunc (processNormalKeys);
	glutSpecialFunc (processSpecialKeys);

	init();
	glutDisplayFunc (displayFcn);

	glutMainLoop ( );

}


The format of using GLUT keyboard function shouldn't be a problem. I'm still lack of experience in openGL programming, hope to get help from u all ;)
Your color is currently always red with the following command on line 11.
glColor3f (1.0, 0.0, 0.0);

In order to use the colors you have set you need to use:
glColor3f (red, green, blue);

Does that do what you need?

Also, if you need assistance with opengl you will probably get better results if you use the opengl forum, there are lots of helpful people there: http://www.opengl.org/discussion_boards/
still cant, the color still unable to change. I have tried to use ASCII key to assign it to F1 F2 F3 key but failed. I have posted on that forum le,waiting for reply. Still, any one have other solution?
Here is the code for this technique you can use it in your program
http://codeincodeblock.blogspot.com/2011/11/how-to-change-color-of-object-in.html
yea thanks, i get the idea of it, seems lik missing few function. But i want to know the purpose of void releaseKey() function, does it necessary needed in the program?
Strictly necessary in the program.In function presskey() when we press key it set the value to 1 or -1 by checking this value we change the color so if we don't reset the value after keyrelease then the color of object will change continuously to stop this releasekey() function is needed.
Topic archived. No new replies allowed.