I’m new to OpenGL and have been trying to get this working for a while. Any help would be appreciated.
I am trying to color each vertex of a square using smooth shading according to key presses. For example when I push 1 on the keyboard that vertex alone changes color to red. In my code below, the coloring is flat and as a result the whole square changes color to red.
My understanding is that glut uses a loop that keeps refreshing the image, in this case my polygon and therfore included glBegin90 and glEnd() like in the last if statement in the code below to no avail. I have also tried different approaches such as removing the glShadeModel(GL_SMOOTH) in the if statements with no success.
If I just set the color of each vertex in my display() function it displays the colors as it should using smooth shading that is default in opengl.
I’m seeking an explanation of how to setup the keypress function to change the color of each vertex individually according to various key presses. Thanks in advance
void keyPress(unsigned char key, int x, int y) {
if(key == '1') {
// set to smooth color shading
glShadeModel(GL_SMOOTH);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
}
else if(key == '2') {
// set to smooth color shading
glShadeModel(GL_SMOOTH);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.5, 0.5);
}
else if(key == '3') {
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.5, 0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.5, -0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.5, 0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.5, -0.5);
}
else if(key == '4') {
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.5, -0.5);
glColor3f(1.0, 0.0, 1.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 1.0);
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.5, 0.5);
glEnd();
}
else if(key == 'q' || key == 'Q') {
exit(0);
}
glutPostRedisplay();
}