Search code examples
copenglglutopengl-compat

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


I am writing a program to change the size and color of a shape. I have reached the stage where I can change the size of the current shape, but the color does not change. I want to change the color to R, G, B form. Thank you. Please forgive wherever my English is not grammatical. English is not my first language

#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;
}

Solution

  • Use the variables which define the color (red, green, blue) in MyDisplay:

    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
    
        // [...]
    }
    

    Add glutPostRedisplay to the menu event call back, which is executed when the color is changed:

    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
    }
    

    enter image description here