-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
123 lines (115 loc) · 3.77 KB
/
model.cpp
File metadata and controls
123 lines (115 loc) · 3.77 KB
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
117
118
119
120
121
122
123
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "model.h"
#include "my_gl.h"
Model::Model(const std::string& filename, Vector3 position, Quaternion rotation, Vector3 scale,float transparency) : position_(position), rotation_(rotation), scale_(scale),transparency_(transparency) {
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) { std::cout << ":("; return;}
else {std::cout << "opened successfully\n";}
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;
float Vx,Vy,Vz;
iss >> Vx >> Vy >> Vz;
Vector3 v(Vx, Vy, Vz);
verts_.push_back(v);
} else if (!line.compare(0, 3, "vn ")) {
iss >> trash >> trash;
Vector3 n;
iss >> n.x >> n.y >> n.z;
normals_.push_back(n);
}else if (!line.compare(0, 3, "vt ")) {
iss >> trash >> trash;
Vector2 uv;
iss >> uv.x >> uv.y;
uv_coords_.push_back(uv);
}
else if (!line.compare(0, 2, "f ")) {
polygon f;
int v, uv, n, i = 0;
iss >> trash;
while (iss >> v >> trash >> uv >> trash >> n) {
v--;
uv--;
n--;
f.verts[i] = v;
f.uv_coords[i] = uv;
f.normals[i] = n;
i++;
}
faces_.push_back(f);
}
}
in.close();
load_texture(filename, "_diffuse.tga", diffuse_map_);
load_texture(filename, "_nm.tga", normal_map_)? DoesHaveNMap = true : DoesHaveNMap = false ;
load_texture(filename, "_spec.tga", specular_map_)? DoesHaveSpecMap = true : DoesHaveSpecMap = false ;
std::cerr << "# v# " << verts_.size() << " f# " << faces_.size() << std::endl;
}
Model::~Model() = default;
// number of verts
int Model::nverts() const {
return (int)verts_.size();
}
// number of faces
int Model::nfaces() const {
return (int)faces_.size();
}
// face
polygon Model::face(int idx) {
return faces_[idx];
}
// array of verts
Vector3 Model::vert(int i) {
return verts_[i];
}
Vector3 Model::normal(int i) {
normals_[i].Normalize();
return normals_[i];
}
Vector3 Model::normal1(Vector2 UV) {
TGAColor c = normal_map_.get(int(UV.x*normal_map_.get_width()), int(UV.y*normal_map_.get_height()));
Vector3 res;
res.x = (float)c.bgra[2]/255.0f*2.f-1.f;
res.y = (float)c.bgra[1]/255.0f*2.f-1.f;
res.z = (float)c.bgra[0]/255.0f*2.f-1.f;
// std::cout << (int)c[0] << " " << (int)c[1] << " " << (int)c[2] << std::endl;
return res;
}
Vector2 Model::uv_coords(int i) {
return {uv_coords_[i].x, uv_coords_[i].y};
}
TGAColor Model::diffuse_color(Vector2 uv) {
Vector2 uv1(uv.x*diffuse_map_.get_width(), uv.y*diffuse_map_.get_height());
return diffuse_map_.get(uv1.x, uv1.y);
}
float Model::specular(Vector2 uv) {
return specular_map_.get(int(uv.x*specular_map_.get_width()),int(uv.y*specular_map_.get_height()))[0];
}
bool Model::load_texture(const std::string &filename, std::string suffix, TGAImage& img) {
bool isOk = false;
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::cout << "texture file " << texfile << " loading ";
if (img.read_tga_file(texfile.c_str())) {
std::cout << "ok" << std::endl;
isOk = true;
}
else {
std::cout << "failed" << std::endl;
isOk = false;
}
img.flip_vertically();
return isOk;
}
}