goat3d
view exporters/maxgoat/src/maxgoat.cc @ 60:0c3576325480
moving the exporter along slowly
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 30 Mar 2014 08:53:33 +0300 |
parents | d317eb4f83da |
children | fdece14403ff |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <map>
6 #include <windows.h>
7 #include <shlobj.h>
8 #include "max.h"
9 #include "impexp.h" // SceneExport
10 #include "iparamb2.h" // ClassDesc2
11 #include "plugapi.h"
12 #include "IGame.h"
13 #include "IGameExport.h"
14 #include "IConversionmanager.h"
15 #include "goat3d.h"
16 #include "minwin.h"
17 #include "config.h"
20 #pragma comment (lib, "core.lib")
21 #pragma comment (lib, "geom.lib")
22 #pragma comment (lib, "gfx.lib")
23 #pragma comment (lib, "mesh.lib")
24 #pragma comment (lib, "maxutil.lib")
25 #pragma comment (lib, "maxscrpt.lib")
26 #pragma comment (lib, "paramblk2.lib")
27 #pragma comment (lib, "msxml2.lib")
28 #pragma comment (lib, "igame.lib")
29 #pragma comment (lib, "comctl32.lib")
32 #define VERSION(major, minor) \
33 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
35 static FILE *logfile;
36 static HINSTANCE hinst;
38 class GoatExporter : public SceneExport {
39 private:
40 std::map<IGameMaterial*, goat3d_material*> mtlmap;
41 std::map<IGameNode*, goat3d_node*> nodemap;
43 public:
44 IGameScene *igame;
46 int ExtCount();
47 const TCHAR *Ext(int n);
48 const TCHAR *LongDesc();
49 const TCHAR *ShortDesc();
50 const TCHAR *AuthorName();
51 const TCHAR *CopyrightMessage();
52 const TCHAR *OtherMessage1();
53 const TCHAR *OtherMessage2();
54 unsigned int Version();
55 void ShowAbout(HWND win);
57 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
59 void process_materials(goat3d *goat);
61 void process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode);
63 void process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj);
64 void process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj);
65 void process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj);
66 };
69 int GoatExporter::ExtCount()
70 {
71 return 1;
72 }
74 const TCHAR *GoatExporter::Ext(int n)
75 {
76 return L"xml";
77 }
79 const TCHAR *GoatExporter::LongDesc()
80 {
81 return L"Goat3D scene file";
82 }
84 const TCHAR *GoatExporter::ShortDesc()
85 {
86 return L"Goat3D";
87 }
89 const TCHAR *GoatExporter::AuthorName()
90 {
91 return L"John Tsiombikas";
92 }
94 const TCHAR *GoatExporter::CopyrightMessage()
95 {
96 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
97 }
99 const TCHAR *GoatExporter::OtherMessage1()
100 {
101 return L"other1";
102 }
104 const TCHAR *GoatExporter::OtherMessage2()
105 {
106 return L"other2";
107 }
109 unsigned int GoatExporter::Version()
110 {
111 return VERSION(VER_MAJOR, VER_MINOR);
112 }
114 void GoatExporter::ShowAbout(HWND win)
115 {
116 MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
117 }
119 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
120 BOOL non_interactive, DWORD opt)
121 {
122 mw_test();
124 mtlmap.clear();
125 nodemap.clear();
127 char fname[512];
128 wcstombs(fname, name, sizeof fname - 1);
130 fprintf(logfile, "Exporting Goat3D Scene (text) file: %s\n", fname);
131 if(!(igame = GetIGameInterface())) {
132 fprintf(logfile, "failed to get the igame interface\n");
133 return IMPEXP_FAIL;
134 }
135 IGameConversionManager *cm = GetConversionManager();
136 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
137 igame->InitialiseIGame();
138 igame->SetStaticFrame(0);
140 goat3d *goat = goat3d_create();
142 process_materials(goat);
144 // process all nodes
145 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
146 IGameNode *node = igame->GetTopLevelNode(i);
147 process_node(goat, 0, node);
148 }
150 if(goat3d_save(goat, fname) == -1) {
151 goat3d_free(goat);
152 return IMPEXP_FAIL;
153 }
155 goat3d_free(goat);
156 return IMPEXP_SUCCESS;
157 }
159 static const char *max_string(const MCHAR *wstr)
160 {
161 if(!wstr) return 0;
162 static char str[512];
163 wcstombs(str, wstr, sizeof str - 1);
164 return str;
165 }
167 void GoatExporter::process_materials(goat3d *goat)
168 {
169 IGameProperty *prop;
171 int num_mtl = igame->GetRootMaterialCount();
172 for(int i=0; i<num_mtl; i++) {
173 IGameMaterial *maxmtl = igame->GetRootMaterial(i);
174 if(maxmtl) {
175 goat3d_material *mtl = goat3d_create_mtl();
177 const char *name = max_string(maxmtl->GetMaterialName());
178 if(name) {
179 goat3d_set_mtl_name(mtl, name);
180 }
182 // diffuse
183 if((prop = maxmtl->GetDiffuseData())) {
184 Point3 diffuse(1, 1, 1);
185 prop->GetPropertyValue(diffuse);
186 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
187 diffuse[1], diffuse[2]);
188 }
189 // specular
190 if((prop = maxmtl->GetSpecularData())) {
191 Point3 specular(0, 0, 0);
192 prop->GetPropertyValue(specular);
194 float sstr = 1.0;
195 if((prop = maxmtl->GetSpecularLevelData())) {
196 prop->GetPropertyValue(sstr);
197 }
198 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
199 specular[1] * sstr, specular[2] * sstr);
200 }
201 // shininess
202 if((prop = maxmtl->GetGlossinessData())) {
203 float shin;
204 prop->GetPropertyValue(shin);
205 goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
206 }
208 // textures
209 for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
210 IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
212 const char *fname = max_string(tex->GetBitmapFileName());
213 if(!fname) {
214 continue;
215 }
217 int slot = tex->GetStdMapSlot();
218 switch(slot) {
219 case ID_DI: // diffuse
220 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
221 break;
223 case ID_SP:
224 case ID_SS:
225 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
226 break;
228 case ID_SH:
229 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
230 break;
232 case ID_BU:
233 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
234 break;
236 case ID_RL:
237 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
238 break;
240 case ID_RR:
241 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
242 break;
244 default:
245 break;
246 }
247 }
249 goat3d_add_mtl(goat, mtl);
250 mtlmap[maxmtl] = mtl;
251 }
252 }
253 }
255 void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode)
256 {
257 goat3d_node *node = goat3d_create_node();
258 goat3d_add_node(goat, node);
260 const char *name = max_string(maxnode->GetName());
261 if(name) {
262 goat3d_set_node_name(node, name);
263 }
265 // no animation yet, just get the static PRS
266 GMatrix maxmatrix = maxnode->GetObjectTM();
267 Point3 trans = maxmatrix.Translation();
268 Quat rot = maxmatrix.Rotation();
269 Point3 scale = maxmatrix.Scaling();
271 goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0);
272 goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0);
273 goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0);
275 IGameObject *maxobj = maxnode->GetIGameObject();
276 IGameObject::ObjectTypes type = maxobj->GetIGameType();
278 switch(type) {
279 case IGameObject::IGAME_MESH:
280 {
281 goat3d_mesh *mesh = goat3d_create_mesh();
282 if(name) goat3d_set_mesh_name(mesh, name);
283 goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh);
285 // get the node material and assign it to the mesh
286 IGameMaterial *maxmtl = maxnode->GetNodeMaterial();
287 goat3d_material *mtl = mtlmap[maxmtl];
288 if(mtl) {
289 goat3d_set_mesh_mtl(mesh, mtl);
290 }
292 process_mesh(goat, mesh, maxobj);
293 goat3d_add_mesh(goat, mesh);
294 }
295 break;
297 case IGameObject::IGAME_LIGHT:
298 {
299 goat3d_light *light = goat3d_create_light();
300 //if(name) goat3d_set_light_name(light, name);
301 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
303 process_light(goat, light, maxobj);
304 goat3d_add_light(goat, light);
305 }
306 break;
308 case IGameObject::IGAME_CAMERA:
309 {
310 goat3d_camera *cam = goat3d_create_camera();
311 //if(name) goat3d_set_camera_name(camera, name);
312 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
314 process_camera(goat, cam, maxobj);
315 goat3d_add_camera(goat, cam);
316 }
317 break;
319 default:
320 // otherwise don't assign an object, essentially treating it as a null node
321 break;
322 }
325 for(int i=0; i<maxnode->GetChildCount(); i++) {
326 process_node(goat, node, maxnode->GetNodeChild(i));
327 }
328 }
330 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
331 {
332 IGameMesh *maxmesh = (IGameMesh*)maxobj;
334 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
335 maxobj->InitializeData();
337 int num_verts = maxmesh->GetNumberOfVerts();
338 int num_faces = maxmesh->GetNumberOfFaces();
339 //assert(maxmesh->GetNumberOfTexVerts() == num_verts);
341 float *vertices = new float[num_verts * 3];
342 float *normals = new float[num_verts * 3];
343 //float *texcoords = new float[num_verts * 2];
344 int *indices = new int[num_faces * 3];
346 for(int i=0; i<num_verts; i++) {
347 Point3 v = maxmesh->GetVertex(i, true);
348 vertices[i * 3] = v.x;
349 vertices[i * 3 + 1] = v.y;
350 vertices[i * 3 + 2] = v.z;
351 }
353 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
354 Point3 norm = maxmesh->GetNormal(i);
356 int vidx = maxmesh->GetNormalVertexIndex(i);
357 normals[vidx * 3] = norm.x;
358 normals[vidx * 3 + 1] = norm.y;
359 normals[vidx * 3 + 2] = norm.z;
360 }
362 /*for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
363 Point3 tex = maxmesh->GetTexVertex(i);
365 texcoords[i * 2] = tex.x;
366 texcoords[i * 2 + 1] = tex.y;
367 }*/
369 // get the faces
370 for(int i=0; i<num_faces; i++) {
371 FaceEx *face = maxmesh->GetFace(i);
372 indices[i * 3] = face->vert[0];
373 indices[i * 3 + 1] = face->vert[1];
374 indices[i * 3 + 2] = face->vert[2];
375 // TODO at some point I'll have to split based on normal/texcoord indices
376 }
378 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts);
379 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts);
380 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts);
381 goat3d_set_mesh_faces(mesh, indices, num_faces);
383 delete [] vertices;
384 delete [] normals;
385 //delete [] texcoords;
386 delete [] indices;
387 }
389 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
390 {
391 }
393 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
394 {
395 }
398 // ------------------------------------------
400 class GoatClassDesc : public ClassDesc2 {
401 public:
402 int IsPublic() { return TRUE; }
403 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
404 const TCHAR *ClassName() { return L"GoatExporter"; }
405 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
406 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
407 const TCHAR *Category() { return L"Mutant Stargoat"; }
409 const TCHAR *InternalName() { return L"GoatExporter"; }
410 HINSTANCE HInstance() { return hinst; }
411 };
413 // TODO: make 2 class descriptors, one for goat3d, one for goat3danim
414 static GoatClassDesc class_desc;
416 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
417 {
418 if(reason == DLL_PROCESS_ATTACH) {
419 hinst = inst_handle;
420 DisableThreadLibraryCalls(hinst);
421 }
422 return TRUE;
423 }
425 extern "C" {
427 __declspec(dllexport) const TCHAR *LibDescription()
428 {
429 return L"test exporter";
430 }
432 __declspec(dllexport) int LibNumberClasses()
433 {
434 return 1;
435 }
437 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
438 {
439 return i == 0 ? &class_desc : 0;
440 }
442 __declspec(dllexport) ULONG LibVersion()
443 {
444 return Get3DSMAXVersion();
445 }
447 __declspec(dllexport) int LibInitialize()
448 {
449 static char path[1024];
451 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
452 strcat(path, "/testexp.log");
454 if((logfile = fopen(path, "w"))) {
455 setvbuf(logfile, 0, _IONBF, 0);
456 }
457 return TRUE;
458 }
460 __declspec(dllexport) int LibShutdown()
461 {
462 if(logfile) {
463 fclose(logfile);
464 logfile = 0;
465 }
466 return TRUE;
467 }
469 } // extern "C"