提问者:小点点

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


我在写一个程序,允许用户通过菜单选项画出不同的形状,画出后的形状需要在同一屏幕上,但问题是在菜单中选择另一个选项画出另一个形状后,前一个形状就消失了。我该怎么解决这个?这是我的程序:

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()中。


共1个答案

匿名用户

您必须为每个形状使用单独的布尔状态变量(regangle_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();
}