-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjReader.cpp
61 lines (56 loc) · 2.05 KB
/
ObjReader.cpp
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
#include "ObjReader.h"
#include "Model.h"
#include "Utilities.h"
#include <fstream>
#include <iostream>
void ObjReader::readVerticesObj(Model* target, std::string objFileLoc)
{
std::vector<float> positions;
std::vector<float> textureCoords;
std::vector<float> normals;
std::string read = Utilities::getFileContent(objFileLoc);
int vex = 0;
for(std::string lineString : Utilities::splitString(&read, 0, '\n'))
{
//vertices
if (lineString.substr(0, 2) == "v ")
{
Utilities::addValues(&positions, 2, &lineString);
}
//texture coords
else if (lineString.substr(0, 2) == "vt")
{
Utilities::addValues(&textureCoords, 3, &lineString);
}
//normals
else if (lineString.substr(0, 2) == "vn")
{
Utilities::addValues(&normals, 3, &lineString);
}
//faces
else if (lineString.substr(0, 2) == "f ")
{
//example split: 5/1/1 3/2/1 1/3/1 - > 5/1/1
std::vector<std::string> triangles = Utilities::splitString(&lineString, 2, ' ');
target->indexData.push_back(vex + 2); //blender starts indexing with 1 not 0
target->indexData.push_back(vex + 1);
target->indexData.push_back(vex);
vex += 3;
for (std::string tri : triangles)
{
//example split: 5/1/1 - > 5
std::vector<int> faces = Utilities::splitStringInts(&tri, 0, '/');
int i = faces[0] - 1;
int j = faces[1] - 1;
int k = faces[2] - 1;
Model::Vertex newVer
{
positions[3 * i], positions[3 * i + 1], positions[3 * i + 2],
normals[3 * k], normals[3 * k + 1], normals[3 * k + 2],
textureCoords[2 * j], textureCoords[2 * j + 1]
};
target->vertexData.push_back(newVer);
}
}
}
}