goat3d

view exporters/maxgoat/src/maxgoat.cc @ 58:d317eb4f83da

- made everything compile properly on windows again - removed libanim/libvmath, we'll use them as external dependencies - added new maxgoat_stub 3dsmax plugin project. Gets loaded as a max plugin and loads the actual maxgoat (and later maxgoat_anim) exporters on demand, to allow reloading the actual exporters without having to restart 3dsmax (which takes AGES).
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Mar 2014 03:19:55 +0200
parents 0fe02696fb1e
children 0c3576325480
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 fprintf(logfile, "Exporting Goat3D Scene (text) file: %s\n", fname);
128 if(!(igame = GetIGameInterface())) {
129 fprintf(logfile, "failed to get the igame interface\n");
130 return IMPEXP_FAIL;
131 }
132 IGameConversionManager *cm = GetConversionManager();
133 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
134 igame->InitialiseIGame();
135 igame->SetStaticFrame(0);
137 goat3d *goat = goat3d_create();
139 process_materials(goat);
141 // process all nodes
142 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
143 IGameNode *node = igame->GetTopLevelNode(i);
144 process_node(goat, 0, node);
145 }
147 if(goat3d_save(goat, fname) == -1) {
148 goat3d_free(goat);
149 return IMPEXP_FAIL;
150 }
152 goat3d_free(goat);
153 return IMPEXP_SUCCESS;
154 }
156 static const char *max_string(const MCHAR *wstr)
157 {
158 if(!wstr) return 0;
159 static char str[512];
160 wcstombs(str, wstr, sizeof str - 1);
161 return str;
162 }
164 void GoatExporter::process_materials(goat3d *goat)
165 {
166 IGameProperty *prop;
168 int num_mtl = igame->GetRootMaterialCount();
169 for(int i=0; i<num_mtl; i++) {
170 IGameMaterial *maxmtl = igame->GetRootMaterial(i);
171 if(maxmtl) {
172 goat3d_material *mtl = goat3d_create_mtl();
174 const char *name = max_string(maxmtl->GetMaterialName());
175 if(name) {
176 goat3d_set_mtl_name(mtl, name);
177 }
179 // diffuse
180 if((prop = maxmtl->GetDiffuseData())) {
181 Point3 diffuse(1, 1, 1);
182 prop->GetPropertyValue(diffuse);
183 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
184 diffuse[1], diffuse[2]);
185 }
186 // specular
187 if((prop = maxmtl->GetSpecularData())) {
188 Point3 specular(0, 0, 0);
189 prop->GetPropertyValue(specular);
191 float sstr = 1.0;
192 if((prop = maxmtl->GetSpecularLevelData())) {
193 prop->GetPropertyValue(sstr);
194 }
195 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
196 specular[1] * sstr, specular[2] * sstr);
197 }
198 // shininess
199 if((prop = maxmtl->GetGlossinessData())) {
200 float shin;
201 prop->GetPropertyValue(shin);
202 goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
203 }
205 // textures
206 for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
207 IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
209 const char *fname = max_string(tex->GetBitmapFileName());
210 if(!fname) {
211 continue;
212 }
214 int slot = tex->GetStdMapSlot();
215 switch(slot) {
216 case ID_DI: // diffuse
217 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
218 break;
220 case ID_SP:
221 case ID_SS:
222 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
223 break;
225 case ID_SH:
226 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
227 break;
229 case ID_BU:
230 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
231 break;
233 case ID_RL:
234 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
235 break;
237 case ID_RR:
238 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
239 break;
241 default:
242 break;
243 }
244 }
246 goat3d_add_mtl(goat, mtl);
247 mtlmap[maxmtl] = mtl;
248 }
249 }
250 }
252 void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode)
253 {
254 goat3d_node *node = goat3d_create_node();
255 goat3d_add_node(goat, node);
257 const char *name = max_string(maxnode->GetName());
258 if(name) {
259 goat3d_set_node_name(node, name);
260 }
262 // no animation yet, just get the static PRS
263 GMatrix maxmatrix = maxnode->GetObjectTM();
264 Point3 trans = maxmatrix.Translation();
265 Quat rot = maxmatrix.Rotation();
266 Point3 scale = maxmatrix.Scaling();
268 goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0);
269 goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0);
270 goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0);
272 IGameObject *maxobj = maxnode->GetIGameObject();
273 IGameObject::ObjectTypes type = maxobj->GetIGameType();
275 switch(type) {
276 case IGameObject::IGAME_MESH:
277 {
278 goat3d_mesh *mesh = goat3d_create_mesh();
279 if(name) goat3d_set_mesh_name(mesh, name);
280 goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh);
282 // get the node material and assign it to the mesh
283 IGameMaterial *maxmtl = maxnode->GetNodeMaterial();
284 goat3d_material *mtl = mtlmap[maxmtl];
285 if(mtl) {
286 goat3d_set_mesh_mtl(mesh, mtl);
287 }
289 process_mesh(goat, mesh, maxobj);
290 goat3d_add_mesh(goat, mesh);
291 }
292 break;
294 case IGameObject::IGAME_LIGHT:
295 {
296 goat3d_light *light = goat3d_create_light();
297 //if(name) goat3d_set_light_name(light, name);
298 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
300 process_light(goat, light, maxobj);
301 goat3d_add_light(goat, light);
302 }
303 break;
305 case IGameObject::IGAME_CAMERA:
306 {
307 goat3d_camera *cam = goat3d_create_camera();
308 //if(name) goat3d_set_camera_name(camera, name);
309 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
311 process_camera(goat, cam, maxobj);
312 goat3d_add_camera(goat, cam);
313 }
314 break;
316 default:
317 // otherwise don't assign an object, essentially treating it as a null node
318 break;
319 }
322 for(int i=0; i<maxnode->GetChildCount(); i++) {
323 process_node(goat, node, maxnode->GetNodeChild(i));
324 }
325 }
327 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
328 {
329 IGameMesh *maxmesh = (IGameMesh*)maxobj;
331 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
332 maxobj->InitializeData();
334 int num_verts = maxmesh->GetNumberOfVerts();
335 int num_faces = maxmesh->GetNumberOfFaces();
336 //assert(maxmesh->GetNumberOfTexVerts() == num_verts);
338 float *vertices = new float[num_verts * 3];
339 float *normals = new float[num_verts * 3];
340 //float *texcoords = new float[num_verts * 2];
341 int *indices = new int[num_faces * 3];
343 for(int i=0; i<num_verts; i++) {
344 Point3 v = maxmesh->GetVertex(i, true);
345 vertices[i * 3] = v.x;
346 vertices[i * 3 + 1] = v.y;
347 vertices[i * 3 + 2] = v.z;
348 }
350 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
351 Point3 norm = maxmesh->GetNormal(i);
353 int vidx = maxmesh->GetNormalVertexIndex(i);
354 normals[vidx * 3] = norm.x;
355 normals[vidx * 3 + 1] = norm.y;
356 normals[vidx * 3 + 2] = norm.z;
357 }
359 /*for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
360 Point3 tex = maxmesh->GetTexVertex(i);
362 texcoords[i * 2] = tex.x;
363 texcoords[i * 2 + 1] = tex.y;
364 }*/
366 // get the faces
367 for(int i=0; i<num_faces; i++) {
368 FaceEx *face = maxmesh->GetFace(i);
369 indices[i * 3] = face->vert[0];
370 indices[i * 3 + 1] = face->vert[1];
371 indices[i * 3 + 2] = face->vert[2];
372 // TODO at some point I'll have to split based on normal/texcoord indices
373 }
375 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts);
376 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts);
377 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts);
378 goat3d_set_mesh_faces(mesh, indices, num_faces);
380 delete [] vertices;
381 delete [] normals;
382 //delete [] texcoords;
383 delete [] indices;
384 }
386 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
387 {
388 }
390 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
391 {
392 }
395 // ------------------------------------------
397 class GoatClassDesc : public ClassDesc2 {
398 public:
399 int IsPublic() { return TRUE; }
400 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
401 const TCHAR *ClassName() { return L"GoatExporter"; }
402 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
403 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
404 const TCHAR *Category() { return L"Mutant Stargoat"; }
406 const TCHAR *InternalName() { return L"GoatExporter"; }
407 HINSTANCE HInstance() { return hinst; }
408 };
410 // TODO: make 2 class descriptors, one for goat3d, one for goat3danim
411 static GoatClassDesc class_desc;
413 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
414 {
415 if(reason == DLL_PROCESS_ATTACH) {
416 hinst = inst_handle;
417 DisableThreadLibraryCalls(hinst);
418 }
419 return TRUE;
420 }
422 extern "C" {
424 __declspec(dllexport) const TCHAR *LibDescription()
425 {
426 return L"test exporter";
427 }
429 __declspec(dllexport) int LibNumberClasses()
430 {
431 return 1;
432 }
434 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
435 {
436 return i == 0 ? &class_desc : 0;
437 }
439 __declspec(dllexport) ULONG LibVersion()
440 {
441 return Get3DSMAXVersion();
442 }
444 __declspec(dllexport) int LibInitialize()
445 {
446 static char path[1024];
448 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
449 strcat(path, "/testexp.log");
451 if((logfile = fopen(path, "w"))) {
452 setvbuf(logfile, 0, _IONBF, 0);
453 }
454 return TRUE;
455 }
457 __declspec(dllexport) int LibShutdown()
458 {
459 if(logfile) {
460 fclose(logfile);
461 logfile = 0;
462 }
463 return TRUE;
464 }
466 } // extern "C"