Sign in to follow this  
BladeWise

VBOs, VertexArrays and good way to store data...

Recommended Posts

Hi all, my question can sound strange but... what's the best way to store mesh data? I'm not referring to file formats, I'm pretty interested in the inner format... In many posts/lessons/tutorials, I found that a good way is: 1. Store vertices coordinates as a list/array; 2. Store faces as a list/array of vertices IDs; 3. Store vertices normals (or face normals) in a proper array/list; 4. Store vertices/faces colors in a proper array/list. In this case, anyway, it's not possible to use VertexArrays or VBOs, since in these cases we need full arrays, so we should create the vertices list, reading the faces array and creating a new array filled with vertices informations... or I am wrong? So, what's the best way, if the goal is developing a Game Engine (which is supposed to use a dynamical geometry... since every frame is a almost different mesh...), to store these data? Storing vertices as a simple buffer of coordinates? Storing data as I described before (faces as vertices IDs array), is or not a bad idea if we want to use VertexArrays? Maybe I'm a bit confused...

Share this post


Link to post
Share on other sites
What do you mean "full arrays"? You can do anything with VBOs that you can with vertex arrays. And anything with vertex arrays that you can with immediate mode (abeit you may need to rearrange your data , if for instance, your model is a md2 and is stored all crazy). Getting back to the "full arrays" thing, by that do you mean having like position, normal, color, texture coordinate1, texture coordinate2 ect. Because that can be accomplished with VBOs as well. (Though personally Id reccomend interleaving them anyways, as at one time it was touted as faster, AND its easier to need to just bind one buffer for each type)

hope that helps
-Dan

Share this post


Link to post
Share on other sites
As "full array" I'm referring to the data structure containg all (redoundant) vertices of a mesh... let's say we have a cube... using the suggested arrangement we have:
8 vertices
6 faces (if using QUADS, with 4 vertices for each face)
24 normals

Using VertexArrays, if I'm not wrong, we need to point a buffer containing all vertices ordered as faces, so we have:
24 vertices
NO faces (it's not needed to reconstruct faces)
24 normals

I think I need data contiguous in memory to use
glBindBufferARB(...);
or
glVertexPointer(...);
since both needs a pointer to a memory location... thanks the stride parameter we can use data mixed in some way, but they needs to be periodical in some way... So, my question (re-reading my post I must say I'm not clear about this...) is: to use vertex buffers, I need to repeat vertices of the mesh shared among faces, arranging them in appropriate memory structures?

Share this post


Link to post
Share on other sites
Are you speaking about using glDrawElements instead of glDrawArrays? Uhmm, I suppose this is what I need... if I'm not wrong, in this way I could store only non-redundant vertices in the array, and then I could specify how faces are built, using indices... but... what about normals/colors/texture coords? What if the same vertex uses a different normal depending on the face is using it? I thought glDrawElements draws data depending on the index, retrieving vertex informations (normal, color, texture coords) from the same index in the correct array... something like this:
passing index array {0,3,7,4} to draw the top face of a cube, will use {0,3,7,4} normals in the current normals array...
drawing next the front face of the cube as {0,1,2,3}, vertex[0] should be the same as before, but normals[0] should be different...
Does anyone has a code snippet, or a link to a tutorial, about using vertex arrays by indices (glDrawElements) with normals?
Maybe I'm getting this in the wrong way, any explanation is welcome... :(

Share this post


Link to post
Share on other sites
you can only pass use 1 index for the array. This means that if normals or texture coordinates are different you have to add a new verticle. If you have sharp edges then you have dublicate vertex data. It works quite well for smooth gemetry, where you have only one normal per vertex, however.

Share this post


Link to post
Share on other sites
So, to preserve sharp edges, the only way is duplicate vertices?
I was looking for a way to store not duplicated data (vertices, normals, colors...), and a face structure to store only indices... so, using vertex arrays, I could point data arrays, specifying only which elements should be taken... is it a bad idea? Or better, is it possible?

Share this post


Link to post
Share on other sites
Wenn you use VA or VBO you can only specify one index array which is used for the vertex, normals, color, etc. array. So, if you want to preserve the sharp edges you have to dublicate vertex data.

you could make multiple normal arrays and use them with the same vertex array, but this can quicly turn complicated.

Share this post


Link to post
Share on other sites
I'm not exactly sure why everyone is so worried about duplicating vertex info when it comes to indexed polys. How much memory does a standard vertex take up? 3 floats for position, 3 more for normals, and 2 for texture coords. Assuming a float is 32 bits, that's a whopping 32 bytes. If you had a model that was 5000 polys with absolutely no duplicate verticies you're looking at a grand total of... about 450kb. Tack on another 250kb for indices and your full model takes up 700kb of video memory. Even if your card only has 32 meg of ram, we're talking about piddly amounts here. If anything you should be worrying about how many textures you can store.

[/end math lesson]

The simple fact of the matter is that indexed geometry is really the most efficent way to send geometry to your card. All current drivers are hard wired to be the most efficient with that type of data. On top of that 99% of the time it's the least memory intensive method out there, especially with "real world" data. I would seriously suggest learning how to work with it if you're at all serious about graphics development.
Go on! You'll like it! You'll see! ^_^

Share this post


Link to post
Share on other sites
:D
Yes, you are right, my worries at first were about simple shapes... like cubes... in that (pathological) case redoundant vertices are a lot... thinking about the problem I realized that cubes are not a good example for vertex arrays! :P

At the moment I managed to create my data structure to use vertex arrays... the main problem in my case is about the language I'm using: C# and TAO framework are less "raw" than C++... so something like structures cannot be uses so carefree (as I have seen in many samples about VertexArrays)... since TAO wrappers uses only float[] or float[,] or float[,,] (I don't know why 3 dimensional arrays....--') or ref float, and doesn't allow pointers, I had to create a class with raw data and some properties to make easier initializing data... piff... I hope that everything is going to work ^^'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this