c++ - 为什么多个函数调用在 Opengl 中不起作用?

标签 c++ opengl glut

我尝试在我的 Opengl 项目中使用不同的函数来制作各种对象并通过一个函数调用,例如 display()。但它不起作用,当我调用两个函数然后显示一个函数时,另一个函数没有显示,我不知道为什么,

我试过这种方式:

#include <windows.h>  // for MS Windows
#include <GL/glut.h>  // GLUT, include glu.h and gl.h
#include <math.h>


void box() {

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer (background)
    // Draw a Red 1x1 Square centered at origin
    glBegin(GL_POLYGON);              // Each set of 4 vertices form a quad
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.9f, 0.7f);
    glVertex2f(0.9f, 0.7f);
    glVertex2f(0.9f, -0.7f);    // x, y

    glEnd();


    glFlush();  // Render now


}

void triangle()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);              // Each set of 4 vertices form a quad
    glColor3f(0.0f, 1.0f, 0.0f); // green

    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.4f, 0.7f);
    glVertex2f(0.1f, -0.7f);    // x, y

    glEnd();


    glFlush();  // Render now
}
void display()
{
    box();
    triangle();
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(1000, 600);// Set the window's initial width & height
    glutCreateWindow("OpenGL Setup Test");
    //gluOrtho2D(-0.1,0.7,-0.1,0.3); // Create a window with the given title
    //glutInitWindowSize(500, 500);// Set the window's initial width & height
    glutDisplayFunc(display);// Register display callback handler for window re-paint
    glutMainLoop();           // Enter the event-processing loop
    return 0;
}

请提出任何建议。

最佳答案

您在渲染每个形状之前清除缓冲区,从而删除您刚刚绘制的先前形状。相反,您应该在每次 display 时只清除一次。 glFlush 也是如此:

void box() {
    // Draw a Red 1x1 Square centered at origin
    glBegin(GL_POLYGON);
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.9f, 0.7f);
    glVertex2f(0.9f, 0.7f);
    glVertex2f(0.9f, -0.7f);    // x, y
    glEnd();
}

void triangle()
{
    glBegin(GL_POLYGON);
    glColor3f(0.0f, 1.0f, 0.0f); // green
    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.4f, 0.7f);
    glVertex2f(0.1f, -0.7f);    // x, y
    glEnd();
}

void display()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer (background)
    box();
    triangle();
    glFlush();  // Render now
}

关于c++ - 为什么多个函数调用在 Opengl 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66533962/

相关文章:

c++ - C++错误链接器命令失败,退出代码为1(使用-v查看调用)

opengl - 带有软件光栅器的 Mesa 3D OpenGL 3.2

c++ - OpenGL 和 Glut 中的空白屏幕

c++ - Windows环境下的Chromium调试

c++ - 仅使用虚拟复制构造函数 move 适应 std::any 的类型是否安全?

opengl - 无法释放共享上下文创建的纹理

c++ - 在opengl中绘制几何图形

c++ - GLUT:错误的鼠标坐标?

c++ - 如何使用 glut 和 openGL 改变颜色?

c++ - 遍历多维 vector (使用 Auto 关键字?)