goat3d
view exporters/maxgoat/src/maxgoat.cc @ 51:fa5c52ea9d59
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 17 Jan 2014 18:16:09 +0200 |
parents | 9ba3e2fb8a33 |
children | d317eb4f83da |
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 = mtlmap[maxmtl];
284 if(mtl) {
285 goat3d_set_mesh_mtl(mesh, mtl);
286 }
288 process_mesh(goat, mesh, maxobj);
289 goat3d_add_mesh(goat, mesh);
290 }
291 break;
293 case IGameObject::IGAME_LIGHT:
294 {
295 goat3d_light *light = goat3d_create_light();
296 //if(name) goat3d_set_light_name(light, name);
297 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
299 process_light(goat, light, maxobj);
300 goat3d_add_light(goat, light);
301 }
302 break;
304 case IGameObject::IGAME_CAMERA:
305 {
306 goat3d_camera *cam = goat3d_create_camera();
307 //if(name) goat3d_set_camera_name(camera, name);
308 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
310 process_camera(goat, cam, maxobj);
311 goat3d_add_camera(goat, cam);
312 }
313 break;
315 default:
316 // otherwise don't assign an object, essentially treating it as a null node
317 break;
318 }
321 for(int i=0; i<maxnode->GetChildCount(); i++) {
322 process_node(goat, node, maxnode->GetNodeChild(i));
323 }
324 }
326 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
327 {
328 IGameMesh *maxmesh = (IGameMesh*)maxobj;
330 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
331 maxobj->InitializeData();
333 int num_verts = maxmesh->GetNumberOfVerts();
334 int num_faces = maxmesh->GetNumberOfFaces();
335 //assert(maxmesh->GetNumberOfTexVerts() == num_verts);
337 float *vertices = new float[num_verts * 3];
338 float *normals = new float[num_verts * 3];
339 //float *texcoords = new float[num_verts * 2];
340 int *indices = new int[num_faces * 3];
342 for(int i=0; i<num_verts; i++) {
343 Point3 v = maxmesh->GetVertex(i, true);
344 vertices[i * 3] = v.x;
345 vertices[i * 3 + 1] = v.y;
346 vertices[i * 3 + 2] = v.z;
347 }
349 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
350 Point3 norm = maxmesh->GetNormal(i);
352 int vidx = maxmesh->GetNormalVertexIndex(i);
353 normals[vidx * 3] = norm.x;
354 normals[vidx * 3 + 1] = norm.y;
355 normals[vidx * 3 + 2] = norm.z;
356 }
358 /*for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
359 Point3 tex = maxmesh->GetTexVertex(i);
361 texcoords[i * 2] = tex.x;
362 texcoords[i * 2 + 1] = tex.y;
363 }*/
365 // get the faces
366 for(int i=0; i<num_faces; i++) {
367 FaceEx *face = maxmesh->GetFace(i);
368 indices[i * 3] = face->vert[0];
369 indices[i * 3 + 1] = face->vert[1];
370 indices[i * 3 + 2] = face->vert[2];
371 // TODO at some point I'll have to split based on normal/texcoord indices
372 }
374 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts);
375 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts);
376 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts);
377 goat3d_set_mesh_faces(mesh, indices, num_faces);
379 delete [] vertices;
380 delete [] normals;
381 //delete [] texcoords;
382 delete [] indices;
383 }
385 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
386 {
387 }
389 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
390 {
391 }
394 // ------------------------------------------
396 class GoatClassDesc : public ClassDesc2 {
397 public:
398 int IsPublic() { return TRUE; }
399 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
400 const TCHAR *ClassName() { return L"GoatExporter"; }
401 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
402 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
403 const TCHAR *Category() { return L"Mutant Stargoat"; }
405 const TCHAR *InternalName() { return L"GoatExporter"; }
406 HINSTANCE HInstance() { return hinst; }
407 };
409 static GoatClassDesc class_desc;
411 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
412 {
413 if(reason == DLL_PROCESS_ATTACH) {
414 hinst = inst_handle;
415 DisableThreadLibraryCalls(hinst);
416 }
417 return TRUE;
418 }
420 extern "C" {
422 __declspec(dllexport) const TCHAR *LibDescription()
423 {
424 return L"test exporter";
425 }
427 __declspec(dllexport) int LibNumberClasses()
428 {
429 return 1;
430 }
432 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
433 {
434 return i == 0 ? &class_desc : 0;
435 }
437 __declspec(dllexport) ULONG LibVersion()
438 {
439 return Get3DSMAXVersion();
440 }
442 __declspec(dllexport) int LibInitialize()
443 {
444 static char path[1024];
446 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
447 strcat(path, "/testexp.log");
449 if((logfile = fopen(path, "w"))) {
450 setvbuf(logfile, 0, _IONBF, 0);
451 }
452 return TRUE;
453 }
455 __declspec(dllexport) int LibShutdown()
456 {
457 if(logfile) {
458 fclose(logfile);
459 logfile = 0;
460 }
461 return TRUE;
462 }
464 } // extern "C"