OpenGL - difficulties with indexes

I have a custom file format that contains all the necessary information for a 3D mesh (exported from 3ds Max). I extracted data for vertices, vertex indices, and normals.

I pass OpenGL vertex data, vertex indices and normal data, and I pass the grid with glDrawElements(GL_TRIANGLES,...)

Everything looks right, but normal. The problem is that normals have different indices. And since OpenGL can use only one index buffer, it uses this index buffer for both vertices and normals.

I would be very grateful if you would suggest me how to solve this problem.

It is important to note that vertex / normal data data is not sorted and therefore I cannot use glDrawArrays(GL_TRIANGLES,...) functionality glDrawArrays(GL_TRIANGLES,...) - the grid does not display correctly.

Is there a method / algorithm that I can use to sort the data so that the grid can be drawn correctly using glDrawArrays(GL_TRIANGLES,..) ? But even if there is an algorithm, another problem arises - I will have to duplicate some vertices (because my vertex buffer consists of unique vertices, for example, if you have a cube, my buffer will have only 8 vertices), and I'm not sure how it is to do.

+3
c ++ rendering 3d opengl mesh
source share
2 answers

I managed to do this without passing the index buffer to OpenGL using glDrawArrays(GL_TRIANGLES,..) I did the following: Fill in the vertex array, vertex index array, normal array, and normal index array. Then I created new vertex and normal arrays with sorted data and passed them to OpenGL.

 for i = 0; i < vertexIndices.size(); ++i newVertexArray[i] = oldVertexArray[vertexIndices[i]]; for i = 0; i < normalsIndices.size(); ++i newNormalsArray[i] = oldNormalsArray[normalsIndices[i]]; 

I optimized it a bit without populating the index arrays at all. But optimization depends on how the programmer reads the grid data.

0
source share

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 
+9
source share

All Articles