nem0 / OpenFBX

Lightweight open source FBX importer
MIT License
1.15k stars 135 forks source link

Add samples on how to parse fbx files into 3D mesh #63

Closed GCourtney27 closed 3 years ago

GCourtney27 commented 3 years ago

Adding a sample inside the project that shows how to parse the information obtained by OpenFBX would be useful instead of showing how to make an OBJ file from the FBX file in main. By following the OBJ->FBX sample code inside main.cpp I am getting these results: image Obviously, it is an indexing problem (on my end) but I can't figure out how to index the data provided without a sample of some kind to follow.

nem0 commented 3 years ago

Hi, you can see real world example here https://github.com/nem0/LumixEngine/blob/master/src/renderer/editor/fbx_importer.cpp#L847 geometry is processed here https://github.com/nem0/LumixEngine/blob/master/src/renderer/editor/fbx_importer.cpp#L624

GCourtney27 commented 3 years ago

Thanks for that. It's an impressive example but pretty complicated to follow. I'd just like to import a simple static mesh with no skinning or culling structures applied.

nem0 commented 3 years ago

I'm sorry, I don't have simpler example. For simple mesh, you just have to use vertices from ofbx::Geometry::getVertices and indices from getFaceIndices. Only catch is that if you don't use ofbx::LoadFlags::TRIANGULATE, negative indices signals end of polygon. That's how it's stored directly in FBX file and without triangulation ofbx just provides raw fbx data. See https://github.com/nem0/OpenFBX/blob/master/demo/main.cpp#L280

GCourtney27 commented 3 years ago

Oh okay, I tried writing the loop again and apparently in fbx if the index is negative that means it is the last index in the polygon (like you said) you need to make it positive then subtract one from it. In the sample code it was either make it positive or add one. So I replaced everything inside the indicies for loop with idx = faceIndices[i]; if (idx < 0) idx = -(idx + 1); then add the index to the list of indices and continue. That fixed the issue: image Thanks!