-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloth.cpp
291 lines (227 loc) · 8.48 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
279
280
281
282
283
284
285
286
287
288
289
290
#include "Cloth.h"
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "Mass.h"
#include <stdlib.h>
#include<cmath>
Cloth::Cloth(glm::vec3 position,int sliceX,int sliceY)
{
this->sliceX = sliceX;
this->sliceY = sliceY;
this->position = position;
this->t = 0;
this->windY = 1.0;
this->random = 4/rand();
}
Cloth::~Cloth()
{
}
/**
From my understanding all that is required here is to implement draw is to attach
the vao that the buffer data was created from.
*/
void Cloth::draw(float delta){
glBindVertexArray(vaoID);
glDrawElements(GL_TRIANGLES, this->indicesSize, GL_UNSIGNED_INT, (void*)0);
glBindVertexArray(0);
}
void Cloth::rebind() {
glBindBuffer(GL_ARRAY_BUFFER, this->positionID);
glBufferData(GL_ARRAY_BUFFER, this->positionSize * sizeof(GLfloat), positionCords, GL_STATIC_DRAW);
}
void Cloth::normalizeNormals(){
for(int i = 0; i < normalsSize; i+= 3){
glm::vec3 normal = glm::vec3(normalsCords[i], normalsCords[i+1], normalsCords[i+2]);
normal = glm::normalize(normal);
normalsCords[i] = normal.x;
normalsCords[i+1] = normal.y;
normalsCords[i+2] = normal.z;
}
}
void Cloth::calculateNormals(){
for(int i = 0; i < normalsSize; i++){
normalsCords[i] = 1.0f;
}
for(int i = 0; i < indicesSize; i+= 3){
int index1 = indices[i];
int index2 = indices[i+1];
int index3 = indices[i+2];
glm::vec3 point1 = glm::vec3(positionCords[index1*4], positionCords[(index1*4)+1], positionCords[(index1*4)+2]);
glm::vec3 point2 = glm::vec3(positionCords[index2*4], positionCords[(index2*4)+1], positionCords[(index2*4)+2]);
glm::vec3 point3 = glm::vec3(positionCords[index3*4], positionCords[(index3*4)+1], positionCords[(index3*4)+2]);
glm::vec3 v1 = point2-point1;
glm::vec3 v2 = point3-point1;
glm::vec3 crossProduct = glm::cross(v2,v1);
normalsCords[index1*3] += crossProduct.x;
normalsCords[(index1*3) + 1] += crossProduct.y;
normalsCords[(index1*3) + 2] += crossProduct.z;
normalsCords[index2*3] += crossProduct.x;
normalsCords[(index2*3) + 1] += crossProduct.y;
normalsCords[(index2*3) + 2] += crossProduct.z;
normalsCords[index3*3] += crossProduct.x;
normalsCords[(index3*3) + 1] += crossProduct.y;
normalsCords[(index3*3) + 2] += crossProduct.z;
}
normalizeNormals();
glBindBuffer(GL_ARRAY_BUFFER, this->normalID);
glBufferData(GL_ARRAY_BUFFER, this->normalsSize * sizeof(GLfloat), normalsCords, GL_STATIC_DRAW);
}
void Cloth::update(float delta) {
windY = sin(glutGet(GLUT_ELAPSED_TIME) / 100);
windX = cos(glutGet(GLUT_ELAPSED_TIME) / 200) * 0.5;
windZ = cos(glutGet(GLUT_ELAPSED_TIME) / 300) * 0.5;
int k = 0;
for (int i = 0; i < masses.size(); i++) {
float x = this->masses[i]->position.x;
float y = this->masses[i]->position.y;
float z = this->masses[i]->position.z;
this->masses[i]->addForce(glm::vec3(0, -10.4, 0));
this->masses[i]->addForce(glm::vec3(sin(x*y*t)*random+windX,cos(z*t)+windY*random, sin(cos(5*x*y*z)))*4.8f+windZ*random);
this->masses[i]->calculateNewPosition();
this->positionCords[k++] = this->masses[i]->position.x;
this->positionCords[k++] = this->masses[i]->position.y;
this->positionCords[k++] = this->masses[i]->position.z;
k++; //skip w
t+= 0.009;
}
for (int i = 0; i < 16; i++) {
for (int n = 0; n < masses.size(); n++) {
this->masses[n]->constraintSolve();
}
}
calculateNormals();
rebind();
}
void Cloth::createVertices(glm::vec3 topLeft, int slicesX, int slicesY) {
this->normalsCords = new GLfloat[slicesX * slicesY * 3];
this->positionCords = new GLfloat[slicesX * slicesY * 4];
float SQUARE_SIZE = 1.0;
int k = 0;
int n = 0;
int j = 0;
for (int x = 0; x < slicesX; x++) {
for (int y = 0; y < slicesY; y++) {
// position
positionCords[k++] = topLeft.x + x*SQUARE_SIZE; //x
positionCords[k++] = topLeft.y - y*SQUARE_SIZE; //y
positionCords[k++] = topLeft.z;//z
positionCords[k++] = 1.0; //w
// normals
normalsCords[n++] = 0.0;//x
normalsCords[n++] = 0.0;//y
normalsCords[n++] = 1.0;//z
}
}
this->positionSize = k;
this->normalsSize = n;
}
void Cloth::createIndices(int slicesX, int slicesY) {
this->indicesSize = ((sliceX-1)*(sliceY-1))*6;
this->indices = new GLuint[this->indicesSize];
int k = 0;
for (int x = 0; x < slicesX-1; x++) {
for (int y = 0; y < slicesY-1; y++) {
//left triangle
this->indices[k] = (x*slicesY + y); k++;
this->indices[k] = ((x+1)*slicesY + y); k++;
this->indices[k] = (x*slicesY + (y+1)); k++;
//Right triangle
this->indices[k] = (x*slicesY + (y+1)); k++;
this->indices[k] = ((x+1)*slicesY + (y+1)); k++;
this->indices[k] = ((x+1)*slicesY + y); k++;
}
}
}
void Cloth::fillMasses() {
int k = 0;
for (int i = 0; i < positionSize / 4; i++) {
GLfloat x = positionCords[k++];
GLfloat y = positionCords[k++];
GLfloat z = positionCords[k++];
k++; //skip the w
this->masses.push_back(new Mass(glm::vec3(x, y, z), true));
}
}
void Cloth::attachSprings(int slicesX, int slicesY) {
int k = 0;
for (int x = 0; x < slicesX-1; x++) {
for (int y = 0; y < slicesY-1; y++) {
// Left
Mass* m1 = this->masses[(x*slicesY + y)];
Mass* m2 = this->masses[(x*slicesY + (y+1))];
Spring spring0(m1, m2);
m1->addSpring(spring0);
m2->addSpring(spring0);
// Top
m1 = this->masses[(x*slicesY + y)];
m2 = this->masses[((x+1)*slicesY + y)];
Spring spring1(m1, m2);
m1->addSpring(spring1);
m2->addSpring(spring1);
// Diagonal
m1 = this->masses[(x*slicesY + (y+1))];
m2 = this->masses[((x+1)*slicesY + y)];
Spring spring2(m1, m2);
m1->addSpring(spring2);
m2->addSpring(spring2);
// Middle Line
m1 = this->masses[(x*slicesY + y)];
m2 = this->masses[((x+1)*slicesY + (y+1))];
Spring spring3(m1, m2);
m1->addSpring(spring3);
m2->addSpring(spring3);
// Bottom line
if (y == slicesY-2) {
m1 = this->masses[(x*slicesY + (y+1))];
m2 = this->masses[((x+1)*slicesY + (y+1))];
Spring spring4(m1, m2);
m1->addSpring(spring4);
m2->addSpring(spring4);
}
if (x == slicesX-2) {
m1 = this->masses[((x+1)*slicesY + (y))];
m2 = this->masses[((x+1)*slicesY + (y+1))];
Spring spring5(m1, m2);
m1->addSpring(spring5);
m2->addSpring(spring5);
}
}
}
}
/**
Once again this will just stick some vertices positions to "hang"
the cloth.
*/
void Cloth::stickTop(int sliceX, int slicesY) {
this->leftPin = masses[0];
this->rightPin = masses[(sliceX-1)*slicesY];
this->masses[0]->movable = false;
this->masses[(sliceX-1)*slicesY]->movable = false;
}
/**
Just filling the data here.
*/
void Cloth::initCloth(){
glGenVertexArrays(1, &vaoID);
glGenBuffers(1, &positionID);
glGenBuffers(1, &normalID);
glGenBuffers(1, &eboID);
glBindVertexArray(vaoID);
Cloth::createVertices(position, sliceX, sliceY);
Cloth::createIndices(sliceX, sliceY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesSize * sizeof(GLuint), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, positionID);
glBufferData(GL_ARRAY_BUFFER, positionSize * sizeof(GLfloat), positionCords, GL_STREAM_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, normalID);
glBufferData(GL_ARRAY_BUFFER,this->normalsSize * sizeof(GLfloat), normalsCords, GL_STREAM_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
fillMasses();
attachSprings(sliceX, sliceY);
stickTop(sliceX, sliceY);
}