goat3d

annotate exporters/maxgoat/src/maxgoat.cc @ 50:0be413ac2e0a

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