exporter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #define MAYA_DEFAULT_CAMERA "camera1"
  21. #define ANIM_TX BIT( 0 )
  22. #define ANIM_TY BIT( 1 )
  23. #define ANIM_TZ BIT( 2 )
  24. #define ANIM_QX BIT( 3 )
  25. #define ANIM_QY BIT( 4 )
  26. #define ANIM_QZ BIT( 5 )
  27. typedef enum {
  28. WRITE_MESH,
  29. WRITE_ANIM,
  30. WRITE_CAMERA
  31. } exportType_t;
  32. typedef struct {
  33. idCQuat q;
  34. idVec3 t;
  35. } jointFrame_t;
  36. typedef struct {
  37. idCQuat q;
  38. idVec3 t;
  39. float fov;
  40. } cameraFrame_t;
  41. /*
  42. ==============================================================================================
  43. idTokenizer
  44. ==============================================================================================
  45. */
  46. class idTokenizer {
  47. private:
  48. int currentToken;
  49. idStrList tokens;
  50. public:
  51. idTokenizer() { Clear(); };
  52. void Clear( void ) { currentToken = 0; tokens.Clear(); };
  53. int SetTokens( const char *buffer );
  54. const char *NextToken( const char *errorstring = NULL );
  55. bool TokenAvailable( void ) { return currentToken < tokens.Num(); };
  56. int Num( void ) { return tokens.Num(); };
  57. void UnGetToken( void ) { if ( currentToken > 0 ) { currentToken--; } };
  58. const char *GetToken( int index ) { if ( ( index >= 0 ) && ( index < tokens.Num() ) ) { return tokens[ index ]; } else { return NULL; } };
  59. const char *CurrentToken( void ) { return GetToken( currentToken ); };
  60. };
  61. /*
  62. ==============================================================================================
  63. idExportOptions
  64. ==============================================================================================
  65. */
  66. class idNamePair {
  67. public:
  68. idStr from;
  69. idStr to;
  70. };
  71. class idAnimGroup {
  72. public:
  73. idStr name;
  74. idStrList joints;
  75. };
  76. class idExportOptions {
  77. private:
  78. idTokenizer tokens;
  79. void Reset( const char *commandline );
  80. public:
  81. idStr commandLine;
  82. idStr src;
  83. idStr dest;
  84. idStr game;
  85. idStr prefix;
  86. float scale;
  87. exportType_t type;
  88. bool ignoreMeshes;
  89. bool clearOrigin;
  90. bool clearOriginAxis;
  91. bool ignoreScale;
  92. int startframe;
  93. int endframe;
  94. int framerate;
  95. float xyzPrecision;
  96. float quatPrecision;
  97. idStr align;
  98. idList<idNamePair> renamejoints;
  99. idList<idNamePair> remapjoints;
  100. idStrList keepjoints;
  101. idStrList skipmeshes;
  102. idStrList keepmeshes;
  103. idList<idAnimGroup *> exportgroups;
  104. idList<idAnimGroup> groups;
  105. float rotate;
  106. float jointThreshold;
  107. int cycleStart;
  108. idExportOptions( const char *commandline, const char *ospath );
  109. bool jointInExportGroup( const char *jointname );
  110. };
  111. /*
  112. ==============================================================================
  113. idExportJoint
  114. ==============================================================================
  115. */
  116. class idExportJoint {
  117. public:
  118. idStr name;
  119. idStr realname;
  120. idStr longname;
  121. int index;
  122. int exportNum;
  123. bool keep;
  124. float scale;
  125. float invscale;
  126. MFnDagNode *dagnode;
  127. idHierarchy<idExportJoint> mayaNode;
  128. idHierarchy<idExportJoint> exportNode;
  129. idVec3 t;
  130. idMat3 wm;
  131. idVec3 idt;
  132. idMat3 idwm;
  133. idVec3 bindpos;
  134. idMat3 bindmat;
  135. int animBits;
  136. int firstComponent;
  137. jointFrame_t baseFrame;
  138. int depth;
  139. idExportJoint();
  140. idExportJoint &operator=( const idExportJoint &other );
  141. };
  142. /*
  143. ==============================================================================
  144. misc structures
  145. ==============================================================================
  146. */
  147. typedef struct {
  148. idExportJoint *joint;
  149. float jointWeight;
  150. idVec3 offset;
  151. } exportWeight_t;
  152. typedef struct {
  153. idVec3 pos;
  154. idVec2 texCoords;
  155. int startweight;
  156. int numWeights;
  157. } exportVertex_t;
  158. typedef struct {
  159. int indexes[ 3 ];
  160. } exportTriangle_t;
  161. typedef struct {
  162. idVec2 uv[ 3 ];
  163. } exportUV_t;
  164. ID_INLINE int operator==( exportVertex_t a, exportVertex_t b ) {
  165. if ( a.pos != b.pos ) {
  166. return false;
  167. }
  168. if ( ( a.texCoords[ 0 ] != b.texCoords[ 0 ] ) || ( a.texCoords[ 1 ] != b.texCoords[ 1 ] ) ) {
  169. return false;
  170. }
  171. if ( ( a.startweight != b.startweight ) || ( a.numWeights != b.numWeights ) ) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. /*
  177. ========================================================================
  178. .MD3 triangle model file format
  179. ========================================================================
  180. */
  181. #define MD3_IDENT (('3'<<24)+('P'<<16)+('D'<<8)+'I')
  182. #define MD3_VERSION 15
  183. // limits
  184. #define MD3_MAX_LODS 4
  185. #define MD3_MAX_TRIANGLES 8192 // per surface
  186. #define MD3_MAX_VERTS 4096 // per surface
  187. #define MD3_MAX_SHADERS 256 // per surface
  188. #define MD3_MAX_FRAMES 1024 // per model
  189. #define MD3_MAX_SURFACES 32 // per model
  190. #define MD3_MAX_TAGS 16 // per frame
  191. // vertex scales
  192. #define MD3_XYZ_SCALE (1.0/64)
  193. // surface geometry should not exceed these limits
  194. #define SHADER_MAX_VERTEXES 1000
  195. #define SHADER_MAX_INDEXES (6*SHADER_MAX_VERTEXES)
  196. // the maximum size of game reletive pathnames
  197. #define MAX_Q3PATH 64
  198. typedef struct md3Frame_s {
  199. idVec3 bounds[2];
  200. idVec3 localOrigin;
  201. float radius;
  202. char name[16];
  203. } md3Frame_t;
  204. typedef struct md3Tag_s {
  205. char name[MAX_Q3PATH]; // tag name
  206. idVec3 origin;
  207. idVec3 axis[3];
  208. } md3Tag_t;
  209. /*
  210. ** md3Surface_t
  211. **
  212. ** CHUNK SIZE
  213. ** header sizeof( md3Surface_t )
  214. ** shaders sizeof( md3Shader_t ) * numShaders
  215. ** triangles[0] sizeof( md3Triangle_t ) * numTriangles
  216. ** st sizeof( md3St_t ) * numVerts
  217. ** XyzNormals sizeof( md3XyzNormal_t ) * numVerts * numFrames
  218. */
  219. typedef struct {
  220. int ident; //
  221. char name[MAX_Q3PATH]; // polyset name
  222. int flags;
  223. int numFrames; // all surfaces in a model should have the same
  224. int numShaders; // all surfaces in a model should have the same
  225. int numVerts;
  226. int numTriangles;
  227. int ofsTriangles;
  228. int ofsShaders; // offset from start of md3Surface_t
  229. int ofsSt; // texture coords are common for all frames
  230. int ofsXyzNormals; // numVerts * numFrames
  231. int ofsEnd; // next surface follows
  232. } md3Surface_t;
  233. typedef struct {
  234. char name[MAX_Q3PATH];
  235. int shaderIndex; // for in-game use
  236. } md3Shader_t;
  237. typedef struct {
  238. int indexes[3];
  239. } md3Triangle_t;
  240. typedef struct {
  241. float st[2];
  242. } md3St_t;
  243. typedef struct {
  244. short xyz[3];
  245. short normal;
  246. } md3XyzNormal_t;
  247. typedef struct {
  248. int ident;
  249. int version;
  250. char name[MAX_Q3PATH]; // model name
  251. int flags;
  252. int numFrames;
  253. int numTags;
  254. int numSurfaces;
  255. int numSkins;
  256. int ofsFrames; // offset for first frame
  257. int ofsTags; // numFrames * numTags
  258. int ofsSurfaces; // first surface, others follow
  259. int ofsEnd; // end of file
  260. } md3Header_t;
  261. /*
  262. ==============================================================================
  263. idExportMesh
  264. ==============================================================================
  265. */
  266. class idExportMesh {
  267. public:
  268. idStr name;
  269. idStr shader;
  270. bool keep;
  271. idList<exportVertex_t> verts;
  272. idList<exportTriangle_t> tris;
  273. idList<exportWeight_t> weights;
  274. idList<exportUV_t> uv;
  275. idExportMesh() { keep = true; };
  276. void ShareVerts( void );
  277. void GetBounds( idBounds &bounds ) const;
  278. void Merge( idExportMesh *mesh );
  279. };
  280. /*
  281. ==============================================================================
  282. idExportModel
  283. ==============================================================================
  284. */
  285. class idExportModel {
  286. public:
  287. idExportJoint *exportOrigin;
  288. idList<idExportJoint> joints;
  289. idHierarchy<idExportJoint> mayaHead;
  290. idHierarchy<idExportJoint> exportHead;
  291. idList<int> cameraCuts;
  292. idList<cameraFrame_t> camera;
  293. idList<idBounds> bounds;
  294. idList<jointFrame_t> jointFrames;
  295. idList<jointFrame_t *> frames;
  296. int frameRate;
  297. int numFrames;
  298. int skipjoints;
  299. int export_joints;
  300. idList<idExportMesh *> meshes;
  301. idExportModel();
  302. ~idExportModel();
  303. idExportJoint *FindJointReal( const char *name );
  304. idExportJoint *FindJoint( const char *name );
  305. bool WriteMesh( const char *filename, idExportOptions &options );
  306. bool WriteAnim( const char *filename, idExportOptions &options );
  307. bool WriteCamera( const char *filename, idExportOptions &options );
  308. };
  309. /*
  310. ==============================================================================
  311. Maya
  312. ==============================================================================
  313. */
  314. class idMayaExport {
  315. private:
  316. idExportModel model;
  317. idExportOptions &options;
  318. void FreeDagNodes( void );
  319. float TimeForFrame( int num ) const;
  320. int GetMayaFrameNum( int num ) const;
  321. void SetFrame( int num );
  322. void GetBindPose( MObject &jointNode, idExportJoint *joint, float scale );
  323. void GetLocalTransform( idExportJoint *joint, idVec3 &pos, idMat3 &mat );
  324. void GetWorldTransform( idExportJoint *joint, idVec3 &pos, idMat3 &mat, float scale );
  325. void CreateJoints( float scale );
  326. void PruneJoints( idStrList &keepjoints, idStr &prefix );
  327. void RenameJoints( idList<idNamePair> &renamejoints, idStr &prefix );
  328. bool RemapParents( idList<idNamePair> &remapjoints );
  329. MObject FindShader( MObject& setNode );
  330. void GetTextureForMesh( idExportMesh *mesh, MFnDagNode &dagNode );
  331. idExportMesh *CopyMesh( MFnSkinCluster &skinCluster, float scale );
  332. void CreateMesh( float scale );
  333. void CombineMeshes( void );
  334. void GetAlignment( idStr &alignName, idMat3 &align, float rotate, int startframe );
  335. const char *GetObjectType( MObject object );
  336. float GetCameraFov( idExportJoint *joint );
  337. void GetCameraFrame( idExportJoint *camera, idMat3 &align, cameraFrame_t *cam );
  338. void CreateCameraAnim( idMat3 &align );
  339. void GetDefaultPose( idMat3 &align );
  340. void CreateAnimation( idMat3 &align );
  341. public:
  342. idMayaExport( idExportOptions &exportOptions ) : options( exportOptions ) { };
  343. ~idMayaExport();
  344. void ConvertModel( void );
  345. void ConvertToMD3( void );
  346. };