goat3d

view exporters/maxgoat/src/maxgoat.cc @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents d0260d80ae09
children 9ba3e2fb8a33
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 "config.h"
19 #pragma comment (lib, "core.lib")
20 #pragma comment (lib, "geom.lib")
21 #pragma comment (lib, "gfx.lib")
22 #pragma comment (lib, "mesh.lib")
23 #pragma comment (lib, "maxutil.lib")
24 #pragma comment (lib, "maxscrpt.lib")
25 #pragma comment (lib, "paramblk2.lib")
26 #pragma comment (lib, "msxml2.lib")
27 #pragma comment (lib, "igame.lib")
28 #pragma comment (lib, "comctl32.lib")
31 #define VERSION(major, minor) \
32 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
34 static FILE *logfile;
35 static HINSTANCE hinst;
37 class GoatExporter : public SceneExport {
38 private:
39 //std::map<IGameMaterial*, goat3d_material*> mtlmap;
40 //std::map<IGameNode*, goat3d_node*> nodemap;
42 public:
43 IGameScene *igame;
45 int ExtCount();
46 const TCHAR *Ext(int n);
47 const TCHAR *LongDesc();
48 const TCHAR *ShortDesc();
49 const TCHAR *AuthorName();
50 const TCHAR *CopyrightMessage();
51 const TCHAR *OtherMessage1();
52 const TCHAR *OtherMessage2();
53 unsigned int Version();
54 void ShowAbout(HWND win);
56 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
58 void process_materials(goat3d *goat);
60 void process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode);
62 void process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj);
63 void process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj);
64 void process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj);
65 };
68 int GoatExporter::ExtCount()
69 {
70 return 1;
71 }
73 const TCHAR *GoatExporter::Ext(int n)
74 {
75 return L"xml";
76 }
78 const TCHAR *GoatExporter::LongDesc()
79 {
80 return L"Goat3D scene file";
81 }
83 const TCHAR *GoatExporter::ShortDesc()
84 {
85 return L"Goat3D";
86 }
88 const TCHAR *GoatExporter::AuthorName()
89 {
90 return L"John Tsiombikas";
91 }
93 const TCHAR *GoatExporter::CopyrightMessage()
94 {
95 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
96 }
98 const TCHAR *GoatExporter::OtherMessage1()
99 {
100 return L"other1";
101 }
103 const TCHAR *GoatExporter::OtherMessage2()
104 {
105 return L"other2";
106 }
108 unsigned int GoatExporter::Version()
109 {
110 return VERSION(VER_MAJOR, VER_MINOR);
111 }
113 void GoatExporter::ShowAbout(HWND win)
114 {
115 MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
116 }
118 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
119 BOOL non_interactive, DWORD opt)
120 {
121 //mtlmap.clear();
122 //nodemap.clear();
124 char fname[512];
125 wcstombs(fname, name, sizeof fname - 1);
127 if(!(igame = GetIGameInterface())) {
128 fprintf(logfile, "failed to get the igame interface\n");
129 return IMPEXP_FAIL;
130 }
131 IGameConversionManager *cm = GetConversionManager();
132 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
133 igame->InitialiseIGame();
134 igame->SetStaticFrame(0);
136 goat3d *goat = goat3d_create();
138 process_materials(goat);
140 // process all nodes
141 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
142 IGameNode *node = igame->GetTopLevelNode(i);
143 process_node(goat, 0, node);
144 }
146 if(goat3d_save(goat, fname) == -1) {
147 goat3d_free(goat);
148 return IMPEXP_FAIL;
149 }
151 goat3d_free(goat);
152 return IMPEXP_SUCCESS;
153 }
155 static const char *max_string(const MCHAR *wstr)
156 {
157 if(!wstr) return 0;
158 static char str[512];
159 wcstombs(str, wstr, sizeof str - 1);
160 return str;
161 }
163 void GoatExporter::process_materials(goat3d *goat)
164 {
165 IGameProperty *prop;
167 int num_mtl = igame->GetRootMaterialCount();
168 for(int i=0; i<num_mtl; i++) {
169 IGameMaterial *maxmtl = igame->GetRootMaterial(i);
170 if(maxmtl) {
171 goat3d_material *mtl = goat3d_create_mtl();
173 const char *name = max_string(maxmtl->GetMaterialName());
174 if(name) {
175 goat3d_set_mtl_name(mtl, name);
176 }
178 // diffuse
179 if((prop = maxmtl->GetDiffuseData())) {
180 Point3 diffuse(1, 1, 1);
181 prop->GetPropertyValue(diffuse);
182 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
183 diffuse[1], diffuse[2]);
184 }
185 // specular
186 if((prop = maxmtl->GetSpecularData())) {
187 Point3 specular(0, 0, 0);
188 prop->GetPropertyValue(specular);
190 float sstr = 1.0;
191 if((prop = maxmtl->GetSpecularLevelData())) {
192 prop->GetPropertyValue(sstr);
193 }
194 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
195 specular[1] * sstr, specular[2] * sstr);
196 }
197 // shininess
198 if((prop = maxmtl->GetGlossinessData())) {
199 float shin;
200 prop->GetPropertyValue(shin);
201 goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
202 }
204 // textures
205 for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
206 IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
208 const char *fname = max_string(tex->GetBitmapFileName());
209 if(!fname) {
210 continue;
211 }
213 int slot = tex->GetStdMapSlot();
214 switch(slot) {
215 case ID_DI: // diffuse
216 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
217 break;
219 case ID_SP:
220 case ID_SS:
221 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
222 break;
224 case ID_SH:
225 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
226 break;
228 case ID_BU:
229 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
230 break;
232 case ID_RL:
233 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
234 break;
236 case ID_RR:
237 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
238 break;
240 default:
241 break;
242 }
243 }
245 goat3d_add_mtl(goat, mtl);
246 //mtlmap[maxmtl] = mtl;
247 }
248 }
249 }
251 void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode)
252 {
253 goat3d_node *node = goat3d_create_node();
254 goat3d_add_node(goat, node);
256 const char *name = max_string(maxnode->GetName());
257 if(name) {
258 goat3d_set_node_name(node, name);
259 }
261 // no animation yet, just get the static PRS
262 GMatrix maxmatrix = maxnode->GetObjectTM();
263 Point3 trans = maxmatrix.Translation();
264 Quat rot = maxmatrix.Rotation();
265 Point3 scale = maxmatrix.Scaling();
267 goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0);
268 goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0);
269 goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0);
271 IGameObject *maxobj = maxnode->GetIGameObject();
272 IGameObject::ObjectTypes type = maxobj->GetIGameType();
274 switch(type) {
275 case IGameObject::IGAME_MESH:
276 {
277 goat3d_mesh *mesh = goat3d_create_mesh();
278 if(name) goat3d_set_mesh_name(mesh, name);
279 goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh);
281 // get the node material and assign it to the mesh
282 IGameMaterial *maxmtl = maxnode->GetNodeMaterial();
283 goat3d_material *mtl = 0;//mtlmap[maxmtl];
284 if(mtl) {
285 goat3d_set_mesh_mtl(mesh, mtl);
286 }
288 process_mesh(goat, mesh, maxobj);
289 }
290 break;
292 case IGameObject::IGAME_LIGHT:
293 {
294 goat3d_light *light = goat3d_create_light();
295 //if(name) goat3d_set_light_name(light, name);
296 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
298 process_light(goat, light, maxobj);
299 }
300 break;
302 case IGameObject::IGAME_CAMERA:
303 {
304 goat3d_camera *cam = goat3d_create_camera();
305 //if(name) goat3d_set_camera_name(camera, name);
306 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
308 process_camera(goat, cam, maxobj);
309 }
310 break;
312 default:
313 // otherwise don't assign an object, essentially treating it as a null node
314 break;
315 }
318 for(int i=0; i<maxnode->GetChildCount(); i++) {
319 process_node(goat, node, maxnode->GetNodeChild(i));
320 }
321 }
323 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
324 {
325 IGameMesh *maxmesh = (IGameMesh*)maxobj;
327 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
328 maxobj->InitializeData();
330 int num_verts = maxmesh->GetNumberOfVerts();
331 assert(maxmesh->GetNumberOfTexVerts() == num_verts);
333 float *vertices = new float[num_verts * 3];
334 float *normals = new float[num_verts * 3];
335 float *texcoords = new float[num_verts * 2];
337 for(int i=0; i<num_verts; i++) {
338 Point3 v = maxmesh->GetVertex(i, true);
339 vertices[i * 3] = v.x;
340 vertices[i * 3 + 1] = v.y;
341 vertices[i * 3 + 2] = v.z;
342 }
344 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
345 Point3 norm = maxmesh->GetNormal(i);
347 int vidx = maxmesh->GetNormalVertexIndex(i);
348 normals[vidx * 3] = norm.x;
349 normals[vidx * 3 + 1] = norm.y;
350 normals[vidx * 3 + 2] = norm.z;
351 }
353 for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
354 Point3 tex = maxmesh->GetTexVertex(i);
356 texcoords[i * 2] = tex.x;
357 texcoords[i * 2 + 1] = tex.y;
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);
364 delete [] vertices;
365 delete [] normals;
366 delete [] texcoords;
367 }
369 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
370 {
371 }
373 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
374 {
375 }
378 // ------------------------------------------
380 class GoatClassDesc : public ClassDesc2 {
381 public:
382 int IsPublic() { return TRUE; }
383 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
384 const TCHAR *ClassName() { return L"GoatExporter"; }
385 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
386 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
387 const TCHAR *Category() { return L"Mutant Stargoat"; }
389 const TCHAR *InternalName() { return L"GoatExporter"; }
390 HINSTANCE HInstance() { return hinst; }
391 };
393 static GoatClassDesc class_desc;
395 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
396 {
397 if(reason == DLL_PROCESS_ATTACH) {
398 hinst = inst_handle;
399 DisableThreadLibraryCalls(hinst);
400 }
401 return TRUE;
402 }
404 extern "C" {
406 __declspec(dllexport) const TCHAR *LibDescription()
407 {
408 return L"test exporter";
409 }
411 __declspec(dllexport) int LibNumberClasses()
412 {
413 return 1;
414 }
416 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
417 {
418 return i == 0 ? &class_desc : 0;
419 }
421 __declspec(dllexport) ULONG LibVersion()
422 {
423 return Get3DSMAXVersion();
424 }
426 __declspec(dllexport) int LibInitialize()
427 {
428 static char path[1024];
430 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
431 strcat(path, "/testexp.log");
433 if((logfile = fopen(path, "w"))) {
434 setvbuf(logfile, 0, _IONBF, 0);
435 }
436 return TRUE;
437 }
439 __declspec(dllexport) int LibShutdown()
440 {
441 if(logfile) {
442 fclose(logfile);
443 logfile = 0;
444 }
445 return TRUE;
446 }
448 } // extern "C"