-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloth_vclass.inl
486 lines (387 loc) · 11.1 KB
/
cloth_vclass.inl
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
const int cClothMaxConstraints = 16;
#ifdef NDEBUG
const int cClothWidth = 65;
const int cClothHeight = 65;
#else
const int cClothWidth = 20;
const int cClothHeight = 20;
#endif
const int cClothSize = cClothWidth*cClothHeight;
const float cClothRestLength = (1.f/(float)(cClothWidth))*5.f;
const int cIndicesArrSize = 32768;
///////////////////////////////////////////////////////////////////////////////
// Structs
///////////////////////////////////////////////////////////////////////////////
typedef struct ClothConstraints
{
int cIndex[cClothMaxConstraints];
int cIndexCount;
} ClothConstraints, *PClothConstraints;
typedef struct Cloth
{
Vec4 m_x[cClothSize];
Vec4 m_oldx[cClothSize];
Vec4 m_a[cClothSize];
Vec4 m_vGravity;
Vec4 fTimeStep;
Vec4 restlength;
Vec4 hook[2];
Vec4 worldTrans;
bool clothInit;
float rot;
float dist;
ClothConstraints cnstr[cClothSize];
int NUM_ITERATIONS;
int NUM_PARTICLES;
LPDIRECT3DVERTEXBUFFER9 pVB;
LPDIRECT3DINDEXBUFFER9 pVI;
int vertCount;
int primCount;
void AccumulateForces();
void Verlet();
void SatisfyConstraints();
void TimeStep();
} Cloth, *PCloth;
///////////////////////////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////////////////////////
__declspec(align(128)) Cloth g_cloth;
///////////////////////////////////////////////////////////////////////////////
// Functions
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Get cloth index
///////////////////////////////////////////////////////////////////////////////
inline int GetI(int x, int y)
{
return((y*cClothWidth) + x);
}
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
HRESULT ClothInit(void)
{
HRESULT hr = S_OK;
int w = cClothWidth;
int h = cClothHeight;
memset(&g_cloth, 0x00, sizeof(g_cloth));
g_cloth.worldTrans = Vec4(1.f, 2.f, 0.f, 0.f);
//build patch constraints
for(int yy=0; yy<=h-2; yy++)
{
for(int xx=0; xx<=w-2; xx++)
{
/////////////////////
// (xx,yy)----(xx+1,yy)
// !
// !
// (xx,yy+1)
int ii, jj;
jj = 0; ii = GetI(xx, yy);
g_cloth.cnstr[ii].cIndex[jj++] = GetI(xx+1, yy);
g_cloth.cnstr[ii].cIndex[jj++] = GetI(xx, yy+1);
g_cloth.cnstr[ii].cIndexCount = jj;
}
}
//bottom gaps
for(int xx=0; xx<=w-2; xx++)
{
/////////////////////
// (xx,h-1)----(xx+1,h-1)
int ii, jj;
ii = GetI(xx, h-1);
jj = g_cloth.cnstr[ii].cIndexCount;
assert(jj == 0);
g_cloth.cnstr[ii].cIndex[jj++] = GetI(xx+1, h-1);
g_cloth.cnstr[ii].cIndexCount = jj;
}
//right gaps
for(int yy=0; yy<=h-2; yy++)
{
/////////////////////
// (w-1,yy)
// !
// !
// (w-1,yy+1)
int ii, jj;
ii = GetI(w-1, yy);
jj = g_cloth.cnstr[ii].cIndexCount;
assert(jj == 0);
g_cloth.cnstr[ii].cIndex[jj++] = GetI(w-1, yy+1);
g_cloth.cnstr[ii].cIndexCount = jj;
}
//setup intial rest points
for(int yy=0; yy<=h-1; yy++)
{
for(int xx=0; xx<=w-1; xx++)
{
int ii = GetI(xx, yy);
float x = ((float)xx)*cClothRestLength;
float y = ((float)yy)*cClothRestLength;
Vec4 localPos = Vec4(x, y, 0.f, 0.f);
g_cloth.m_x[ii] =
g_cloth.m_oldx[ii] = localPos + g_cloth.worldTrans;
}
}
g_cloth.hook[1] = g_cloth.m_x[0];
g_cloth.hook[0] = g_cloth.m_x[cClothWidth-1];
//test
Vec4 diff = g_cloth.hook[1] - g_cloth.hook[0];
Vec4 d = Vec4::Sqrt(Vec4::Dot(diff,diff))*Vec4(0.5f);
Vec4::GetX(&g_cloth.dist, d);
//other inital values
g_cloth.m_vGravity = Vec4(0.f, -1.5f, 0.f, 0.f);
g_cloth.NUM_PARTICLES = cClothSize;
g_cloth.NUM_ITERATIONS = CLOTH_NUM_ITERATIONS;
g_cloth.restlength = Vec4(cClothRestLength);
//setup index buffers
WORD indicesArr[cIndicesArrSize]; //nasty, create tmp array on stack
memset(indicesArr, 0x00, sizeof(indicesArr));
IDirect3DDevice9* pd3dDevice = DXUTGetD3D9Device();
if( FAILED( pd3dDevice->CreateIndexBuffer( cIndicesArrSize*sizeof(WORD), //2-bytes per index
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT,
&g_cloth.pVI, NULL ) ) )
{
return (E_FAIL);
}
//p0------p1
//! !
//! !
//! !
//p2------p3
int ii=0;
for(int yy=0; yy<=h-2; yy++)
{
for(int xx=0; xx<=w-2; xx++)
{
//p0-p1
indicesArr[ii++] = GetI(xx, yy);
indicesArr[ii++] = GetI(xx+1, yy);
g_cloth.primCount++;
//p1-p3
indicesArr[ii++] = GetI(xx+1, yy);
indicesArr[ii++] = GetI(xx+1, yy+1);
g_cloth.primCount++;
//p3-p2
indicesArr[ii++] = GetI(xx+1, yy+1);
indicesArr[ii++] = GetI(xx, yy+1);
g_cloth.primCount++;
//p2-p0
indicesArr[ii++] = GetI(xx, yy+1);
indicesArr[ii++] = GetI(xx, yy);
g_cloth.primCount++;
}
}
VOID* pIndices = 0;
hr = g_cloth.pVI->Lock(0, sizeof(indicesArr), (VOID**)&pIndices, 0 );
if (FAILED(hr))
{
SAFE_RELEASE(g_cloth.pVI);
return (E_FAIL);
}
int size = sizeof(indicesArr);
memcpy( pIndices, indicesArr, size );
g_cloth.pVI->Unlock();
// Create the vertex buffer.
g_cloth.vertCount = ii;
if( FAILED( pd3dDevice->CreateVertexBuffer
(
g_cloth.vertCount * sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_cloth.pVB, NULL
) ) )
{
return (E_FAIL);
}
ClothCopyVertices(true);
return(hr);
}
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
void ClothShutDown(void)
{
SAFE_RELEASE(g_cloth.pVB);
SAFE_RELEASE(g_cloth.pVI);
memset(&g_cloth, 0x00, sizeof(g_cloth));
}
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
HRESULT ClothCopyVertices(bool addColor)
{
HRESULT hr = S_OK;
int w = cClothWidth;
int h = cClothHeight;
// Fill the vertex buffer.
VOID* pVertices = 0;
if( FAILED(
g_cloth.pVB->Lock ( 0, g_cloth.vertCount * sizeof( CUSTOMVERTEX ), ( void** )&pVertices, 0 )
) )
{
return E_FAIL;
}
//FLOAT x, y, z; // The untransformed, 3D position for the vertex
//DWORD color; // The vertex color
//memcpy( pVertices, g_Vertices, sizeof( g_Vertices ) );
CUSTOMVERTEX* pV = (CUSTOMVERTEX*)pVertices;
int kk=0;
if (addColor)
{
for(int yy=0; yy<=h-1; yy++)
{
for(int xx=0; xx<=w-1; xx++)
{
int ii = GetI(xx, yy);
__declspec(align(128)) float f[4];
//VStore(f, g_cloth.m_x[ii]);
g_cloth.m_x[ii].Store(f);
pV[kk].x = f[3];
pV[kk].y = f[2];
pV[kk].z = f[1];
//add some cool colors since I won't lit the patches
float xF = (1.f-((float)xx * ((float)1.f/(w-1))))*255.f;
float yF = (1.f-((float)yy * ((float)1.f/(h-1))))*255.f;
pV[kk].color = D3DCOLOR_ARGB(0xff, (short)xF, (short)yF, 0xff);
kk++;
}
}
}
else
{
for(int yy=0; yy<=h-1; yy++)
{
for(int xx=0; xx<=w-1; xx++)
{
int ii = GetI(xx, yy);
__declspec(align(128)) float f[4];
//VStore(f, g_cloth.m_x[ii]);
g_cloth.m_x[ii].Store(f);
pV[kk].x = f[3];
pV[kk].y = f[2];
pV[kk].z = f[1];
kk++;
}
}
}
g_cloth.pVB->Unlock();
return(hr);
}
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
HRESULT ClothAnimateAndRender(IDirect3DDevice9* pd3dDevice, D3DXMATRIXA16* mWorld, D3DXMATRIXA16* mView, D3DXMATRIXA16* mProj, float fTimeStep, int reps, double *totalTimeOut)
{
HRESULT hr = S_OK;
if (!g_cloth.clothInit)
{
ClothInit();
g_cloth.clothInit = !g_cloth.clothInit;
}
ClothUIHack();
g_cloth.fTimeStep = Vec4(fTimeStep);
*totalTimeOut = 0.;
for(int ii=0; ii<reps; ii++)
{
double time;
PerformanceCounterStart();
g_cloth.TimeStep();
time = PerformanceCounterEnd();
*totalTimeOut += time;
}
ClothCopyVertices(false);
// Turn off culling, so we see the front and back of the triangle
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting, since we are providing our own vertex colors
pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
pd3dDevice->SetTransform( D3DTS_WORLD, mWorld );
pd3dDevice->SetTransform( D3DTS_VIEW, mView );
pd3dDevice->SetTransform( D3DTS_PROJECTION, mProj );
// Render the vertex buffer contents
pd3dDevice->SetStreamSource( 0, g_cloth.pVB, 0, sizeof( CUSTOMVERTEX ) );
pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
pd3dDevice->SetIndices( g_cloth.pVI);
pd3dDevice->DrawIndexedPrimitive( D3DPT_LINELIST, 0, 0, g_cloth.vertCount, 0, g_cloth.primCount);
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
return(hr);
}
void ClothUIHack(void)
{
float s = sinf(g_cloth.rot);
float c = cosf(g_cloth.rot);
float x0 = -g_cloth.dist;
float x0r = x0*c;
float z0r = x0*s;
float x1 = +g_cloth.dist;
float x1r = x1*c;
float z1r = x1*s;
Vec4 pa = Vec4(x0r, 0.f, z0r, 0.f) + g_cloth.worldTrans;
Vec4 pb = Vec4(x1r, 0.f, z1r, 0.f) + g_cloth.worldTrans;
g_cloth.hook[0] = pa;
g_cloth.hook[1] = pb;
}
void ClothSetGlobalParam(float rot, float trans, float gravity)
{
g_cloth.rot = VCLASS_DTOR(rot);
g_cloth.worldTrans = Vec4(1.f, trans, 0.f, 0.f);
g_cloth.m_vGravity = Vec4(0.f, gravity, 0.f, 0.f);
}
///////////////////////////////////////////////////////////////////////////////
// Simulation Code
///////////////////////////////////////////////////////////////////////////////
// Verlet integration step
void Cloth::Verlet()
{
Vec4 d1 = Vec4(0.99901f);
Vec4 d2 = Vec4(0.99898f);
for(int i=0; i<NUM_PARTICLES; i++)
{
Vec4& x = m_x[i];
Vec4 temp = x;
Vec4& oldx = m_oldx[i];
Vec4& a = m_a[i];
x += (d1*x)-(d2*oldx)+a*fTimeStep*fTimeStep;
oldx = temp;
}
}
// This function should accumulate forces for each particle
void Cloth::AccumulateForces()
{
// All particles are influenced by gravity
for(int i=0; i<NUM_PARTICLES; i++) m_a[i] = m_vGravity;
}
// Here constraints should be satisfied
void Cloth::SatisfyConstraints()
{
Vec4 half = Vec4(0.5f);
// Implements simulation of a stick in a box
for(int j=0; j<NUM_ITERATIONS; j++)
{
// First satisfy (C1)
m_x[0] = hook[0];
m_x[cClothWidth-1] = hook[1];
// For all particles
for(int i=0; i<NUM_PARTICLES-1; i++)
{
// Then satisfy (C2)
Vec4& x1 = m_x[i];
for(int cc=0; cc<cnstr[i].cIndexCount; cc++)
{
//get 2nd particle index
int i2 = cnstr[i].cIndex[cc];
Vec4& x2 = m_x[i2];
Vec4 delta = x2-x1;
Vec4 deltalength = Vec4::Sqrt(Vec4::Dot(delta,delta));
Vec4 diff = (deltalength-restlength)/deltalength;
x1 += delta*half*diff;
x2 -= delta*half*diff;
}
}
}
}
void Cloth::TimeStep()
{
AccumulateForces();
Verlet();
SatisfyConstraints();
}