20 std::vector<float>& normals) {
21 std::ifstream file(fileName, std::ios_base::in);
23 std::vector<std::array<float, 3>> verts;
24 std::vector<std::array<float, 3>> vns;
25 std::vector<std::array<std::array<int, 2>, 3>> faces;
28 std::getline(file, line);
29 std::istringstream iss(line);
34 if (type[0] ==
'#' || type[0] ==
'o') {
36 }
else if (type ==
"v") {
40 verts.push_back({v0, v1, v2});
41 }
else if (type ==
"vn") {
45 vns.push_back({v0, v1, v2});
46 }
else if (type ==
"f") {
47 const char* linePtr = line.c_str() + 2;
50 std::array<std::array<int, 2>, 3> face;
51 for (
int i = 0; i < 3; i++) {
52 sscanf(linePtr,
"%d//%d", &v, &n);
55 while (linePtr[0] !=
' ' && linePtr[0] != 0) ++linePtr;
56 while (linePtr[0] ==
' ') ++linePtr;
58 faces.push_back(face);
60 printf(
"ERROR bad obj line %s\n", line.c_str());
64 positions.resize(9 * faces.size());
65 normals.resize(9 * faces.size());
68 for (uint64_t i = 0; i < faces.size(); i++) {
69 for (uint64_t j = 0; j < 3; j++) {
70 for (uint64_t k = 0; k < 3; k++) {
71 positions[c] = verts[faces[i][j][0]][k];
72 normals[c++] = vns[faces[i][j][1]][k];
void load_obj_file(std::string fileName, std::vector< float > &positions, std::vector< float > &normals)
Utility to load .obj files, containing 3D models of robots.