Cheetah Software  1.0
obj_loader.h File Reference

Utility to load .obj files, containing 3D models of robots. More...

#include <string>
#include <vector>
+ Include dependency graph for obj_loader.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void load_obj_file (std::string fileName, std::vector< float > &positions, std::vector< float > &normals)
 

Detailed Description

Utility to load .obj files, containing 3D models of robots.

Definition in file obj_loader.h.

Function Documentation

void load_obj_file ( std::string  fileName,
std::vector< float > &  positions,
std::vector< float > &  normals 
)

Definition at line 19 of file obj_loader.cpp.

20  {
21  std::ifstream file(fileName, std::ios_base::in);
22  std::string line;
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;
26 
27  while (!file.eof()) {
28  std::getline(file, line);
29  std::istringstream iss(line);
30  std::string type;
31  iss >> type;
32  float v0, v1, v2;
33 
34  if (type[0] == '#' || type[0] == 'o') {
35  continue;
36  } else if (type == "v") {
37  iss >> v0;
38  iss >> v1;
39  iss >> v2;
40  verts.push_back({v0, v1, v2});
41  } else if (type == "vn") {
42  iss >> v0;
43  iss >> v1;
44  iss >> v2;
45  vns.push_back({v0, v1, v2});
46  } else if (type == "f") {
47  const char* linePtr = line.c_str() + 2;
48  int v, n;
49 
50  std::array<std::array<int, 2>, 3> face;
51  for (int i = 0; i < 3; i++) {
52  sscanf(linePtr, "%d//%d", &v, &n);
53  face[i][0] = v - 1;
54  face[i][1] = n - 1;
55  while (linePtr[0] != ' ' && linePtr[0] != 0) ++linePtr;
56  while (linePtr[0] == ' ') ++linePtr;
57  }
58  faces.push_back(face);
59  } else {
60  printf("ERROR bad obj line %s\n", line.c_str());
61  }
62  }
63 
64  positions.resize(9 * faces.size());
65  normals.resize(9 * faces.size());
66 
67  uint64_t c = 0;
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];
73  }
74  }
75  }
76 }

+ Here is the caller graph for this function: