-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloth.cpp
278 lines (225 loc) · 8.39 KB
/
cloth.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include "cloth.h"
using namespace glm;
using namespace std;
Cloth::Cloth(const int x, const int z, int w, int h) : xParticles(x), zParticles(z), width(w), height(h) {
float height = 15.0f;
float distX = (float)width / (float)xParticles;
float distZ = (float)height / (float)zParticles;
float centerX = ((float)width - (float)distX) / 2.0f;
float centerZ = ((float)height - (float)distZ) / 2.0f;
for (float z = 0; z < zParticles; z += 1.0f) {
for (float x = 0; x < xParticles; x += 1.0f) {
vertices.push_back(vec3(x * distX - centerX, height, z * distZ - centerZ));
initPositions.push_back(vec3(x * distX - centerX, height, z * distZ - centerZ));
normals.push_back(vec3(0, 1, 0));
uvs.push_back(vec2(x / xParticles, z / zParticles));
forces.push_back(vec3(0));
velocities.push_back(vec3(0));
prevPositions.push_back(vertices[vertices.size() - 1]);
}
}
// Indexed rendering
for (int i = 0; i < xParticles - 1; i++) {
for (int j = 0; j < zParticles - 1; j++) {
// Triangle 1
indices.push_back(i * xParticles + j);
indices.push_back((i + 1) * xParticles + j);
indices.push_back(i * xParticles + (j + 1));
// Triangle 2
indices.push_back((i + 1) * xParticles + j);
indices.push_back((i + 1) * xParticles + (j + 1));
indices.push_back((i)*xParticles + (j + 1));
}
}
// for (int i = 0; i < indices.size(); i++) cout << indices.at(i) << endl;
// cout << indices.size() << endl;
// cout << vertices.size() << endl;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Vertices(Particles for the cloth)
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vec3), &vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vec3), 0);
glEnableVertexAttribArray(vPosition1);
// Normals
glGenBuffers(1, &NBO);
glBindBuffer(GL_ARRAY_BUFFER, NBO);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vec3), 0);
glEnableVertexAttribArray(vPosition2);
// UV's for texture
glGenBuffers(1, &UVBO);
glBindBuffer(GL_ARRAY_BUFFER, UVBO);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(vec2), &uvs[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vec2), 0);
glEnableVertexAttribArray(vPosition3);
restLengthX = distX;
restLengthZ = distZ;
restLengthXZ = sqrt(pow(distX, 2) + pow(distZ, 2));
}
Cloth::~Cloth() {}
int Cloth::getVertex(int direction, int vertex) {
if (directions.WEST == direction) {
return vertex - 1;
}
else if (directions.NORTHWEST == direction) {
return vertex + xParticles - 1;
}
else if (directions.NORTH == direction) {
return vertex + xParticles;
}
else if (directions.NORTHEAST == direction) {
return vertex + xParticles + 1;
}
else if (directions.EAST == direction) {
return vertex + 1;
}
else if (directions.SOUTHEAST == direction) {
return vertex - xParticles + 1;
}
else if (directions.SOUTH == direction) {
return vertex - xParticles;
}
else {
return vertex - xParticles - 1;
}
}
vec3 Cloth::getSpringForce(int direction, int vertex) {
float restLength = 0;
if (direction == directions.WEST || direction == directions.EAST)
restLength = restLengthX;
else if (direction == directions.NORTH || direction == directions.SOUTH)
restLength = restLengthZ;
else
restLength = restLengthXZ;
vec3 delta = vertices[vertex] - vertices[getVertex(direction, vertex)];
float currentLength = glm::length(delta); // distance
float diff = (currentLength - restLength);
return normalize(delta) * diff * springFactor;
}
void Cloth::springs(int v, vec3& spring, vector<vec3>& springDirections) {
if (v % xParticles != 0) { // WEST
vec3 force = getSpringForce(directions.WEST, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.WEST, v)]));
}
if (v < vertices.size() - xParticles) { // NORTH
if (v % xParticles != 0) {
vec3 force = getSpringForce(directions.NORTHWEST, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.NORTHWEST, v)]));
}
vec3 force = getSpringForce(directions.NORTH, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.NORTH, v)]));
if ((v + 1) % xParticles != 0) {
vec3 force = getSpringForce(directions.NORTHEAST, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.NORTHEAST, v)]));
}
}
if ((v + 1) % xParticles != 0) { // EAST
vec3 force = getSpringForce(directions.EAST, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.EAST, v)]));
}
if (v > xParticles - 1) { // SOUTH
if ((v + 1) % xParticles != 0) {
vec3 force = getSpringForce(directions.SOUTHEAST, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.SOUTHEAST, v)]));
}
vec3 force = getSpringForce(directions.SOUTH, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.SOUTH, v)]));
if (v % xParticles != 0) {
vec3 force = getSpringForce(directions.SOUTHWEST, v);
spring += force;
springDirections.push_back((vertices[v] - vertices[getVertex(directions.SOUTHWEST, v)]));
}
}
}
// Force equation from https://graphics.stanford.edu/~mdfisher/cloth.html#EquationsOfMotion
// F(v) = Mg + Fwind + Fairresistance - k*sum(x_current - x_rest)
void Cloth::Forces(GLFWwindow* window) {
for (int v = 0; v < vertices.size(); v++) {
vec3 wind = vec3(0, 0, 0);
vec3 spring = vec3(0, 0, 0);
vector<vec3> springDirections;
springs(v, spring, springDirections);
// Calculate normals
vec3 normal = vec3(0);
for (int i = 1; i < springDirections.size(); i++) {
normal += cross(springDirections[i], springDirections[i - 1]);
}
normals[v] = normalize(normal);
// Wind
wind.x = sin(vertices[v].x * vertices[v].y * glfwGetTime());
wind.y = cos(vertices[v].z * glfwGetTime());
wind.z = sin(cos(5 * vertices[v].x * vertices[v].y * vertices[v].z));
vec3 forceAirResistance = vec3(0);
// Controls
if (glfwGetKey(window, GLFW_KEY_SPACE)) {
playSimulation = true;
}
if (glfwGetKey(window, GLFW_KEY_H)) {
vec3 dir = vec3(20, 0, 10);
wind = dir * glm::length(normals[v] * normalize(dir));
}
if (glfwGetKey(window, GLFW_KEY_F)) {
for (int v = 0; v < vertices.size(); v++) {
vertices[v] = initPositions[v];
velocities[v] = vec3(0);
prevPositions[v] = vertices[v];
}
}
if (playSimulation) {
forces[v] = wind + forceAirResistance + gravity - spring;
}
forces[vertices.size() - xParticles] = vec3(0);
forces[vertices.size() - 1] = vec3(0);
}
}
void Cloth::eulerIntegration(float delta) {
for (int v = 0; v < vertices.size(); v++) {
vec3 acceleration = forces[v] * 1.0f; // mass
velocities[v] = dampingFactor * velocities[v] + acceleration * delta;
vertices[v] = vertices[v] + velocities[v] * delta;
}
}
void Cloth::verletIntegration(float delta, int n_iterations) {
for (int v = 0; v < vertices.size(); v++) {
vec3 position_prim = vec3(0.f);
vec3 acceleration = forces[v] * 1.0f; // mass
for (int i = 0; i < n_iterations; i++) {
position_prim = vertices[v] + ((vertices[v] - prevPositions[v]) * dampingFactor) +
(acceleration * delta * delta);
prevPositions[v] = vertices[v];
}
vertices[v] = position_prim;
}
}
// Updating the simualtion every frame according to the forces
void Cloth::update(GLFWwindow* window, float delta) {
// verletIntegration(delta, 1);
Forces(window);
eulerIntegration(delta);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vec3), &vertices[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NBO);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_DYNAMIC_DRAW);
}
void Cloth::draw(GLFWwindow* window) {
glLineWidth(1.0);
glPointSize(4.0);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
if (glfwGetKey(window, GLFW_KEY_W)) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (glfwGetKey(window, GLFW_KEY_Q)) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
// glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
// glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}