goat3d

view exporters/maxgoat/src/maxgoat.cc @ 62:55571a390144

remove redundtant stuff from maxgoat.rc
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 31 Mar 2014 20:00:31 +0300
parents 0c3576325480
children 3751aabbc5b3
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"
18 #include "logger.h"
19 #include "resource.h"
22 #pragma comment (lib, "core.lib")
23 #pragma comment (lib, "geom.lib")
24 #pragma comment (lib, "gfx.lib")
25 #pragma comment (lib, "mesh.lib")
26 #pragma comment (lib, "maxutil.lib")
27 #pragma comment (lib, "maxscrpt.lib")
28 #pragma comment (lib, "paramblk2.lib")
29 #pragma comment (lib, "msxml2.lib")
30 #pragma comment (lib, "igame.lib")
31 #pragma comment (lib, "comctl32.lib")
34 #define VERSION(major, minor) \
35 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
37 HINSTANCE hinst;
39 class GoatExporter : public SceneExport {
40 private:
41 std::map<IGameMaterial*, goat3d_material*> mtlmap;
42 std::map<IGameNode*, goat3d_node*> nodemap;
44 public:
45 IGameScene *igame;
47 int ExtCount();
48 const TCHAR *Ext(int n);
49 const TCHAR *LongDesc();
50 const TCHAR *ShortDesc();
51 const TCHAR *AuthorName();
52 const TCHAR *CopyrightMessage();
53 const TCHAR *OtherMessage1();
54 const TCHAR *OtherMessage2();
55 unsigned int Version();
56 void ShowAbout(HWND win);
58 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
60 void process_materials(goat3d *goat);
62 void process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode);
64 void process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj);
65 void process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj);
66 void process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj);
67 };
70 int GoatExporter::ExtCount()
71 {
72 return 1;
73 }
75 const TCHAR *GoatExporter::Ext(int n)
76 {
77 return L"xml";
78 }
80 const TCHAR *GoatExporter::LongDesc()
81 {
82 return L"Goat3D scene file";
83 }
85 const TCHAR *GoatExporter::ShortDesc()
86 {
87 return L"Goat3D";
88 }
90 const TCHAR *GoatExporter::AuthorName()
91 {
92 return L"John Tsiombikas";
93 }
95 const TCHAR *GoatExporter::CopyrightMessage()
96 {
97 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
98 }
100 const TCHAR *GoatExporter::OtherMessage1()
101 {
102 return L"other1";
103 }
105 const TCHAR *GoatExporter::OtherMessage2()
106 {
107 return L"other2";
108 }
110 unsigned int GoatExporter::Version()
111 {
112 return VERSION(VER_MAJOR, VER_MINOR);
113 }
115 void GoatExporter::ShowAbout(HWND win)
116 {
117 MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
118 }
120 static INT_PTR CALLBACK handle_dlg_events(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
121 {
122 switch(msg) {
123 case WM_INITDIALOG:
124 CheckDlgButton(win, IDC_GOAT_NODES, 1);
125 CheckDlgButton(win, IDC_GOAT_MESHES, 1);
126 CheckDlgButton(win, IDC_GOAT_LIGHTS, 1);
127 CheckDlgButton(win, IDC_GOAT_CAMERAS, 1);
128 break;
130 case WM_COMMAND:
131 switch(LOWORD(wparam)) {
132 case IDOK:
133 EndDialog(win, 1);
134 break;
136 case IDCANCEL:
137 EndDialog(win, 0);
138 break;
140 default:
141 return 0;
142 }
143 break;
145 default:
146 return 0;
147 }
149 return 1;
150 }
152 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
153 BOOL non_interactive, DWORD opt)
154 {
155 if(!DialogBox(hinst, MAKEINTRESOURCE(IDD_GOAT_SCE), 0, handle_dlg_events)) {
156 maxlog("canceled!\n");
157 return IMPEXP_CANCEL;
158 }
160 mtlmap.clear();
161 nodemap.clear();
163 char fname[512];
164 wcstombs(fname, name, sizeof fname - 1);
166 maxlog("Exporting Goat3D Scene (text) file: %s\n", fname);
167 if(!(igame = GetIGameInterface())) {
168 maxlog("failed to get the igame interface\n");
169 return IMPEXP_FAIL;
170 }
171 IGameConversionManager *cm = GetConversionManager();
172 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
173 igame->InitialiseIGame();
174 igame->SetStaticFrame(0);
176 goat3d *goat = goat3d_create();
178 process_materials(goat);
180 // process all nodes
181 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
182 IGameNode *node = igame->GetTopLevelNode(i);
183 process_node(goat, 0, node);
184 }
186 if(goat3d_save(goat, fname) == -1) {
187 goat3d_free(goat);
188 return IMPEXP_FAIL;
189 }
191 goat3d_free(goat);
192 return IMPEXP_SUCCESS;
193 }
195 static const char *max_string(const MCHAR *wstr)
196 {
197 if(!wstr) return 0;
198 static char str[512];
199 wcstombs(str, wstr, sizeof str - 1);
200 return str;
201 }
203 void GoatExporter::process_materials(goat3d *goat)
204 {
205 IGameProperty *prop;
207 int num_mtl = igame->GetRootMaterialCount();
208 for(int i=0; i<num_mtl; i++) {
209 IGameMaterial *maxmtl = igame->GetRootMaterial(i);
210 if(maxmtl) {
211 goat3d_material *mtl = goat3d_create_mtl();
213 const char *name = max_string(maxmtl->GetMaterialName());
214 if(name) {
215 goat3d_set_mtl_name(mtl, name);
216 }
218 // diffuse
219 if((prop = maxmtl->GetDiffuseData())) {
220 Point3 diffuse(1, 1, 1);
221 prop->GetPropertyValue(diffuse);
222 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
223 diffuse[1], diffuse[2]);
224 }
225 // specular
226 if((prop = maxmtl->GetSpecularData())) {
227 Point3 specular(0, 0, 0);
228 prop->GetPropertyValue(specular);
230 float sstr = 1.0;
231 if((prop = maxmtl->GetSpecularLevelData())) {
232 prop->GetPropertyValue(sstr);
233 }
234 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
235 specular[1] * sstr, specular[2] * sstr);
236 }
237 // shininess
238 if((prop = maxmtl->GetGlossinessData())) {
239 float shin;
240 prop->GetPropertyValue(shin);
241 goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
242 }
244 // textures
245 for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
246 IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
248 const char *fname = max_string(tex->GetBitmapFileName());
249 if(!fname) {
250 continue;
251 }
253 int slot = tex->GetStdMapSlot();
254 switch(slot) {
255 case ID_DI: // diffuse
256 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
257 break;
259 case ID_SP:
260 case ID_SS:
261 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
262 break;
264 case ID_SH:
265 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
266 break;
268 case ID_BU:
269 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
270 break;
272 case ID_RL:
273 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
274 break;
276 case ID_RR:
277 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
278 break;
280 default:
281 break;
282 }
283 }
285 goat3d_add_mtl(goat, mtl);
286 mtlmap[maxmtl] = mtl;
287 }
288 }
289 }
291 void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode)
292 {
293 goat3d_node *node = goat3d_create_node();
294 goat3d_add_node(goat, node);
296 const char *name = max_string(maxnode->GetName());
297 if(name) {
298 goat3d_set_node_name(node, name);
299 }
301 // no animation yet, just get the static PRS
302 GMatrix maxmatrix = maxnode->GetObjectTM();
303 Point3 trans = maxmatrix.Translation();
304 Quat rot = maxmatrix.Rotation();
305 Point3 scale = maxmatrix.Scaling();
307 goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0);
308 goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0);
309 goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0);
311 IGameObject *maxobj = maxnode->GetIGameObject();
312 IGameObject::ObjectTypes type = maxobj->GetIGameType();
314 switch(type) {
315 case IGameObject::IGAME_MESH:
316 {
317 goat3d_mesh *mesh = goat3d_create_mesh();
318 if(name) goat3d_set_mesh_name(mesh, name);
319 goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh);
321 // get the node material and assign it to the mesh
322 IGameMaterial *maxmtl = maxnode->GetNodeMaterial();
323 goat3d_material *mtl = mtlmap[maxmtl];
324 if(mtl) {
325 goat3d_set_mesh_mtl(mesh, mtl);
326 }
328 process_mesh(goat, mesh, maxobj);
329 goat3d_add_mesh(goat, mesh);
330 }
331 break;
333 case IGameObject::IGAME_LIGHT:
334 {
335 goat3d_light *light = goat3d_create_light();
336 //if(name) goat3d_set_light_name(light, name);
337 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
339 process_light(goat, light, maxobj);
340 goat3d_add_light(goat, light);
341 }
342 break;
344 case IGameObject::IGAME_CAMERA:
345 {
346 goat3d_camera *cam = goat3d_create_camera();
347 //if(name) goat3d_set_camera_name(camera, name);
348 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
350 process_camera(goat, cam, maxobj);
351 goat3d_add_camera(goat, cam);
352 }
353 break;
355 default:
356 // otherwise don't assign an object, essentially treating it as a null node
357 break;
358 }
361 for(int i=0; i<maxnode->GetChildCount(); i++) {
362 process_node(goat, node, maxnode->GetNodeChild(i));
363 }
364 }
366 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
367 {
368 IGameMesh *maxmesh = (IGameMesh*)maxobj;
370 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
371 maxobj->InitializeData();
373 int num_verts = maxmesh->GetNumberOfVerts();
374 int num_faces = maxmesh->GetNumberOfFaces();
375 //assert(maxmesh->GetNumberOfTexVerts() == num_verts);
377 float *vertices = new float[num_verts * 3];
378 float *normals = new float[num_verts * 3];
379 //float *texcoords = new float[num_verts * 2];
380 int *indices = new int[num_faces * 3];
382 for(int i=0; i<num_verts; i++) {
383 Point3 v = maxmesh->GetVertex(i, true);
384 vertices[i * 3] = v.x;
385 vertices[i * 3 + 1] = v.y;
386 vertices[i * 3 + 2] = v.z;
387 }
389 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
390 Point3 norm = maxmesh->GetNormal(i);
392 int vidx = maxmesh->GetNormalVertexIndex(i);
393 normals[vidx * 3] = norm.x;
394 normals[vidx * 3 + 1] = norm.y;
395 normals[vidx * 3 + 2] = norm.z;
396 }
398 /*for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
399 Point3 tex = maxmesh->GetTexVertex(i);
401 texcoords[i * 2] = tex.x;
402 texcoords[i * 2 + 1] = tex.y;
403 }*/
405 // get the faces
406 for(int i=0; i<num_faces; i++) {
407 FaceEx *face = maxmesh->GetFace(i);
408 indices[i * 3] = face->vert[0];
409 indices[i * 3 + 1] = face->vert[1];
410 indices[i * 3 + 2] = face->vert[2];
411 // TODO at some point I'll have to split based on normal/texcoord indices
412 }
414 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts);
415 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts);
416 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts);
417 goat3d_set_mesh_faces(mesh, indices, num_faces);
419 delete [] vertices;
420 delete [] normals;
421 //delete [] texcoords;
422 delete [] indices;
423 }
425 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
426 {
427 }
429 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
430 {
431 }
434 // ------------------------------------------
436 class GoatClassDesc : public ClassDesc2 {
437 public:
438 int IsPublic() { return TRUE; }
439 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
440 const TCHAR *ClassName() { return L"GoatExporter"; }
441 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
442 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
443 const TCHAR *Category() { return L"Mutant Stargoat"; }
445 const TCHAR *InternalName() { return L"GoatExporter"; }
446 HINSTANCE HInstance() { return hinst; }
447 };
449 // TODO: make 2 class descriptors, one for goat3d, one for goat3danim
450 static GoatClassDesc class_desc;
452 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
453 {
454 if(reason == DLL_PROCESS_ATTACH) {
455 hinst = inst_handle;
456 DisableThreadLibraryCalls(hinst);
457 }
458 return TRUE;
459 }
461 extern "C" {
463 __declspec(dllexport) const TCHAR *LibDescription()
464 {
465 return L"test exporter";
466 }
468 __declspec(dllexport) int LibNumberClasses()
469 {
470 return 1;
471 }
473 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
474 {
475 return i == 0 ? &class_desc : 0;
476 }
478 __declspec(dllexport) ULONG LibVersion()
479 {
480 return Get3DSMAXVersion();
481 }
483 __declspec(dllexport) int LibInitialize()
484 {
485 static char path[1024];
487 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
488 strcat(path, "/testexp.log");
490 maxlog_open(path);
491 return TRUE;
492 }
494 __declspec(dllexport) int LibShutdown()
495 {
496 maxlog_close();
497 return TRUE;
498 }
500 } // extern "C"