OpenGL绘图异常:在display函数之外进行绘图操作

问题描述:

在一段OpenGL代码中,出现了如下的错误提示:

if (showCard)
{
    drawSchoolBuilding();
    drawFlower();
    drawBalloon();
    drawCloud();
}

此部分运行时发生异常内容:

在OpenGL中,不能在display函数之外进行绘图操作。正确的做法是在display函数内根据标志变量进行条件判断,决定是否绘制特定的图形。

解决方案:

以下是修正后的代码:

#define FREEGLUT_STATIC
#include <math.h>
#include <GL/freeglut.h>

int width = 800;
int height = 600;
float cloudX = 0.0;
float balloonY = 0.0;
bool showCard = false; // Flag to indicate whether to show the entire card

void when_in_mainloop()
{
    glutPostRedisplay();
}

void keyboard_input(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q')
        exit(0);
    else if (key == 'c' || key == 'C')
        showCard = !showCard;
}

void drawSchoolBuilding()
{
    // Draw school building
    glColor3f(0.7, 0.7, 0.7); // Light gray color
    glBegin(GL_POLYGON);
    glVertex2f(0.1, 0.1);
    glVertex2f(0.1, 0.6);
    glVertex2f(0.4, 0.6);
    glVertex2f(0.4, 0.1);
    glEnd();

    glColor3f(0.9, 0.9, 0.9); // White color
    glBegin(GL_POLYGON);
    glVertex2f(0.15, 0.6);
    glVertex2f(0.4, 0.6);
    glVertex2f(0.275, 0.8);
    glEnd();
}

void drawFlower()
{
    // Draw flower
    glColor3f(1.0, 0.0, 0.0); // Red color
    glBegin(GL_POLYGON);
    glVertex2f(0.6, 0.2);
    glVertex2f(0.6, 0.3);
    glVertex2f(0.55, 0.35);
    glVertex2f(0.5, 0.3);
    glVertex2f(0.5, 0.2);
    glEnd();

    glColor3f(0.0, 1.0, 0.0); // Green color
    glBegin(GL_POLYGON);
    glVertex2f(0.55, 0.35);
    glVertex2f(0.55, 0.45);
    glVertex2f(0.525, 0.475);
    glVertex2f(0.5, 0.45);
    glVertex2f(0.5, 0.35);
    glEnd();

    glColor3f(1.0, 1.0, 0.0); // Yellow color
    glBegin(GL_POLYGON);
    glVertex2f(0.525, 0.475);
    glVertex2f(0.525, 0.5);
    glVertex2f(0.5125, 0.5125);
    glVertex2f(0.5, 0.5);
    glVertex2f(0.5, 0.475);
    glEnd();
}

void drawBalloon()
{
    // Draw balloon
    glColor3f(0.0, 0.0, 1.0); // Blue color
    glBegin(GL_POLYGON);
    glVertex2f(0.7, 0.2 + balloonY);
    glVertex2f(0.675, 0.225 + balloonY);
    glVertex2f(0.725, 0.225 + balloonY);
    glEnd();

    glColor3f(1.0, 0.0, 0.0); // Red color
    glBegin(GL_POLYGON);
    glVertex2f(0.7, 0.2 + balloonY);
    glVertex2f(0.675, 0.15 + balloonY);
    glVertex2f(0.725, 0.15 + balloonY);
    glEnd();
}

void drawCloud()
{
    // Draw cloud
    glColor3f(1.0, 1.0, 1.0); // White color
    glBegin(GL_POLYGON);
    glVertex2f(0.1 + cloudX, 0.75);
    glVertex2f(0.125 + cloudX, 0.775);
    glVertex2f(0.175 + cloudX, 0.775);
    glVertex2f(0.2 + cloudX, 0.75);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.125 + cloudX, 0.775);
    glVertex2f(0.125 + cloudX, 0.825);
    glVertex2f(0.175 + cloudX, 0.825);
    glVertex2f(0.175 + cloudX, 0.775);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.175 + cloudX, 0.775);
    glVertex2f(0.2 + cloudX, 0.75);
    glVertex2f(0.25 + cloudX, 0.75);
    glVertex2f(0.275 + cloudX, 0.775);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.175 + cloudX, 0.825);
    glVertex2f(0.175 + cloudX, 0.875);
    glVertex2f(0.2 + cloudX, 0.9);
    glVertex2f(0.225 + cloudX, 0.875);
    glVertex2f(0.25 + cloudX, 0.875);
    glVertex2f(0.275 + cloudX, 0.9);
    glVertex2f(0.3 + cloudX, 0.875);
    glVertex2f(0.3 + cloudX, 0.825);
    glEnd();
}

void display(void)
{
    glClearColor(0.529, 0.808, 0.922, 0); // Light blue color
    glClear(GL_COLOR_BUFFER_BIT);

    if (showCard)
    {
        drawSchoolBuilding();
        drawFlower();
        drawBalloon();
        drawCloud();
    }

    glFlush();
}

void timer(int value)
{
    cloudX += 0.001;
    balloonY += 0.001;

    glutPostRedisplay();
    glutTimerFunc(10, timer, 0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow('Hope Elementary Invitation Card');
    glutDisplayFunc(display);
    glutIdleFunc(when_in_mainloop);
    glutKeyboardFunc(keyboard_input);
    glutTimerFunc(10, timer, 0);
    glutMainLoop();

    return 0;
}

代码说明:

  • showCard标志变量用于控制是否绘制整个卡片。
  • display函数内,根据showCard的值判断是否绘制各个图形。
  • 按下键盘上的'C'键可以切换显示整个卡片。
  • 代码中使用了glutPostRedisplay()函数来重新绘制画面。
  • timer函数用于控制云朵和气球的移动。

运行结果:

现在,按下键盘上的'C'键可以切换显示整个卡片,当标志变量showCard为true时,将绘制出整个邀请卡。否则,只显示背景颜色。完成后,你可以通过键盘交互来控制卡片的展示。

注意:

  • 由于OpenGL是基于状态机的,因此在绘制不同的图形时,需要根据需要设置不同的颜色、线宽、填充模式等参数。
  • 在绘制复杂图形时,可以考虑使用纹理贴图来提高效率。
  • 在使用OpenGL进行绘图时,需要了解OpenGL的渲染流程,以及各种函数的用法。
OpenGL绘图异常:在display函数之外进行绘图操作

原文地址: https://www.cveoy.top/t/topic/Fuu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录