【问题标题】:How to make multiple shapes appear on the same screen?如何让多个形状出现在同一个屏幕上?
【发布时间】:2021-07-24 13:49:01
【问题描述】:

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

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

【问题讨论】:

  • 根据你的形状看起来如何(当然如果它们不移动),你可能会在重绘时不清除缓冲区(意思是:只是在你以前的图像上绘制) .然而,这是一个丑陋的解决方案,并且有几个问题,比如无法对窗口大小的变化做出反应。最好创建一个要绘制的形状列表,并在每次重绘窗口时绘制该列表中的所有形状。

标签: c++ opengl glut


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2023-03-22
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多