#include "WorldObject.hpp" inline WorldObject::~WorldObject() {} template inline WorldObjectAbstract::~WorldObjectAbstract() {} template void WorldObjectAbstract::LoadMeshFromObjFile(std::string file) { tinyobj::ObjReader reader; tinyobj::ObjReaderConfig reader_config; reader_config.mtl_search_path = "./"; reader_config.triangulate = false; reader_config.vertex_color = false; if (!reader.ParseFromFile(file, reader_config)) throw new std::runtime_error("Obj loader failure"); const tinyobj::attrib_t& attrib = reader.GetAttrib(); const std::vector& shapes = reader.GetShapes(); try { mMesh.vertices.reserve(attrib.vertices.size()); } catch (const std::length_error& ex) { throw ex; } // Reinterprete vertices to our format for (size_t i = 0; i < attrib.vertices.size(); i += 3) mMesh.vertices.push_back(Vertex(attrib.vertices[i], attrib.vertices[i+1], attrib.vertices[i+2])); // Reinterprete normales to our format for (size_t n = 0; n < attrib.normals.size(); n += 3) mMesh.normales.push_back(M3D_F3(attrib.normals[n], attrib.normals[n+1], attrib.normals[n+2])); // Loop over shapes/subparts for (size_t s = 0; s < shapes.size(); s++) { MeshPart subpart; // Loop over faces(polygon) size_t index_offset = 0; for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]); try { subpart.indices.reserve(shapes[s].mesh.indices.size()); } catch (const std::length_error& ex) { throw ex; } // Loop over indices in the face. for (size_t v = 0; v < fv; v++) { tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; subpart.indices.push_back(idx.vertex_index); // Check if `normal_index` is zero or positive. negative = no normal data if (idx.normal_index >= 0) mMesh.vertices[idx.vertex_index].norm_index = idx.normal_index; // Check if `texcoord_index` is zero or positive. negative = no texcoord data //if (idx.texcoord_index >= 0) {} } index_offset += fv; // per-face material //shapes[s].mesh.material_ids[f]; } mMesh.parts.push_back(subpart); } // Refresh AABB bounds UpdateAABBFromMesh(); }