#define GLM_ENABLE_EXPERIMENTAL #include #include #include #include #include #include #include #include "game.h" #include "tiny_obj_loader.h" namespace std { template<> struct hash { size_t operator()(vertex const& v) const { return hash()(v.pos) ^ (hash()(v.tex_coord) << 1); } }; } int loadModel(std::vector &vertices, std::vector &indices, const char *filename){ tinyobj::attrib_t attrib; std::vector shapes; std::vector materials; std::string warn, err; if(!tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename)){ printf("Loading failed! %s\n", err.c_str()); return 1; } std::unordered_map uniqueVertices = {}; for(const auto& shape : shapes){ for(const auto& index : shape.mesh.indices){ vertex new_vertex = {}; new_vertex.pos = { attrib.vertices[3 * index.vertex_index + 0], attrib.vertices[3 * index.vertex_index + 1], attrib.vertices[3 * index.vertex_index + 2] }; new_vertex.tex_coord = { attrib.texcoords[2 * index.texcoord_index + 0], 1.0f - attrib.texcoords[2 * index.texcoord_index + 1] }; if(uniqueVertices.count(new_vertex) == 0){ uniqueVertices[new_vertex] = (uint32_t)vertices.size(); vertices.push_back(new_vertex); } indices.push_back(uniqueVertices[new_vertex]); } } return 0; }