I see you've not had experience with indices. I'll try to explain this the best I can.
What you do is the following:
You have an array with all the vertices, and an array with all the uv coordinates. Then also you'll have an array with faces. These arrays can have different number of elements.
These faces only contain indices for vertices and uv coordinates, not the actual vertices and uv coordinates. The indices are used because faces share vertices and uv coordinates, and this reduces memory usage, but complicates things a bit.
So, when you render a face you get the vertices indicated by the vertex indices, and uv indices.
Imagine structures like these:
Code:
TYPE
TVector3f = array[0..2] of single; {this is for storing a vertex}
TVector2f = array[0..1] of single; {this is for storing a uv coordinate}
TFace = record
iv: array[0..2] of longint;
iuv: array[0..2] of longint;
end;
VAR
nVertices, nUV, nFaces: longint;
vertices: array of TVector3f;
uv: array of TVector2f;
faces: array of TFace;
First you read the vertices and uv coordinates into their respective arrays. Then you read the faces one by one into the faces array. Each new face goes to the next position in the array (increment by one). Store the number of vertices, uv coordinates and faces into nVertices, nUV and nFaces.
Once you've read all the information then you need to render the faces like this:
Code:
glBegin(GL_TRIANGLES);
for i := 0 to (nFaces-1) do begin
glTexCoord2f(uv[faces[i].iuv[0]]);
glVertex3f(vertices[faces[i].iv[0]]);
glTexCoord2f(uv[faces[i].iuv[1]]);
glVertex3f(vertices[faces[i].iv[1]]);
glTexCoord2f(uv[faces[i].iuv[2]]);
glVertex3f(vertices[faces[i].iv[2]]);
end;
glEnd();
I hope you understood.
In case you want to store everything as a single array then you'll need to do pretty much as above. Read vertices and uv coordinates, but the TFace type will be like this.
Code:
TFace = record {remember that triangles have tree vertices and three uv coords}
v: array[0..2] of TVector3f;
uv: array[0..2] of TVector2f;
end;
Then when you read the faces from the obj file, you get the three indices for faces and three indices for uv coords, and then you look up the vertices and uv coords in the arrays and store them in the current face.
We'll consider that 'i' is the index of the current face you're reading from the .obj file and that you store the indices in the following variables:
Code:
VAR
face_indices, uv_indices: array[0..2] of longint;
Then we assign the vertices and uv coords to the face:
Code:
faces[i].v[0] := vertices[face_indices[0]];
faces[i].v[1] := vertices[face_indices[1]];
faces[i].v[2] := vertices[face_indices[2]];
faces[i].uv[0] := uv[uv_indices[0]];
faces[i].uv[1] := uv[uv_indices[1]];
faces[i].uv[2] := uv[uv_indices[2]];
This may make rendering easier, but it takes somewhat more memory.
REMARK: I think this is the longest post I've ever written.
Bookmarks