[]
Some Definitions OnlineURL: https://www.opengl.org/wiki/Vertex_Specification (**)
URL: http://www.swiftless.com/tutorials/opengl4/4-opengl-4-vao.html
[]
The question is why we first create and bind a vertex array object before any gl** ?
OR
The vertex array object is similar to what before OpenGL3.0 ?
[]
Maybe Here is The Answer.
URL http://stackoverflow.com/questions/8704801/glvertexattribpointer-clarification
____________________________________________________________
___
___*****
Backup AND Hope This is Legal : ) Answer By Robert Rouhani (In http://stackoverflow.com/)
Some of the terminology is a bit off:
- A
Vertex Array
is just an array (typically afloat[]
) that contains vertex data. It doesn't need to be bound to anything. Not to be confused with aVertex Array Object
or VAO, which I will go over later
- A
Buffer Object
, commonly referred to as aVertex Buffer Object
when storing vertices, or VBO for short, is what you're calling just aBuffer
.The main issue here is that you're confusing vertex attributes with VAOs. Vertex attributes are just the new way of defining vertices, texcoords, normals, etc. for drawing. VAOs store state. I'm first going to explain how drawing works with vertex attributes, then explain how you can cut down the number of method calls with VAOs:
- Nothing gets saved back to the vertex array,
glVertexAttribPointer
works exactly likeglVertexPointer
orglTexCoordPointer
work, just instead of named attributes, you get to provide a number that specifies your own attribute. You pass this value asindex
. All yourglVertexAttribPointer
calls get queued up for the next time you callglDrawArrays
orglDrawElements
. If you have a VAO bound, the VAO will store the settings for all your attributes.
- You must enable an attribute before you can use it in a shader. For example, if you want to send vertices over to a shader, you're most likely going to send it as the first attribute, 0. So before you render, you need to enable it with
glEnableVertexAttribArray(0);
.
- Now that an attribute is enabled, you need to define the data it's going to use. In order to do so you need to bind your VBO -
glBindBuffer(GL_ARRAY_BUFFER, myBuffer);
.
- And now we can define the attribute -
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
. In order of parameter: 0 is the attribute you're defining, 3 is the size of each vertex,GL_FLOAT
is the type,GL_FALSE
means to not normalize each vertex, the last 2 zeros mean that there's no stride or offset on the vertices.
- Draw something with it -
glDrawArrays(GL_TRIANGLES, 0, 6);
Wrap that in
- The next thing you draw may not use attribute 0 (realistically it will, but this is an example), so we can disable it -
glDisableVertexAttribArray(0);
glUseProgram()
calls and you have a rendering system that works with shaders properly. But let's say you have 5 different attributes, vertices, texcoords, normals, color, and lightmap coordinates. First of all, you would be making a singleglVertexAttribPointer
call for each of these attributes, and you'd have to enable all the attributes beforehand. Let's say you define the attributes 0-4 as I have them listed. You would enable all of them like so:
for (int i = 0; i < 5; i++)
glEnableVertexAttribArray(i);
And then you would have to bind different VBOs for each attribute (unless you store them all in one VBO and use offsets/stride), then you need to make 5 differentglVertexAttribPointer
calls, fromglVertexAttribPointer(0,...);
toglVertexAttribPointer(4,...);
for vertices to lightmap coordinates respectively.
Hopefully that system alone makes sense. Now I'm going to move on to VAOs to explain how to use them to cut down on the number of method calls when doing this type of rendering. Note that using a VAO is not necessary.
AVertex Array Object
or VAO is used to store the state of all theglVertexAttribPointer
calls and the VBOs that were targeted when each of theglVertexAttribPointer
calls were made.
You generate one with a call toglGenVertexArrays
. To store everything you need in a VAO, bind it withglBindVertexArray
, then do a full draw call. All thedrawbind calls get intercepted and stored by the VAO. You can unbind the VAO withglBindVertexArray(0);
Now when you want to draw the object, you don't need to re-call all the VBO binds or theglVertexAttribPointer
calls, you just need to bind the VAO withglBindVertexArray
then callglDrawArrays
orglDrawElements
and you'll be drawing the exact same thing as though you were making all those method calls. You probably want to unbind the VAO afterwards too.
Once you unbind the VAO, all the state returns to how it was before you bound the VAO. I'm not sure if any changes you make while the VAO is bound is kept, but that can easily be figured out with a test program. I guess you can think ofglBindVertexArray(0);
as binding to the "default" VAO...
Update: Someone brought to my attention the need for the actual draw call. As it turns out, you don't actually need to do a FULL draw call when setting up the VAO, just all the binding stuff. Don't know why I thought it was necessary earlier, but it's fixed now.
___
___
___
URL: http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=8 (Tut3.3 source)
There is however one important thing we need to make clear. Vertex position is a vertex attribute. Another attributes can be color, texture coordinates and so on. But we can have only one index array for ALL vertex attributes. We cannot have separate indices for positions, colors or texture coordinates. This makes indexed rendering not appropriate for rendering some objects. In case of heightmap, it's perfect choice (or rendering closed surfaces or skin). But for objects, like our pyramid from last tutorial, it's not much of a good choice, because there are vertices with same position, but different colors. We would have to make a new vertex for each such distinct combination. And this doesn't make things easier for sure.
No comments:
Post a Comment