-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.cpp
executable file
·116 lines (107 loc) · 2.52 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
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
111
112
113
114
115
116
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <QColor>
#include <QMessageBox>
#include "model.h"
Model::Model(const char *filename) : verts_(), faces_()
{
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail())
{
QMessageBox::critical(0, "Error", "Couldn't load model!");
exit(1);
}
std::string line;
while (!in.eof())
{
std::getline(in, line);
std::istringstream iss(line.c_str());
//std::cout << iss << '\n';
char trash;
if (!line.compare(0, 2, "v "))
{
iss >> trash;
std::cout << trash << "E\n";
v3df v;
for (int i=0;i<3;i++)
{
iss >> v[i];
std::cout << v[i] << "E\n";
}
verts_.push_back(v);
}
else if (!line.compare(0, 3, "vn "))
{
iss >> trash >> trash;
v3df n;
for (int i=0;i<3;i++) iss >> n[i];
norms_.push_back(n);
}
else if (!line.compare(0, 3, "vt "))
{
iss >> trash >> trash;
v2df uv;
for (int i=0;i<2;i++) iss >> uv[i];
uv_.push_back(uv);
}
else if (!line.compare(0, 2, "f "))
{
std::vector<v3di> f;
v3di tmp;
iss >> trash;
while (iss >> tmp[0] >> trash >> tmp[1] >> trash >> tmp[2])
{
for (int i=0; i<3; i++) tmp[i]--; // in wavefront obj all indices start at 1, not zero
f.push_back(tmp);
}
faces_.push_back(f);
}
}
load_texture(filename, "_diffuse.tga", diffusemap_);
}
Model::~Model() {}
int Model::nverts()
{
return (int)verts_.size();
}
int Model::nfaces()
{
return (int)faces_.size();
}
std::vector<int> Model::face(int idx)
{
//return faces_[idx];
std::vector<int> face;
for (int i=0; i<(int)faces_[idx].size(); i++)
face.push_back(faces_[idx][i][0]);
return face;
}
v3df Model::vert(int i)
{
return verts_[i];
}
void Model::load_texture(std::string filename, const char *suffix, TGAImage &img)
{
std::string texfile(filename);
size_t dot = texfile.find_last_of(".");
if (dot!=std::string::npos)
{
texfile = texfile.substr(0,dot) + std::string(suffix);
std::cerr << "texture file " << texfile << " loading " << (img.read_tga_file(texfile.c_str()) ? "ok" : "failed") << std::endl;
img.flip_vertically();
}
}
QRgb Model::diffuse(v2di uv)
{
TGAColor getdiffuse = diffusemap_.get(uv.x, uv.y);
//
return qRgb(getdiffuse.r, getdiffuse.g, getdiffuse.b);
}
v2di Model::uv(int iface, int nvert) {
int idx = faces_[iface][nvert][1];
return v2di(uv_[idx].x*diffusemap_.get_width(), uv_[idx].y*diffusemap_.get_height());
}