meshexport.cpp
上传用户:qccn516
上传日期:2013-05-02
资源大小:3382k
文件大小:8k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /* Mesh Export plugin for 3DStudio
  2.  *
  3.  * Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program 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.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19. #include "max.h"
  20. #pragma comment(lib, "core.lib")
  21. #pragma comment(lib, "maxutil.lib")
  22. #pragma comment(lib, "geom.lib")
  23. #pragma comment(lib, "mesh.lib")
  24. /*****************************************************************************/
  25. /*                                                                           */
  26. /* Mesh Enum Proc                                                            */
  27. /*                                                                           */
  28. /*****************************************************************************/
  29. #define MESH_RAW_MAGIC ('m' | ('r' << 8) | ('0' << 16) | ('2' << 24))
  30. struct Vertex {
  31. float xyz[3];
  32. float normal[3];
  33. float texcoord[2];
  34. };
  35. class MeshEnumProc : public ITreeEnumProc {
  36. public:
  37. MeshEnumProc(const char *name,IScene *scene,Interface *i,DWORD options);
  38. ~MeshEnumProc();
  39. int callback(INode *node);
  40. void export(INode *node,TriObject *tri);
  41. FILE *file;
  42. IScene *scene;
  43. Interface *i;
  44. int selected;
  45. int num_surfaces;
  46. };
  47. MeshEnumProc::MeshEnumProc(const char *name,IScene *scene,Interface *i,DWORD options) {
  48. file = fopen(name,"wb");
  49. if(!file) {
  50. char error[1024];
  51. sprintf(error,"Mesh Export:nerror create "%s" file",name);
  52. MessageBox(0,error,"error",MB_OK);
  53. return;
  54. }
  55. this->scene = scene;
  56. this->i = i;
  57. selected = (options & SCENE_EXPORT_SELECTED) ? TRUE : FALSE;
  58. num_surfaces = 0;
  59. int magic = MESH_RAW_MAGIC;
  60. fwrite(&magic,sizeof(int),1,file);
  61. fwrite(&num_surfaces,sizeof(int),1,file);
  62. scene->EnumTree(this);
  63. }
  64. MeshEnumProc::~MeshEnumProc() {
  65. // info
  66. char buffer[1024];
  67. sprintf(buffer,"OKnsurfaces %d",num_surfaces);
  68. MessageBox(0,buffer,"Mesh Export",MB_OK);
  69. // write number of surfaces
  70. fseek(file,sizeof(int),SEEK_SET);
  71. fwrite(&num_surfaces,sizeof(int),1,file);
  72. fclose(file);
  73. }
  74. /*
  75.  */
  76. int MeshEnumProc::callback(INode *node) {
  77. if(selected && node->Selected() == FALSE) return TREE_CONTINUE;
  78. Object *obj = node->EvalWorldState(i->GetTime()).obj;
  79. if(obj->CanConvertToType(Class_ID(TRIOBJ_CLASS_ID,0))) { 
  80. TriObject *tri = (TriObject*)obj->ConvertToType(i->GetTime(),Class_ID(TRIOBJ_CLASS_ID,0));
  81. export(node,tri);
  82. if(obj != tri) delete tri;
  83. }
  84. return TREE_CONTINUE;
  85. }
  86. /*
  87.  */
  88. void MeshEnumProc::export(INode *node,TriObject *tri) {
  89. Mesh* mesh = &tri->GetMesh();
  90. num_surfaces++;
  91. // name
  92. char name[128];
  93. memset(name,0,sizeof(name));
  94. strcpy(name,node->GetName());
  95. fwrite(name,sizeof(name),1,file);
  96. // number of vertexes
  97. int num_vertex = mesh->numFaces * 3;
  98. fwrite(&num_vertex,sizeof(int),1,file);
  99. // vertexes
  100. mesh->buildNormals();
  101. Matrix3 transform = node->GetObjTMAfterWSM(i->GetTime());
  102. Matrix3 rotate = transform;
  103. rotate.NoTrans();
  104. rotate.NoScale();
  105. // negative scale
  106. int index[3];
  107. if(DotProd(CrossProd(transform.GetRow(0),transform.GetRow(1)),transform.GetRow(2)) < 0.0) {
  108. index[0] = 2; index[1] = 1; index[2] = 0;
  109. } else {
  110. index[0] = 0; index[1] = 1; index[2] = 2;
  111. }
  112. Vertex *vertex = new Vertex[mesh->numFaces * 3];
  113. for(int i = 0, j = 0; i < mesh->numFaces; i++, j += 3) {
  114. Point3 v;
  115. Face *f = &mesh->faces[i];
  116. for(int k = 0; k < 3; k++) {
  117. v = transform * mesh->verts[f->v[index[k]]];
  118. vertex[j + k].xyz[0] = v.x;
  119. vertex[j + k].xyz[1] = v.y;
  120. vertex[j + k].xyz[2] = v.z;
  121. // get normal (see asciiexp plugin)
  122. RVertex *rv = mesh->getRVertPtr(f->v[index[k]]);
  123. int num_normals;
  124. if(rv->rFlags & SPECIFIED_NORMAL) v = rv->rn.getNormal();
  125. else if((num_normals = rv->rFlags & NORCT_MASK) && f->smGroup) {
  126. if(num_normals == 1) v = rv->rn.getNormal();
  127. else for(int l = 0; l < num_normals; l++) {
  128. if(rv->ern[l].getSmGroup() & f->smGroup) v = rv->ern[l].getNormal();
  129. }
  130. } else v = mesh->getFaceNormal(i);
  131. v = rotate * v;
  132. vertex[j + k].normal[0] = v.x;
  133. vertex[j + k].normal[1] = v.y;
  134. vertex[j + k].normal[2] = v.z;
  135. // texture coords
  136. if(mesh->numTVerts) {
  137. v = mesh->tVerts[mesh->tvFace[i].t[index[k]]];
  138. vertex[j + k].texcoord[0] = v.x;
  139. vertex[j + k].texcoord[1] = 1.0f - v.y;
  140. } else {
  141. vertex[j + k].texcoord[0] = 0;
  142. vertex[j + k].texcoord[1] = 0;
  143. }
  144. }
  145. }
  146. fwrite(vertex,sizeof(Vertex),mesh->numFaces * 3,file);
  147. delete vertex;
  148. }
  149. /*****************************************************************************/
  150. /*                                                                           */
  151. /* Mesh Export                                                               */
  152. /*                                                                           */
  153. /*****************************************************************************/
  154. class MeshExport : public SceneExport {
  155. public:
  156. MeshExport() { }
  157. ~MeshExport() { }
  158. int ExtCount() { return 1; }
  159. const TCHAR *Ext(int i) { return (i == 0) ? "mesh" : ""; }
  160. const TCHAR *LongDesc() { return "Mesh Export plugin http://frustum.org"; }
  161. const TCHAR *ShortDesc() { return "Mesh Export"; }
  162. const TCHAR *AuthorName() { return "Alexander Zaprjagaev"; }
  163. const TCHAR *CopyrightMessage() { return ""; }
  164. const TCHAR *OtherMessage1() { return ""; }
  165. const TCHAR *OtherMessage2() { return ""; }
  166. unsigned int Version() { return 100; }
  167. void ShowAbout(HWND hWnd) { MessageBox(hWnd,"Mesh Export pluginnhttp://frustum.org","about",MB_OK); }
  168. BOOL SupportsOptions(int ext,DWORD options) { return (options == SCENE_EXPORT_SELECTED) ? TRUE : FALSE; }
  169. int DoExport(const TCHAR *name,ExpInterface *ei,Interface *i,BOOL suppressPrompts = FALSE,DWORD options = 0);
  170. };
  171. int MeshExport::DoExport(const TCHAR *name,ExpInterface *ei,Interface *i,BOOL suppressPrompts,DWORD options) {
  172. MeshEnumProc mesh(name,ei->theScene,i,options);
  173. return 1;
  174. }
  175. /*****************************************************************************/
  176. /*                                                                           */
  177. /* DllMain                                                                   */
  178. /*                                                                           */
  179. /*****************************************************************************/
  180. HINSTANCE hInstance;
  181. int controlsInit = FALSE;
  182. BOOL WINAPI DllMain(HINSTANCE hinstDLL,ULONG fdwReason,LPVOID lpvReserved) {
  183. hInstance = hinstDLL;
  184. if(!controlsInit) {
  185. controlsInit = TRUE;
  186. InitCustomControls(hInstance);
  187. InitCommonControls();
  188. }
  189. return TRUE;
  190. }
  191. class MeshClassDesc : public ClassDesc {
  192. public:
  193. int IsPublic() { return 1; }
  194. void *Create(BOOL loading = FALSE) { return new MeshExport; }
  195. const TCHAR *ClassName() { return "Mesh Export"; }
  196. SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
  197. Class_ID ClassID() { return Class_ID(0xdeadbeef,0x4ba8d931); }
  198. const TCHAR *Category() { return ""; }
  199. };
  200. static MeshClassDesc MeshDesc;
  201. __declspec(dllexport) const TCHAR *LibDescription() {
  202. return "Mesh Export Plugin";
  203. }
  204. __declspec(dllexport) int LibNumberClasses() {
  205. return 1;
  206. }
  207. __declspec(dllexport) ClassDesc *LibClassDesc(int i) {
  208. return (i == 0) ? &MeshDesc : 0;
  209. }
  210. __declspec(dllexport) ULONG LibVersion() {
  211. return VERSION_3DSMAX;
  212. }
  213. __declspec(dllexport )ULONG CanAutoDefer() {
  214. return 1;
  215. }