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

问题描述 投票:1回答:1

我不了解此主要功能的工作原理。我有一个显示函数,它使用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;
}

问题是,我需要使用单独的VAO和VBO在同一窗口上创建两个不同的三角形。我为第二个三角形创建了单独的VAO和VBO。但是,当我什至不知道何时调用显示函数时,我都看不到要生成和链接缓冲区,绘制数组,切换到第二个缓冲区以及绘制这些数组的含义。

我的显示功能如下:

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();
}
c opengl glut
1个回答
1
投票

所有操作都可以在名为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);
© www.soinside.com 2019 - 2024. All rights reserved.