-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cpp
77 lines (67 loc) · 1.77 KB
/
object.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <sstream>
#include <fstream>
#include "object.h"
//https://www.andrewnoske.com/wiki/OBJ_file_format#Example_1a:_A_square_facing_forwards
//https://github.com/ssloy/tinyrenderer/blob/master/model.cpp
Object::Object(const std::string filename)
{
std::ifstream in;
in.open(filename, std::ifstream::in);
if (in.fail()) {
std::cout << "ERROR: Failed to open object file:" << filename << std::endl;
return;
}
std::string line;
while (!in.eof()) {
std::getline(in, line);
std::istringstream iss(line.c_str());
char trash;
if (!line.compare(0, 2, "v ")) { // verts
iss >> trash;
vec3 v;
iss >> v.x;
iss >> v.y;
iss >> v.z;
verts_.push_back(v);
} else if (!line.compare(0, 2, "f ")) { // faces
iss >> trash;
int v;
poly p;
while (iss >> v) {
p.verts.push_back(v-1);
}
p.vert_count = p.verts.size();
polys_.push_back(p);
}
}
in.close();
}
int Object::PolyCount()
{
return polys_.size();
}
std::vector<poly> Object::Polys()
{
return polys_;
}
void Object::PrintVerts()
{
std::cout << "---VERTS---" << std::endl;
for (int i = 0; i < verts_.size(); i++) {
std::cout << i << ": ";
std::cout << verts_[i].x << ", ";
std::cout << verts_[i].y << ", ";
std::cout << verts_[i].z << std::endl;
}
}
void Object::PrintPolys()
{
std::cout << "---POLYS---" << std::endl;
for (int i = 0; i < polys_.size(); i++) {
std::cout << i << ": ";
for (int j = 0; j < polys_[i].verts.size(); j++) {
std::cout << polys_[i].verts[j] << " ";
}
}
}