/
Cloth.cpp
339 lines (274 loc) · 9.94 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
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
/* Cloth class implementation, osg part by Janusz Goldasz */
#include "Cloth.h"
#include "Cooking.h"
#include "Stream.h"
#define TEAR_MEMORY_FACTOR 3
#include <osg/Point>
// -----------------------------------------------------------------------
void Cloth::init(NxScene *scene, NxClothDesc &desc, NxClothMeshDesc &meshDesc)
{
mScene = scene;
// if we want tearing we must tell the cooker
// this way it will generate some space for particles that will be generated during tearing
if (desc.flags & NX_CLF_TEARABLE)
meshDesc.flags |= NX_CLOTH_MESH_TEARABLE;
// Enable Debug rendering for this cloth
desc.flags |= NX_CLF_VISUALIZATION;
cookMesh(meshDesc);
releaseMeshDescBuffers(meshDesc);
allocateReceiveBuffers(meshDesc.numVertices, meshDesc.numTriangles);
desc.clothMesh = mClothMesh;
desc.meshData = mReceiveBuffers;
mCloth = scene->createCloth(desc);
mInitDone = true;
}
void Cloth::generateRegularMeshDesc(NxClothMeshDesc &desc, NxReal w, NxReal h, NxReal d, bool
texCoords)
{
int numX = (int)(w / d) + 1;
int numY = (int)(h / d) + 1;
desc.numVertices = (numX+1) * (numY+1);
desc.numTriangles = numX*numY*2;
desc.pointStrideBytes = sizeof(NxVec3);
desc.triangleStrideBytes = 3*sizeof(NxU32);
desc.vertexMassStrideBytes = sizeof(NxReal);
desc.vertexFlagStrideBytes = sizeof(NxU32);
desc.points = (NxVec3*)malloc(sizeof(NxVec3)*desc.numVertices);
desc.triangles = (NxU32*)malloc(sizeof(NxU32)*desc.numTriangles*3);
desc.vertexMasses = 0;
desc.vertexFlags = 0;
desc.flags = 0;
mMaxVertices = TEAR_MEMORY_FACTOR * desc.numVertices;
mMaxIndices = 3 * desc.numTriangles;
_geo_cloth = new osg::Geode;
int i,j;
// create particles - POINTS
{
osg::Geometry* pointsGeom = new osg::Geometry();
osg::Vec3Array* vertices = new osg::Vec3Array;
NxVec3 *p = (NxVec3*)desc.points;
for (i = 0; i <= numY; i++) {
for (j = 0; j <= numX; j++) {
p->set(d*j, 0.0f, d*i);
vertices->push_back(osg::Vec3f(d*j, d*i, 0.0f));
p++;
}
}
pointsGeom->setVertexArray(vertices);
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,0.8f));
pointsGeom->setColorArray(colors);
pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
// set the normal in the same way color.
osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
pointsGeom->setNormalArray(normals);
pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
osg::StateSet* stateSet = new osg::StateSet();
pointsGeom->setStateSet(stateSet);
pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices->size()));
osg::Point* point = new osg::Point;
point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.05f));
point->setSize(20.0f);
stateSet->setAttribute(point);
osg::PointSprite *sprite = new osg::PointSprite();
stateSet->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON);
// add the points geometry to the geode.
_geo_cloth->addDrawable(pointsGeom);
}
if (texCoords) {
mTempTexCoords = (float *)malloc(sizeof(float)*2*TEAR_MEMORY_FACTOR*desc.numVertices);
float *f = mTempTexCoords;
float dx = 1.0f; if (numX > 0) dx /= numX;
float dy = 1.0f; if (numY > 0) dy /= numY;
for (i = 0; i <= numY; i++) {
for (j = 0; j <= numX; j++) {
*f++ = j*dx;
*f++ = i*dy;
}
}
mNumTempTexCoords = desc.numVertices;
}
else
{
mNumTempTexCoords = 0;
mTempTexCoords = NULL;
}
NxU32 *id = (NxU32*)desc.triangles;
for (i = 0; i < numY; i++) {
for (j = 0; j < numX; j++) {
NxU32 i0 = i * (numX+1) + j;
NxU32 i1 = i0 + 1;
NxU32 i2 = i0 + (numX+1);
NxU32 i3 = i2 + 1;
if ((j+i)%2) {
*id++ = i0; *id++ = i2; *id++ = i1;
*id++ = i1; *id++ = i2; *id++ = i3;
}
else {
*id++ = i0; *id++ = i2; *id++ = i3;
*id++ = i0; *id++ = i3; *id++ = i1;
}
}
}
}
void Cloth::releaseMeshDescBuffers(const NxClothMeshDesc& desc)
{
NxVec3* p = (NxVec3*)desc.points;
NxU32* t = (NxU32*)desc.triangles;
NxReal* m = (NxReal*)desc.vertexMasses;
NxU32* f = (NxU32*)desc.vertexFlags;
free(p);
free(t);
free(m);
free(f);
}
bool Cloth::cookMesh(NxClothMeshDesc& desc)
{
// Store correct number to detect tearing mesh in time
mLastNumVertices = desc.numVertices;
// we cook the mesh on the fly through a memory stream
// we could also use a file stream and pre-cook the mesh
MemoryWriteBuffer wb;
assert(desc.isValid());
bool success = CookClothMesh(desc, wb);
if (!success)
return false;
MemoryReadBuffer rb(wb.data);
mClothMesh = mScene->getPhysicsSDK().createClothMesh(rb);
return true;
}
void Cloth::allocateReceiveBuffers(int numVertices, int numTriangles)
{
// here we setup the buffers through which the SDK returns the dynamic cloth data
// we reserve more memory for vertices than the initial mesh takes
// because tearing creates new vertices
// the SDK only tears cloth as long as there is room in these buffers
//NxU32 maxVertices = TEAR_MEMORY_FACTOR * numVertices;
//NxU32 maxIndices = 3*numTriangles;
if (mVertexRenderBuffer == NULL)
{
// Allocate Render Buffer for Vertices if it hasn't been done before
mVertexRenderBuffer = (RenderBufferVertexElement*)malloc(sizeof(RenderBufferVertexElement) * mMaxVertices);
memset(mVertexRenderBuffer, 0, sizeof(RenderBufferVertexElement) * mMaxVertices);
}
if (mIndexRenderBuffer == NULL)
{
// Allocate Render Buffer for Indices if it hasn't been done before
mIndexRenderBuffer = (NxU32*)malloc(sizeof(NxU32) * mMaxIndices);
memset(mIndexRenderBuffer, 0, sizeof(NxU32) * mMaxIndices);
}
mReceiveBuffers.verticesPosBegin = &(mVertexRenderBuffer[0].position.x);
mReceiveBuffers.verticesNormalBegin = &(mVertexRenderBuffer[0].normal.x);
mReceiveBuffers.verticesPosByteStride = sizeof(RenderBufferVertexElement);
mReceiveBuffers.verticesNormalByteStride = sizeof(RenderBufferVertexElement);
mReceiveBuffers.maxVertices = mMaxVertices;
mReceiveBuffers.numVerticesPtr = &mNumVertices;
// the number of triangles is constant, even if the cloth is torn
NxU32 maxIndices = 3*numTriangles;
mReceiveBuffers.indicesBegin = mIndexRenderBuffer;
mReceiveBuffers.indicesByteStride = sizeof(NxU32);
mReceiveBuffers.maxIndices = maxIndices;
mReceiveBuffers.numIndicesPtr = &mNumIndices;
if (mNumTempTexCoords > 0)
{
// Copy Tex Coords from temp buffers to graphics buffer
assert(mNumTempTexCoords == numVertices);
for (NxU32 i = 0; i < mNumTempTexCoords; i++)
{
mVertexRenderBuffer[i].texCoord[0] = mTempTexCoords[2*i+0];
mVertexRenderBuffer[i].texCoord[1] = mTempTexCoords[2*i+1];
}
// Get rid of temp buffer
mNumTempTexCoords = 0;
free (mTempTexCoords);
mTempTexCoords = NULL;
}
// the parent index information would be needed if we used textured cloth
mReceiveBuffers.parentIndicesBegin = (NxU32*)malloc(sizeof(NxU32)*mMaxVertices);
mReceiveBuffers.parentIndicesByteStride = sizeof(NxU32);
mReceiveBuffers.maxParentIndices = mMaxVertices;
mReceiveBuffers.numParentIndicesPtr = &mNumParentIndices;
mReceiveBuffers.dirtyBufferFlagsPtr = &mMeshDirtyFlags;
// init the buffers in case we want to draw the mesh
// before the SDK as filled in the correct values
mMeshDirtyFlags = 0;
mNumVertices = 0;
mNumIndices = 0;
mNumParentIndices = 0;
}
// -----------------------------------------------------------------------
bool Cloth::createTexture(std::string filename)
{
/*BmpLoader bl;
if (!bl.loadBmp(filename)) return false;
glGenTextures(1, &mTexId);
if (!mTexId) return false;
glBindTexture(GL_TEXTURE_2D, mTexId);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
bl.mWidth, bl.mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bl.mRGB);*/
return true;
}
void Cloth::releaseReceiveBuffers()
{
free (mReceiveBuffers.parentIndicesBegin);
mReceiveBuffers.setToDefault();
}
void Cloth::updateTextureCoordinates()
{
NxU32 numVertices = *mReceiveBuffers.numVerticesPtr;
NxU32 *parent = (NxU32 *)mReceiveBuffers.parentIndicesBegin + mLastNumVertices;
for (NxU32 i = mLastNumVertices; i < numVertices; i++, parent++) {
mVertexRenderBuffer[i].texCoord[0] = mVertexRenderBuffer[*parent].texCoord[0];
mVertexRenderBuffer[i].texCoord[1] = mVertexRenderBuffer[*parent].texCoord[1];
}
mTeared |= mLastNumVertices != numVertices;
mLastNumVertices = numVertices;
}
Cloth* createFreeCloth(NxScene* scene, osg::Vec3f origin)
{
Cloth *regularCloth = new Cloth(scene, origin, 8.0f, 8.0f, 0.2f, 0);
if (!regularCloth->getNxCloth())
{
printf("Error: Unable to create the cloth for the current scene.\n");
delete regularCloth;
} else
{
//regularCloth->getNxCloth()->attachToShape(*capsule->getShapes(), 0);
}
return regularCloth;
}
Cloth* createAttachedCloth(NxScene* scene, osg::Vec3f origin)
{
float w = 8.0f;
float h = 8.0f;
float d = 0.2f;
Cloth *regularCloth = new Cloth(scene, origin, w, h, d, 0);
if (!regularCloth->getNxCloth())
{
printf("Error: Unable to create the cloth for the current scene.\n");
delete regularCloth;
} else
{
int numX = (int)(w/d)+1;
int numY = (int)(h/d)+1;
int i1 = 0;
NxVec3 p1 = regularCloth->getNxCloth()->getPosition(i1);
int i2 = numX;
NxVec3 p2 = regularCloth->getNxCloth()->getPosition(i2);
int i3 = numX*(numY+1);
NxVec3 p3 = regularCloth->getNxCloth()->getPosition(i3);
int i4 = (numX+1)*(numY+1)-1;
NxVec3 p4 = regularCloth->getNxCloth()->getPosition(i4);
regularCloth->getNxCloth()->attachVertexToGlobalPosition(i1,p1);
regularCloth->getNxCloth()->attachVertexToGlobalPosition(i2,p2);
regularCloth->getNxCloth()->attachVertexToGlobalPosition(i3,p3);
regularCloth->getNxCloth()->attachVertexToGlobalPosition(i4,p4);
}
return regularCloth;
}