ProtoTank/Engine/World/WorldObject.tpp

69 lines
2.2 KiB
C++

#include "WorldObject.hpp"
inline WorldObject::~WorldObject() {}
template<class D>
inline WorldObjectAbstract<D>::~WorldObjectAbstract() {}
template<class D>
void WorldObjectAbstract<D>::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<tinyobj::shape_t>& 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]));
// 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++) {
subpart.indices.push_back(shapes[s].mesh.indices[index_offset + v].vertex_index);
// Check if `normal_index` is zero or positive. negative = no normal data
//if (idx.normal_index >= 0) {}
// 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();
}