Vertex arrays and texture coordinates

Started by
9 comments, last by alex997 20 years ago
I have 2 questions about vertex arrays... ** QUESTION 1 ** I want to load a milkshape 3D model and render it using vertex arrays to make it nice and quick (I can already do it with a non-VA implementation). I was planning to use an interleaved array to specify the vertex position, normal, and texture coordinates. The model uses a single texture, but different parts or the model will be textured from different areas of the texture. The problem I forsee is this: it seems that I can only specify one pair of texture coordinates for each vertex. Since the whole point of vertex arrays is to allow polys to share vertices, what can I do if they share the position and normal data, but not the texture co-ordinates? For example, a simple cube with different areas of the texture on each face. A corner vertex has one position and one normal, but 3 different texture co-ordinate pairs are required, depending on which poly the vertex is being used for. If I just use the VA for vertex and normal information, how can I insert the texture info later? Is there a solution to this problem? How do other people do it? ** QUESTION 2 ** I want to use vertex arrays to render my particle system (i.e. 4 vertices per particle, loads of particles). The problem is all my particles use the whole texture, and so all use the same co-ordinates. It seems a bit wasteful to have 5000 (or whatever) copies of the texture co-ordinates in the array, but is there a way round this? As I see it if I only use a vertex array for the position data I can''t use textures, but if I have texture data in the array it''s very wasteful. Any help on any of this is much appreciated.
Advertisement
Awwwww, c''mon! Does no-one have any answers? Others must have faced the same problem?! Someone throw some ideas in here!
Q1. You duplicate your vertex info, there is no other solution to this problem, unless not using indexed primitives. Other people are doing it like everybody: they duplicate the vertices as needed. The minimal amount of vertices to render an indexed cube is 24, but it''s a bad example, because it''s a "worst case". You can still share a lot of vertices in your average model.

Q2. You can live with it (it''s not that bad), use an extension (point sprites), or just not use interleaved arrays, but flat arrays, make your tex coords static and your positions dynamic.

Y.
Ok, those answers are pretty much what I feared!

For point 1, it sounds like a fair bit of extra work to sort which vertices share textures, so I might use display lists for the moment and come back to it in a couple of weeks.

For point 2, I think I can still use interleaved arrays, just change the vertex position data and leave the texture data static. Since I use an array of structs it's not difficult to do this, and it's still an interleaved array. Pity I have to duplicate all that data though, but I guess it'll still be faster than doing it the old way, just less memory efficient.

[edit] Thanks for your reply!

[edited by - alex997 on March 29, 2004 8:21:21 AM]
"The minimal amount of vertices to render an indexed cube is 24"

not at all, it depends how you setup your texture. you can do this with 14 vertices. you''ll loose some space in your texture, but you can re-use it for something else.
something like:
     _ ___|_|_|_|_|_|_|    |_|


anyway, that was only an example, but the way you setup your textures can affect a lot the number of redundant vertices you have to create...

about point sprites, they might not be very well suited to what you want, as you can''t rotate them, neither can you rotate their texcoords...
just an idea: if memory consumption worries you, you can generate texcoords in a vertex shader, by assigning each vertex an "id", 0 means texcoords {0, 0}, 1 == {1, 0}, 2 == {0, 1}, 3 == {1, 1}.
quote:Original post by alex997
For point 1, it sounds like a fair bit of extra work to sort which vertices share textures, so I might use display lists for the moment and come back to it in a couple of weeks.
[edited by - alex997 on March 29, 2004 8:21:21 AM]


Its not really. I wrote a lovely little dynamic array template rather like STLs vector in which when i add to it, it checks to see if the element you''re adding is the same as an element it already stores. if it is it returns the index value to the element already stored if not it adds the new elemnet to the array.
so i''d have

CDArray m_verts;
CDArray<trIANGLE> m_tris;

in my mesh class, then when i load in my ms3d model i iterate thro all the triangles in the model and create the VERTEX for each vert of the tri (as ms3d stores the uv co-ords in the TRI struct and the xyz data in the VERT) then add it to my array. this return the position at which its stored in my VERTEX array so i use this as the index in my TRIANGLE which i then add to my m_tris array.

when i need to draw it i just use the Lock() method of my CDArray class which returns a VERTEX* which is the array, so i use this to set the arrays for glDrawElements.

theres a bit of work to set it up but once its all there it works sweet.

Sod



i is 1337
SoDdit,

Doesn''t the MS3D structure already sort out the vertices for you? It has a vertex (xyz) array, then in the tri structure it indexes the vertices used for that triangle. No vertices are repeated in the vertex array anyway, so it sounds to me like your code is redundant.
quote:Original post by alex997
I want to use vertex arrays to render my particle system (i.e. 4 vertices per particle, loads of particles). The problem is all my particles use the whole texture, and so all use the same co-ordinates. It seems a bit wasteful to have 5000 (or whatever) copies of the texture co-ordinates in the array, but is there a way round this? As I see it if I only use a vertex array for the position data I can't use textures, but if I have texture data in the array it's very wasteful.


why not just use a static texArray member variable for the class? something like:

class ParticleBase{protected:    static float texArray[HOWEVER_BIG];};


in the off chance that you ever need to change the values for a particular class, just sub-class ParticleBase and include a new member var to hold that class's texArray, and then override the Draw method.

[EDIT: ah, just noticed that you're using interleaved arrays. well, you could always convert to non-interleaved arrays if the memory space is that big a deal to you. i suspect it's not, esp for particles which have a pretty small memory footprint (course you tend to have lots of those bastards, so whatever).]

-me

[edited by - Palidine on March 29, 2004 12:35:18 PM]
quote:Original post by sBibi
"The minimal amount of vertices to render an indexed cube is 24"

not at all, it depends how you setup your texture. you can do this with 14 vertices.
...
something like:
     _ ___|_|_|_|_|_|_|    |_| 


and then the normals came and ruined everything!
quote:Original post by Anonymous Poster
SoDdit,

Doesn''t the MS3D structure already sort out the vertices for you? It has a vertex (xyz) array, then in the tri structure it indexes the vertices used for that triangle. No vertices are repeated in the vertex array anyway, so it sounds to me like your code is redundant.


it stores the vertex data in one array and then the triangle array stores the indices to that array as well as the uv co-ords for each vertex
i wanted a single array of vertices with xyz,nxnynz,uv in it for easy use in vertex arrays.
i is 1337

This topic is closed to new replies.

Advertisement