-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
executable file
·133 lines (109 loc) · 4.16 KB
/
Model.cpp
File metadata and controls
executable file
·133 lines (109 loc) · 4.16 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
124
125
126
127
128
129
130
131
132
#include "Model.h"
void Model::resetVars(){
this->model_x = 0;
this->model_y = 0;
this->model_z = 0;
this->model_rotx = 0;
this->model_roty = 0;
this->model_rotz = 0;
}
void Model::saveModel(){
std::ofstream saveObj;
saveObj.open("output.obj");
// go through each vertice and push it to file 'output.obj'
for (unsigned int h = 0; h < this->vertex_list.size(); h += 3){
saveObj << "v " << this->vertex_list.at(h) << " " <<
this->vertex_list.at(h+1) << " " << this->vertex_list.at(h+2) << std::endl;
}
// go through each 's' polygon and push it to file 'output.obj' (s_list contains a list of strings)
for (unsigned int h =0; h < this->s_list.size(); h++){
saveObj << this->s_list.at(h) << std::endl;
}
}
void Model::calculateVertex(){
this->mean_x = 0;
this->mean_y = 0;
this->mean_z = 0;
for (int q = 0; q < (this->vertex_list.size() / 3) ; q++){
//cout << model_object.vertex_list.at(q) << endl;
this->mean_x += this->vertex_list.at(3*q);
}
for (int q = 0; q < (this->vertex_list.size() / 3) ; q++){
//cout << model_object.vertex_list.at(q) << endl;
this->mean_y += this->vertex_list.at(3*q + 1);
}
for (int q = 0; q < (this->vertex_list.size() / 3) ; q++){
//cout << model_object.vertex_list.at(q) << endl;
this->mean_z += this->vertex_list.at(3*q+2);
}
this->mean_x = this->mean_x / (this->vertex_list.size() / 3);
this->mean_y = this->mean_y / (this->vertex_list.size() / 3);
this->mean_z = this->mean_z / (this->vertex_list.size() / 3);
}
void Model::getMaxMins(){
this->max_x = this->vertex_list.at(0);
for (int q = 0; q < ( this->vertex_list.size() / 3 ); q++){
if (this->vertex_list.at(3*q) > this->max_x){
this->max_x = this->vertex_list.at(3*q);
}
}
this->min_x = this->vertex_list.at(0);
for (int q = 0; q < ( this->vertex_list.size() / 3 ); q++){
if (this->vertex_list.at(3*q) < this->min_x){
this->min_x = this->vertex_list.at(3*q);
}
}
this->max_y = this->vertex_list.at(1);
for (int q = 0; q < ( this->vertex_list.size() / 3 ); q++){
if (this->vertex_list.at(3*q + 1) > this->max_y){
this->max_y = this->vertex_list.at(3*q + 1);
}
}
this->min_y = this->vertex_list.at(1);
for (int q = 0; q < ( this->vertex_list.size() / 3 ); q++){
if (this->vertex_list.at(3*q + 1) < this->min_y){
this->min_y = this->vertex_list.at(3*q + 1);
}
}
this->max_z = this->vertex_list.at(2);
for (int q = 0; q < ( this->vertex_list.size() / 3 ); q++){
if (this->vertex_list.at(3*q + 2) > this->max_z){
this->max_z = this->vertex_list.at(3*q + 2);
}
}
this->min_z = this->vertex_list.at(2);
for (int q = 0; q < ( this->vertex_list.size() / 3 ); q++){
if (this->vertex_list.at(3*q + 2) < this->min_z){
this->min_z = this->vertex_list.at(3*q + 2);
}
}
float scale_x = this->max_x - this->min_x;
float scale_y = this->max_y - this->min_y;
float scale_z = this->max_z - this->min_z;
this->max_scale = scale_x; //get the max scale
if (scale_y > this->max_scale) //the scale is max(xmax - xmin, ymax -ymin, zmax -zmin)
this->max_scale = scale_y;
if (scale_z > this->max_scale)
this->max_scale = scale_z;
}
void Model::applyTransfToMatrix(){
this->calculateVertex();
this->getMaxMins();
// translate all vertices by substracting them by their means
for (unsigned int h = 0; h < ( this->vertex_list.size() / 3); h++){ // substract each x of each vertice by mean x
this->vertex_list.at(3*h) = this->vertex_list.at(3*h) - this->mean_x;
}
for (unsigned int h = 0; h < ( this->vertex_list.size() / 3); h++){
this->vertex_list.at(3*h + 1) = this->vertex_list.at(3*h + 1) - this->mean_y;
}
for (unsigned int h = 0; h < ( this->vertex_list.size() / 3); h++){
this->vertex_list.at(3*h + 2) = this->vertex_list.at(3*h + 2) - this->mean_z;
}
//calculateVertex();
for (unsigned int h = 0; h < this->vertex_list.size() ; h++){ // scale every coordinate in the vector object
this->vertex_list.at(h) = this->vertex_list.at(h) / this->max_scale;
}
for (unsigned int h = 0; h < (this->vertex_list.size() / 3); h++){ // this is a translation of (0,0,-2)
this->vertex_list.at(3*h + 2) -= 2; // equivalent to glTranslatef(0,0,-2) but applied
}
}