File types that use separate indexes for vertices and normals do not exactly match the OpenGL vertex model. As you noticed, OpenGL uses one set of indexes.
What you need to do is create an OpenGL vertex for each unique pair (vertex index, normal index) at your input. This requires a little work, but it is not very difficult, especially if you use accessible data structures. STL map works well for this, a pair of vertex index, normal index, is used as a key. I am not going to provide full C ++ code, but I can sketch it out.
Let's say you already read your vertices as an inVertices array / vector data inVertices , where the coordinates for the vertex with index vertexIdx are stored in inVertices[vertexIdx] . The same is true for normals, where a normal vector with index normalIdx stored in inNormals[normalIdx] .
Now you can read the list of triangles with each corner of each triangle given by both a vertexIdx and normalIdx . We will build a new array / vector combinedVertices that contains both vertex and normal coordinates, plus the new index index combinedIndices . Pseudocode:
nextCombinedIdx = 0 indexMap = empty loop over triangles in input file loop over 3 corners of triangle read vertexIdx and normalIdx for the corner if indexMap.contains(key(vertexIdx, normalIdx)) then combinedIdx = indexMap.get(key(vertexIdx, normalIdx)) else combinedIdx = nextCombinedIdx indexMap.add(key(vertexIdx, normalIdx), combinedIdx) nextCombinedIdx = nextCombinedIdx + 1 combinedVertices.add(inVertices[vertexIdx], inNormals[normalIdx]) end if combinedIndices.add(combinedIdx) end loop end loop
Reto koradi
source share