简体   繁体   English

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

[英]How to make multiple shapes appear on the same screen?

I'm writing a program that allows the user to draw different shapes through the menu options, the shapes after being drawn need to be on the same screen, but the problem is that after choosing another option in the menu to draw another shape, the previous shape disappears.我正在编写一个程序,允许用户通过菜单选项绘制不同的形状,绘制后的形状需要在同一个屏幕上,但问题是在菜单中选择另一个选项绘制另一个形状后,之前的形状消失了。 How can I fix this?我怎样才能解决这个问题? This is my program:这是我的程序:

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();
}

The glClear(GL_COLOR_BUFFER_BIT) is only in display() . glClear(GL_COLOR_BUFFER_BIT)仅在display()中。

You have to use a separate Boolean state variables for each shape ( rectangle_shape , circle_shape ), instead of one integral variable that indicates the shape ( shape ):您必须为每个形状( rectangle_shapecircle_shape )使用单独的 Boolean state 变量,而不是一个指示形状的整数变量( 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM