{"id":79,"date":"2013-11-08T15:46:23","date_gmt":"2013-11-08T15:46:23","guid":{"rendered":"\/\/3dbym.ru\/2013\/11\/loading-the-obj-format\/"},"modified":"2013-11-08T15:46:23","modified_gmt":"2013-11-08T15:46:23","slug":"loading-the-obj-format","status":"publish","type":"post","link":"https:\/\/3dbym.ru\/2013\/11\/loading-the-obj-format\/","title":{"rendered":"Loading the OBJ Format"},"content":{"rendered":"
Because the OBJ does not contain any sort of header, you need some sort of resizable array to hold your vertices, faces, and the other com\u00adponents. In the implementation shown here, everything is loaded into an STL vector, a kind of resizable array.<\/p>\n
Each data type has its own structure. Vector3 contains two floats and is used to hold a single vertex position, or vertex normal. Vector2 is a lot like its bigger brother, but holds only two values, perfect for texture coordi\u00adnates. Last of all is SObjFace, which contains 12 unsigned integers, three for each vertex indexes, texture coord indexes, and normal indexes. Even if all the face variables are not always used, the storage is still there.<\/p>\n
Now you are ready to read in the file. The best way to do this is read in one line at a time, check the prefix, and extract the rest of the values using the sscanf function. Each type of value (vertex data, vertex normal, texture coordinate, and face) has its own arrays to hold it. If the loader finds a line that starts with something that does not signify a recognized chunk, it simply reads the line and discards it.<\/p>\n
In the CObj class, there are two Boolean values: m_bHasTexCoords and m_bHasNormals. These variables are set to true if a texture coord (for m_bHasTexCoords) or vertex normal line (for m_bHasNormals) is found in the file. Although this does work, it has a few flaws. If for some reason the faces do not come after all the vertex info, the loader will not know what type of face to read and will default to reading vertices only.<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
Because the OBJ does not contain any sort of header, you need some sort of resizable array to hold your vertices, faces, and the other com\u00adponents. In the implementation shown here, everything is loaded into an STL vector, a kind of resizable array. Each data type has its own structure. Vector3 contains two floats and […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts\/79"}],"collection":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":0,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}