goat3d

annotate exporters/maxgoat/src/maxgoat.cc @ 28:9ba3e2fb8a33

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