-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.cpp
82 lines (75 loc) · 1.95 KB
/
model.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
78
79
80
81
82
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "model.h"
Model::Model(const char *filename) : verts_(), faces_() {
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) 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 ")) {
iss >> trash;
Vec3f v;
for (int i=0;i<3;i++) iss >> v.raw[i];
verts_.push_back(v);
} else if (!line.compare(0, 2, "f ")) {
Face f;
Face uv;
int itrash, idx, idx2;
iss >> trash;
for(int i=0; i<3; i++) {
iss >> idx >> trash >> idx2 >> trash >> itrash;
idx--; // in wavefront obj all indices start at 1, not zero
idx2--;
f.idx[i] = idx;
uv.idx[i]= idx2;
}
faces_.push_back(f);
uv_faces_.push_back(uv);
}
else if(!line.compare(0, 4, "vt "))
{
iss >> trash >> trash;
Vec3f v;
for (int i=0;i<3;i++) iss >> v.raw[i];
uv_.push_back(v);
}
}
std::cerr << "# v# " << verts_.size()
<< "# vt# " << uv_.size()
<< " f# " << faces_.size() << std::endl;
}
Model::~Model() {
}
int Model::nverts() {
return (int)verts_.size();
}
int Model::nfaces() {
return (int)faces_.size();
}
const Face& Model::face(int idx) const {
return faces_[idx];
}
const Face& Model::uv_face(int idx) const {
return uv_faces_[idx];
}
Vec3f Model::vert(int i) {
return verts_[i];
}
Vec3f Model::uv(int i) {
return uv_[i];
}
void Model::load_texture(std::string filename){
texture_.read_tga_file(filename.c_str());
texture_.flip_vertically();
}
TGAColor Model::diffuse(int x, int y)
{
return texture_.get(x, y);
}