-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloth.h
146 lines (114 loc) · 3.59 KB
/
Cloth.h
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
#ifndef _CLOTH_H_
#define _CLOTH_H_
#include "NxPhysics.h"
#include <iostream>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgDB/ReadFile>
#include <osg/Texture2D>
#include <osg/Image>
#include <osg/PolygonStipple>
#include <osg/PointSprite>
#include <osg/StateSet>
#include "ClothCallback.h"
/* cloth actor */
class Cloth : public osg::MatrixTransform {
public:
Cloth(NxScene *scene, osg::Vec3f origin, NxReal w, NxReal h, NxReal d,
char *texFilename = NULL, bool tearLines = false)
: mVertexRenderBuffer(NULL), mScene(NULL), mCloth(NULL), mClothMesh(NULL),
mIndexRenderBuffer(NULL), mTempTexCoords(NULL), mNumTempTexCoords(0),
mTexId(0), mTeared(false)
{
bool gHardwareSimulation = false;
mInitDone = false;
float offset = 10.0f; // tmp
clothDesc.globalPose.t = NxVec3(origin.x(), origin.z()+offset, origin.y());
clothDesc.thickness = 0.2f;
clothDesc.bendingStiffness = 0.5f;
clothDesc.stretchingStiffness = 0.8f;
clothDesc.density = 1.0f;
clothDesc.friction = 0.5f;
clothDesc.dampingCoefficient = 0.5f;
clothDesc.solverIterations = 5;
clothDesc.flags |= NX_CLF_BENDING;
clothDesc.flags != NX_CLF_BENDING_ORTHO;
clothDesc.flags |= NX_CLF_COLLISION_TWOWAY; // cloth-solid intraction
clothDesc.flags |= NX_CLF_SELFCOLLISION;
clothDesc.flags != NX_CLF_DAMPING ;
if (gHardwareSimulation)
clothDesc.flags |= NX_CLF_HARDWARE;
generateRegularMeshDesc(meshDesc, w, h, d, texFilename != NULL);
init(scene, clothDesc, meshDesc);
if (texFilename)
createTexture(texFilename);
setMatrix(osg::Matrix::identity());
addChild( _geo_cloth );
_geo_cloth->getDrawable(0)->setUpdateCallback(new ClothUpdateCallback(mCloth));
}
~Cloth()
{
if (mInitDone) {
if (mCloth) mScene->releaseCloth(*mCloth);
if (mClothMesh) mScene->getPhysicsSDK().releaseClothMesh(*mClothMesh);
releaseReceiveBuffers();
// temp texcoords are only used temporary and should be deallocated way earlier
assert(mTempTexCoords == NULL);
if (mTexId) {
//glDeleteTextures(1, &mTexId);
mTexId = 0;
}
// delete the rendering buffers
free (mVertexRenderBuffer);
free (mIndexRenderBuffer);
}
};
Cloth(NxScene *scene, NxClothDesc &desc, NxReal w, NxReal h, NxReal d,
std::string imagefile);
NxCloth *getNxCloth() { return mCloth; }
private:
void init(NxScene *scene, NxClothDesc &desc, NxClothMeshDesc &meshDesc);
void generateRegularMeshDesc(NxClothMeshDesc &desc, NxReal w, NxReal h, NxReal d,
bool texCoords);
void releaseMeshDescBuffers(const NxClothMeshDesc& desc);
bool cookMesh(NxClothMeshDesc& desc);
void allocateReceiveBuffers(int numVertices, int numTriangles);
void releaseReceiveBuffers();
bool createTexture(std::string filename);
void updateTextureCoordinates();
bool mInitDone;
NxClothMeshDesc meshDesc;
NxClothDesc clothDesc;
NxMeshData mReceiveBuffers;
NxScene *mScene;
NxCloth *mCloth;
NxClothMesh *mClothMesh;
int mTexId;
// Structure for the rendering buffer
struct RenderBufferVertexElement
{
NxVec3 position;
NxVec3 normal;
float texCoord[2];
};
// The rendering buffers
RenderBufferVertexElement* mVertexRenderBuffer;
NxU32* mIndexRenderBuffer;
NxU32 mMaxVertices;
NxU32 mMaxIndices;
NxU32 mNumIndices;
NxU32 mNumParentIndices;
NxU32 mNumVertices;
NxU32 mLastNumVertices;
NxU32 mMeshDirtyFlags;
bool mTeared;
// Temporary storage for tex coords
float *mTempTexCoords;
NxU32 mNumTempTexCoords;
osg::Geode* _geo_cloth;
};
// for demo scenes
Cloth* createFreeCloth(NxScene* scene, osg::Vec3f origin);
Cloth* createAttachedCloth(NxScene* scene, osg::Vec3f origin);
#endif