程序中,对形状的尺寸进行放大是成功的,但是改变颜色效果不佳

In the program, it was successful to expand the size of the shape, but changing the color is not working well

我正在编写一个程序来改变形状的大小和颜色。我已经到了可以更改当前形状大小但颜色不变的阶段。我想将颜色更改为 R、G、B 形式。谢谢你。请原谅我的英语不符合语法的地方。英语不是我的第一语言

#include <glut.h>
#include <stdio.h>
#include <stdlib.h>

constexpr auto RED = 1;
constexpr auto GREEN = 2;
constexpr auto BLUE = 3;
float red = 1.0, green = 1.0, blue = 1.0;

GLboolean IsSphere = true;
GLboolean IsSmall = true;

void MyDisplay() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.5, 0.0, 0.5);
    if ((IsSphere) && (IsSmall))
        glutWireSphere(0.2, 15, 15);
    else if ((IsSphere) && (!IsSmall))
        glutWireSphere(0.4, 15, 15);
    else if ((!IsSphere) && (IsSmall))
        glutWireTorus(0.1, 0.3, 40, 20);
    else glutWireTorus(0.2, 0.5, 40, 20);
    glFlush();
}

void MyMainMenu(int entryID) {
    if (entryID == 1)
        IsSphere = true;
    else if (entryID == 2)
        IsSphere = false;
    else if (entryID == 3)
        exit(0);
    glutPostRedisplay();
}

void MySubMenu(int entryID) {
    if (entryID == 1)
        IsSmall = true;
    else if (entryID == 2)
        IsSmall = false;
    glutPostRedisplay();
}

void MySubMenu2(int entryID)
{
    switch (entryID) {
    case RED: red = 1.0; green = 0.0; blue = 0.0; break;
    case GREEN: red = 0.0; green = 1.0; blue = 0.0; break;
    case BLUE: red = 0.0; green = 0.0; blue = 1.0; break;
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("OpenGL Example Drawing");
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    GLint MySubMenuID = glutCreateMenu(MySubMenu);
    glutAddMenuEntry("Small One", 1);
    glutAddMenuEntry("Big One", 2);
    GLint MySubMenu2ID = glutCreateMenu(MySubMenu2);
    glutAddMenuEntry("RED", RED);
    glutAddMenuEntry("GREEN", GREEN);
    glutAddMenuEntry("BLUE", BLUE);
    GLint MyMainMenuID = glutCreateMenu(MyMainMenu);
    glutAddMenuEntry("Draw Sphere", 1);
    glutAddMenuEntry("Draw Torus", 2);
    glutAddSubMenu("Change Size", MySubMenuID);
    glutAddSubMenu("Change Color", MySubMenu2ID);
    glutAddMenuEntry("Exit", 3);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutDisplayFunc(MyDisplay);
    glutMainLoop();
    return 0;
}

MyDisplay中使用定义颜色的变量(redgreenblue):

float red = 0.5f, green = 0.0f, blue = 0.5f; // <--- init 0.5, 0, 0.5

void MyDisplay() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(red, green, blue); // <----- red, green, blue

    // [...]
}

在菜单事件回调中添加glutPostRedisplay,颜色改变时执行:

void MySubMenu2(int entryID)
{
    switch (entryID) {
        case RED: red = 1.0; green = 0.0; blue = 0.0; break;
        case GREEN: red = 0.0; green = 1.0; blue = 0.0; break;
        case BLUE: red = 0.0; green = 0.0; blue = 1.0; break;
    }
    glutPostRedisplay(); // <---- this is missing
}