More efficient way of using VAOs in OpenGL

Each mesh is the set of a VBO and an EBO, assuming you have multiple meshes with the same vertex format (ex: position, color), you could use the same VAO for each mesh, like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> ...
GLuint vao;
GLuint vbos[2];
GLuint ebos[2];
glGenBuffers(2, vbos);
glGenBuffers(2, ebos);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data_1), vertex_data_1, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data_2), vertex_data_2, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
while (!glfwWindowShouldClose((window))) {
...
glBindVertexArray(vao);
// Draw the first mesh
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[0]);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
// Draw the second mesh
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[1]);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
....
</code>
<code> ... GLuint vao; GLuint vbos[2]; GLuint ebos[2]; glGenBuffers(2, vbos); glGenBuffers(2, ebos); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data_1), vertex_data_1, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data_2), vertex_data_2, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); while (!glfwWindowShouldClose((window))) { ... glBindVertexArray(vao); // Draw the first mesh glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[0]); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); // Draw the second mesh glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[1]); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); glfwSwapBuffers(window); glfwPollEvents(); } .... </code>
    ...
    GLuint vao;
    GLuint vbos[2];
    GLuint ebos[2];

    glGenBuffers(2, vbos);
    glGenBuffers(2, ebos);
    glGenVertexArrays(1, &vao);

    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data_1), vertex_data_1, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data_2), vertex_data_2, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[0]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    while (!glfwWindowShouldClose((window))) {
        ...
        glBindVertexArray(vao);

        // Draw the first mesh
        glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[0]);
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);

        // Draw the second mesh
        glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12);
        
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebos[1]);
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);        
        glfwPollEvents(); 
    }
    ....

So whenever you need to draw a mesh you must first:

  1. Connect the vbo using glBindBuffer()
  2. Associate the current VBO with each attribute of the VAO using glVertexAttribPointer
  3. Connect and associate the EBO using glBindBuffer.

This mechanism works, but I think it is not optimal as you have to make many calls to draw each mesh, so I would like to know your opinion on the most efficient way, below are some solutions I have thought of:

  1. Use one VAO for each mesh; each VAO would be identical to the others, except for the VBO and EBO attached to it, so before drawing a mesh you only need to call glBindVertexArray

  2. Put all vertex data in one VBO and all indexes in one EBO, and use only one VAO (we are basically talking about Batch rendering)

Is there some solution I missed?

Considering the three solutions, depending on the number of meshes (considering the case where there are few meshes and the case where there are many), what is the ranking for efficiency (speed, memory, security)?

Also in the case that I can use DSA architecture:

Is it possible to define a single VAO by specifying the attribute format once and change the associated VBO through the glBindVertexBuffer function, or is it necessary to make multiple calls? if so which ones?

In this case how does the previously drafted ranking change?

New contributor

Phoenix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật