goat3d

annotate exporters/maxgoat/src/maxgoat.cc @ 60:0c3576325480

moving the exporter along slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 30 Mar 2014 08:53:33 +0300
parents d317eb4f83da
children fdece14403ff
rev   line source
nuclear@5 1 #include <stdio.h>
nuclear@5 2 #include <string.h>
nuclear@27 3 #include <stdlib.h>
nuclear@5 4 #include <errno.h>
nuclear@27 5 #include <map>
nuclear@5 6 #include <windows.h>
nuclear@5 7 #include <shlobj.h>
nuclear@5 8 #include "max.h"
nuclear@5 9 #include "impexp.h" // SceneExport
nuclear@5 10 #include "iparamb2.h" // ClassDesc2
nuclear@5 11 #include "plugapi.h"
nuclear@5 12 #include "IGame.h"
nuclear@5 13 #include "IGameExport.h"
nuclear@5 14 #include "IConversionmanager.h"
nuclear@25 15 #include "goat3d.h"
nuclear@60 16 #include "minwin.h"
nuclear@10 17 #include "config.h"
nuclear@5 18
nuclear@5 19
nuclear@5 20 #pragma comment (lib, "core.lib")
nuclear@5 21 #pragma comment (lib, "geom.lib")
nuclear@5 22 #pragma comment (lib, "gfx.lib")
nuclear@5 23 #pragma comment (lib, "mesh.lib")
nuclear@5 24 #pragma comment (lib, "maxutil.lib")
nuclear@5 25 #pragma comment (lib, "maxscrpt.lib")
nuclear@5 26 #pragma comment (lib, "paramblk2.lib")
nuclear@5 27 #pragma comment (lib, "msxml2.lib")
nuclear@5 28 #pragma comment (lib, "igame.lib")
nuclear@5 29 #pragma comment (lib, "comctl32.lib")
nuclear@5 30
nuclear@5 31
nuclear@10 32 #define VERSION(major, minor) \
nuclear@10 33 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
nuclear@10 34
nuclear@5 35 static FILE *logfile;
nuclear@5 36 static HINSTANCE hinst;
nuclear@5 37
nuclear@5 38 class GoatExporter : public SceneExport {
nuclear@27 39 private:
nuclear@28 40 std::map<IGameMaterial*, goat3d_material*> mtlmap;
nuclear@28 41 std::map<IGameNode*, goat3d_node*> nodemap;
nuclear@27 42
nuclear@5 43 public:
nuclear@5 44 IGameScene *igame;
nuclear@5 45
nuclear@5 46 int ExtCount();
nuclear@5 47 const TCHAR *Ext(int n);
nuclear@5 48 const TCHAR *LongDesc();
nuclear@5 49 const TCHAR *ShortDesc();
nuclear@5 50 const TCHAR *AuthorName();
nuclear@5 51 const TCHAR *CopyrightMessage();
nuclear@5 52 const TCHAR *OtherMessage1();
nuclear@5 53 const TCHAR *OtherMessage2();
nuclear@5 54 unsigned int Version();
nuclear@5 55 void ShowAbout(HWND win);
nuclear@5 56
nuclear@5 57 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
nuclear@5 58
nuclear@27 59 void process_materials(goat3d *goat);
nuclear@27 60
nuclear@27 61 void process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode);
nuclear@27 62
nuclear@27 63 void process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj);
nuclear@27 64 void process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj);
nuclear@27 65 void process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj);
nuclear@5 66 };
nuclear@5 67
nuclear@5 68
nuclear@5 69 int GoatExporter::ExtCount()
nuclear@5 70 {
nuclear@5 71 return 1;
nuclear@5 72 }
nuclear@5 73
nuclear@5 74 const TCHAR *GoatExporter::Ext(int n)
nuclear@5 75 {
nuclear@27 76 return L"xml";
nuclear@5 77 }
nuclear@5 78
nuclear@5 79 const TCHAR *GoatExporter::LongDesc()
nuclear@5 80 {
nuclear@5 81 return L"Goat3D scene file";
nuclear@5 82 }
nuclear@5 83
nuclear@5 84 const TCHAR *GoatExporter::ShortDesc()
nuclear@5 85 {
nuclear@5 86 return L"Goat3D";
nuclear@5 87 }
nuclear@5 88
nuclear@5 89 const TCHAR *GoatExporter::AuthorName()
nuclear@5 90 {
nuclear@5 91 return L"John Tsiombikas";
nuclear@5 92 }
nuclear@5 93
nuclear@5 94 const TCHAR *GoatExporter::CopyrightMessage()
nuclear@5 95 {
nuclear@5 96 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
nuclear@5 97 }
nuclear@5 98
nuclear@5 99 const TCHAR *GoatExporter::OtherMessage1()
nuclear@5 100 {
nuclear@5 101 return L"other1";
nuclear@5 102 }
nuclear@5 103
nuclear@5 104 const TCHAR *GoatExporter::OtherMessage2()
nuclear@5 105 {
nuclear@5 106 return L"other2";
nuclear@5 107 }
nuclear@5 108
nuclear@5 109 unsigned int GoatExporter::Version()
nuclear@5 110 {
nuclear@10 111 return VERSION(VER_MAJOR, VER_MINOR);
nuclear@5 112 }
nuclear@5 113
nuclear@5 114 void GoatExporter::ShowAbout(HWND win)
nuclear@5 115 {
nuclear@5 116 MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
nuclear@5 117 }
nuclear@5 118
nuclear@5 119 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
nuclear@5 120 BOOL non_interactive, DWORD opt)
nuclear@5 121 {
nuclear@60 122 mw_test();
nuclear@60 123
nuclear@28 124 mtlmap.clear();
nuclear@28 125 nodemap.clear();
nuclear@27 126
nuclear@5 127 char fname[512];
nuclear@5 128 wcstombs(fname, name, sizeof fname - 1);
nuclear@5 129
nuclear@58 130 fprintf(logfile, "Exporting Goat3D Scene (text) file: %s\n", fname);
nuclear@5 131 if(!(igame = GetIGameInterface())) {
nuclear@5 132 fprintf(logfile, "failed to get the igame interface\n");
nuclear@5 133 return IMPEXP_FAIL;
nuclear@5 134 }
nuclear@5 135 IGameConversionManager *cm = GetConversionManager();
nuclear@5 136 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
nuclear@5 137 igame->InitialiseIGame();
nuclear@5 138 igame->SetStaticFrame(0);
nuclear@5 139
nuclear@25 140 goat3d *goat = goat3d_create();
nuclear@5 141
nuclear@27 142 process_materials(goat);
nuclear@27 143
nuclear@27 144 // process all nodes
nuclear@27 145 for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
nuclear@27 146 IGameNode *node = igame->GetTopLevelNode(i);
nuclear@27 147 process_node(goat, 0, node);
nuclear@27 148 }
nuclear@5 149
nuclear@25 150 if(goat3d_save(goat, fname) == -1) {
nuclear@25 151 goat3d_free(goat);
nuclear@25 152 return IMPEXP_FAIL;
nuclear@25 153 }
nuclear@25 154
nuclear@25 155 goat3d_free(goat);
nuclear@5 156 return IMPEXP_SUCCESS;
nuclear@5 157 }
nuclear@5 158
nuclear@25 159 static const char *max_string(const MCHAR *wstr)
nuclear@25 160 {
nuclear@25 161 if(!wstr) return 0;
nuclear@25 162 static char str[512];
nuclear@25 163 wcstombs(str, wstr, sizeof str - 1);
nuclear@25 164 return str;
nuclear@25 165 }
nuclear@25 166
nuclear@27 167 void GoatExporter::process_materials(goat3d *goat)
nuclear@5 168 {
nuclear@5 169 IGameProperty *prop;
nuclear@5 170
nuclear@5 171 int num_mtl = igame->GetRootMaterialCount();
nuclear@25 172 for(int i=0; i<num_mtl; i++) {
nuclear@25 173 IGameMaterial *maxmtl = igame->GetRootMaterial(i);
nuclear@25 174 if(maxmtl) {
nuclear@25 175 goat3d_material *mtl = goat3d_create_mtl();
nuclear@5 176
nuclear@25 177 const char *name = max_string(maxmtl->GetMaterialName());
nuclear@25 178 if(name) {
nuclear@25 179 goat3d_set_mtl_name(mtl, name);
nuclear@5 180 }
nuclear@5 181
nuclear@25 182 // diffuse
nuclear@25 183 if((prop = maxmtl->GetDiffuseData())) {
nuclear@25 184 Point3 diffuse(1, 1, 1);
nuclear@5 185 prop->GetPropertyValue(diffuse);
nuclear@25 186 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
nuclear@25 187 diffuse[1], diffuse[2]);
nuclear@5 188 }
nuclear@25 189 // specular
nuclear@25 190 if((prop = maxmtl->GetSpecularData())) {
nuclear@25 191 Point3 specular(0, 0, 0);
nuclear@5 192 prop->GetPropertyValue(specular);
nuclear@25 193
nuclear@25 194 float sstr = 1.0;
nuclear@25 195 if((prop = maxmtl->GetSpecularLevelData())) {
nuclear@25 196 prop->GetPropertyValue(sstr);
nuclear@25 197 }
nuclear@25 198 goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
nuclear@25 199 specular[1] * sstr, specular[2] * sstr);
nuclear@5 200 }
nuclear@25 201 // shininess
nuclear@25 202 if((prop = maxmtl->GetGlossinessData())) {
nuclear@25 203 float shin;
nuclear@5 204 prop->GetPropertyValue(shin);
nuclear@25 205 goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
nuclear@5 206 }
nuclear@5 207
nuclear@25 208 // textures
nuclear@25 209 for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
nuclear@25 210 IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
nuclear@5 211
nuclear@25 212 const char *fname = max_string(tex->GetBitmapFileName());
nuclear@25 213 if(!fname) {
nuclear@25 214 continue;
nuclear@25 215 }
nuclear@25 216
nuclear@25 217 int slot = tex->GetStdMapSlot();
nuclear@25 218 switch(slot) {
nuclear@25 219 case ID_DI: // diffuse
nuclear@25 220 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
nuclear@25 221 break;
nuclear@25 222
nuclear@25 223 case ID_SP:
nuclear@25 224 case ID_SS:
nuclear@25 225 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
nuclear@25 226 break;
nuclear@25 227
nuclear@25 228 case ID_SH:
nuclear@25 229 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
nuclear@25 230 break;
nuclear@25 231
nuclear@25 232 case ID_BU:
nuclear@25 233 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
nuclear@25 234 break;
nuclear@25 235
nuclear@25 236 case ID_RL:
nuclear@25 237 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
nuclear@25 238 break;
nuclear@25 239
nuclear@25 240 case ID_RR:
nuclear@25 241 goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
nuclear@25 242 break;
nuclear@25 243
nuclear@25 244 default:
nuclear@25 245 break;
nuclear@5 246 }
nuclear@5 247 }
nuclear@25 248
nuclear@25 249 goat3d_add_mtl(goat, mtl);
nuclear@28 250 mtlmap[maxmtl] = mtl;
nuclear@5 251 }
nuclear@5 252 }
nuclear@25 253 }
nuclear@5 254
nuclear@27 255 void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode)
nuclear@25 256 {
nuclear@27 257 goat3d_node *node = goat3d_create_node();
nuclear@27 258 goat3d_add_node(goat, node);
nuclear@25 259
nuclear@27 260 const char *name = max_string(maxnode->GetName());
nuclear@27 261 if(name) {
nuclear@27 262 goat3d_set_node_name(node, name);
nuclear@27 263 }
nuclear@27 264
nuclear@27 265 // no animation yet, just get the static PRS
nuclear@27 266 GMatrix maxmatrix = maxnode->GetObjectTM();
nuclear@27 267 Point3 trans = maxmatrix.Translation();
nuclear@27 268 Quat rot = maxmatrix.Rotation();
nuclear@27 269 Point3 scale = maxmatrix.Scaling();
nuclear@27 270
nuclear@27 271 goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0);
nuclear@27 272 goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0);
nuclear@27 273 goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0);
nuclear@27 274
nuclear@27 275 IGameObject *maxobj = maxnode->GetIGameObject();
nuclear@27 276 IGameObject::ObjectTypes type = maxobj->GetIGameType();
nuclear@27 277
nuclear@27 278 switch(type) {
nuclear@27 279 case IGameObject::IGAME_MESH:
nuclear@27 280 {
nuclear@27 281 goat3d_mesh *mesh = goat3d_create_mesh();
nuclear@27 282 if(name) goat3d_set_mesh_name(mesh, name);
nuclear@27 283 goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh);
nuclear@27 284
nuclear@27 285 // get the node material and assign it to the mesh
nuclear@27 286 IGameMaterial *maxmtl = maxnode->GetNodeMaterial();
nuclear@28 287 goat3d_material *mtl = mtlmap[maxmtl];
nuclear@27 288 if(mtl) {
nuclear@27 289 goat3d_set_mesh_mtl(mesh, mtl);
nuclear@27 290 }
nuclear@27 291
nuclear@27 292 process_mesh(goat, mesh, maxobj);
nuclear@30 293 goat3d_add_mesh(goat, mesh);
nuclear@27 294 }
nuclear@27 295 break;
nuclear@27 296
nuclear@27 297 case IGameObject::IGAME_LIGHT:
nuclear@27 298 {
nuclear@27 299 goat3d_light *light = goat3d_create_light();
nuclear@27 300 //if(name) goat3d_set_light_name(light, name);
nuclear@27 301 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light);
nuclear@27 302
nuclear@27 303 process_light(goat, light, maxobj);
nuclear@30 304 goat3d_add_light(goat, light);
nuclear@27 305 }
nuclear@27 306 break;
nuclear@27 307
nuclear@27 308 case IGameObject::IGAME_CAMERA:
nuclear@27 309 {
nuclear@27 310 goat3d_camera *cam = goat3d_create_camera();
nuclear@27 311 //if(name) goat3d_set_camera_name(camera, name);
nuclear@27 312 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam);
nuclear@27 313
nuclear@27 314 process_camera(goat, cam, maxobj);
nuclear@30 315 goat3d_add_camera(goat, cam);
nuclear@27 316 }
nuclear@27 317 break;
nuclear@27 318
nuclear@27 319 default:
nuclear@27 320 // otherwise don't assign an object, essentially treating it as a null node
nuclear@27 321 break;
nuclear@27 322 }
nuclear@27 323
nuclear@27 324
nuclear@27 325 for(int i=0; i<maxnode->GetChildCount(); i++) {
nuclear@27 326 process_node(goat, node, maxnode->GetNodeChild(i));
nuclear@25 327 }
nuclear@5 328 }
nuclear@5 329
nuclear@27 330 void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
nuclear@27 331 {
nuclear@27 332 IGameMesh *maxmesh = (IGameMesh*)maxobj;
nuclear@27 333
nuclear@27 334 maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs
nuclear@27 335 maxobj->InitializeData();
nuclear@27 336
nuclear@27 337 int num_verts = maxmesh->GetNumberOfVerts();
nuclear@30 338 int num_faces = maxmesh->GetNumberOfFaces();
nuclear@28 339 //assert(maxmesh->GetNumberOfTexVerts() == num_verts);
nuclear@27 340
nuclear@27 341 float *vertices = new float[num_verts * 3];
nuclear@27 342 float *normals = new float[num_verts * 3];
nuclear@28 343 //float *texcoords = new float[num_verts * 2];
nuclear@30 344 int *indices = new int[num_faces * 3];
nuclear@27 345
nuclear@27 346 for(int i=0; i<num_verts; i++) {
nuclear@27 347 Point3 v = maxmesh->GetVertex(i, true);
nuclear@27 348 vertices[i * 3] = v.x;
nuclear@27 349 vertices[i * 3 + 1] = v.y;
nuclear@27 350 vertices[i * 3 + 2] = v.z;
nuclear@27 351 }
nuclear@27 352
nuclear@27 353 for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) {
nuclear@27 354 Point3 norm = maxmesh->GetNormal(i);
nuclear@27 355
nuclear@27 356 int vidx = maxmesh->GetNormalVertexIndex(i);
nuclear@27 357 normals[vidx * 3] = norm.x;
nuclear@27 358 normals[vidx * 3 + 1] = norm.y;
nuclear@27 359 normals[vidx * 3 + 2] = norm.z;
nuclear@27 360 }
nuclear@27 361
nuclear@28 362 /*for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) {
nuclear@27 363 Point3 tex = maxmesh->GetTexVertex(i);
nuclear@27 364
nuclear@27 365 texcoords[i * 2] = tex.x;
nuclear@27 366 texcoords[i * 2 + 1] = tex.y;
nuclear@28 367 }*/
nuclear@27 368
nuclear@30 369 // get the faces
nuclear@30 370 for(int i=0; i<num_faces; i++) {
nuclear@30 371 FaceEx *face = maxmesh->GetFace(i);
nuclear@30 372 indices[i * 3] = face->vert[0];
nuclear@30 373 indices[i * 3 + 1] = face->vert[1];
nuclear@30 374 indices[i * 3 + 2] = face->vert[2];
nuclear@30 375 // TODO at some point I'll have to split based on normal/texcoord indices
nuclear@30 376 }
nuclear@30 377
nuclear@27 378 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts);
nuclear@27 379 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts);
nuclear@28 380 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts);
nuclear@30 381 goat3d_set_mesh_faces(mesh, indices, num_faces);
nuclear@27 382
nuclear@27 383 delete [] vertices;
nuclear@27 384 delete [] normals;
nuclear@28 385 //delete [] texcoords;
nuclear@30 386 delete [] indices;
nuclear@27 387 }
nuclear@27 388
nuclear@27 389 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
nuclear@27 390 {
nuclear@27 391 }
nuclear@27 392
nuclear@27 393 void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj)
nuclear@27 394 {
nuclear@27 395 }
nuclear@27 396
nuclear@27 397
nuclear@5 398 // ------------------------------------------
nuclear@5 399
nuclear@5 400 class GoatClassDesc : public ClassDesc2 {
nuclear@5 401 public:
nuclear@5 402 int IsPublic() { return TRUE; }
nuclear@5 403 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
nuclear@5 404 const TCHAR *ClassName() { return L"GoatExporter"; }
nuclear@5 405 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
nuclear@5 406 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
nuclear@5 407 const TCHAR *Category() { return L"Mutant Stargoat"; }
nuclear@5 408
nuclear@5 409 const TCHAR *InternalName() { return L"GoatExporter"; }
nuclear@5 410 HINSTANCE HInstance() { return hinst; }
nuclear@5 411 };
nuclear@5 412
nuclear@58 413 // TODO: make 2 class descriptors, one for goat3d, one for goat3danim
nuclear@5 414 static GoatClassDesc class_desc;
nuclear@5 415
nuclear@5 416 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
nuclear@5 417 {
nuclear@5 418 if(reason == DLL_PROCESS_ATTACH) {
nuclear@5 419 hinst = inst_handle;
nuclear@5 420 DisableThreadLibraryCalls(hinst);
nuclear@5 421 }
nuclear@5 422 return TRUE;
nuclear@5 423 }
nuclear@5 424
nuclear@5 425 extern "C" {
nuclear@5 426
nuclear@5 427 __declspec(dllexport) const TCHAR *LibDescription()
nuclear@5 428 {
nuclear@5 429 return L"test exporter";
nuclear@5 430 }
nuclear@5 431
nuclear@5 432 __declspec(dllexport) int LibNumberClasses()
nuclear@5 433 {
nuclear@5 434 return 1;
nuclear@5 435 }
nuclear@5 436
nuclear@5 437 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
nuclear@5 438 {
nuclear@5 439 return i == 0 ? &class_desc : 0;
nuclear@5 440 }
nuclear@5 441
nuclear@5 442 __declspec(dllexport) ULONG LibVersion()
nuclear@5 443 {
nuclear@5 444 return Get3DSMAXVersion();
nuclear@5 445 }
nuclear@5 446
nuclear@5 447 __declspec(dllexport) int LibInitialize()
nuclear@5 448 {
nuclear@5 449 static char path[1024];
nuclear@5 450
nuclear@5 451 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
nuclear@5 452 strcat(path, "/testexp.log");
nuclear@5 453
nuclear@5 454 if((logfile = fopen(path, "w"))) {
nuclear@5 455 setvbuf(logfile, 0, _IONBF, 0);
nuclear@5 456 }
nuclear@5 457 return TRUE;
nuclear@5 458 }
nuclear@5 459
nuclear@5 460 __declspec(dllexport) int LibShutdown()
nuclear@5 461 {
nuclear@5 462 if(logfile) {
nuclear@5 463 fclose(logfile);
nuclear@5 464 logfile = 0;
nuclear@5 465 }
nuclear@5 466 return TRUE;
nuclear@5 467 }
nuclear@5 468
nuclear@5 469 } // extern "C"