i've made an obj-loader for opengl. Therefore i read all faces (one by one) and write the ordered vertices, normals and uv-coords in (openGL-)buffers (GL_ARRAY_BUFFER). That means i dulicate all necessary vertices, normals and uv-coords depending on what the faces-section in the obj-file says. Then i draw them with glDrawArrays()
... all fine.
To save some memory, i thought to use an index list, and use glDrawElements(), but i got one problem. See the two following Pictures:
Consider vertex P0 and the triangles and what it would be in the face-section of an OBJ-file
Triangle
T1 --> f vertex(0)/P0/normal(0) vertex(1)/P1/normal(1) vertex(2)/P2/normal(2)
T2 --> f vertex(0)/P0/normal(0) vertex(1)/P1/normal(1) vertex(5)/P5/normal(5)
T3 --> f vertex(0)/P0/normal(0) vertex(2)/P2/normal(2) vertex(3)/P3/normal(3)
... all fine, but
T4 --> f vertex(0)/P'0/normal(0) vertex(3)/P3/normal(3) vertex(4)/P4/normal(4)
T5 --> f vertex(0)/P'0/normal(0) vertex(4)/P4/normal(4) vertex(5)/P5/normal(5)
ok whats the Problem?
vertex(0)/P0/normal(0) != vertex(0)/P'0/normal(0)
The solution i thought, is something like:
- a list for each vertices, normals, uvs
- read the faces-section of OBJ file
- get the index of the vertex -> write the vertex on position(index) in the vertex-List
- write the normal on position(index) in the normal-List
- write the uv on position(index) in the uv-List
- now the problem vertex with different uv-coord, therefore
- check the list-element if not "empty" or not 0 --> ok there is a vertex and we must duplicate the current vertex, normal, and UV and write them on a new index in all the list, and write the new index to the index list
now my Question...
is there a better way to handle the problem with the different uv-coords for the same vertex in the face-section of the obj file?