Obj file loader with different indexes

Reading my obj file, and just taking the vertices and their indices into account, my object maps the desired object in relation to the vertices; so far i only use a basic array of colors so i can figure out if my loader is working. I realized that OpenGL was not drawing my object correctly because the uv indices were read with the vertex indices - I was reading all the indices from the string:

f 2/3/4 3/6/1 1/3/6

      

Into one vector, which then went into GL_ELEMENT_ARRAY_BUFFER. Unfortunately, OpenGL (and DirectX) allows more than one index buffer to be used. So this means that I cannot have a separate vector that has only uv indices, with different vertex indices; or another one holding normal indices. Having only one index buffer, and therefore only one vector, which contains indexes for vertices, textures, and normals; how can I train OpenGL to draw the primitive prescribed in the obj file without dropping one index in favor of the other?

Note. I am drawing a primitive using glDrawElements (..); I've used glDrawArrays (..) in the past, but this time I want to use the old method for efficiency.

+3


source to share


2 answers


If they allow for multiple index buffers, it just gives you a good performance gun to shoot in the foot.

However, you can unpack your model data into (eg interleaved) VBO using the [Position, Normal, Texccord] structure and fill it manually with the appropriate data. This will consume more memory, but will allow you to just make it undeclared.



The "efficiency" you are talking about will only happen in triplets that have duplicates in common. If they do, you need to find them, specify unique triplets, and create an index buffer for them.

Good luck :)

+2


source


Instead of creating separate vertex arrays with vertex holding elements and uvs

verts = [vec3f, vec3f, ...]
norms = [vec3f, vec3f, ...]
uvs = [vec2f, vec2f, ...]

      

You have to create a single array that defines each vertex in terms of all three attributes



verts = [(vec3f, vec3f, vec2f), (vec3f, vec3f, vec2f), ...]

      

Then one vertex will correspond to each index. This means that you will have to duplicate some of your information from the OBJ file into the vertex array, since each vertex corresponds to a unique triplet in the OBJ file. You can specify your attributes using different sizes in the arguments stride

and position

for glVertexAttribPointer

+1


source







All Articles