Sign in to follow this  

ASE and vertex array

This topic is 4283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I´m trying to render ASE with vertex array and DrawElements-function, starting with a simple cube model. The problem is, that I have 8 vertices, 12 faces (thats allright so long), but I have 12 textcoords and 12 indices for textcoords. Am I´m wrong, or do I need 8 texcoords for using the vertex array??? I dont understand. Do I need to reorder ASE texcoords so that they fit to the indices (for the vertices)? Thx for your help

Share this post


Link to post
Share on other sites
Data in ASE does not map directly to vertex arrays in OpenGL, becasue ASE stores attributes with separate index arrays while OpenGL requires a single index array for all attributes. You need to manually go through all faces and recreate the attribute arrays so all attributes can be indexed with a single index array. Yes, this means you must duplicate certain attributes.

A cube is the worst case scenario when it comes to shared vertices in a vertex array. A cube may have 8 corners, but it actually have 24 vertices. Each corner of the cube have the same position, but different normals for example, therefore they are all different vertices.

Share this post


Link to post
Share on other sites
:O

So I can´t use vertex arrays with indices???
What is the difference between DrawElements and DrawArrays, if I have to duplicate vertices?

Thanks for your help

Share this post


Link to post
Share on other sites
Can anyone please hint me, on how to generate indexed vertex arrays?
this is the ASE file, I want to process:

