| 98 | } |
| 99 | |
| 100 | bool OBJModel::read(const std::string& fileName) { |
| 101 | faces.clear(); |
| 102 | vertList.clear(); |
| 103 | normList.clear(); |
| 104 | uvList.clear(); |
| 105 | |
| 106 | std::ifstream ifs(fileName.c_str(), std::ios::in); |
| 107 | std::string line; |
| 108 | while (ifs.good() && !ifs.eof()) { |
| 109 | std::getline(ifs, line); |
| 110 | if (line.size()) { |
| 111 | // parse it |
| 112 | switch (line[0]) { |
| 113 | case 'v': |
| 114 | if (line.size() > 5) { // there have to be enough characters for a full vert |
| 115 | OBJVert v; |
| 116 | |
| 117 | switch (line[1]) { |
| 118 | case 'n': |
| 119 | v.read3(line.c_str() + 2); |
| 120 | normList.push_back(v); |
| 121 | break; |
| 122 | |
| 123 | case 't': |
| 124 | v.read2(line.c_str() + 2); |
| 125 | uvList.push_back(v); |
| 126 | break; |
| 127 | |
| 128 | default: |
| 129 | v.read3(line.c_str() + 1); |
| 130 | vertList.push_back(v); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | break; |
| 135 | |
| 136 | case 'f': { |
| 137 | std::vector<std::string> verts = TextUtils::tokenize(std::string(line.c_str() + 2), std::string(" ")); |
| 138 | OBJFace face; |
| 139 | for (size_t v = 0; v < verts.size(); v++) { |
| 140 | std::vector<std::string> indexes = TextUtils::tokenize(verts[v], std::string("/")); |
| 141 | if (indexes.size()) { |
| 142 | face.verts.push_back(atoi(indexes[0].c_str()) - 1); |
| 143 | |
| 144 | if (indexes.size() > 1 && indexes[1].size()) { |
| 145 | face.uvs.push_back(atoi(indexes[1].c_str()) - 1); |
| 146 | } |
| 147 | else { |
| 148 | face.uvs.push_back(0); |
| 149 | } |
| 150 | |
| 151 | if (indexes.size() > 2 && indexes[2].size()) { |
| 152 | face.norms.push_back(atoi(indexes[2].c_str()) - 1); |
| 153 | } |
| 154 | else { |
| 155 | face.norms.push_back(0); |
| 156 | } |
| 157 | } |
no test coverage detected