在 GLUT 中我的显示函数在哪里被调用?

Where is my display function being called in GLUT?

我不明白这个主要功能是如何工作的。我有一个显示函数,它使用 glDrawArrays,但我没有看到它被调用。我只看到它被用作 glutDisplayFunction 的参数。

这是我的主要内容:

int main(int argc, char** argv){

    // Set up the window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Hello Triangle");
    // Tell glut where the display function is
    glutDisplayFunc(display);

     // A call to glewInit() must be done after glut is initialized!
    GLenum res = glewInit();
    // Check for any errors
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      return 1;
    }
    // Set up your objects and shaders
    init();
    // Begin infinite event loop
    glutMainLoop();
    return 0;
}

问题是,我需要在同一个 window 上使用不同的 VAO 和 VBO 创建两个不同的三角形。我为我的第二个三角形创建了单独的 VAO 和 VBO。但是,当我什至不知道我的显示函数何时被调用时,我不明白我打算如何生成和 link 我的缓冲区、绘制我的数组、切换到我的第二个缓冲区并绘制那些数组。

我的显示函数是这样的:

void display(){

glClear(GL_COLOR_BUFFER_BIT);
// NB: Make the call to draw the geometry in the currently activated vertex buffer. This is where the GPU starts to work!
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}

所有操作都可以在从 main 调用的名为 asyouwant 的单独函数中完成 示例:

#include <GL/glut.h>

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello world :D");
    glutDisplayFunc(displayMe);         // = > draw in displayme function
    glutMainLoop();
    return 0;
}


void displayMe(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);
    glEnd();
// a second geoform
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-0.5, 0.0, 0.0);
        glVertex3f(-0.5, -0.5, 0.0);
        glVertex3f(0.0, -0.5, 0.0);
    glEnd();

    glFlush();
}

作为补充:用于 VAO 和缓冲区 1-初始化(声明 VAO ,声明顶点缓冲区,...)

GLuint VaoID;
glGenVertexArrays(1, &VaoID);
glBindVertexArray(VaoID);

// 一个包含 3 个向量的数组,代表 3 个顶点

static const GLfloat g_vertex_buffer_data[] = {
   -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f,  1.0f, 0.0f,
};

只限一次

// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

2- 使用它(在显示功能中绑定和绘制)

// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
   0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
   3,                  // size
   GL_FLOAT,           // type
   GL_FALSE,           // normalized?
   0,                  // stride
   (void*)0            // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);