[code=cpp]
*MESH {
*TIMEVALUE 0
*MESH_NUMVERTEX 8
*MESH_NUMFACES 12
*MESH_VERTEX_LIST {
*MESH_VERTEX 0 -5.4310 -4.3964 -2.0128
*MESH_VERTEX 1 4.5690 -4.3964 -2.0128
*MESH_VERTEX 2 -5.4310 5.6036 -2.0128
*MESH_VERTEX 3 4.5690 5.6036 -2.0128
*MESH_VERTEX 4 -5.4310 -4.3964 7.9872
*MESH_VERTEX 5 4.5690 -4.3964 7.9872
*MESH_VERTEX 6 -5.4310 5.6036 7.9872
*MESH_VERTEX 7 4.5690 5.6036 7.9872
}
*MESH_FACE_LIST {
*MESH_FACE 0: A: 0 B: 2 C: 3 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 2 *MESH_MTLID 1
*MESH_FACE 1: A: 3 B: 1 C: 0 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 2 *MESH_MTLID 1
*MESH_FACE 2: A: 4 B: 5 C: 7 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 3 *MESH_MTLID 0
*MESH_FACE 3: A: 7 B: 6 C: 4 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 3 *MESH_MTLID 0
*MESH_FACE 4: A: 0 B: 1 C: 5 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 4 *MESH_MTLID 4
*MESH_FACE 5: A: 5 B: 4 C: 0 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 4 *MESH_MTLID 4
*MESH_FACE 6: A: 1 B: 3 C: 7 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 5 *MESH_MTLID 3
*MESH_FACE 7: A: 7 B: 5 C: 1 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 5 *MESH_MTLID 3
*MESH_FACE 8: A: 3 B: 2 C: 6 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 6 *MESH_MTLID 5
*MESH_FACE 9: A: 6 B: 7 C: 3 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 6 *MESH_MTLID 5
*MESH_FACE 10: A: 2 B: 0 C: 4 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 7 *MESH_MTLID 2
*MESH_FACE 11: A: 4 B: 6 C: 2 AB: 1 BC: 1 CA: 0 *MESH_SMOOTHING 7 *MESH_MTLID 2
}
*MESH_NUMTVERTEX 12
*MESH_TVERTLIST {
*MESH_TVERT 0 0.0000 0.0000 0.0000
*MESH_TVERT 1 1.0000 0.0000 0.0000
*MESH_TVERT 2 0.0000 1.0000 0.0000
*MESH_TVERT 3 1.0000 1.0000 0.0000
*MESH_TVERT 4 0.0000 0.0000 0.0000
*MESH_TVERT 5 1.0000 0.0000 0.0000
*MESH_TVERT 6 0.0000 1.0000 0.0000
*MESH_TVERT 7 1.0000 1.0000 0.0000
*MESH_TVERT 8 0.0000 0.0000 0.0000
*MESH_TVERT 9 1.0000 0.0000 0.0000
*MESH_TVERT 10 0.0000 1.0000 0.0000
*MESH_TVERT 11 1.0000 1.0000 0.0000
}
*MESH_NUMTVFACES 12
*MESH_TFACELIST {
*MESH_TFACE 0 9 11 10
*MESH_TFACE 1 10 8 9
*MESH_TFACE 2 8 9 11
*MESH_TFACE 3 11 10 8
*MESH_TFACE 4 4 5 7
*MESH_TFACE 5 7 6 4
*MESH_TFACE 6 0 1 3
*MESH_TFACE 7 3 2 0
*MESH_TFACE 8 4 5 7
*MESH_TFACE 9 7 6 4
*MESH_TFACE 10 0 1 3
*MESH_TFACE 11 3 2 0
}
*MESH_NUMCVERTEX 0
*MESH_NORMALS {
*MESH_FACENORMAL 0 0.0000 0.0000 -1.0000
*MESH_VERTEXNORMAL 0 0.0000 0.0000 -1.0000
*MESH_VERTEXNORMAL 2 0.0000 0.0000 -1.0000
*MESH_VERTEXNORMAL 3 0.0000 0.0000 -1.0000
*MESH_FACENORMAL 1 0.0000 0.0000 -1.0000
*MESH_VERTEXNORMAL 3 0.0000 0.0000 -1.0000
*MESH_VERTEXNORMAL 1 0.0000 0.0000 -1.0000
*MESH_VERTEXNORMAL 0 0.0000 0.0000 -1.0000
*MESH_FACENORMAL 2 0.0000 0.0000 1.0000
*MESH_VERTEXNORMAL 4 0.0000 0.0000 1.0000
*MESH_VERTEXNORMAL 5 0.0000 0.0000 1.0000
*MESH_VERTEXNORMAL 7 0.0000 0.0000 1.0000
*MESH_FACENORMAL 3 0.0000 0.0000 1.0000
*MESH_VERTEXNORMAL 7 0.0000 0.0000 1.0000
*MESH_VERTEXNORMAL 6 0.0000 0.0000 1.0000
*MESH_VERTEXNORMAL 4 0.0000 0.0000 1.0000
*MESH_FACENORMAL 4 0.0000 -1.0000 0.0000
*MESH_VERTEXNORMAL 0 0.0000 -1.0000 0.0000
*MESH_VERTEXNORMAL 1 0.0000 -1.0000 0.0000
*MESH_VERTEXNORMAL 5 0.0000 -1.0000 0.0000
*MESH_FACENORMAL 5 0.0000 -1.0000 0.0000
*MESH_VERTEXNORMAL 5 0.0000 -1.0000 0.0000
*MESH_VERTEXNORMAL 4 0.0000 -1.0000 0.0000
*MESH_VERTEXNORMAL 0 0.0000 -1.0000 0.0000
*MESH_FACENORMAL 6 1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 1 1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 3 1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 7 1.0000 0.0000 0.0000
*MESH_FACENORMAL 7 1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 7 1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 5 1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 1 1.0000 0.0000 0.0000
*MESH_FACENORMAL 8 0.0000 1.0000 0.0000
*MESH_VERTEXNORMAL 3 0.0000 1.0000 0.0000
*MESH_VERTEXNORMAL 2 0.0000 1.0000 0.0000
*MESH_VERTEXNORMAL 6 0.0000 1.0000 0.0000
*MESH_FACENORMAL 9 0.0000 1.0000 0.0000
*MESH_VERTEXNORMAL 6 0.0000 1.0000 0.0000
*MESH_VERTEXNORMAL 7 0.0000 1.0000 0.0000
*MESH_VERTEXNORMAL 3 0.0000 1.0000 0.0000
*MESH_FACENORMAL 10 -1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 2 -1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 0 -1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 4 -1.0000 0.0000 0.0000
*MESH_FACENORMAL 11 -1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 4 -1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 6 -1.0000 0.0000 0.0000
*MESH_VERTEXNORMAL 2 -1.0000 0.0000 0.0000
}
}
[/code]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need to dublicate some vertices yes, because texcoords are not shared between faces like vertices

MESH_TFACELIST are indexes to texcoords for every face just like MESH_FACE_LIST specifies indices for vertices.

Loop thrue MESH_FACE_LIST and pick all data and build your vertexbuffer

Share this post


