00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "physxClothNode.h"
00016 #include "physxCloth.h"
00017 #include "physxFileStream.h"
00018
00019 #include "geomVertexFormat.h"
00020 #include "geomVertexWriter.h"
00021
00022 TypeHandle PhysxClothNode::_type_handle;
00023
00024
00025
00026
00027
00028
00029 void PhysxClothNode::
00030 allocate(PhysxCloth *cloth) {
00031
00032 _cloth = cloth;
00033
00034
00035 NxClothMeshDesc meshDesc;
00036 _cloth->ptr()->getClothMesh()->saveToDesc(meshDesc);
00037
00038 NxU32 numVertices = meshDesc.numVertices;
00039 NxU32 numTriangles = meshDesc.numTriangles;
00040
00041 NxReal factor = 1.0f;
00042
00043
00044
00045 NxU32 maxVertices = factor * numVertices;
00046 _mesh.verticesPosBegin = (NxVec3 *)malloc(sizeof(NxVec3) * maxVertices);
00047 _mesh.verticesNormalBegin = (NxVec3 *)malloc(sizeof(NxVec3) * maxVertices);
00048 _mesh.verticesPosByteStride = sizeof(NxVec3);
00049 _mesh.verticesNormalByteStride = sizeof(NxVec3);
00050 _mesh.maxVertices = maxVertices;
00051 _mesh.numVerticesPtr = (NxU32 *)malloc(sizeof(NxU32));
00052
00053
00054 NxU32 maxIndices = 3 * numTriangles;
00055 _mesh.indicesBegin = (NxU32 *)malloc(sizeof(NxU32) * maxIndices);
00056 _mesh.indicesByteStride = sizeof(NxU32);
00057 _mesh.maxIndices = maxIndices;
00058 _mesh.numIndicesPtr = (NxU32 *)malloc(sizeof(NxU32));
00059
00060 NxU32 maxParentIndices = maxVertices;
00061 _mesh.parentIndicesBegin = (NxU32 *)malloc(sizeof(NxU32)*maxParentIndices);
00062 _mesh.parentIndicesByteStride = sizeof(NxU32);
00063 _mesh.maxParentIndices = maxParentIndices;
00064 _mesh.numParentIndicesPtr = (NxU32 *)malloc(sizeof(NxU32));
00065
00066 *(_mesh.numVerticesPtr) = 0;
00067 *(_mesh.numIndicesPtr) = 0;
00068
00069 _cloth->ptr()->setMeshData(_mesh);
00070 }
00071
00072
00073
00074
00075
00076
00077 void PhysxClothNode::
00078 update() {
00079
00080 NxU32 numVertices = *(_mesh.numVerticesPtr);
00081
00082 if (numVertices == _numVertices) {
00083 update_geom();
00084 }
00085 else {
00086 update_texcoords();
00087 create_geom();
00088 _numVertices = numVertices;
00089 }
00090 }
00091
00092
00093
00094
00095
00096
00097 void PhysxClothNode::
00098 create_geom() {
00099
00100 _prim->clear_vertices();
00101
00102 GeomVertexWriter vwriter = GeomVertexWriter(_vdata, InternalName::get_vertex());
00103 GeomVertexWriter nwriter = GeomVertexWriter(_vdata, InternalName::get_normal());
00104 GeomVertexWriter twriter = GeomVertexWriter(_vdata, InternalName::get_texcoord());
00105
00106
00107 NxU32 numVertices = *(_mesh.numVerticesPtr);
00108 NxVec3 *v = (NxVec3 *)_mesh.verticesPosBegin;
00109 NxVec3 *n = (NxVec3 *)_mesh.verticesNormalBegin;
00110
00111 for (unsigned int i=0; i < numVertices; i++) {
00112 vwriter.add_data3f(v->x, v->y, v->z);
00113 v++;
00114 nwriter.add_data3f(n->x, n->y, n->z);
00115 n++;
00116 }
00117
00118
00119 NxReal tex_u;
00120 NxReal tex_v;
00121
00122 if (_texcoords) {
00123 for (unsigned int i=0; i < numVertices; i++) {
00124 tex_u = _texcoords[2*i];
00125 tex_v = _texcoords[2*i+1];
00126 twriter.add_data2f(tex_u, tex_v);
00127 }
00128 }
00129
00130
00131 NxU32 numIndices = *(_mesh.numIndicesPtr);
00132 NxU32 *idx = (NxU32 *)_mesh.indicesBegin;
00133
00134 for (unsigned int i=0; i < numIndices; i++) {
00135 _prim->add_vertex(*idx);
00136 idx++;
00137 }
00138
00139 _prim->close_primitive();
00140 }
00141
00142
00143
00144
00145
00146
00147 void PhysxClothNode::
00148 update_geom() {
00149
00150 GeomVertexWriter vwriter = GeomVertexWriter(_vdata, InternalName::get_vertex());
00151 GeomVertexWriter nwriter = GeomVertexWriter(_vdata, InternalName::get_normal());
00152
00153 NxU32 numVertices = *(_mesh.numVerticesPtr);
00154 NxVec3 *v = (NxVec3 *)_mesh.verticesPosBegin;
00155 NxVec3 *n = (NxVec3 *)_mesh.verticesNormalBegin;
00156
00157 for (unsigned int i=0; i < numVertices; i++) {
00158 vwriter.set_data3f(v->x, v->y, v->z);
00159 v++;
00160 nwriter.set_data3f(n->x, n->y, n->z);
00161 n++;
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170 void PhysxClothNode::
00171 update_texcoords() {
00172
00173 if (!_texcoords) {
00174 return;
00175 }
00176
00177 NxU32 numVertices = *(_mesh.numVerticesPtr);
00178 NxU32 *parent = (NxU32 *)_mesh.parentIndicesBegin + _numTexcoords;
00179
00180 for (NxU32 i=_numTexcoords; i < numVertices; i++, parent++) {
00181 _texcoords[2*i] = _texcoords[2*(*parent)];
00182 _texcoords[2*i+1] = _texcoords[2*(*parent)+1];
00183 }
00184
00185 _numTexcoords = numVertices;
00186 }
00187
00188
00189
00190
00191
00192
00193 bool PhysxClothNode::
00194 set_texcoords(const Filename &filename) {
00195
00196 if (filename.empty()) {
00197 return false;
00198 }
00199
00200 Filename fn(filename);
00201 fn.resolve_filename(get_model_path());
00202
00203 if (!filename.exists()) {
00204 return false;
00205 }
00206
00207 PhysxFileStream fs(filename.c_str(), true);
00208
00209 fs.readByte();
00210 fs.readByte();
00211 fs.readByte();
00212 fs.readByte();
00213 fs.readByte();
00214 fs.readByte();
00215 fs.readByte();
00216 fs.readByte();
00217 fs.readByte();
00218
00219 _numTexcoords = fs.readDword();
00220 _texcoords = new float[2 * _numTexcoords];
00221
00222 for (unsigned int i=0; i<_numTexcoords; i++) {
00223 _texcoords[2*i] = fs.readFloat();
00224 _texcoords[2*i+1] = fs.readFloat();
00225 }
00226
00227 return true;
00228 }
00229