PDA

View Full Version : Simple question about where to bind VAO



Aidoru
10-15-2015, 03:51 AM
Hello,
I have a simple question (I think), just to do a correct code:

if I have that code:


glGenVertexArrays(1, &vao);
glBindVertexArray(vao); //(LOCATION A) Better to put here the bind??
glGenBuffers(1, &vbo_vertex);

//(LOCATION B)

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex);

//(LOCATION C)

glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);

//(LOCATION D)
glVertexAttribPointer(
0, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer( GL_ARRAY_BUFFER, 0);


Which is the better location to put the "glBindVertexArray"?
In all of these location I get the same correct result.

A last question, in my paint function I can draw without bind vao ( I can delete it in my code without problem), all I have to do is to call "glEnableVertexAttribArray(0);", so bind the vao or enable the vertex attribute is the same thing?

Thank you

Alfonse Reinheart
10-15-2015, 06:42 AM
You're not asking the right question. The question shouldn't be "what line of code do I put the VAO bind on?" It ought to be "what functions modify the currently bound VAO's state?" Because once you know what, the former question answers itself.

All of the functions listed on this page (https://www.opengl.org/wiki/Vertex_Specification) modify VAO state (except where otherwise stated) modify the currently bound VAO. Note that glBindBuffer only modifies the current VAO when binding to GL_ELEMENT_ARRAY_BUFFER. If a function isn't listed there, it doesn't modify VAO state.

GClements
10-15-2015, 07:33 AM
Which is the better location to put the "glBindVertexArray"?

It doesn't matter. None of the functions which are called between locations A and D modify the VAO state. glVertexAttribPointer() and glEnableVertexAttribArray() modify the VAO state, so it needs to be bound before calling those functions.



in my paint function I can draw without bind vao ( I can delete it in my code without problem), all I have to do is to call "glEnableVertexAttribArray(0);", so bind the vao or enable the vertex attribute is the same thing?

The compatibility profile has a "default VAO" which is bound at start-up and whenever you call glBindVertexArray(0). So if you don't create and bind a VAO, the default VAO is used. Prior to OpenGL 3.0, vertex attribute state was global state, so the effect was as if you only have the default VAO and it is always bound.

A VAO simply encapsulates all of the state related to vertex attributes in an object. This allows you to change all attribute-related state with a single glBindVertexArray() call, rather than needing several calls per attribute.

If you're using the compatibility profile and you only have a single "mesh", there's no particular need to use VAOs. The core profile doesn't have a default VAO, so if you're using the core profile you have to create and bind a VAO prior to specifying attribute state or rendering. For either profile, if you need to perform multiple rendering operations with different attribute data, creating a VAO for each combination of settings and switching between VAOs is simpler and more efficient than creating a single VAO and continually changing its state.

Aidoru
10-16-2015, 02:58 AM
Thank you very much Alfonse e GClaments, now it's clear.