-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloth_handler.cpp
More file actions
231 lines (195 loc) · 7.57 KB
/
cloth_handler.cpp
File metadata and controls
231 lines (195 loc) · 7.57 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "cloth_handler.h"
#include <glm/vec3.hpp>
#include "spring.h"
ClothHandler::ClothHandler(glm::vec3 positions[Global::cloth_rows][Global::cloth_cols], float masses[Global::cloth_rows][Global::cloth_cols],
float spring_stiffness, float spring_rest_len, glm::vec3 pos){
this->cloth_position = pos;
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->cloth_vertices[i][j] = ClothVertex(positions[i][j], masses[i][j]);
}
}
// v00 v01
// v01 v02
// ...
// v07 v08
// v08 v09
// v10 v11
// v11 v12
// ...
// v17 v18
// v18 v19
// v20 v21
// v21 v22
// ...
// ...
// ...
// v97 v98
// v98 v99
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols-1; j++){
horizontal_structural_springs[i][j] = Spring(spring_stiffness, spring_rest_len, &this->cloth_vertices[i][j], &this->cloth_vertices[i][j+1]);
}
}
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols-3; j++){
horizontal_bend_springs[i][j] = Spring(0.05 * spring_stiffness, 3 * spring_rest_len, &this->cloth_vertices[i][j], &this->cloth_vertices[i][j+3]);
}
}
// v00 v10
// v01 v11
// ...
// v08 v18
// v09 v19
// v10 v20
// v11 v21
// ...
// v18 v28
// v19 v29
// v20 v30
// ...
// ...
// ...
// v88 v98
// v89 v99
for(int i = 0; i < Global::cloth_rows-1; i++){
for(int j = 0; j < Global::cloth_cols; j++){
vertical_structural_springs[i][j] = Spring(spring_stiffness, spring_rest_len, &this->cloth_vertices[i][j], &this->cloth_vertices[i+1][j]);
}
}
for(int i = 0; i < Global::cloth_rows-3; i++){
for(int j = 0; j < Global::cloth_cols; j++){
vertical_bend_springs[i][j] = Spring(0.05 * spring_stiffness, 3 * spring_rest_len, &this->cloth_vertices[i][j], &this->cloth_vertices[i+3][j]);
}
}
// v00 v11
// v01 v12
// v02 v13
// ...
// v88 v99
//
// v10 v01
// v11 v02
// v12 v03
// ...
// v98 v89
//
for(int i = 0; i < Global::cloth_rows-1; i++){
for(int j = 0; j < Global::cloth_cols-1; j++){
falling_shear_springs[i][j] = Spring(0.1 * spring_stiffness, spring_rest_len * sqrt(2.0), &this->cloth_vertices[i][j], &this->cloth_vertices[i+1][j+1]);
rising_shear_springs[i][j] = Spring(0.1 * spring_stiffness, spring_rest_len * sqrt(2.0), &this->cloth_vertices[i+1][j], &this->cloth_vertices[i][j+1]);
}
}
}
void ClothHandler::CalculateVertexNextPos(float delta_t){
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->cloth_vertices[i][j].ResetForce();
}
}
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols-1; j++){
this->horizontal_structural_springs[i][j].AddForce(delta_t);
}
}
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols-3; j++){
this->horizontal_bend_springs[i][j].AddForce(delta_t);
}
}
for(int i = 0; i < Global::cloth_rows-1; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->vertical_structural_springs[i][j].AddForce(delta_t);
}
}
for(int i = 0; i < Global::cloth_rows-3; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->vertical_bend_springs[i][j].AddForce(delta_t);
}
}
for(int i = 0; i < Global::cloth_rows-1; i++){
for(int j = 0; j < Global::cloth_cols-1; j++){
this->falling_shear_springs[i][j].AddForce(delta_t);
this->rising_shear_springs[i][j].AddForce(delta_t);
}
}
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->cloth_vertices[i][j].CalculateNextPos(delta_t);
}
}
}
void ClothHandler::UpdateVertexPositions(){
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->cloth_vertices[i][j].UpdatePos();
}
}
}
void ClothHandler::UpdateVertexNormals(){
glm::vec3 v1;
glm::vec3 v2;
glm::vec3 normal;
for(int i = 1; i < Global::cloth_rows - 1; i++){
for(int j = 1; j < Global::cloth_cols - 1; j++){
glm::vec3 curr_ver_pos = this->cloth_vertices[i][j].position;
v1 = this->cloth_vertices[i-1][j].position - curr_ver_pos;
v2 = this->cloth_vertices[i-1][j-1].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i-1][j].normal += normal;
this->cloth_vertices[i-1][j-1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i][j-1].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i][j-1].normal += normal;
this->cloth_vertices[i-1][j-1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i+1][j-1].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i][j-1].normal += normal;
this->cloth_vertices[i+1][j-1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i+1][j].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i+1][j].normal += normal;
this->cloth_vertices[i+1][j-1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i+1][j+1].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i+1][j].normal += normal;
this->cloth_vertices[i+1][j+1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i][j+1].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i][j+1].normal += normal;
this->cloth_vertices[i+1][j+1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i-1][j+1].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i][j+1].normal += normal;
this->cloth_vertices[i-1][j+1].normal += normal;
v1 = v2;
v2 = this->cloth_vertices[i-1][j].position - curr_ver_pos;
normal = glm::normalize(glm::cross(v1, v2));
this->cloth_vertices[i][j].normal += normal;
this->cloth_vertices[i-1][j].normal += normal;
this->cloth_vertices[i-1][j+1].normal += normal;
}
}
for(int i = 0; i < Global::cloth_rows; i++){
for(int j = 0; j < Global::cloth_cols; j++){
this->cloth_vertices[i][j].normal = glm::normalize(this->cloth_vertices[i][j].normal);
}
}
}
void ClothHandler::PinVertices(glm::vec2 v1, glm::vec2 v2){
this->cloth_vertices[(int)v1.x][(int)v1.y].is_pinned = true;
this->cloth_vertices[(int)v2.x][(int)v2.y].is_pinned = true;
}