-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCloth.cpp
207 lines (171 loc) · 5.66 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
/*
* Cloth.cpp
*
* Created on: 15/10/2014
* Author: sam
*/
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "Cloth.h"
// width/height:
// Width/Height in space
// particleWidth, particleHeight:
// Width/Height in particles
Cloth::Cloth(float width, float height, int particleWidth, int particleHeight, float weight, float damping, glm::mat4 transform) {
this->particleWidth = particleWidth;
this->particleHeight = particleHeight;
this->isTextured = true;
this->textureID = 0;
float
particleSeperationX = width/particleWidth,
particleSeperationY = height/particleHeight,
numParticles = particleWidth * particleHeight,
particleMass = numParticles/weight,
particleDamping = 1-damping;
//Create the particles
for (int y = 0; y < particleHeight; y++)
for (int x = 0; x < particleWidth; x++)
particles.push_back(Particle(
glm::vec3(transform * glm::vec4(x*particleSeperationX, -y*particleSeperationY, 0, 1)),
particleMass, particleDamping,
glm::vec2((float)x/(particleWidth-1), -(float)y/(particleHeight-1))
));
//Create the triangles
for (int y = 1; y < particleHeight; y++)
for (int x = 1; x < particleWidth; x++) {
// p1 - p2
// | / |
// p3 - p4
Particle
*p1 = &particles[(y-1)*particleWidth+x-1],
*p2 = &particles[(y-1)*particleWidth+x],
*p3 = &particles[y*particleWidth+x-1],
*p4 = &particles[y*particleWidth+x];
triangles.push_back(Triangle(p3, p1, p2));
triangles.push_back(Triangle(p2, p4, p3));
}
//Define constraints between the particles
for (int i = 0; i < numParticles; i++) {
bool
leftCol = i%particleWidth == 0,
rightCol = (i+1)%particleWidth == 0,
topRow = i < particleWidth,
topRows = i < particleWidth*2, //top 2 rows
leftCols = leftCol || (i-1)%particleWidth == 0; //2 leftmost columns
//vertical (up)
if (!topRow)
constraints.push_back(Constraint(
&particles[i], &particles[i-particleWidth]
));
if (!topRows)
constraints.push_back(Constraint(
&particles[i], &particles[i-(particleWidth*2)]
));
//horizontal (left)
if (!leftCol)
constraints.push_back(Constraint(
&particles[i], &particles[i-1]
));
if (!leftCols)
constraints.push_back(Constraint(
&particles[i], &particles[i-2]
));
//diagonal \ .
if (!topRow && !leftCol)
constraints.push_back(Constraint(
&particles[i], &particles[i-1-particleWidth]
));
//diagonal / .
if (!topRow && !rightCol)
constraints.push_back(Constraint(
&particles[i], &particles[i+1-particleWidth]
));
}
//give the cloth a tiny push in the z direction
particles[numParticles/2+particleWidth/2].force.z -= 1;
//Make some points static
//particles[particleWidth/2].isStatic = true; //top middle
particles[0].isStatic = true; //top left
particles[particleWidth-1].isStatic = true; //top right
//particles[numParticles-particleWidth].isStatic = true; //bottom left
//particles[numParticles-1].isStatic = true; //bottom right
//for (int i = 0; i < particleHeight; i++) //left edge
// particles[i*particleWidth].isStatic = true;
}
Cloth::~Cloth() {
// TODO Auto-generated destructor stub
}
//mode:
// GL_TRIANGLES for solid
// GL_LINE_LOOP for wireframe
void Cloth::display(GLenum mode) {
//Bind the texture
if (isTextured) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
}
//Calculate normals
for (int y = 0; y < particleHeight; y++)
for (int x = 0; x < particleWidth; x++) {
glm::vec3 vy, vx;
Particle *p = &particles[y*particleWidth+x];
if (y > 0) vy = particles[(y-1)*particleWidth+x].pos;
else vy = p->pos;
if (y < particleHeight) vy -= particles[(particleHeight-1)*particleWidth+x].pos;
else vy -= p->pos;
if (x > 0) vx = particles[y*particleWidth+x-1].pos;
else vx = p->pos;
if (x < particleWidth-1) vx -= particles[y*particleWidth+x+1].pos;
else vx -= p->pos;
p->normal = glm::normalize(glm::cross(vy, vx));
}
//Draw the cloth
glBegin(mode);
for (std::vector<Triangle>::size_type i = 0; i < triangles.size(); i++)
triangles[i].display();
glEnd();
//Unbind the texture
if (isTextured) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void Cloth::displayConstraints() {
for (std::vector<Constraint>::size_type i = 0; i < constraints.size(); i++)
constraints[i].display();
}
void Cloth::step(float timeStep) {
//Gravity
for (std::vector<Particle>::size_type i = 0; i < particles.size(); i++)
particles[i].force += glm::vec3(0, -0.3, 0) * timeStep;
//Satisfy constraints
for (int n = 0; n < 20; n++)
for (std::vector<Constraint>::size_type i = 0; i < constraints.size(); i++)
constraints[i].satisfy();
//Apply forces
for (std::vector<Particle>::size_type i = 0; i < particles.size(); i++)
particles[i].step(timeStep);
}
void Cloth::loadTexture(char* filename) {
GLenum
texture = GL_TEXTURE0,
target = GL_TEXTURE_2D;
//Init the texture storage, and set some parameters.
glActiveTexture(texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &textureID);
glBindTexture(target, textureID);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//Load the png file from disk and decode it
// (assumes the file is a png with OpenGL-friendly dimensions)
int imageWidth, imageHeight, comp;
stbi_set_flip_vertically_on_load(1);
unsigned char* imageData = stbi_load(filename, &imageWidth, &imageHeight, &comp, STBI_rgb);
//Load the texture into GL
glTexImage2D(target, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB,
GL_UNSIGNED_BYTE, imageData);
isTextured = true;
}