如何让多个形状出现在同一个屏幕上?

How to make multiple shapes appear on the same screen?

我正在编写一个程序,允许用户通过菜单选项绘制不同的形状,绘制后的形状需要在同一屏幕上,但问题是在菜单中选择另一个选项后绘制另一个形状,之前的形状消失。我怎样才能解决这个问题?这是我的程序:

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    if (shape == 1)
    {
        draw_rectangle();
    }

    if (shape == 2)
    {
        draw_circle();
    }

    glFlush();
}

void menu(int choice)
{
    switch (choice)
    {
    case 1:
        shape = 1;
        break;
    case 2:
        shape = 2;
        break;
    }
    glutPostRedisplay();
}

glClear(GL_COLOR_BUFFER_BIT)仅在display()

你必须为每个形状使用一个单独的布尔状态变量(rectangle_shapecircle_shape),而不是一个指示形状的整数变量(shape):

bool rectangle_shape = false;
bool circle_shape = false;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    if (rectangle_shape)
    {
        draw_rectangle();
    }

    if (circle_shape)
    {
        draw_circle();
    }

    glFlush();
}

void menu(int choice)
{
    switch (choice)
    {
    case 1:
        rectangle_shape = !rectangle_shape;
        break;
    case 2:
        circle_shape = !circle_shape;
        break;
    }
    glutPostRedisplay();
}