在 OpenGL 4 中创建第二个 VAO 并绘制两个形状

Posted

技术标签:

【中文标题】在 OpenGL 4 中创建第二个 VAO 并绘制两个形状【英文标题】:Creating a second VAO and drawing two shapes in OpenGL 4 【发布时间】:2015-05-08 20:16:22 【问题描述】:

我正在尝试解决 Anton 的 OpenGL 4 教程一书中的实验。在第一章的实验中,它要求创建第二个 VAO 来绘制 2 个形状而不是一个,但我不知道如何做到这一点,如何同时显示第二个?

    #include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main () 
    // start GL context and O/S window using the GLFW helper library
    if (!glfwInit ()) 
        fprintf (stderr, "ERROR: could not start GLFW3\n");
        return 1;
     
    GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
    if (!window) 
        fprintf (stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    
    glfwMakeContextCurrent (window);

    // start GLEW extension handler
    glewExperimental = GL_TRUE;
    glewInit ();

    // get version info
    const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString (GL_VERSION); // version as a string
    printf ("Renderer: %s\n", renderer);
    printf ("OpenGL version supported %s\n", version);

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
    glEnable (GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

    GLfloat points[] = 
    0.0f,  0.5f,  0.0f,
    0.5f, -0.5f,  0.0f,
    -0.5f, -0.5f,  0.0f,
    0.0f,  0.5f,  0.0f,
    0.5f,  0.5f,  0.0f,
    0.5f, -0.5f,  0.0f
    ;

    GLuint vbo = 0;
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, 18 * sizeof (float), points, GL_STATIC_DRAW);

    GLuint vao = 0;
    glGenVertexArrays (1, &vao);
    glBindVertexArray (vao);
    glEnableVertexAttribArray (0);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    //second vao
    GLuint vao_two = 0;
    glGenVertexArrays(1, &vao_two);
    glBindVertexArray(vao_two);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    const char* vertex_shader =
    "#version 400\n"
    "in vec3 vp;"
    "in vec3 vp2;"
    "void main() "
    " gl_Position = vec4(vp.x, vp.y + 0.2, vp.z, 1.0);"
    " gl_Position2 = vec4(vp2.x, vp2.y - 0.9, vp2.z, 1.0);"
    "";

    const char* fragment_shader =
    "#version 400\n"
    "out vec4 frag_colour;"
    "void main () "
    "  frag_colour = vec4 (0.0, 1.0, 0.0, 1.0);"
    "";

    GLuint vs = glCreateShader (GL_VERTEX_SHADER);
    glShaderSource (vs, 1, &vertex_shader, NULL);
    glCompileShader (vs);

    GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
    glShaderSource (fs, 1, &fragment_shader, NULL);
    glCompileShader (fs);

    GLuint shader_programme = glCreateProgram();
    glAttachShader (shader_programme, fs);
    glAttachShader (shader_programme, vs);
    glLinkProgram(shader_programme);

    glClearColor(0.6f, 0.6f, 0.6f, 0.0f);

    while (!glfwWindowShouldClose (window)) 
        // wipe the drawing surface clear
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram (shader_programme);
        glBindVertexArray (vao);
        // draw points 0-3 from the currently bound VAO with current in-use shader
        glDrawArrays (GL_TRIANGLES, 0, 6);

        glBindVertexArray (vao_two);
        glDrawArrays (GL_TRIANGLES, 0, 6);

        // update other events like input handling 
        glfwPollEvents ();
        // put the stuff we've been drawing onto the display
        glfwSwapBuffers (window);
    
    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;

【问题讨论】:

你为什么使用“in vec3 vp2;”和“ gl_Position2 = vec4(vp2.x, vp2.y - 0.9, vp2.z, 1.0);”在你的着色器程序中?他们是消耗品 【参考方案1】:

实际上,您正在绘制两个不同的形状,但是,因为您在两个顶点属性对象(vao 和 vao_two)中使用相同的顶点缓冲区对象,它们具有相同的坐标并且它们重叠。 尝试制作 2 个具有不同坐标的点的 GLfloat 数组,并将每个点分配给不同的顶点缓冲区对象,然后将每个顶点缓冲区对象分配给不同的顶点属性对象,那么您的形状将是不同的。

【讨论】:

【参考方案2】:

先绑定缓冲区,然后全部绘制:

    glBindVertexArray(vao);
    glBindVertexArray(vao2);
    glDrawArrays(GL_TRIANGLES, 0, 6);

或者,您可以先绘制第一个形状,然后绘制第二个:

    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(vao2);
    glDrawArrays(GL_TRIANGLES, 3, 3);

【讨论】:

以上是关于在 OpenGL 4 中创建第二个 VAO 并绘制两个形状的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 应用程序中创建第二个 RedisTemplate 实例

如何在我的项目中创建第二个目标来为 iOS 创建一个预填充的数据库

无法使用 VAO 和 EBO (openGL) 绘制多个对象

渲染一个包含两个 VBO 的 VAO

多个网格、多个 VBO、多个 VAO、OpenGL 4.1

VAO 绘制错误的索引缓冲区