[692] | 1 | #include "mesh.h"
|
---|
| 2 |
|
---|
| 3 | namespace OgreMayaExporter
|
---|
| 4 | {
|
---|
| 5 | /***** Class Mesh *****/
|
---|
| 6 | // constructor
|
---|
| 7 | Mesh::Mesh(const MString& name)
|
---|
| 8 | {
|
---|
| 9 | m_name = name;
|
---|
| 10 | m_numTriangles = 0;
|
---|
| 11 | m_pSkinCluster = NULL;
|
---|
| 12 | m_pSkeleton = NULL;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | // destructor
|
---|
| 16 | Mesh::~Mesh()
|
---|
| 17 | {
|
---|
| 18 | clear();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | // clear data
|
---|
| 22 | void Mesh::clear()
|
---|
| 23 | {
|
---|
| 24 | m_name = "";
|
---|
| 25 | m_numTriangles = 0;
|
---|
| 26 | for (int i=0; i<m_submeshes.size(); i++)
|
---|
| 27 | delete m_submeshes[i];
|
---|
| 28 | m_vertices.clear();
|
---|
| 29 | m_uvsets.clear();
|
---|
| 30 | m_submeshes.clear();
|
---|
| 31 | if (m_pSkinCluster)
|
---|
| 32 | delete m_pSkinCluster;
|
---|
| 33 | m_pSkinCluster = NULL;
|
---|
| 34 | if (m_pSkeleton)
|
---|
| 35 | delete m_pSkeleton;
|
---|
| 36 | m_pSkeleton = NULL;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | // get pointer to linked skeleton
|
---|
| 40 | Skeleton* Mesh::getSkeleton()
|
---|
| 41 | {
|
---|
| 42 | return m_pSkeleton;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /*******************************************************************************
|
---|
| 46 | * Load mesh data from a maya Fn *
|
---|
| 47 | *******************************************************************************/
|
---|
| 48 | MStatus Mesh::load(MDagPath& meshDag,ParamList ¶ms)
|
---|
| 49 | {
|
---|
| 50 | MStatus stat;
|
---|
| 51 | // check that given DagPath corresponds to a mesh node
|
---|
| 52 | if (!meshDag.hasFn(MFn::kMesh))
|
---|
| 53 | return MS::kFailure;
|
---|
| 54 |
|
---|
| 55 | // get the mesh object
|
---|
| 56 | MFnMesh mesh(meshDag);
|
---|
| 57 | //initialise variables
|
---|
| 58 | std::vector<MFloatArray> weights;
|
---|
| 59 | std::vector<MIntArray> jointIds;
|
---|
| 60 | unsigned int numJoints = 0;
|
---|
| 61 | std::vector<vertexInfo> vertices;
|
---|
| 62 | MFloatPointArray points;
|
---|
| 63 | MFloatVectorArray normals;
|
---|
| 64 | vertices.resize(mesh.numVertices());
|
---|
| 65 | weights.resize(mesh.numVertices());
|
---|
| 66 | jointIds.resize(mesh.numVertices());
|
---|
| 67 | // get uv texture coordinate sets' names
|
---|
| 68 | MStringArray uvsets;
|
---|
| 69 | if (mesh.numUVSets() > 0)
|
---|
| 70 | {
|
---|
| 71 | stat = mesh.getUVSetNames(uvsets);
|
---|
| 72 | if (MS::kSuccess != stat)
|
---|
| 73 | {
|
---|
| 74 | std::cout << "Error retrieving UV sets names\n";
|
---|
| 75 | return MS::kFailure;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | //Save uvsets info
|
---|
| 79 | for (int i=m_uvsets.size(); i<uvsets.length(); i++)
|
---|
| 80 | {
|
---|
| 81 | uvset uv;
|
---|
| 82 | uv.size = 2;
|
---|
| 83 | m_uvsets.push_back(uv);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | // check the "opposite" attribute to see if we have to flip normals
|
---|
| 87 | bool opposite = false;
|
---|
| 88 | mesh.findPlug("opposite",true).getValue(opposite);
|
---|
| 89 |
|
---|
| 90 | if (params.exportVBA || params.exportSkeleton)
|
---|
| 91 | {
|
---|
| 92 | // get connected skin cluster (if present)
|
---|
| 93 | bool foundSkinCluster = false;
|
---|
| 94 | MItDependencyNodes kDepNodeIt( MFn::kSkinClusterFilter );
|
---|
| 95 | for( ;!kDepNodeIt.isDone() && !foundSkinCluster; kDepNodeIt.next())
|
---|
| 96 | {
|
---|
| 97 | MObject kObject = kDepNodeIt.item();
|
---|
| 98 | m_pSkinCluster = new MFnSkinCluster(kObject);
|
---|
| 99 | unsigned int uiNumGeometries = m_pSkinCluster->numOutputConnections();
|
---|
| 100 | for(unsigned int uiGeometry = 0; uiGeometry < uiNumGeometries; ++uiGeometry )
|
---|
| 101 | {
|
---|
| 102 | unsigned int uiIndex = m_pSkinCluster->indexForOutputConnection(uiGeometry);
|
---|
| 103 | MObject kOutputObject = m_pSkinCluster->outputShapeAtIndex(uiIndex);
|
---|
| 104 | if(kOutputObject == mesh.object())
|
---|
| 105 | {
|
---|
| 106 | std:: cout << "Found skin cluster " << m_pSkinCluster->name().asChar() << " for mesh "
|
---|
| 107 | << mesh.name().asChar() << "\n";
|
---|
| 108 | foundSkinCluster = true;
|
---|
| 109 | }
|
---|
| 110 | else
|
---|
| 111 | {
|
---|
| 112 | delete m_pSkinCluster;
|
---|
| 113 | m_pSkinCluster = NULL;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // load connected skeleton (if present)
|
---|
| 119 | if (m_pSkinCluster)
|
---|
| 120 | {
|
---|
| 121 | std::cout << "Loading skeleton data...\n";
|
---|
| 122 | if (!m_pSkeleton)
|
---|
| 123 | m_pSkeleton = new Skeleton();
|
---|
| 124 | stat = m_pSkeleton->load(m_pSkinCluster,params);
|
---|
| 125 | if (MS::kSuccess != stat)
|
---|
| 126 | {
|
---|
| 127 | std::cout << "Error loading skeleton data\n";
|
---|
| 128 | }
|
---|
| 129 | else
|
---|
| 130 | std::cout << "OK\n";
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | // get connected shaders
|
---|
| 134 | MObjectArray shaders;
|
---|
| 135 | MIntArray shaderPolygonMapping;
|
---|
| 136 | stat = mesh.getConnectedShaders(0,shaders,shaderPolygonMapping);
|
---|
| 137 | if (MS::kSuccess != stat)
|
---|
| 138 | {
|
---|
| 139 | std::cout << "Error getting connected shaders\n";
|
---|
| 140 | return MS::kFailure;
|
---|
| 141 | }
|
---|
| 142 | std::cout << "Found " << shaders.length() << " connected shaders\n";
|
---|
| 143 | if (shaders.length() <= 0)
|
---|
| 144 | {
|
---|
| 145 | std::cout << "No connected shaders, skipping mesh\n";
|
---|
| 146 | return MS::kFailure;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | // create a series of arrays of faces for each different submesh
|
---|
| 150 | std::vector<faceArray> polygonSets;
|
---|
| 151 | polygonSets.resize(shaders.length());
|
---|
| 152 |
|
---|
| 153 | // Get faces data
|
---|
| 154 | // prepare vertex table
|
---|
| 155 | for (int i=0; i<vertices.size(); i++)
|
---|
| 156 | vertices[i].next = -2;
|
---|
| 157 | //get vertex positions from mesh data
|
---|
| 158 | if (params.exportWorldCoords)
|
---|
| 159 | mesh.getPoints(points,MSpace::kWorld);
|
---|
| 160 | else
|
---|
| 161 | mesh.getPoints(points,MSpace::kTransform);
|
---|
| 162 | //get list of normals from mesh data
|
---|
| 163 | if (params.exportWorldCoords)
|
---|
| 164 | mesh.getNormals(normals,MSpace::kWorld);
|
---|
| 165 | else
|
---|
| 166 | mesh.getNormals(normals,MSpace::kTransform);
|
---|
| 167 | //get list of vertex weights
|
---|
| 168 | if (m_pSkinCluster)
|
---|
| 169 | {
|
---|
| 170 | std::cout << "Get vbas\n";
|
---|
| 171 | MItGeometry iterGeom(meshDag);
|
---|
| 172 | for (i=0; !iterGeom.isDone(); iterGeom.next(), i++)
|
---|
| 173 | {
|
---|
| 174 | MObject component = iterGeom.component();
|
---|
| 175 | MFloatArray vertexWeights;
|
---|
| 176 | stat=m_pSkinCluster->getWeights(meshDag,component,vertexWeights,numJoints);
|
---|
| 177 | weights[i]=vertexWeights;
|
---|
| 178 | if (MS::kSuccess != stat)
|
---|
| 179 | {
|
---|
| 180 | std::cout << "Error retrieving vertex weights\n";
|
---|
| 181 | }
|
---|
| 182 | // get ids for the joints
|
---|
| 183 | if (m_pSkeleton)
|
---|
| 184 | {
|
---|
| 185 | MDagPathArray influenceObjs;
|
---|
| 186 | m_pSkinCluster->influenceObjects(influenceObjs,&stat);
|
---|
| 187 | if (MS::kSuccess != stat)
|
---|
| 188 | {
|
---|
| 189 | std::cout << "Error retrieving influence objects for given skin cluster\n";
|
---|
| 190 | }
|
---|
| 191 | jointIds[i].setLength(weights[i].length());
|
---|
| 192 | for (int j=0; j<influenceObjs.length(); j++)
|
---|
| 193 | {
|
---|
| 194 | bool foundJoint = false;
|
---|
| 195 | for (int k=0; k<m_pSkeleton->getJoints().size() && !foundJoint; k++)
|
---|
| 196 | {
|
---|
| 197 | if (influenceObjs[j].partialPathName() == m_pSkeleton->getJoints()[k].name)
|
---|
| 198 | {
|
---|
| 199 | foundJoint=true;
|
---|
| 200 | jointIds[i][j] = m_pSkeleton->getJoints()[k].id;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | // create an iterator to go through mesh polygons
|
---|
| 208 | if (mesh.numPolygons() > 0)
|
---|
| 209 | {
|
---|
| 210 | std::cout << "Iterate over mesh polygons\n";
|
---|
| 211 | MItMeshPolygon faceIter(mesh.object(),&stat);
|
---|
| 212 | if (MS::kSuccess != stat)
|
---|
| 213 | {
|
---|
| 214 | std::cout << "Error accessing mesh polygons\n";
|
---|
| 215 | return MS::kFailure;
|
---|
| 216 | }
|
---|
| 217 | std::cout << "num polygons = " << mesh.numPolygons() << "\n";
|
---|
| 218 | // iterate over mesh polygons
|
---|
| 219 | for (; !faceIter.isDone(); faceIter.next())
|
---|
| 220 | {
|
---|
| 221 | int numTris=0;
|
---|
| 222 | faceIter.numTriangles(numTris);
|
---|
| 223 | // for every triangle composing current polygon extract triangle info
|
---|
| 224 | for (int iTris=0; iTris<numTris; iTris++)
|
---|
| 225 | {
|
---|
| 226 | MPointArray triPoints;
|
---|
| 227 | MIntArray tempTriVertexIdx,triVertexIdx;
|
---|
| 228 | int idx;
|
---|
| 229 | // create a new face to store triangle info
|
---|
| 230 | face newFace;
|
---|
| 231 | // extract triangle vertex indices
|
---|
| 232 | faceIter.getTriangle(iTris,triPoints,tempTriVertexIdx);
|
---|
| 233 | // convert indices to face-relative indices
|
---|
| 234 | MIntArray polyIndices;
|
---|
| 235 | faceIter.getVertices(polyIndices);
|
---|
| 236 | unsigned int iPoly, iObj;
|
---|
| 237 | for (iObj=0; iObj < tempTriVertexIdx.length(); ++iObj)
|
---|
| 238 | {
|
---|
| 239 | // iPoly is face-relative vertex index
|
---|
| 240 | for (iPoly=0; iPoly < polyIndices.length(); ++iPoly)
|
---|
| 241 | {
|
---|
| 242 | if (tempTriVertexIdx[iObj] == polyIndices[iPoly])
|
---|
| 243 | {
|
---|
| 244 | triVertexIdx.append(iPoly);
|
---|
| 245 | break;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | // iterate over triangle's vertices
|
---|
| 250 | for (int i=0; i<3; i++)
|
---|
| 251 | {
|
---|
| 252 | bool different = true;
|
---|
| 253 | int vtxIdx = faceIter.vertexIndex(triVertexIdx[i]);
|
---|
| 254 | int nrmIdx = faceIter.normalIndex(triVertexIdx[i]);
|
---|
| 255 |
|
---|
| 256 | // get vertex color
|
---|
| 257 | MColor color;
|
---|
| 258 | if (faceIter.hasColor(triVertexIdx[i]))
|
---|
| 259 | {
|
---|
| 260 | stat = faceIter.getColor(color,triVertexIdx[i]);
|
---|
| 261 | if (MS::kSuccess != stat)
|
---|
| 262 | {
|
---|
| 263 | color = MColor(1,1,1,1);
|
---|
| 264 | }
|
---|
| 265 | if (color.r > 1)
|
---|
| 266 | color.r = 1;
|
---|
| 267 | if (color.g > 1)
|
---|
| 268 | color.g = 1;
|
---|
| 269 | if (color.b > 1)
|
---|
| 270 | color.b = 1;
|
---|
| 271 | if (color.a > 1)
|
---|
| 272 | color.a = 1;
|
---|
| 273 | }
|
---|
| 274 | else
|
---|
| 275 | {
|
---|
| 276 | color = MColor(1,1,1,1);
|
---|
| 277 | }
|
---|
| 278 | if (vertices[vtxIdx].next == -2) // first time we encounter a vertex in this position
|
---|
| 279 | {
|
---|
| 280 | // save vertex position
|
---|
| 281 | points[vtxIdx].cartesianize();
|
---|
| 282 | vertices[vtxIdx].pointIdx = vtxIdx;
|
---|
| 283 | // save vertex normal
|
---|
| 284 | vertices[vtxIdx].normalIdx = nrmIdx;
|
---|
| 285 | // save vertex colour
|
---|
| 286 | vertices[vtxIdx].r = color.r;
|
---|
| 287 | vertices[vtxIdx].g = color.g;
|
---|
| 288 | vertices[vtxIdx].b = color.b;
|
---|
| 289 | vertices[vtxIdx].a = color.a;
|
---|
| 290 | // save vertex texture coordinates
|
---|
| 291 | vertices[vtxIdx].u.resize(uvsets.length());
|
---|
| 292 | vertices[vtxIdx].v.resize(uvsets.length());
|
---|
| 293 | // save vbas
|
---|
| 294 | vertices[vtxIdx].vba.resize(weights[vtxIdx].length());
|
---|
| 295 | for (int j=0; j<weights[vtxIdx].length(); j++)
|
---|
| 296 | {
|
---|
| 297 | vertices[vtxIdx].vba[j] = (weights[vtxIdx])[j];
|
---|
| 298 | }
|
---|
| 299 | // save joint ids
|
---|
| 300 | vertices[vtxIdx].jointIds.resize(jointIds[vtxIdx].length());
|
---|
| 301 | for (int j=0; j<jointIds[vtxIdx].length(); j++)
|
---|
| 302 | {
|
---|
| 303 | vertices[vtxIdx].jointIds[j] = (jointIds[vtxIdx])[j];
|
---|
| 304 | }
|
---|
| 305 | // save uv sets data
|
---|
| 306 | for (int j=0; j<uvsets.length(); j++)
|
---|
| 307 | {
|
---|
| 308 | float2 uv;
|
---|
| 309 | stat = faceIter.getUV(triVertexIdx[i],uv,&uvsets[j]);
|
---|
| 310 | if (MS::kSuccess != stat)
|
---|
| 311 | {
|
---|
| 312 | uv[0] = 0;
|
---|
| 313 | uv[1] = 0;
|
---|
| 314 | }
|
---|
| 315 | vertices[vtxIdx].u[j] = uv[0];
|
---|
| 316 | vertices[vtxIdx].v[j] = (-1)*(uv[1]-1);
|
---|
| 317 | }
|
---|
| 318 | // save vertex index in face info
|
---|
| 319 | newFace.v[i] = m_vertices.size() + vtxIdx;
|
---|
| 320 | // update value of index to next vertex info (-1 means nothing next)
|
---|
| 321 | vertices[vtxIdx].next = -1;
|
---|
| 322 | }
|
---|
| 323 | else // already found at least 1 vertex in this position
|
---|
| 324 | {
|
---|
| 325 | // check if a vertex with same attributes has been saved already
|
---|
| 326 | for (int k=vtxIdx; k!=-1 && different; k=vertices[k].next)
|
---|
| 327 | {
|
---|
| 328 | different = false;
|
---|
| 329 |
|
---|
| 330 | if (params.exportVertNorm)
|
---|
| 331 | {
|
---|
| 332 | MFloatVector n1 = normals[vertices[k].normalIdx];
|
---|
| 333 | MFloatVector n2 = normals[nrmIdx];
|
---|
| 334 | if (n1.x!=n2.x || n1.y!=n2.y || n1.z!=n2.z)
|
---|
| 335 | {
|
---|
| 336 | different = true;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | if ((params.exportVertCol) &&
|
---|
| 341 | (vertices[k].r!=color.r || vertices[k].g!=color.g || vertices[k].b!= color.b || vertices[k].a!=color.a))
|
---|
| 342 | {
|
---|
| 343 | different = true;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | if (params.exportTexCoord)
|
---|
| 347 | {
|
---|
| 348 | for (int j=0; j<uvsets.length(); j++)
|
---|
| 349 | {
|
---|
| 350 | float2 uv;
|
---|
| 351 | stat = faceIter.getUV(triVertexIdx[i],uv,&uvsets[j]);
|
---|
| 352 | if (MS::kSuccess != stat)
|
---|
| 353 | {
|
---|
| 354 | uv[0] = 0;
|
---|
| 355 | uv[1] = 0;
|
---|
| 356 | }
|
---|
| 357 | uv[1] = (-1)*(uv[1]-1);
|
---|
| 358 | if (vertices[k].u[j]!=uv[0] || vertices[k].v[j]!=uv[1])
|
---|
| 359 | {
|
---|
| 360 | different = true;
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 | idx = k;
|
---|
| 365 | }
|
---|
| 366 | // if no identical vertex has been saved, then save the vertex info
|
---|
| 367 | if (different)
|
---|
| 368 | {
|
---|
| 369 | vertexInfo vtx;
|
---|
| 370 | // save vertex position
|
---|
| 371 | vtx.pointIdx = vtxIdx;
|
---|
| 372 | // save vertex normal
|
---|
| 373 | vtx.normalIdx = nrmIdx;
|
---|
| 374 | // save vertex colour
|
---|
| 375 | vtx.r = color.r;
|
---|
| 376 | vtx.g = color.g;
|
---|
| 377 | vtx.b = color.b;
|
---|
| 378 | vtx.a = color.a;
|
---|
| 379 | // save vertex vba
|
---|
| 380 | vtx.vba.resize(weights[vtxIdx].length());
|
---|
| 381 | for (int j=0; j<weights[vtxIdx].length(); j++)
|
---|
| 382 | {
|
---|
| 383 | vtx.vba[j] = (weights[vtxIdx])[j];
|
---|
| 384 | }
|
---|
| 385 | // save joint ids
|
---|
| 386 | vtx.jointIds.resize(jointIds[vtxIdx].length());
|
---|
| 387 | for (j=0; j<jointIds[vtxIdx].length(); j++)
|
---|
| 388 | {
|
---|
| 389 | vtx.jointIds[j] = (jointIds[vtxIdx])[j];
|
---|
| 390 | }
|
---|
| 391 | // save vertex texture coordinates
|
---|
| 392 | vtx.u.resize(uvsets.length());
|
---|
| 393 | vtx.v.resize(uvsets.length());
|
---|
| 394 | for (j=0; j<uvsets.length(); j++)
|
---|
| 395 | {
|
---|
| 396 | float2 uv;
|
---|
| 397 | stat = faceIter.getUV(triVertexIdx[i],uv,&uvsets[j]);
|
---|
| 398 | if (MS::kSuccess != stat)
|
---|
| 399 | {
|
---|
| 400 | uv[0] = 0;
|
---|
| 401 | uv[1] = 0;
|
---|
| 402 | }
|
---|
| 403 | vtx.u[j] = uv[0];
|
---|
| 404 | vtx.v[j] = (-1)*(uv[1]-1);
|
---|
| 405 | }
|
---|
| 406 | vtx.next = -1;
|
---|
| 407 | vertices.push_back(vtx);
|
---|
| 408 | // save vertex index in face info
|
---|
| 409 | newFace.v[i] = m_vertices.size() + vertices.size()-1;
|
---|
| 410 | vertices[idx].next = vertices.size()-1;
|
---|
| 411 | }
|
---|
| 412 | else
|
---|
| 413 | {
|
---|
| 414 | newFace.v[i] = m_vertices.size() + idx;
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | } // end iteration of triangle vertices
|
---|
| 418 | // add face info to the array corresponding to the submesh it belongs
|
---|
| 419 | // skip faces with no shaders assigned
|
---|
| 420 | if (shaderPolygonMapping[faceIter.index()] >= 0)
|
---|
| 421 | polygonSets[shaderPolygonMapping[faceIter.index()]].push_back(newFace);
|
---|
| 422 | } // end iteration of triangles
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | std::cout << "done reading mesh triangles\n";
|
---|
| 426 |
|
---|
| 427 | // if we are using shared geometry, then create a list of vertices for the whole mesh
|
---|
| 428 | if (params.useSharedGeom)
|
---|
| 429 | {
|
---|
| 430 | std::cout << "Create list of shared vertices\n";
|
---|
| 431 | for (i=0; i<vertices.size(); i++)
|
---|
| 432 | {
|
---|
| 433 | vertex v;
|
---|
| 434 | vertexInfo vInfo = vertices[i];
|
---|
| 435 | // save vertex coordinates
|
---|
| 436 | v.x = points[vInfo.pointIdx].x;
|
---|
| 437 | v.y = points[vInfo.pointIdx].y;
|
---|
| 438 | v.z = points[vInfo.pointIdx].z;
|
---|
| 439 | // save vertex normal
|
---|
| 440 | if (opposite)
|
---|
| 441 | {
|
---|
| 442 | v.n.x = -normals[vInfo.normalIdx].x;
|
---|
| 443 | v.n.y = -normals[vInfo.normalIdx].y;
|
---|
| 444 | v.n.z = -normals[vInfo.normalIdx].z;
|
---|
| 445 | }
|
---|
| 446 | else
|
---|
| 447 | {
|
---|
| 448 | v.n.x = normals[vInfo.normalIdx].x;
|
---|
| 449 | v.n.y = normals[vInfo.normalIdx].y;
|
---|
| 450 | v.n.z = normals[vInfo.normalIdx].z;
|
---|
| 451 | }
|
---|
| 452 | v.n.normalize();
|
---|
| 453 | // save vertex color
|
---|
| 454 | v.r = vInfo.r;
|
---|
| 455 | v.g = vInfo.g;
|
---|
| 456 | v.b = vInfo.b;
|
---|
| 457 | v.a = vInfo.a;
|
---|
| 458 | // save vertex bone assignements
|
---|
| 459 | for (int k=0; k<vInfo.vba.size(); k++)
|
---|
| 460 | {
|
---|
| 461 | vba newVba;
|
---|
| 462 | newVba.jointIdx = vInfo.jointIds[k];
|
---|
| 463 | newVba.weight = vInfo.vba[k];
|
---|
| 464 | v.vbas.push_back(newVba);
|
---|
| 465 | }
|
---|
| 466 | // save texture coordinates
|
---|
| 467 | for (k=0; k<vInfo.u.size(); k++)
|
---|
| 468 | {
|
---|
| 469 | texcoords newTexCoords;
|
---|
| 470 | newTexCoords.u = vInfo.u[k];
|
---|
| 471 | newTexCoords.v = vInfo.v[k];
|
---|
| 472 | newTexCoords.w = 0;
|
---|
| 473 | v.texcoords.push_back(newTexCoords);
|
---|
| 474 | }
|
---|
| 475 | // add newly created vertex to vertices list
|
---|
| 476 | m_vertices.push_back(v);
|
---|
| 477 | }
|
---|
| 478 | std::cout << "done creating vertices list\n";
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | // create a submesh for every different shader linked to the mesh
|
---|
| 482 | for (i=0; i<shaders.length(); i++)
|
---|
| 483 | {
|
---|
| 484 | // check if the submesh has at least 1 triangle
|
---|
| 485 | if (polygonSets[i].size() > 0)
|
---|
| 486 | {
|
---|
| 487 | //create new submesh
|
---|
| 488 | Submesh* pSubmesh = new Submesh();
|
---|
| 489 |
|
---|
| 490 | //load linked shader
|
---|
| 491 | stat = pSubmesh->loadMaterial(shaders[i],uvsets,params);
|
---|
| 492 | if (stat != MS::kSuccess)
|
---|
| 493 | {
|
---|
| 494 | MFnDependencyNode shadingGroup(shaders[i]);
|
---|
| 495 | std::cout << "Error loading submesh linked to shader " << shadingGroup.name().asChar() << "\n";
|
---|
| 496 | return MS::kFailure;
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | //load vertex and face data
|
---|
| 500 | stat = pSubmesh->load(polygonSets[i],vertices,points,normals,uvsets,params,opposite);
|
---|
| 501 |
|
---|
| 502 | //add submesh to current mesh
|
---|
| 503 | m_submeshes.push_back(pSubmesh);
|
---|
| 504 | //update number of triangles composing the mesh
|
---|
| 505 | m_numTriangles += pSubmesh->numTriangles();
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | return MS::kSuccess;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 |
|
---|
| 513 | // Write mesh data to Ogre XML
|
---|
| 514 | MStatus Mesh::writeXML(ParamList ¶ms)
|
---|
| 515 | {
|
---|
| 516 | MStatus stat;
|
---|
| 517 | // start mesh description
|
---|
| 518 | params.outMesh << "<mesh>\n";
|
---|
| 519 | // write shared geometry (if used)
|
---|
| 520 | if (params.useSharedGeom)
|
---|
| 521 | {
|
---|
| 522 | params.outMesh << "\t<sharedgeometry vertexcount=\"" << m_vertices.size() << "\">\n";
|
---|
| 523 | params.outMesh << "\t\t<vertexbuffer positions=\"true\" normals=";
|
---|
| 524 | if (params.exportVertNorm)
|
---|
| 525 | params.outMesh << "\"true\"";
|
---|
| 526 | else
|
---|
| 527 | params.outMesh << "\"false\"";
|
---|
| 528 | params.outMesh << " colours_diffuse=";
|
---|
| 529 | if (params.exportVertCol)
|
---|
| 530 | params.outMesh << "\"true\"";
|
---|
| 531 | else
|
---|
| 532 | params.outMesh << "\"false\"";
|
---|
| 533 | params.outMesh << " colours_specular=\"false\" texture_coords=\"";
|
---|
| 534 | if (params.exportTexCoord)
|
---|
| 535 | params.outMesh << m_uvsets.size() << "\">\n";
|
---|
| 536 | else
|
---|
| 537 | params.outMesh << 0 << "\">\n";
|
---|
| 538 | // write vertex data
|
---|
| 539 | for (int i=0; i < m_vertices.size(); i++)
|
---|
| 540 | {
|
---|
| 541 | params.outMesh << "\t\t\t<vertex>\n";
|
---|
| 542 | //write vertex position
|
---|
| 543 | params.outMesh << "\t\t\t\t<position x=\"" << m_vertices[i].x << "\" y=\""
|
---|
| 544 | << m_vertices[i].y << "\" " << "z=\"" << m_vertices[i].z << "\"/>\n";
|
---|
| 545 | //write vertex normal
|
---|
| 546 | if (params.exportVertNorm)
|
---|
| 547 | {
|
---|
| 548 | params.outMesh << "\t\t\t\t<normal x=\"" << m_vertices[i].n.x << "\" y=\""
|
---|
| 549 | << m_vertices[i].n.y << "\" " << "z=\"" << m_vertices[i].n.z << "\"/>\n";
|
---|
| 550 | }
|
---|
| 551 | //write vertex colour
|
---|
| 552 | if (params.exportVertCol)
|
---|
| 553 | {
|
---|
| 554 | float r,g,b,a;
|
---|
| 555 | if (params.exportVertColWhite)
|
---|
| 556 | {
|
---|
| 557 | r = g = b = a = 1.0f;
|
---|
| 558 | }
|
---|
| 559 | else
|
---|
| 560 | {
|
---|
| 561 | r = m_vertices[i].r;
|
---|
| 562 | g = m_vertices[i].g;
|
---|
| 563 | b = m_vertices[i].b;
|
---|
| 564 | a = m_vertices[i].a;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | params.outMesh << "\t\t\t\t<colour_diffuse value=\"" << r << " " << g
|
---|
| 568 | << " " << b << " " << a << "\"/>\n";
|
---|
| 569 | }//write vertex texture coordinates
|
---|
| 570 | if (params.exportTexCoord)
|
---|
| 571 | {
|
---|
| 572 | for (int j=0; j<m_uvsets.size(); j++)
|
---|
| 573 | {
|
---|
| 574 | if (j < m_vertices[i].texcoords.size())
|
---|
| 575 | {
|
---|
| 576 | params.outMesh << "\t\t\t\t<texcoord u=\"" << m_vertices[i].texcoords[j].u << "\" v=\"" <<
|
---|
| 577 | m_vertices[i].texcoords[j].v << "\"/>\n";
|
---|
| 578 | }
|
---|
| 579 | else
|
---|
| 580 | {
|
---|
| 581 | params.outMesh << "\t\t\t\t<texcoord u=\"0\" v=\"0\"/>\n";
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 | }
|
---|
| 585 | params.outMesh << "\t\t\t</vertex>\n";
|
---|
| 586 | }
|
---|
| 587 | params.outMesh << "\t\t</vertexbuffer>\n";
|
---|
| 588 | params.outMesh << "\t</sharedgeometry>\n";
|
---|
| 589 | }
|
---|
| 590 | // write submeshes data
|
---|
| 591 | params.outMesh << "\t<submeshes>\n";
|
---|
| 592 | for (int i=0; i < m_submeshes.size(); i++)
|
---|
| 593 | {
|
---|
| 594 | stat = m_submeshes[i]->writeXML(params);
|
---|
| 595 | if (MS::kSuccess != stat)
|
---|
| 596 | {
|
---|
| 597 | std::cout << "Error writing submesh " << m_submeshes[i]->name().asChar() << ", aborting operation\n";
|
---|
| 598 | return MS::kFailure;
|
---|
| 599 | }
|
---|
| 600 | }
|
---|
| 601 | params.outMesh << "\t</submeshes>\n";
|
---|
| 602 | // write skeleton link
|
---|
| 603 | if (params.exportSkeleton && m_pSkeleton)
|
---|
| 604 | {
|
---|
| 605 | int ri = params.skeletonFilename.rindex('\\');
|
---|
| 606 | int end = params.skeletonFilename.length() - 1;
|
---|
| 607 | MString filename = params.skeletonFilename.substring(ri+1,end);
|
---|
| 608 | if (filename.substring(filename.length()-4,filename.length()-1) == MString(".xml")
|
---|
| 609 | && filename.length() >= 5)
|
---|
| 610 | filename = filename.substring(0,filename.length()-5);
|
---|
| 611 | params.outMesh << "\t<skeletonlink name=\"" << filename.asChar() << "\"/>\n";
|
---|
| 612 | }
|
---|
| 613 | // Write shared geometry bone assignments
|
---|
| 614 | if (params.useSharedGeom && params.exportVBA)
|
---|
| 615 | {
|
---|
| 616 | params.outMesh << "\t<boneassignments>\n";
|
---|
| 617 | for (int i=0; i<m_vertices.size(); i++)
|
---|
| 618 | {
|
---|
| 619 | for (int j=0; j<m_vertices[i].vbas.size(); j++)
|
---|
| 620 | {
|
---|
| 621 | if (m_vertices[i].vbas[j].weight > 0.001)
|
---|
| 622 | {
|
---|
| 623 | params.outMesh << "\t\t<vertexboneassignment vertexindex=\"" << i
|
---|
| 624 | << "\" boneindex=\"" << m_vertices[i].vbas[j].jointIdx << "\" weight=\""
|
---|
| 625 | << m_vertices[i].vbas[j].weight <<"\"/>\n";
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | }
|
---|
| 629 | params.outMesh << "\t</boneassignments>\n";
|
---|
| 630 | }
|
---|
| 631 | // write submesh names
|
---|
| 632 | params.outMesh << "\t<submeshnames>\n";
|
---|
| 633 | for (i=0; i<m_submeshes.size(); i++)
|
---|
| 634 | {
|
---|
| 635 | if (m_submeshes[i]->name() != "")
|
---|
| 636 | params.outMesh << "\t\t<submeshname name=\"" << m_submeshes[i]->name().asChar() << "\" index=\"" << i << "\"/>\n";
|
---|
| 637 | }
|
---|
| 638 | params.outMesh << "\t</submeshnames>\n";
|
---|
| 639 | // end mesh description
|
---|
| 640 | params.outMesh << "</mesh>\n";
|
---|
| 641 |
|
---|
| 642 | return MS::kSuccess;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | }; //end of namespace
|
---|