00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "physxClothMeshDesc.h"
00016 #include "physxManager.h"
00017
00018 #include "nodePathCollection.h"
00019 #include "geomNode.h"
00020 #include "geomVertexReader.h"
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 void PhysxClothMeshDesc::
00033 set_num_vertices(unsigned int numVertices) {
00034
00035
00036 if (_desc.points) {
00037 delete [] _points;
00038 }
00039
00040 _points = new NxVec3[numVertices];
00041
00042 _desc.numVertices = numVertices;
00043 _desc.points = _points;
00044
00045
00046 if (_texcoords) {
00047 delete [] _texcoords;
00048 }
00049
00050 _texcoords = new LPoint2f[numVertices];
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 void PhysxClothMeshDesc::
00060 set_vertex(unsigned int idx, const LPoint3f &vert, const LPoint2f &texcoord) {
00061
00062 nassertv(_desc.numVertices > idx);
00063
00064 _points[idx] = PhysxManager::point3_to_nxVec3(vert);
00065 _texcoords[idx] = texcoord;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 void PhysxClothMeshDesc::
00078 set_num_triangles(unsigned int numTriangles) {
00079
00080 if (_desc.triangles) {
00081 delete [] _triangles;
00082 }
00083
00084 _triangles = new NxU32[3 * numTriangles];
00085
00086 _desc.numTriangles = numTriangles;
00087 _desc.triangles = _triangles;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 void PhysxClothMeshDesc::
00097 set_triangle(unsigned int idx,
00098 unsigned int i1, unsigned int i2, unsigned int i3) {
00099
00100 nassertv(_desc.numTriangles > idx);
00101
00102 idx = 3 * idx;
00103 _triangles[idx] = i1;
00104 _triangles[idx + 1] = i2;
00105 _triangles[idx + 2] = i3;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 void PhysxClothMeshDesc::
00124 set_from_node_path(const NodePath &np) {
00125
00126 pvector<LPoint3f> dataVertices;
00127 pvector<LPoint2f> dataTexcoords;
00128 pvector<int> dataIndices;
00129
00130
00131 NodePathCollection npc = np.find_all_matches( "**/+GeomNode" );
00132 for (int i=0; i<npc.get_num_paths(); i++) {
00133 NodePath gnp = npc.get_path(i);
00134 GeomNode *gnode = DCAST(GeomNode, gnp.node());
00135
00136 for (int j=0; j<gnode->get_num_geoms(); j++) {
00137 CPT(Geom) geom = gnode->get_geom(j);
00138 CPT(GeomVertexData) vdata = geom->get_vertex_data();
00139 GeomVertexReader reader;
00140
00141
00142 reader = GeomVertexReader(vdata, InternalName::get_vertex());
00143 while (!reader.is_at_end()) {
00144 dataVertices.push_back(reader.get_data3f());
00145 }
00146
00147
00148 reader = GeomVertexReader(vdata, InternalName::get_texcoord());
00149 while (!reader.is_at_end()) {
00150 dataTexcoords.push_back(reader.get_data2f());
00151 }
00152
00153
00154 for (int k=0; k<geom->get_num_primitives(); k++) {
00155
00156 CPT(GeomPrimitive) prim = geom->get_primitive(k);
00157 prim = prim->decompose();
00158
00159 for (int l=0; l<prim->get_num_primitives(); l++) {
00160
00161 int s = prim->get_primitive_start(l);
00162 int e = prim->get_primitive_end(l);
00163
00164 for (int l=s; l<e; l++) {
00165 dataIndices.push_back(prim->get_vertex(l));
00166 }
00167 }
00168 }
00169 }
00170 }
00171
00172
00173 int i;
00174
00175 NxU32 numVertices = dataVertices.size();
00176 NxU32 numTriangles = dataIndices.size() / 3;
00177
00178 _points = new NxVec3[numVertices];
00179 _triangles = new NxU32[3 * numTriangles];
00180 _texcoords = new LPoint2f[numVertices];
00181
00182 i = 0;
00183 pvector<LPoint3f>::const_iterator vit;
00184 for (vit=dataVertices.begin(); vit!=dataVertices.end(); vit++) {
00185 LPoint3f v = *vit;
00186
00187 _points[i] = PhysxManager::point3_to_nxVec3(v);
00188 i++;
00189 }
00190
00191 i = 0;
00192 pvector<LPoint2f>::const_iterator tcit;
00193 for (tcit=dataTexcoords.begin(); tcit!=dataTexcoords.end(); tcit++) {
00194 LPoint2f tc = *tcit;
00195
00196 _texcoords[i] = tc;
00197 i++;
00198 }
00199
00200 i = 0;
00201 pvector<int>::const_iterator iit;
00202 for(iit=dataIndices.begin(); iit!=dataIndices.end(); iit++) {
00203 NxU32 idx = *iit;
00204
00205 _triangles[i] = idx;
00206 i++;
00207 }
00208
00209 _desc.numVertices = numVertices;
00210 _desc.points = _points;
00211 _desc.numTriangles = numTriangles;
00212 _desc.triangles = _triangles;
00213 }
00214