3D formats: obj and duplicate vertices
I am writing a python converter that takes in an .obj file and outputs it in another format. I've noticed that .obj files have a lot of duplicate vertices, and a lot of the faces refer to these duplicates.
So I wrote the script to only add a vertex once and ignore a开发者_如何学JAVAny duplicates, and if any face references a duplicate vertex, I simply re-direct it to the correct index.
I check for dupes by using a dictionary where the keys are the x,y,z values and the value is just a "1" just so I can add it as an entry. I then check whether the key exists to determine whether the vertex in question is a dupe or not.
But is there a reason why obj files have duplicate vertices? Is the resulting model different if I decided to just take out all the duplicate entries? I have a particular obj file with 2849 vertices except 845 of them are duplicated.
You should use a set()
instead of a dictionnary to store your unique vertices, you won't need anymore to use a dummy value for it.
About duplicated vertices, it seems to be a "bug" of the model maker software or a "bug" of the software developper (have a look here)
Most likely it's because of how objects are rendered in OpenGL. Depending on how you want to set up your rendering, OBJ files are very easy to read in and render in OpenGL. One reason they may have duplicate vertices is if you want to do interleaved vertex arrays, it may end up being easier to create the interleaved array from an OBJ text file with duplicated vertices than with non duplicated vertices, since calling glDrawArrays() with GL_TRIANGLES wants three vertices in a row that define a face and it doesn't use indices. The lack of indices for glDrawArrays makes it necessary to have duplicate vertices. Of course, there may be some sort of clever c++ trick that I'm not aware of that could get around this limitation, but I haven't figured it out yet anyways.
精彩评论