PDA

View Full Version : drawing arrays of points



ghagha
07-14-2007, 05:21 AM
hi

I have an array of vertices, an array of texcoords, an array of vertices indices and an array of texcoords indices.

The problem is that I have 2 arrays of indices, one for vertices and one for texcoords, if it's only one array of indices I can render it with glDrawelements, but how I can pass 2 arrays of indices ? I need call glDrawElements 2 times?

k_szczech
07-14-2007, 07:58 AM
You can only use one array of indices at a time.
It can be done with vertex/geometry shaders on DX10 hardware.
On any other hardware each element in your vertex array will be used with the same element in texcoord array.
If for example, you have a cube and you want different texture coordinates for each face, then you have to specify separate vertices for each face - that means 24 vertices, not 8.

ghagha
07-14-2007, 11:48 AM
but in the application that I'm coding I receive 2 arrays of data (vertices positions and normals (or texcoords too)) and 1 array of indices for each array of data. for example, for a cube I receive this array of vertices:
-50 50 50 50 50 50 -50 -50 50 50 -50 50 -50 50 -50 50 50 -50 -50 -50 -50 50 -50 -50

and this array of normals:
0 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 -1 0 0 -1 0 0 -1 0 0 -1

and this array of indices:
0 0 2 1 3 2 0 0 3 2 1 3 0 4 1 5 5 6 0 4 5 6 4 7 6 8 7 9 3 10 6 8 3 10 2 11 0 12 4 13 6 14 0 12 6 14 2 15 3 16 7 17 5 18 3 16 5 18 1 19 5 20 7 21 6 22 5 20 6 22 4 23

in the array of indices I have the indices for vertex and normals, example
0 2 3 0 3 1 0 1 5 0 5 4 ... are indices for vertices array
0 1 2 0 2 3 4 5 6 .. are indices for normal array

in a first time I only read the vertices array and the vertices indices and I render it with:
glVertexPointer(3, GL_FLOAT, 0, (*iter2)->vertexdata);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawElements(GL_TRIANGLES, (*iter2)->nindices, GL_UNSIGNED_INT, (*iter2)->indices);
glDisableClientState(GL_VERTEX_ARRAY);

but now I want render the vertices positions and the normals (or texcoords if teh app receive it), but I dont know how I can send to opengl 2 array os indices. any idea about this?

k_szczech
07-15-2007, 05:43 AM
As I said- you can't do this. You will have to convert your arrays into new vertex and normal array with single index array. For example:


vertex pos array: (1,1,1), (2,2,2), (3,3,3), (4,4,4)
index array (pos): 0, 1, 2, 2, 3, 0

normal array: (5,5,5), (6,6,6)
index array (normal): 0, 0, 0, 1, 1, 0Vertices 0 and 1 are always used with normal 0.
Vertex 3 is always used with normal 1.
Vertex 2 is used with normal 0 and with normal 1 - that's why we have to make a duplicate of this vertex.

After conversion:


vertex pos array: (1,1,1), (2,2,2), (3,3,3), (3,3,3), (4,4,4)
normal array: (5,5,5), (5,5,5), (5,5,5), (6,6,6), (6,6,6)

index array: 0, 1, 2, 3, 4, 0Now you can use it with OpenGL.

As you can see, you have to put every unique pair of vertex and normal vector into final array.

Algorithm is quite simple.
Original index arrays were:
0, 1, 2, 2, 3, 0
and
0, 0, 0, 1, 1, 0
You can write it down as:
0-0, 1-0, 2-0, 2-1, 3-1, 0-0

So, first we have vertex 0 - normal 0. This is our first new element in arrays. We put this vertex and normal into output vertex and normal array and put index 0 into output index array.
Then we have vertex 0 - normal 1. This is different element, so add vertex 0 and normal 1 into our output arrays and we add 1 to output index array.
Then it's 2-0 - it's another unique element (#2 in output index array). 2-1 is the same vertex but different normal so it's another unique element (#3). Then, there's 3-1 (#4) and then there's 0-0 which is allready in output vertex and normal array so we just add index #0 to output index array.

07-15-2007, 05:50 AM
you will have to create (on the CPU, in your code) a new vertex array and a new texture coordinate array or normal array based on all your data.

or, for small arrays or when performance isn't as critical, travel through the arrays in your code and send each vertex/texcoord/normal to OpenGL with glVertex3f(v), glNormalf(v), and glTexCoord2f(v), etc...

that would be a less efficient way because of all the function calls.

07-15-2007, 05:56 AM
dang, you got one posted a few minutes faster than me!

your post is much better tho...

ghagha
07-15-2007, 07:01 PM
thanks for the info, k_szczech and brybry ;)
I converted the arrays and my app is ok now