Link to post
Share on other sites
Quote:
Original post by lem
:O

So I can´t use vertex arrays with indices???

You can. But you must use a single index array for all attributes, not one index array per attribute.

Quote:
Original post by lem
What is the difference between DrawElements and DrawArrays, if I have to duplicate vertices?

Thanks for your help

glDrawElements takes an arbitrary index array, and glDrawArrays assumes a linear index array (index starts from 0 and increases by 1 for each index).

Now note the difference between duplicating vertices and duplicating attributes. You don't have to duplicate whole vertices, but only certain attributes. The reason you have to duplicate certain attributes is simply becuase OpenGL indexes a vertex, and if two vertices have same position but different normal, they are different vertices and the position attribute must be duplicated. The vertex cannot be shared simply because the two vertices are not the same.

Quote:
Original post by lem
Can anyone please hint me, on how to generate indexed vertex arrays?

The simplest way to flatten the vertex array is to create a linear vertex array suitable for glDrawArrays and ignore shared vertices. In this case, assuming it really is the worst case scenario cube, this is likely the best you can get anyway.

Simple algorithm:

  1. Create a vertex array large enough to contain 3*MESH_NUMFACES vertices.

  2. Go through each face index, call it i in this example.

  3. Take the corresponding position, normal, texture coordinate and whatever attributes you have, and put it in position i in the vertex array.


Now you have a vertex array you can pass draw as GL_TRIANGLES with glDrawArrays.

Share this post


Link to post
Share on other sites
Thanks for your help, but I dont understand, why to use indeces ...
If number of faces equals number of text faces, I always have to build the array from scratch? So I dont need index/DrawElements - I can build the array and use DrawArray?

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob

Quote:
Original post by lem
Can anyone please hint me, on how to generate indexed vertex arrays?

The simplest way to flatten the vertex array is to create a linear vertex array suitable for glDrawArrays and ignore shared vertices. In this case, assuming it really is the worst case scenario cube, this is likely the best you can get anyway.

Simple algorithm:

  1. Create a vertex array large enough to contain 3*MESH_NUMFACES vertices.

  2. Go through each face index, call it i in this example.

  3. Take the corresponding position, normal, texture coordinate and whatever attributes you have, and put it in position i in the vertex array.


Now you have a vertex array you can pass draw as GL_TRIANGLES with glDrawArrays.


Thanks for your help, but I would like to use indexed vertex array (by using DrawElements) and I need a generic algorithm for building an array.
Your solution means, always building a numfaces*3 (in this case 36) array, which means, that i don´t need indices...


Share this post


Link to post
Share on other sites
maybe ASE is not the right format? :)
are there any 3d editors/formats that save their data in a way, i can use DrawElements?

Share this post


Link to post
Share on other sites
By looking at how the data is exported, no, maybe ASE isn't the best format. But if you already have functions to extract the data from the file, and you can transform the data into a linear vertex array for drawing with glDrawArrays, is this REALLY a problem that MUST be solved? I mean, after all, you said you would like to use indexed vertex arrays, not that you must use them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
By looking at how the data is exported, no, maybe ASE isn't the best format. But if you already have functions to extract the data from the file, and you can transform the data into a linear vertex array for drawing with glDrawArrays, is this REALLY a problem that MUST be solved? I mean, after all, you said you would like to use indexed vertex arrays, not that you must use them.


;-)
I did transform it, and can render it with DrawArrays, but I would like to use DrawElements.
What would be a good format for DrawElements?What 3d editor do you use for exporting mesh data?

Share this post


Link to post
Share on other sites
Using glDrawElements() would really improve performance by alot, because glDrawArrays() isn't really an optimized driver path. Second, you can save alot of overhead with indices in some cases.

I struggled with the same problem not so long ago. Code that handles this exact problem is to be found here:

http://www.mindcontrol.org/~hplus/graphics/vertex-arrays.html

This code handles a vertex array with a texture coordinate array, but you can easily modify it to also handle normals and vertexcolors easily. (I did this too).

Hope I've been able to help you,
Jeroen

Share this post


Link to post
Share on other sites
great stuff, godmodder.
i was breaking my head the whole day, how to code a smart "vertex array builder".
with this code and my ideas, it shouldn´t be a problem :-)

Share this post


Link to post
Share on other sites

This topic is 4283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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