-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavefrontreader.cc
110 lines (101 loc) · 3.57 KB
/
wavefrontreader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "wavefrontreader.h"
#include <array>
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
WaveFrontReader::WaveFrontReader(std::string filename) : m_filename(filename) {}
void WaveFrontReader::readVertices(Mesh &obj) {
std::ifstream myfile(m_filename);
std::string line;
uint64_t vertexIndex{0};
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> textureCoords;
std::vector<glm::vec3> normals;
std::unordered_map<uint64_t, uint64_t> faces;
if (myfile.is_open()) {
while (getline(myfile, line)) {
auto vpos = line.find("v ");
if (vpos == 0) {
std::stringstream conv{line.substr(1)};
float x, y, z;
conv >> x >> y >> z;
std::cout << "v x " << x << " y " << y << " z " << z << std::endl;
vertices.push_back(glm::vec3(x, y, z));
continue;
}
vpos = line.find("vt ");
if (vpos == 0) {
std::stringstream conv{line.substr(2)};
float x, y;
conv >> x >> y;
std::cout << "vt x " << x << " y " << y << std::endl;
textureCoords.push_back(glm::vec2(x, y));
continue;
}
vpos = line.find("vn ");
if (vpos == 0) {
std::stringstream conv{line.substr(2)};
float x, y, z;
conv >> x >> y >> z;
std::cout << "vn x " << x << " y " << y << std::endl;
normals.push_back(glm::vec3(x, y, z));
continue;
}
vpos = line.find("f ");
if (vpos == 0) {
// f 1/2/3 1/2/3 13/1/2
// f 11/108/34 13/109/34 7/106/34
std::istringstream splitter{line.substr(2)};
std::cout << line << std::endl;
std::string face;
Vertex vert;
while (std::getline(splitter, face, ' ')) {
uint64_t indexV{0}, indexVt{0}, indexVn{0};
std::cout << face << std::endl;
auto start_pos = face.find('/'); // get index for vertex
indexV = std::stoul(face.substr(0, start_pos), nullptr);
vert.Coord = vertices[indexV - 1];
auto end_pos = face.find('/', start_pos + 1); // get index for texture
if (start_pos != end_pos - 1) {
indexVt = std::stoul(
face.substr(start_pos + 1, end_pos - (start_pos + 1)), nullptr);
obj.texture_indicies.push_back((uint32_t)indexVt - 1);
vert.TextureCoords = textureCoords[indexVt - 1];
}
if (start_pos != end_pos - 1) { // get index for normal
indexVn = std::stoul(face.substr(end_pos + 1), nullptr);
obj.normal_indicies.push_back((uint32_t)indexVn - 1);
vert.Normal = normals[indexVn - 1];
}
uint64_t faceKey = indexV | indexVt << 16;
auto foundPos = faces.find(faceKey);
if (foundPos != faces.end()) {
obj.indicies.push_back(faces[faceKey]);
} else {
faces[faceKey] = vertexIndex;
obj.vertices.push_back(vert);
obj.indicies.push_back(vertexIndex);
vertexIndex++;
}
}
}
}
for (const auto &vert : obj.vertices) {
std::cout << "v x " << vert.Coord.x << " y " << vert.Coord.y << " z "
<< vert.Coord.z << std::endl;
}
for (const auto &index : obj.indicies) {
std::cout << "index " << index << std::endl;
}
// for (auto pair : faces) {
// std::cout << " faceindex " << pair.second << std::endl;
// }
obj.indicies.shrink_to_fit();
obj.vertices.shrink_to_fit();
myfile.close();
} else
std::cout << "Unable to open file";
}