1 |
|
---|
2 | #include "OgreExporter.h"
|
---|
3 |
|
---|
4 | #include "Ogre.h"
|
---|
5 |
|
---|
6 | //#include <crtdbg.h>
|
---|
7 |
|
---|
8 | #include <zlib.h>
|
---|
9 |
|
---|
10 | /*
|
---|
11 | #include <maya/MFnDagNode.h>
|
---|
12 | #include <maya/MDagPath.h>
|
---|
13 | #include <maya/MFnDependencyNode.h>
|
---|
14 | #include <maya/M3dView.h>
|
---|
15 | #include <maya/MItDependencyGraph.h>
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <maya/MFnMesh.h>
|
---|
19 | #include <maya/MItDag.h>
|
---|
20 | #include <maya/MFnPlugin.h>
|
---|
21 | #include <maya/MItMeshPolygon.h>
|
---|
22 | #include <maya/MItMeshVertex.h>
|
---|
23 | #include <maya/MFloatPointArray.h>
|
---|
24 | #include <maya/MPointArray.h>
|
---|
25 | #include <maya/MMatrix.h>
|
---|
26 |
|
---|
27 | #include <vector>
|
---|
28 | #include <functional>
|
---|
29 | #include <algorithm>
|
---|
30 | #include "Windows.h"
|
---|
31 |
|
---|
32 | const char* const c_pPlugName = "OgreGeometry";
|
---|
33 |
|
---|
34 |
|
---|
35 | MayaMesh::MayaMesh( const MDagPath &meshDagPath, const MDagPath &meshParentPath, const MObject &meshObject ) :
|
---|
36 | m_meshDagPath ( meshDagPath ),
|
---|
37 | m_meshParentPath( meshParentPath ),
|
---|
38 | m_meshObject ( meshObject )
|
---|
39 | {
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | MayaBone::MayaBone( const MDagPath &boneDagPath, const MDagPath &boneParentPath, const MObject &boneObject ) :
|
---|
45 | m_boneDagPath ( boneDagPath ),
|
---|
46 | m_boneParentPath( boneParentPath ),
|
---|
47 | m_boneObject ( boneObject ),
|
---|
48 | m_references ( 0 )
|
---|
49 | {
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | OgreExporter::OgreExporter()
|
---|
59 | {
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|
63 | void* OgreExporter::creator()
|
---|
64 | {
|
---|
65 | return new OgreExporter;
|
---|
66 | }
|
---|
67 |
|
---|
68 | MDagPath GetParentDagPath(const MDagPath& path)
|
---|
69 | {
|
---|
70 | MDagPath pathToParent(path);
|
---|
71 | while (pathToParent.pop() == MS::kSuccess)
|
---|
72 | {
|
---|
73 | if (pathToParent.node().hasFn(MFn::kTransform))
|
---|
74 | return pathToParent;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return pathToParent;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 | void collectStuff( std::vector< MayaMesh > * const pMeshes, std::vector< MayaBone > * const pBones )
|
---|
83 | {
|
---|
84 | assert(pMeshes);
|
---|
85 | assert(pBones);
|
---|
86 |
|
---|
87 | assert( pMeshes->size() == 0 );
|
---|
88 |
|
---|
89 | //pMeshes->resize(0);
|
---|
90 |
|
---|
91 | //pBones->resize(0);
|
---|
92 |
|
---|
93 | MItDag dag_itr( MItDag::kDepthFirst, MFn::kInvalid );
|
---|
94 | while( dag_itr.isDone() == false )
|
---|
95 | {
|
---|
96 | MDagPath dagPath;
|
---|
97 | dag_itr.getPath( dagPath );
|
---|
98 |
|
---|
99 | const MFnDagNode dagNode( dagPath );
|
---|
100 | if (dagNode.isIntermediateObject())
|
---|
101 | {
|
---|
102 | dag_itr.next();
|
---|
103 | continue;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if ( dagPath.hasFn(MFn::kMesh) &&
|
---|
107 | !dagPath.hasFn(MFn::kTransform) )
|
---|
108 | {
|
---|
109 | pMeshes->push_back( MayaMesh( dagPath, GetParentDagPath( dagPath ), dag_itr.item() ) );
|
---|
110 |
|
---|
111 | }
|
---|
112 | else if (dag_itr.item().apiType() == MFn::kJoint ||
|
---|
113 | dag_itr.item().apiType() == MFn::kTransform)
|
---|
114 | {
|
---|
115 | MDagPath boneDagPath;
|
---|
116 |
|
---|
117 | dag_itr.getPath( boneDagPath );
|
---|
118 |
|
---|
119 | pBones->push_back( MayaBone( boneDagPath, GetParentDagPath( boneDagPath ), dag_itr.item() ) );
|
---|
120 | }
|
---|
121 |
|
---|
122 | dag_itr.next();
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | class IndirectVert
|
---|
127 | {
|
---|
128 | public:
|
---|
129 |
|
---|
130 | IndirectVert( void ) :
|
---|
131 | m_newVertIndex( -1 ),
|
---|
132 | m_posIndex ( -1 ),
|
---|
133 | m_normalIndex ( -1 ),
|
---|
134 | m_uv0Index ( -1 ),
|
---|
135 | m_colorIndex ( -1 )
|
---|
136 | {
|
---|
137 | calcHash();
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | IndirectVert( const int newVertIndex, const int posIndex, const int normalIndex, const int uv0Index, const int colorIndex ) :
|
---|
142 | m_newVertIndex( newVertIndex ),
|
---|
143 | m_posIndex ( posIndex ),
|
---|
144 | m_normalIndex ( normalIndex ),
|
---|
145 | m_uv0Index ( uv0Index ),
|
---|
146 | m_colorIndex ( colorIndex )
|
---|
147 | {
|
---|
148 | calcHash();
|
---|
149 | }
|
---|
150 |
|
---|
151 | void calcHash(void)
|
---|
152 | {
|
---|
153 | unsigned char* buf = new unsigned char[sizeof(int) * 4];
|
---|
154 | unsigned char* pBuf = buf;
|
---|
155 | memcpy(pBuf, &m_posIndex, sizeof(int));
|
---|
156 | pBuf += sizeof(int);
|
---|
157 | memcpy(pBuf, &m_normalIndex, sizeof(int));
|
---|
158 | pBuf += sizeof(int);
|
---|
159 | memcpy(pBuf, &m_uv0Index, sizeof(int));
|
---|
160 | pBuf += sizeof(int);
|
---|
161 | memcpy(pBuf, &m_colorIndex, sizeof(int));
|
---|
162 |
|
---|
163 | // Use zlib crc32
|
---|
164 | unsigned long crc = crc32(0L, Z_NULL, 0);
|
---|
165 | m_hash = crc32(crc, buf, sizeof(int) * 4);
|
---|
166 | }
|
---|
167 |
|
---|
168 | const unsigned long hash( void ) const
|
---|
169 | {
|
---|
170 | return m_hash;
|
---|
171 | }
|
---|
172 |
|
---|
173 | bool operator ==( const IndirectVert &other ) const
|
---|
174 | {
|
---|
175 | return m_posIndex == other.m_posIndex &&
|
---|
176 | m_normalIndex == other.m_normalIndex &&
|
---|
177 | m_uv0Index == other.m_uv0Index &&
|
---|
178 | m_colorIndex == other.m_colorIndex;
|
---|
179 | }
|
---|
180 |
|
---|
181 | bool operator !=( const IndirectVert &other ) const
|
---|
182 | {
|
---|
183 | return !(m_hash == other.m_hash);
|
---|
184 | }
|
---|
185 |
|
---|
186 | bool operator <( const IndirectVert &other ) const
|
---|
187 | {
|
---|
188 | return m_hash < other.m_hash;
|
---|
189 | }
|
---|
190 |
|
---|
191 | //Ease of access. Privitise later.
|
---|
192 | //Does not participate in hashing
|
---|
193 | int m_newVertIndex;
|
---|
194 |
|
---|
195 | //Participate in the hashing.
|
---|
196 | int m_posIndex;
|
---|
197 | int m_normalIndex;
|
---|
198 | int m_uv0Index;
|
---|
199 | int m_colorIndex;
|
---|
200 |
|
---|
201 | private:
|
---|
202 |
|
---|
203 | unsigned long m_hash;
|
---|
204 |
|
---|
205 | };
|
---|
206 |
|
---|
207 | typedef std::hash_set< IndirectVert > IndVertHS;
|
---|
208 |
|
---|
209 | struct std::hash<IndirectVert>
|
---|
210 | {
|
---|
211 | size_t operator ()( const IndirectVert &vert ) const
|
---|
212 | {
|
---|
213 | return vert.hash();
|
---|
214 | }
|
---|
215 | };
|
---|
216 |
|
---|
217 | /*
|
---|
218 | void uniqueifyVerts
|
---|
219 | (
|
---|
220 | std::vector< IndirectVert > * const vertList,
|
---|
221 | const std::vector< int > &posIndices,
|
---|
222 | const std::vector< int > &normalIndices,
|
---|
223 | const std::vector< int > &uv0Indices,
|
---|
224 | const std::vector< int > &colorIndices
|
---|
225 | )
|
---|
226 | {
|
---|
227 | assert( posIndices.size() == normalIndices.size() == uv0Indices.size() == colorIndices.size() );
|
---|
228 |
|
---|
229 | std::hash_set< IndirectVert > m_vertHash;
|
---|
230 |
|
---|
231 | const int numVerts = posIndices.size();
|
---|
232 |
|
---|
233 | for( int i=0; i<numVerts; ++i )
|
---|
234 | {
|
---|
235 | const IndirectVert vert( posIndices[ i ], normalIndices[ i ], uv0Indices[ i ], colorIndices[ i ] );
|
---|
236 |
|
---|
237 | if( m_vertHash.find( vert )
|
---|
238 | }
|
---|
239 | }
|
---|
240 | */
|
---|
241 |
|
---|
242 |
|
---|
243 | MStatus exportMesh( const MayaMesh &mesh, Ogre::Mesh * const ogreMesh )
|
---|
244 | {
|
---|
245 | MStatus stat = MS::kSuccess;
|
---|
246 | const MSpace::Space space = MSpace::kWorld;
|
---|
247 |
|
---|
248 | MFnMesh fnMesh( mesh.path(), &stat );
|
---|
249 | if ( MS::kSuccess != stat)
|
---|
250 | {
|
---|
251 | fprintf(stderr,"Failure in MFnMesh initialization.\n");
|
---|
252 | return MS::kFailure;
|
---|
253 | }
|
---|
254 |
|
---|
255 | const MMatrix matrix = mesh.path().inclusiveMatrix( &stat );
|
---|
256 |
|
---|
257 | const float det44 = matrix.det4x4();
|
---|
258 |
|
---|
259 | bool reverseWinding = false;
|
---|
260 |
|
---|
261 | if( det44 < 0.0f )
|
---|
262 | {
|
---|
263 | reverseWinding = true;
|
---|
264 | }
|
---|
265 |
|
---|
266 | MItMeshVertex vtxIter( mesh.object(), &stat );
|
---|
267 | if ( MS::kSuccess != stat)
|
---|
268 | {
|
---|
269 | fprintf(stderr,"Failure in MItMeshVertex initialization.\n");
|
---|
270 | return MS::kFailure;
|
---|
271 | }
|
---|
272 |
|
---|
273 | Ogre::SubMesh * const ogreSubMesh = ogreMesh->createSubMesh();
|
---|
274 |
|
---|
275 | //Default ugly texture for the moment.
|
---|
276 | ogreSubMesh->setMaterialName( "Examples/Test" );
|
---|
277 |
|
---|
278 | // Set up mesh geometry
|
---|
279 | // Always 1 texture layer, 2D coords
|
---|
280 | ogreSubMesh->geometry.numTexCoords = 1;
|
---|
281 | ogreSubMesh->geometry.numTexCoordDimensions[0] = 2;
|
---|
282 | ogreSubMesh->geometry.hasNormals = true;
|
---|
283 | ogreSubMesh->geometry.hasColours = false;
|
---|
284 | ogreSubMesh->geometry.vertexStride = 0;
|
---|
285 | ogreSubMesh->geometry.texCoordStride[0] = 0;
|
---|
286 | ogreSubMesh->geometry.normalStride = 0;
|
---|
287 | ogreSubMesh->useSharedVertices = false;
|
---|
288 | ogreSubMesh->useTriStrips = false;
|
---|
289 |
|
---|
290 | /*
|
---|
291 | // Fill in vertex table
|
---|
292 | std::vector< MPoint > points;
|
---|
293 |
|
---|
294 | for ( ; !vtxIter.isDone(); vtxIter.next() )
|
---|
295 | {
|
---|
296 | const MPoint pt = vtxIter.position( space, &stat );
|
---|
297 | const MPoint ptWorld = vtxIter.position( MSpace::kWorld , &stat );
|
---|
298 | const MPoint ptLocal = vtxIter.position( MSpace::kObject, &stat );
|
---|
299 |
|
---|
300 | points.push_back( pt );
|
---|
301 | }
|
---|
302 | */
|
---|
303 |
|
---|
304 | MFloatPointArray points;
|
---|
305 | fnMesh.getPoints( points, space );
|
---|
306 |
|
---|
307 |
|
---|
308 |
|
---|
309 | MFloatVectorArray norms;
|
---|
310 | fnMesh.getNormals( norms, space );
|
---|
311 | //const int normsLength = norms.length();
|
---|
312 |
|
---|
313 | // Write out the uv table
|
---|
314 | MFloatArray uArray, vArray;
|
---|
315 | fnMesh.getUVs( uArray, vArray );
|
---|
316 | //const int uvLength = uArray.length();
|
---|
317 |
|
---|
318 | assert( uArray.length() == vArray.length() );
|
---|
319 |
|
---|
320 | ogreSubMesh->numFaces = fnMesh.numPolygons( &stat );
|
---|
321 |
|
---|
322 | MItMeshPolygon polyIter( mesh.object(), &stat );
|
---|
323 | if ( MS::kSuccess != stat)
|
---|
324 | {
|
---|
325 | fprintf( stderr, "Failure in MItMeshPolygon initialization.\n" );
|
---|
326 | return stat;
|
---|
327 | }
|
---|
328 |
|
---|
329 | std::vector< int > faceIndices;
|
---|
330 |
|
---|
331 | std::vector< IndirectVert > vertList;
|
---|
332 |
|
---|
333 | std::hash_set< IndirectVert > vertHash;
|
---|
334 |
|
---|
335 |
|
---|
336 | int curVert = 0;
|
---|
337 |
|
---|
338 | for ( ; !polyIter.isDone(); polyIter.next() )
|
---|
339 | {
|
---|
340 | const int vertCount = polyIter.polygonVertexCount( &stat );
|
---|
341 |
|
---|
342 | int start = 0;
|
---|
343 | int end = 3;
|
---|
344 | int delta = 1;
|
---|
345 |
|
---|
346 | if( reverseWinding )
|
---|
347 | {
|
---|
348 | start = 2;
|
---|
349 | end = -1;
|
---|
350 | delta = -1;
|
---|
351 | }
|
---|
352 |
|
---|
353 | for( int i = start; i != end; i+=delta )
|
---|
354 | {
|
---|
355 | const int posIndex = polyIter.vertexIndex( i, &stat );
|
---|
356 |
|
---|
357 | const int normalIndex = polyIter.normalIndex( i, &stat );
|
---|
358 |
|
---|
359 | int uv0Index;
|
---|
360 |
|
---|
361 | polyIter.getUVIndex( i, *&uv0Index );
|
---|
362 |
|
---|
363 | const IndirectVert potentialNewVert( vertList.size(), posIndex, normalIndex, uv0Index, -1 );
|
---|
364 |
|
---|
365 | const IndVertHS::iterator it = vertHash.find( potentialNewVert );
|
---|
366 |
|
---|
367 | if( it != vertHash.end() )
|
---|
368 | {
|
---|
369 | const IndirectVert vert = *it;
|
---|
370 |
|
---|
371 | faceIndices.push_back( vert.m_newVertIndex );
|
---|
372 | }
|
---|
373 | else
|
---|
374 | {
|
---|
375 | vertHash.insert( potentialNewVert );
|
---|
376 | vertList.push_back( potentialNewVert );
|
---|
377 | faceIndices.push_back( potentialNewVert.m_newVertIndex );
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | ogreSubMesh->geometry.numVertices = vertList.size();
|
---|
383 | ogreSubMesh->geometry.pVertices = new Ogre::Real[ogreSubMesh->geometry.numVertices * 3];
|
---|
384 |
|
---|
385 | ogreSubMesh->geometry.pNormals = new Ogre::Real[ogreSubMesh->geometry.numVertices * 3];
|
---|
386 | ogreSubMesh->geometry.pTexCoords[0] = new Ogre::Real[ogreSubMesh->geometry.numVertices * 2];
|
---|
387 |
|
---|
388 | for( int i=0; i<vertList.size(); ++i )
|
---|
389 | {
|
---|
390 | const IndirectVert &vert = vertList[i];
|
---|
391 |
|
---|
392 | {
|
---|
393 | const int posIndex = vert.m_posIndex;
|
---|
394 |
|
---|
395 | assert( posIndex >= 0 );
|
---|
396 | assert( posIndex < points.length() );
|
---|
397 |
|
---|
398 | const MFloatPoint &pt = points[ posIndex ];
|
---|
399 |
|
---|
400 | const int posBaseIndex = i * 3;
|
---|
401 |
|
---|
402 | ogreSubMesh->geometry.pVertices[ posBaseIndex + 0 ] = pt.x;
|
---|
403 | ogreSubMesh->geometry.pVertices[ posBaseIndex + 1 ] = pt.y;
|
---|
404 | ogreSubMesh->geometry.pVertices[ posBaseIndex + 2 ] = pt.z;
|
---|
405 | }
|
---|
406 |
|
---|
407 | {
|
---|
408 | const int normalIndex = vert.m_normalIndex;
|
---|
409 |
|
---|
410 | assert( normalIndex >= 0 );
|
---|
411 | assert( normalIndex < norms.length() );
|
---|
412 |
|
---|
413 | const MFloatVector &normal = norms[normalIndex];
|
---|
414 |
|
---|
415 | const int normBaseIndex = i * 3;
|
---|
416 |
|
---|
417 | ogreSubMesh->geometry.pNormals[ normBaseIndex + 0 ] = normal.x;
|
---|
418 | ogreSubMesh->geometry.pNormals[ normBaseIndex + 1 ] = normal.y;
|
---|
419 | ogreSubMesh->geometry.pNormals[ normBaseIndex + 2 ] = normal.z;
|
---|
420 | }
|
---|
421 |
|
---|
422 | {
|
---|
423 | const int uv0Index = vert.m_uv0Index;
|
---|
424 |
|
---|
425 | assert( uv0Index >= 0 );
|
---|
426 | assert( uv0Index < uArray.length() );
|
---|
427 |
|
---|
428 | const int uv0BaseIndex = i * 2;
|
---|
429 |
|
---|
430 | ogreSubMesh->geometry.pTexCoords[0][ uv0BaseIndex + 0 ] = uArray[ uv0Index ];
|
---|
431 | ogreSubMesh->geometry.pTexCoords[0][ uv0BaseIndex + 1 ] = vArray[ uv0Index ];
|
---|
432 | }
|
---|
433 |
|
---|
434 | }
|
---|
435 |
|
---|
436 | ogreSubMesh->faceVertexIndices = new unsigned short[ faceIndices.size() ];
|
---|
437 |
|
---|
438 | for( i=0; i<faceIndices.size(); ++i )
|
---|
439 | {
|
---|
440 | ogreSubMesh->faceVertexIndices[ i ] = faceIndices[ i ];
|
---|
441 | }
|
---|
442 |
|
---|
443 | /*
|
---|
444 | if (pSkel)
|
---|
445 | delete pSkel;
|
---|
446 | */
|
---|
447 |
|
---|
448 |
|
---|
449 | return stat;
|
---|
450 | }
|
---|
451 |
|
---|
452 | /* Old triagulation.
|
---|
453 | for ( ; !polyIter.isDone(); polyIter.next() )
|
---|
454 | {
|
---|
455 | const int vertCount = polyIter.polygonVertexCount( &stat );
|
---|
456 |
|
---|
457 | if( vertCount != 3 )
|
---|
458 | {
|
---|
459 | int dummy = 0;
|
---|
460 | }
|
---|
461 |
|
---|
462 | //MStatus MItMeshPolygon:: getTriangles ( MPointArray & points, MIntArray & vertexList, MSpace::Space space ) const
|
---|
463 |
|
---|
464 | MPointArray ptArray;
|
---|
465 | MIntArray vertIndices;
|
---|
466 |
|
---|
467 | polyIter.getTriangles( *&ptArray, *&vertIndices, space );
|
---|
468 |
|
---|
469 | assert( vertIndices.length() % 3 == 0 );
|
---|
470 |
|
---|
471 | for( int baseVertIndex=0; baseVertIndex<vertIndices.length(); baseVertIndex+=3 )
|
---|
472 | {
|
---|
473 | for( int offset = 0; offset < 3; ++offset )
|
---|
474 | {
|
---|
475 | const int vertIndex = vertIndices[ baseVertIndex + offset ];
|
---|
476 |
|
---|
477 | int polyLocalIndex = -1;
|
---|
478 |
|
---|
479 | for( int i=0; i<vertCount; ++i )
|
---|
480 | {
|
---|
481 | if( polyIter.vertexIndex( i, &stat ) == vertIndex )
|
---|
482 | {
|
---|
483 | polyLocalIndex = 0;
|
---|
484 | break;
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | assert( polyLocalIndex != -1 );
|
---|
489 |
|
---|
490 | const int posIndex = vertIndex; //polyIter.vertexIndex( vertIndex, &stat );
|
---|
491 |
|
---|
492 | const int normalIndex = polyIter.normalIndex( polyLocalIndex, &stat );
|
---|
493 |
|
---|
494 | int uv0Index;
|
---|
495 |
|
---|
496 | polyIter.getUVIndex( polyLocalIndex, *&uv0Index );
|
---|
497 |
|
---|
498 | const IndirectVert potentialNewVert( vertList.size(), posIndex, normalIndex, uv0Index, -1 );
|
---|
499 |
|
---|
500 | const IndVertHS::iterator it = vertHash.find( potentialNewVert );
|
---|
501 |
|
---|
502 | if( it != vertHash.end() )
|
---|
503 | {
|
---|
504 | const IndirectVert vert = *it;
|
---|
505 |
|
---|
506 | faceIndices.push_back( vert.m_newVertIndex );
|
---|
507 | }
|
---|
508 | else
|
---|
509 | {
|
---|
510 | vertHash.insert( potentialNewVert );
|
---|
511 | vertList.push_back( potentialNewVert );
|
---|
512 | faceIndices.push_back( potentialNewVert.m_newVertIndex );
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 | }
|
---|
517 | */
|
---|
518 |
|
---|
519 |
|
---|
520 | /*
|
---|
521 | MStatus exportMesh( const MayaMesh &mesh, Ogre::Mesh * const ogreMesh )
|
---|
522 | {
|
---|
523 | MStatus stat = MS::kSuccess;
|
---|
524 | const MSpace::Space space = MSpace::kWorld;
|
---|
525 |
|
---|
526 | MFnMesh fnMesh( mesh.path(), &stat );
|
---|
527 | if ( MS::kSuccess != stat)
|
---|
528 | {
|
---|
529 | fprintf(stderr,"Failure in MFnMesh initialization.\n");
|
---|
530 | return MS::kFailure;
|
---|
531 | }
|
---|
532 |
|
---|
533 | MItMeshVertex vtxIter( mesh.object(), &stat );
|
---|
534 | if ( MS::kSuccess != stat)
|
---|
535 | {
|
---|
536 | fprintf(stderr,"Failure in MItMeshVertex initialization.\n");
|
---|
537 | return MS::kFailure;
|
---|
538 | }
|
---|
539 |
|
---|
540 | Ogre::SubMesh * const ogreSubMesh = ogreMesh->createSubMesh();
|
---|
541 |
|
---|
542 | //Default ugly texture for the moment.
|
---|
543 | ogreSubMesh->setMaterialName("BaseWhite");
|
---|
544 |
|
---|
545 | /*
|
---|
546 | int matIdx = msMesh_GetMaterialIndex(pMesh);
|
---|
547 |
|
---|
548 | if (matIdx == -1)
|
---|
549 | {
|
---|
550 | // No material, use blank
|
---|
551 | ogreSubMesh->setMaterialName("BaseWhite");
|
---|
552 | logMgr.logMessage("No Material, using default 'BaseWhite'.");
|
---|
553 | }
|
---|
554 | else
|
---|
555 | {
|
---|
556 |
|
---|
557 | msMaterial *pMat = msModel_GetMaterialAt(pModel, matIdx);
|
---|
558 | ogreSubMesh->setMaterialName(pMat->szName);
|
---|
559 | logMgr.logMessage("SubMesh Material Done.");
|
---|
560 | }
|
---|
561 | * /
|
---|
562 |
|
---|
563 | // Set up mesh geometry
|
---|
564 | // Always 1 texture layer, 2D coords
|
---|
565 | ogreSubMesh->geometry.numTexCoords = 1;
|
---|
566 | ogreSubMesh->geometry.numTexCoordDimensions[0] = 2;
|
---|
567 | ogreSubMesh->geometry.hasNormals = true;
|
---|
568 | ogreSubMesh->geometry.hasColours = false;
|
---|
569 | ogreSubMesh->geometry.vertexStride = 0;
|
---|
570 | ogreSubMesh->geometry.texCoordStride[0] = 0;
|
---|
571 | ogreSubMesh->geometry.normalStride = 0;
|
---|
572 | ogreSubMesh->useSharedVertices = false;
|
---|
573 | ogreSubMesh->useTriStrips = false;
|
---|
574 |
|
---|
575 |
|
---|
576 | // Fill in vertex table
|
---|
577 | std::vector< MPoint > points;
|
---|
578 |
|
---|
579 | for ( ; !vtxIter.isDone(); vtxIter.next() )
|
---|
580 | {
|
---|
581 |
|
---|
582 | points.push_back( vtxIter.position( space ) );
|
---|
583 |
|
---|
584 | /* Need to look this up to see what it is.
|
---|
585 | if (ptgroups && groups)
|
---|
586 | {
|
---|
587 | int compIdx = vtxIter.index();
|
---|
588 | outputSets( mdagPath, compIdx, true );
|
---|
589 | }
|
---|
590 | fprintf(fp,"v %f %f %f\n",p.x,p.y,p.z);
|
---|
591 | v++;
|
---|
592 | * /
|
---|
593 | }
|
---|
594 |
|
---|
595 | ogreSubMesh->geometry.numVertices = points.size();
|
---|
596 | ogreSubMesh->geometry.pVertices = new Ogre::Real[ogreSubMesh->geometry.numVertices * 3];
|
---|
597 |
|
---|
598 | ogreSubMesh->geometry.pNormals = new Ogre::Real[ogreSubMesh->geometry.numVertices * 3];
|
---|
599 | ogreSubMesh->geometry.pTexCoords[0] = new Ogre::Real[ogreSubMesh->geometry.numVertices * 2];
|
---|
600 |
|
---|
601 | /*
|
---|
602 | for( unsigned i=0; i<points.size(); ++i )
|
---|
603 | {
|
---|
604 | const int index = i * 3;
|
---|
605 | ogreSubMesh->geometry.pVertices[ index + 0 ] = points[i].x;
|
---|
606 | ogreSubMesh->geometry.pVertices[ index + 1 ] = points[i].y;
|
---|
607 | ogreSubMesh->geometry.pVertices[ index + 2 ] = points[i].z;
|
---|
608 |
|
---|
609 | }
|
---|
610 | * /
|
---|
611 |
|
---|
612 |
|
---|
613 | // Write out the normal table
|
---|
614 | MFloatVectorArray norms;
|
---|
615 | fnMesh.getNormals( norms, space );
|
---|
616 | const int normsLength = norms.length();
|
---|
617 |
|
---|
618 | assert( normsLength >= points.size() );
|
---|
619 |
|
---|
620 | /*
|
---|
621 | for ( i=0; i<points.size(); ++i )
|
---|
622 | {
|
---|
623 | MFloatVector tmpf = norms[i];
|
---|
624 |
|
---|
625 | const int vertIdx = i * 3;
|
---|
626 |
|
---|
627 | ogreSubMesh->geometry.pNormals[ vertIdx + 0 ] = tmpf.x;
|
---|
628 | ogreSubMesh->geometry.pNormals[ vertIdx + 1 ] = tmpf.y;
|
---|
629 | ogreSubMesh->geometry.pNormals[ vertIdx + 2 ] = tmpf.z;
|
---|
630 | }
|
---|
631 | * /
|
---|
632 |
|
---|
633 | // Write out the uv table
|
---|
634 | MFloatArray uArray, vArray;
|
---|
635 | fnMesh.getUVs( uArray, vArray );
|
---|
636 | const int uvLength = uArray.length();
|
---|
637 |
|
---|
638 | assert( uvLength >= points.size() );
|
---|
639 |
|
---|
640 | /*
|
---|
641 | for ( i=0; i<points.size(); i++ )
|
---|
642 | {
|
---|
643 | const int index = i * 2;
|
---|
644 |
|
---|
645 | //fprintf(fp,"vt %f %f\n",uArray[x],vArray[x]);
|
---|
646 | ogreSubMesh->geometry.pTexCoords[0][ index + 0 ] = uArray[i];
|
---|
647 | ogreSubMesh->geometry.pTexCoords[0][ index + 1 ] = vArray[i];
|
---|
648 | }
|
---|
649 | * /
|
---|
650 |
|
---|
651 |
|
---|
652 | ogreSubMesh->numFaces = fnMesh.numPolygons( &stat );
|
---|
653 |
|
---|
654 | MItMeshPolygon polyIter( mesh.object(), &stat );
|
---|
655 | if ( MS::kSuccess != stat)
|
---|
656 | {
|
---|
657 | fprintf(stderr,"Failure in MItMeshPolygon initialization.\n");
|
---|
658 | return MS::kFailure;
|
---|
659 | }
|
---|
660 |
|
---|
661 | std::vector< int > faceIndices;
|
---|
662 |
|
---|
663 | std::vector< IndirectVert > vertList;
|
---|
664 |
|
---|
665 | std::hash_set< IndirectVert > m_vertHash;
|
---|
666 |
|
---|
667 | int curVert = 0;
|
---|
668 |
|
---|
669 | for ( ; !polyIter.isDone(); polyIter.next() )
|
---|
670 | {
|
---|
671 | for( int vert = 0; vert < 3; ++vert )
|
---|
672 | {
|
---|
673 | //faceIndices.push_back( polyIter.vertexIndex( vert, &stat ) );
|
---|
674 |
|
---|
675 | const int posIndex = polyIter.vertexIndex( vert, &stat )
|
---|
676 |
|
---|
677 | const IndirectVert vert( posIndex, normalIndices[ i ], uv0Indices[ i ], colorIndices[ i ] );
|
---|
678 |
|
---|
679 |
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | ogreSubMesh->faceVertexIndices = new unsigned short[ faceIndices.size() ];
|
---|
684 |
|
---|
685 | for( i=0; i<faceIndices.size(); ++i )
|
---|
686 | {
|
---|
687 | ogreSubMesh->faceVertexIndices[ i ] = faceIndices[ i ];
|
---|
688 | }
|
---|
689 |
|
---|
690 | /*
|
---|
691 | if (pSkel)
|
---|
692 | delete pSkel;
|
---|
693 | * /
|
---|
694 |
|
---|
695 |
|
---|
696 | return stat;
|
---|
697 | }
|
---|
698 | */
|
---|
699 |
|
---|
700 | MStatus OgreExporter::writer( const MFileObject &file, const MString &optionsString, FileAccessMode mode )
|
---|
701 | {
|
---|
702 | /*
|
---|
703 | int tmpDbgFlag = _CRTDBG_ALLOC_MEM_DF;
|
---|
704 | tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF;
|
---|
705 | tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
|
---|
706 | _CrtSetDbgFlag(tmpDbgFlag);
|
---|
707 | */
|
---|
708 |
|
---|
709 |
|
---|
710 | MString mname = file.fullName();
|
---|
711 | m_filename = mname.asChar();
|
---|
712 |
|
---|
713 | Ogre::ResourceGroupManager resGrpMagr;
|
---|
714 | Ogre::MaterialManager matMgr;
|
---|
715 | Ogre::LogManager logMgr;
|
---|
716 |
|
---|
717 | logMgr.createLog("MayaOgreExport.log");
|
---|
718 |
|
---|
719 | logMgr.logMessage("OGRE Maya Exporter Log");
|
---|
720 | logMgr.logMessage("---- ---- -------- ---");
|
---|
721 |
|
---|
722 | std::vector< MayaMesh > meshes;
|
---|
723 | std::vector< MayaBone > bones;
|
---|
724 |
|
---|
725 | collectStuff( &meshes, &bones );
|
---|
726 |
|
---|
727 | if( meshes.size() > 0 )
|
---|
728 | {
|
---|
729 | //Create the Ogre meshes and submeshes.
|
---|
730 | //TODO: find the name of the mesh.
|
---|
731 | Ogre::Mesh * const ogreMesh = new Ogre::Mesh( m_filename.c_str() );
|
---|
732 |
|
---|
733 | for( unsigned i=0; i<meshes.size(); ++i )
|
---|
734 | {
|
---|
735 | exportMesh( meshes[i], ogreMesh );
|
---|
736 | }
|
---|
737 |
|
---|
738 | Ogre::MeshSerializer serializer;
|
---|
739 |
|
---|
740 | serializer.exportMesh( ogreMesh, m_filename.c_str(), false );
|
---|
741 |
|
---|
742 | delete ogreMesh;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | return MStatus::kSuccess;
|
---|
747 | }
|
---|
748 |
|
---|
749 | bool OgreExporter::haveWriteMethod() const
|
---|
750 | {
|
---|
751 | return true;
|
---|
752 | }
|
---|
753 |
|
---|
754 | bool OgreExporter::haveReadMethod() const
|
---|
755 | {
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 |
|
---|
759 | MString OgreExporter::defaultExtension() const
|
---|
760 | {
|
---|
761 | return MString("mesh");
|
---|
762 | }
|
---|
763 |
|
---|
764 | MPxFileTranslator::MFileKind OgreExporter::identifyFile( const MFileObject &file, const char * buffer, short size ) const
|
---|
765 | {
|
---|
766 | //We do not load .mesh files yet.
|
---|
767 | return kNotMyFileType;
|
---|
768 | }
|
---|
769 |
|
---|
770 |
|
---|
771 | bool OgreExporter::RegisterPlugin( MFnPlugin &fnPlugin )
|
---|
772 | {
|
---|
773 | MStatus status = fnPlugin.registerFileTranslator( c_pPlugName,
|
---|
774 | NULL,
|
---|
775 | OgreExporter::creator,
|
---|
776 | NULL,
|
---|
777 | NULL,
|
---|
778 | false );
|
---|
779 |
|
---|
780 | if (!status)
|
---|
781 | {
|
---|
782 | status.perror("Registering .mesh exporter.");
|
---|
783 | return status;
|
---|
784 | }
|
---|
785 |
|
---|
786 | return MStatus::kSuccess;
|
---|
787 | }
|
---|
788 |
|
---|
789 | bool OgreExporter::DeregisterPlugin( MFnPlugin &fnPlugin )
|
---|
790 | {
|
---|
791 | MStatus status = fnPlugin.deregisterFileTranslator( c_pPlugName );
|
---|
792 |
|
---|
793 | if (!status)
|
---|
794 | {
|
---|
795 | status.perror("Deregistering .mesh exporter.");
|
---|
796 | return false;
|
---|
797 | }
|
---|
798 |
|
---|
799 | return true;
|
---|
800 | }
|
---|
801 |
|
---|