View Full Version : Sending UV/Normal index info to glDrawElements

09-24-2009, 11:18 AM
When I parse out 3d model info from almost any format, I get 6 different arrays like so.

Vector* vertArray;
int* vertIndicies;

Vector* normalArray;
int* normalIndicies;

Vector* uvArray;
int* uvIndicies;

When I first set off to making my 3D engine, I used OpenGL's immediate mode (SLOW). I am drawing my objects like so.

//Psuedo Code
for each face
for each vert in face
glTexCoord2dv(getUV( face,vert))
glNormal3dv( getNorm(face,vert))
glVertex3dv( getVert(face,vert))

Just for clarity, here is an example of how the getUV()
function will get the UV. I have an array of faces (aka polygons), The face knows what index its first vert is, and then I offest by the index of the vert in that face (usually 0-3).

getUV(int faceIndex, int vertIndex) {
return uvArray[ uvIndicies[ faceArray[faceIndex].startIndex + vertIndex ] ];

Long story short, this works. But only because I have separate index arrays from my verts, UVs and Normals. As we all know a single vertex can have different UVs or normals, depending on which face we are associating it with.

My problem is when I switched over to vertex arrays I have code like this:

glVertexPointer(3, GL_DOUBLE, sizeof(Vector), vertArray);
glNormalPointer(GL_DOUBLE, sizeof(Vector), normalsArray);
glTexCoordPointer(2, GL_INT, sizeof(Vector), uvArray);
glDrawElements(GL_QUADS, numVertIndices, GL_UNSIGNED_INT, vertIndicies);

Oh, great, my model draws corrrectly, but the UVs, and normals are all wrong.

Is OpenGL using the vertIndices to index into my normal and uv array?! How can I tell OpenGL to use used my other index arrays to get UV and normal data?

Thanks All,

09-24-2009, 11:29 AM
You can't. Either don't use indexes or agree about 1 each vertex attribute per index.

You can duplicate the vertexes that are used with different UV sets to make them follow the rule.

Alternatively, you can supply more then one UV sets and choose one of them on the fly.

09-24-2009, 11:40 AM
Ok... are you talking about something like this then

struct vert
Vector pos;
Vector norm;
Vector uv;

and then say for a Cube, I'd have to have 24 of those vert structs (4 verts for each of the 6 faces).

I thought the whole point of using index arrays was to avoid having to repeat shared verts?


09-24-2009, 11:48 AM
You can get rid of duplicated vertices *only* if all vertex attributes are duplicated.

For a solid cube there are no 2 vertices sharing both normal & vertex, so indexing gives you nothing (24 vertices).

Indexing works for smooth models (almost any character model is smooth), because vertices start sharing normal in this case. The only trouble left is UV.

For a smooth cube you'll have 14 vertices in case of a standard cube unwrapping.