4

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:

a triangulated Quad

a UV-Map

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?

Community
  • 1
  • 1
user2602528
  • 243
  • 1
  • 3
  • 8
  • I'm aware that this is an old question, but I'm facing the same problem myself. I was thinking basically what you have suggested; rebuilding the vertice array, texture array, and Normal array to match the indice array length. This will naturally create duplicates, but it will make it fairly simple to just pass in the appropriate array with glDrawElements. I'm still doing my homework on the subject, though. It would be really helpful if the api had a better way to allow a stride across the indices. In fact, why this is not an option baffles me greatly. – Krythic Jun 14 '17 at 03:38

0 Answers0