goat3d

view exporters/maxgoat/src/maxgoat.cc @ 66:3751aabbc5b3

igame animation api is weird...
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Apr 2014 07:56:43 +0300
parents fdece14403ff
children 8ecaf9cd3ce7
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #include <map>
7 #include <windows.h>
8 #include <shlobj.h>
9 #include "max.h"
10 #include "impexp.h" // SceneExport
11 #include "iparamb2.h" // ClassDesc2
12 #include "plugapi.h"
13 #include "IGame.h"
14 #include "IGameExport.h"
15 #include "IGameControl.h"
16 #include "IConversionmanager.h"
17 #include "goat3d.h"
18 #include "config.h"
19 #include "logger.h"
20 #include "resource.h"
23 #pragma comment (lib, "core.lib")
24 #pragma comment (lib, "geom.lib")
25 #pragma comment (lib, "gfx.lib")
26 #pragma comment (lib, "mesh.lib")
27 #pragma comment (lib, "maxutil.lib")
28 #pragma comment (lib, "maxscrpt.lib")
29 #pragma comment (lib, "paramblk2.lib")
30 #pragma comment (lib, "msxml2.lib")
31 #pragma comment (lib, "igame.lib")
32 #pragma comment (lib, "comctl32.lib")
35 #define COPYRIGHT \
36 L"Copyright 2014 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details."
37 #define VERSION(major, minor) \
38 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
40 static INT_PTR CALLBACK scene_gui_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
41 static INT_PTR CALLBACK anim_gui_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
42 static const char *max_string(const MCHAR *wstr);
44 HINSTANCE hinst;
46 class GoatExporter : public SceneExport {
47 private:
48 std::map<IGameMaterial*, goat3d_material*> mtlmap;
49 std::map<IGameNode*, goat3d_node*> nodemap;
51 public:
52 IGameScene *igame;
54 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
56 void process_materials(goat3d *goat);
58 void process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode);
60 void process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj);
61 void process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj);
62 void process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj);
65 int ExtCount() { return 1; }
66 const TCHAR *Ext(int n) { return L"goatsce"; }
67 const TCHAR *LongDesc() { return L"Goat3D scene file"; }
68 const TCHAR *ShortDesc() { return L"Goat3D"; }
69 const TCHAR *AuthorName() { return L"John Tsiombikas"; }
70 const TCHAR *CopyrightMessage() { return COPYRIGHT; }
71 const TCHAR *OtherMessage1() { return L"other1"; }
72 const TCHAR *OtherMessage2() { return L"other2"; }
73 unsigned int Version() { return VERSION(VER_MAJOR, VER_MINOR); }
74 void ShowAbout(HWND win) { MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0); }
75 };
77 class GoatAnimExporter : public GoatExporter {
78 private:
79 public:
80 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
82 const TCHAR *Ext(int n) { return L"goatanm"; }
83 const TCHAR *LongDesc() { return L"Goat3D animation file"; }
84 };
87 // ---- GoatExporter implementation ----
89 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
90 BOOL non_interactive, DWORD opt)
91 {
92 if(!DialogBox(hinst, MAKEINTRESOURCE(IDD_GOAT_SCE), 0, scene_gui_handler)) {
93 return IMPEXP_CANCEL;
94 }
96 mtlmap.clear();
97 nodemap.clear();
99 char fname[512];
100 wcstombs(fname, name, sizeof fname - 1);
101 for(int i=0; fname[i]; i++) {
102 fname[i] = tolower(fname[i]);
103 }
105 maxlog("Exporting Goat3D Scene (text) file: %s\n", fname);
106 if(!(igame = GetIGameInterface())) {
107 maxlog("failed to get the igame interface\n");
108 return IMPEXP_FAIL;
109 }
110 IGameConversionManager *cm = GetConversionManager();
111 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
112 igame->InitialiseIGame();
114 goat3d *goat = goat3d_create();
116 process_materials(goat);
118 // process all nodes
119 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
120 IGameNode *node = igame->GetTopLevelNode(i);
121 process_node(goat, 0, node);
122 }
124 if(goat3d_save(goat, fname) == -1) {
125 goat3d_free(goat);
126 return IMPEXP_FAIL;
127 }
129 goat3d_free(goat);
130 return IMPEXP_SUCCESS;
131 }
133 void GoatExporter::process_materials(goat3d *goat)
134 {
135 IGameProperty *prop;
137 int num_mtl = igame->GetRootMaterialCount();
138 for(int i=0; i<num_mtl; i++) {
139 IGameMaterial *maxmtl = igame->GetRootMaterial(i);
140 if(maxmtl) {
141 goat3d_material *mtl = goat3d_create_mtl();
143 const char *name = max_string(maxmtl->GetMaterialName());
144 if(name) {
145 goat3d_set_mtl_name(mtl, name);
146 }
148 // diffuse
149 if((prop = maxmtl->GetDiffuseData())) {
150 Point3 diffuse(1, 1, 1);
151 prop->GetPropertyValue(diffuse);
152 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
153 diffuse[1], diffuse[2]);
154 }
155 // specular
156 if((prop = maxmtl->GetSpecularData())) {
157 Point3 specular(0, 0, 0);
158 prop->GetPropertyValue(specular);
160 float sstr = 1.0;
161 if((prop = maxmtl->GetSpecularLevelData())) {
162 prop->GetPropertyValue(sstr);
163 }
164 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
165 specular[1] * sstr, specular[2] * sstr);
166 }
167 // shininess
168 if((prop = maxmtl->GetGlossinessData())) {
169 float shin;
170 prop->GetPropertyValue(shin);
171 goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
172 }
174 // textures
175 for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
176 IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
178 const char *fname = max_string(tex->GetBitmapFileName());
179 if(!fname) {
180 continue;
181 }
183 int slot = tex->GetStdMapSlot();
184 switch(slot) {
185 case ID_DI: // diffuse
186 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
187 break;
189 case ID_SP:
190 case ID_SS:
191 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
192 break;
194 case ID_SH:
195 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
196 break;
198 case ID_BU:
199 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
200 break;
202 case ID_RL:
203 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
204 break;
206 case ID_RR:
207 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
208 break;
210 default:
211 break;
212 }
213 }
215 goat3d_add_mtl(goat, mtl);
216 mtlmap[maxmtl] = mtl;
217 }
218 }
219 }
221 void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode)
222 {
223 goat3d_node *node = goat3d_create_node();
224 goat3d_add_node(goat, node);
226 if(parent) {
227 goat3d_add_node_child(parent, node);
228 }
230 const char *name = max_string(maxnode->GetName());
231 if(name) {
232 goat3d_set_node_name(node, name);
233 }
235 // no animation yet, just get the static PRS
236 GMatrix maxmatrix = maxnode->GetObjectTM();
237 Point3 trans = maxmatrix.Translation();
238 Quat rot = maxmatrix.Rotation();
239 Point3 scale = maxmatrix.Scaling();
241 goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0);
242 goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0);
243 goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0);
245 IGameObject *maxobj = maxnode->GetIGameObject();
246 IGameObject::ObjectTypes type = maxobj->GetIGameType();
248 switch(type) {
249 case IGameObject::IGAME_MESH:
250 {
251 goat3d_mesh *mesh = goat3d_create_mesh();
252 if(name) goat3d_set_mesh_name(mesh, name);
253 goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh);
255 // get the node material and assign it to the mesh
256 IGameMaterial *maxmtl = maxnode->GetNodeMaterial();
257 goat3d_material *mtl = mtlmap[maxmtl];
258 if(mtl) {
259 goat3d_set_mesh_mtl(mesh, mtl);
260 }
262 process_mesh(goat, mesh, maxobj);
263 goat3d_add_mesh(goat, mesh);
264 }
265 break;
267 case IGameObject::IGAME_LIGHT:
268 {
269 goat3d_light *light = goat3d_create_light();
270 //if(name) goat3d_set_light_name(light, name);
271 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
273 process_light(goat, light, maxobj);
274 goat3d_add_light(goat, light);
275 }
276 break;
278 case IGameObject::IGAME_CAMERA:
279 {
280 goat3d_camera *cam = goat3d_create_camera();
281 //if(name) goat3d_set_camera_name(camera, name);
282 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
284 process_camera(goat, cam, maxobj);
285 goat3d_add_camera(goat, cam);
286 }
287 break;
289 default:
290 // otherwise don't assign an object, essentially treating it as a null node
291 break;
292 }
294 // grab the animation data
295 IGameControl *ctrl = maxnode->GetIGameControl();
297 IGameKeyTab tm_keys;
298 if(ctrl->GetFullSampledKeys(tm_keys, 1, IGAME_TM)) {
299 maxlog("node: %s has %d keys\n", name, tm_keys.Count());
300 /*for(int i=0; i<pkeys.Count(); i++) {
301 Point3 p = pkeys[i].linearKey.pval;
302 TimeValue t = pkeys[i].t;
303 goat3d_set_node_position(node, p.x, p.y, p.z, TicksToSec(t));
304 }*/
305 }
307 for(int i=0; i<maxnode->GetChildCount(); i++) {
308 process_node(goat, node, maxnode->GetNodeChild(i));
309 }
310 }
312 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
313 {
314 IGameMesh *maxmesh = (IGameMesh*)maxobj;
316 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
317 maxobj->InitializeData();
319 int num_verts = maxmesh->GetNumberOfVerts();
320 int num_faces = maxmesh->GetNumberOfFaces();
321 //assert(maxmesh->GetNumberOfTexVerts() == num_verts);
323 float *vertices = new float[num_verts * 3];
324 float *normals = new float[num_verts * 3];
325 //float *texcoords = new float[num_verts * 2];
326 int *indices = new int[num_faces * 3];
328 for(int i=0; i<num_verts; i++) {
329 Point3 v = maxmesh->GetVertex(i, true);
330 vertices[i * 3] = v.x;
331 vertices[i * 3 + 1] = v.y;
332 vertices[i * 3 + 2] = v.z;
333 }
335 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
336 Point3 norm = maxmesh->GetNormal(i);
338 int vidx = maxmesh->GetNormalVertexIndex(i);
339 normals[vidx * 3] = norm.x;
340 normals[vidx * 3 + 1] = norm.y;
341 normals[vidx * 3 + 2] = norm.z;
342 }
344 /*for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
345 Point3 tex = maxmesh->GetTexVertex(i);
347 texcoords[i * 2] = tex.x;
348 texcoords[i * 2 + 1] = tex.y;
349 }*/
351 // get the faces
352 for(int i=0; i<num_faces; i++) {
353 FaceEx *face = maxmesh->GetFace(i);
354 indices[i * 3] = face->vert[0];
355 indices[i * 3 + 1] = face->vert[1];
356 indices[i * 3 + 2] = face->vert[2];
357 // TODO at some point I'll have to split based on normal/texcoord indices
358 }
360 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts);
361 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts);
362 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts);
363 goat3d_set_mesh_faces(mesh, indices, num_faces);
365 delete [] vertices;
366 delete [] normals;
367 //delete [] texcoords;
368 delete [] indices;
369 }
371 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
372 {
373 // TODO
374 }
376 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
377 {
378 // TODO
379 }
381 static INT_PTR CALLBACK scene_gui_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
382 {
383 switch(msg) {
384 case WM_INITDIALOG:
385 CheckDlgButton(win, IDC_GOAT_NODES, 1);
386 CheckDlgButton(win, IDC_GOAT_MESHES, 1);
387 CheckDlgButton(win, IDC_GOAT_LIGHTS, 1);
388 CheckDlgButton(win, IDC_GOAT_CAMERAS, 1);
389 break;
391 case WM_COMMAND:
392 switch(LOWORD(wparam)) {
393 case IDOK:
394 EndDialog(win, 1);
395 break;
397 case IDCANCEL:
398 EndDialog(win, 0);
399 break;
401 default:
402 return 0;
403 }
404 break;
406 default:
407 return 0;
408 }
410 return 1;
411 }
415 // ---- GoatAnimExporter implementation ----
417 int GoatAnimExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent, DWORD opt)
418 {
419 if(!DialogBox(hinst, MAKEINTRESOURCE(IDD_GOAT_ANM), 0, anim_gui_handler)) {
420 return IMPEXP_CANCEL;
421 }
423 char fname[512];
424 wcstombs(fname, name, sizeof fname - 1);
425 for(int i=0; fname[i]; i++) {
426 fname[i] = tolower(fname[i]);
427 }
429 maxlog("Exporting Goat3D Animation (text) file: %s\n", fname);
430 if(!(igame = GetIGameInterface())) {
431 maxlog("failed to get the igame interface\n");
432 return IMPEXP_FAIL;
433 }
434 IGameConversionManager *cm = GetConversionManager();
435 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
436 igame->InitialiseIGame();
437 igame->SetStaticFrame(0);
439 goat3d *goat = goat3d_create();
441 // process all nodes
442 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
443 IGameNode *node = igame->GetTopLevelNode(i);
444 process_node(goat, 0, node);
445 }
447 if(goat3d_save_anim(goat, fname) == -1) {
448 goat3d_free(goat);
449 return IMPEXP_FAIL;
450 }
452 goat3d_free(goat);
453 return IMPEXP_SUCCESS;
454 }
456 static INT_PTR CALLBACK anim_gui_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
457 {
458 switch(msg) {
459 case WM_INITDIALOG:
460 CheckDlgButton(win, IDC_GOAT_ANM_FULL, 1);
461 break;
463 case WM_COMMAND:
464 switch(LOWORD(wparam)) {
465 case IDOK:
466 EndDialog(win, 1);
467 break;
469 case IDCANCEL:
470 EndDialog(win, 0);
471 break;
473 default:
474 return 0;
475 }
476 break;
478 default:
479 return 0;
480 }
482 return 1;
483 }
485 // ------------------------------------------
487 class GoatClassDesc : public ClassDesc2 {
488 public:
489 int IsPublic() { return TRUE; }
490 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
491 const TCHAR *ClassName() { return L"GoatExporter"; }
492 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
493 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
494 const TCHAR *Category() { return L"Mutant Stargoat"; }
496 const TCHAR *InternalName() { return L"GoatExporter"; }
497 HINSTANCE HInstance() { return hinst; }
498 };
500 class GoatAnimClassDesc : public ClassDesc2 {
501 public:
502 int IsPublic() { return TRUE; }
503 void *Create(BOOL loading = FALSE) { return new GoatAnimExporter; }
504 const TCHAR *ClassName() { return L"GoatAnimExporter"; }
505 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
506 Class_ID ClassID() { return Class_ID(0x51b94924, 0x2e0332f3); }
507 const TCHAR *Category() { return L"Mutant Stargoat"; }
509 const TCHAR *InternalName() { return L"GoatAnimExporter"; }
510 HINSTANCE HInstance() { return hinst; }
511 };
513 // TODO: make 2 class descriptors, one for goat3d, one for goat3danim
514 static GoatClassDesc class_desc;
515 static GoatAnimClassDesc anim_class_desc;
517 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
518 {
519 if(reason == DLL_PROCESS_ATTACH) {
520 hinst = inst_handle;
521 DisableThreadLibraryCalls(hinst);
522 }
523 return TRUE;
524 }
526 extern "C" {
528 __declspec(dllexport) const TCHAR *LibDescription()
529 {
530 return L"test exporter";
531 }
533 __declspec(dllexport) int LibNumberClasses()
534 {
535 return 1;
536 }
538 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
539 {
540 switch(i) {
541 case 0:
542 return &class_desc;
543 case 1:
544 return &anim_class_desc;
545 default:
546 break;
547 }
548 return 0;
549 }
551 __declspec(dllexport) ULONG LibVersion()
552 {
553 return Get3DSMAXVersion();
554 }
556 __declspec(dllexport) int LibInitialize()
557 {
558 static char path[1024];
560 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
561 strcat(path, "/testexp.log");
563 maxlog_open(path);
564 return TRUE;
565 }
567 __declspec(dllexport) int LibShutdown()
568 {
569 maxlog_close();
570 return TRUE;
571 }
573 } // extern "C"
576 static const char *max_string(const MCHAR *wstr)
577 {
578 if(!wstr) return 0;
579 static char str[512];
580 wcstombs(str, wstr, sizeof str - 1);
581 return str;