PDA

View Full Version : Some questions on vertex arrays



Vexator
07-27-2005, 06:39 AM
in my little engine i store and display my models like this:

i have an array of vertices (x,y,z positions and u,v texture coordinates), one for vertex normals ( x,y,z direction) and one for polygons (3 indices for vertices and 3 indices for normals each).

this works fine if i use the standard code to draw:


glBegin(GL_TRIANGLES);

loop through polygons
{
loop from 0 to 3
{
glNormal3f(normal@current normal index);
glTexCoord2f(texcoord@current vertex index);
glVertex3f(vertex@current vertex index);
}

glEnd();to improve speed i now want to use vertex arrays. i got it working for vertex data and vertex indices so far.


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, Vertices);

glDrawElements(GL_TRIANGLES, NumIndices, GL_UNSIGNED_INT, Indices);

glDisableClientState(GL_VERTEX_ARRAY);but i'm having trouble to make my vertex normals and texture coordinates work:

question #1: do the indices (the last parameter of glDrawElements() only apply to the vertex array or also to the texture coordinates and the vertex normals array, e.g. if the current index is 5, will it only use for the 5th element of the vertices array, or also the 5th elements of the texture coordinates and normals array?

#2: if they apply to all arrays, then i have to make sure that every array has the same size and order, right? so i'll probably have to duplicate some data, as in most cases you have less normals than vertices, but more texture coordinates than vertices etc. is this correct?

#3: so if i end up with more data than without using vertex arrays, will it really speed up my app or even slow it down?

#4: do you know of a 3d format that saves its data in a way that i can easily use it to fill my arrays, i.e. that stores vertices, uv coords and normals in equaliy sized arrays and needs only one set of indices?

sry if these are stupid questions, i'm still new to this stuff and the tutorials i read always just use very simple models like cubes to demonstrate how vertex arrays work :p

thx in advance!

Relic
07-27-2005, 07:23 AM
1. and 2.) Yes, you need to replicate data to get unique vertices in vertex arrays. Each index refers to one entry in each enabled array. All arrays must have their attribute data per vertex.
3.) Depending on the primitives you use the fewer number of drawing calls compared to immediate mode will make up for the additional data. Batch your primitives to use fewer glDraw(Range)Elements calls.
When using vertex buffer objects the graphics driver can move the data to a location nearer to the GPU and that's where it gets really fast.

Vexator
07-27-2005, 08:13 AM
thx for the quick reply! i got texture coordinates working now :) just have to find a way to get my normal data per vertex..

anyone got an idea to #4?

T101
07-27-2005, 08:18 AM
Milkshape3D. Probably others too.
(EDIT)Correction: Ms3d links the normals to the vertices in the triangle structure. But it _is_ easy to add a normal to a vertex, because the vertices are duplicated if they have more than one normal.
So you only need to loop through the triangles, and copy the vertexnormal to the already allocated vertex structure.

Vexator
07-27-2005, 08:32 AM
just wanted to correct you when you did yourself ^^
but you're right, i'll try it this way :) thx!