goat3d
diff exporters/maxgoat/src/maxgoat.cc @ 27:4deb0b12fe14
wtf... corrupted heap?
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 29 Sep 2013 08:20:19 +0300 |
parents | d0260d80ae09 |
children | 9ba3e2fb8a33 |
line diff
1.1 --- a/exporters/maxgoat/src/maxgoat.cc Sat Sep 28 20:36:55 2013 +0300 1.2 +++ b/exporters/maxgoat/src/maxgoat.cc Sun Sep 29 08:20:19 2013 +0300 1.3 @@ -1,6 +1,8 @@ 1.4 #include <stdio.h> 1.5 #include <string.h> 1.6 +#include <stdlib.h> 1.7 #include <errno.h> 1.8 +#include <map> 1.9 #include <windows.h> 1.10 #include <shlobj.h> 1.11 #include "max.h" 1.12 @@ -33,6 +35,10 @@ 1.13 static HINSTANCE hinst; 1.14 1.15 class GoatExporter : public SceneExport { 1.16 +private: 1.17 + //std::map<IGameMaterial*, goat3d_material*> mtlmap; 1.18 + //std::map<IGameNode*, goat3d_node*> nodemap; 1.19 + 1.20 public: 1.21 IGameScene *igame; 1.22 1.23 @@ -49,9 +55,13 @@ 1.24 1.25 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0); 1.26 1.27 - void export_materials(goat3d *goat); 1.28 - void export_meshes(goat3d *goat); 1.29 - void process_node(goat3d *goat, IGameNode *maxnode); 1.30 + void process_materials(goat3d *goat); 1.31 + 1.32 + void process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode); 1.33 + 1.34 + void process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj); 1.35 + void process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj); 1.36 + void process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj); 1.37 }; 1.38 1.39 1.40 @@ -62,7 +72,7 @@ 1.41 1.42 const TCHAR *GoatExporter::Ext(int n) 1.43 { 1.44 - return L"txt"; 1.45 + return L"xml"; 1.46 } 1.47 1.48 const TCHAR *GoatExporter::LongDesc() 1.49 @@ -108,6 +118,9 @@ 1.50 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, 1.51 BOOL non_interactive, DWORD opt) 1.52 { 1.53 + //mtlmap.clear(); 1.54 + //nodemap.clear(); 1.55 + 1.56 char fname[512]; 1.57 wcstombs(fname, name, sizeof fname - 1); 1.58 1.59 @@ -122,20 +135,19 @@ 1.60 1.61 goat3d *goat = goat3d_create(); 1.62 1.63 - export_materials(goat); 1.64 - export_meshes(goat); 1.65 + process_materials(goat); 1.66 + 1.67 + // process all nodes 1.68 + for(int i=0; i<igame->GetTopLevelNodeCount(); i++) { 1.69 + IGameNode *node = igame->GetTopLevelNode(i); 1.70 + process_node(goat, 0, node); 1.71 + } 1.72 1.73 if(goat3d_save(goat, fname) == -1) { 1.74 goat3d_free(goat); 1.75 return IMPEXP_FAIL; 1.76 } 1.77 1.78 - // process all nodes 1.79 - for(int i=0; i<igame->GetTopLevelNodeCount(); i++) { 1.80 - IGameNode *node = igame->GetTopLevelNode(i); 1.81 - process_node(goat, node); 1.82 - } 1.83 - 1.84 goat3d_free(goat); 1.85 return IMPEXP_SUCCESS; 1.86 } 1.87 @@ -148,7 +160,7 @@ 1.88 return str; 1.89 } 1.90 1.91 -void GoatExporter::export_materials(goat3d *goat) 1.92 +void GoatExporter::process_materials(goat3d *goat) 1.93 { 1.94 IGameProperty *prop; 1.95 1.96 @@ -231,19 +243,138 @@ 1.97 } 1.98 1.99 goat3d_add_mtl(goat, mtl); 1.100 + //mtlmap[maxmtl] = mtl; 1.101 } 1.102 } 1.103 } 1.104 1.105 -void GoatExporter::export_meshes(goat3d *goat) 1.106 +void GoatExporter::process_node(goat3d *goat, goat3d_node *parent, IGameNode *maxnode) 1.107 { 1.108 - Tab<IGameNode*> meshes = igame->GetIGameNodeByType(IGameObject::IGAME_MESH); 1.109 + goat3d_node *node = goat3d_create_node(); 1.110 + goat3d_add_node(goat, node); 1.111 1.112 - for(int i=0; i<meshes.Count(); i++) { 1.113 - const char *name = max_string(meshes[i]->GetName()); 1.114 + const char *name = max_string(maxnode->GetName()); 1.115 + if(name) { 1.116 + goat3d_set_node_name(node, name); 1.117 + } 1.118 + 1.119 + // no animation yet, just get the static PRS 1.120 + GMatrix maxmatrix = maxnode->GetObjectTM(); 1.121 + Point3 trans = maxmatrix.Translation(); 1.122 + Quat rot = maxmatrix.Rotation(); 1.123 + Point3 scale = maxmatrix.Scaling(); 1.124 + 1.125 + goat3d_set_node_position(node, trans.x, trans.y, trans.z, 0); 1.126 + goat3d_set_node_rotation(node, rot.x, rot.y, rot.z, rot.w, 0); 1.127 + goat3d_set_node_scaling(node, scale.x, scale.y, scale.z, 0); 1.128 + 1.129 + IGameObject *maxobj = maxnode->GetIGameObject(); 1.130 + IGameObject::ObjectTypes type = maxobj->GetIGameType(); 1.131 + 1.132 + switch(type) { 1.133 + case IGameObject::IGAME_MESH: 1.134 + { 1.135 + goat3d_mesh *mesh = goat3d_create_mesh(); 1.136 + if(name) goat3d_set_mesh_name(mesh, name); 1.137 + goat3d_set_node_object(node, GOAT3D_NODE_MESH, mesh); 1.138 + 1.139 + // get the node material and assign it to the mesh 1.140 + IGameMaterial *maxmtl = maxnode->GetNodeMaterial(); 1.141 + goat3d_material *mtl = 0;//mtlmap[maxmtl]; 1.142 + if(mtl) { 1.143 + goat3d_set_mesh_mtl(mesh, mtl); 1.144 + } 1.145 + 1.146 + process_mesh(goat, mesh, maxobj); 1.147 + } 1.148 + break; 1.149 + 1.150 + case IGameObject::IGAME_LIGHT: 1.151 + { 1.152 + goat3d_light *light = goat3d_create_light(); 1.153 + //if(name) goat3d_set_light_name(light, name); 1.154 + goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light); 1.155 + 1.156 + process_light(goat, light, maxobj); 1.157 + } 1.158 + break; 1.159 + 1.160 + case IGameObject::IGAME_CAMERA: 1.161 + { 1.162 + goat3d_camera *cam = goat3d_create_camera(); 1.163 + //if(name) goat3d_set_camera_name(camera, name); 1.164 + goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam); 1.165 + 1.166 + process_camera(goat, cam, maxobj); 1.167 + } 1.168 + break; 1.169 + 1.170 + default: 1.171 + // otherwise don't assign an object, essentially treating it as a null node 1.172 + break; 1.173 + } 1.174 + 1.175 + 1.176 + for(int i=0; i<maxnode->GetChildCount(); i++) { 1.177 + process_node(goat, node, maxnode->GetNodeChild(i)); 1.178 } 1.179 } 1.180 1.181 +void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj) 1.182 +{ 1.183 + IGameMesh *maxmesh = (IGameMesh*)maxobj; 1.184 + 1.185 + maxmesh->SetCreateOptimizedNormalList(); // not needed any more according to docs 1.186 + maxobj->InitializeData(); 1.187 + 1.188 + int num_verts = maxmesh->GetNumberOfVerts(); 1.189 + assert(maxmesh->GetNumberOfTexVerts() == num_verts); 1.190 + 1.191 + float *vertices = new float[num_verts * 3]; 1.192 + float *normals = new float[num_verts * 3]; 1.193 + float *texcoords = new float[num_verts * 2]; 1.194 + 1.195 + for(int i=0; i<num_verts; i++) { 1.196 + Point3 v = maxmesh->GetVertex(i, true); 1.197 + vertices[i * 3] = v.x; 1.198 + vertices[i * 3 + 1] = v.y; 1.199 + vertices[i * 3 + 2] = v.z; 1.200 + } 1.201 + 1.202 + for(int i=0; i<maxmesh->GetNumberOfNormals(); i++) { 1.203 + Point3 norm = maxmesh->GetNormal(i); 1.204 + 1.205 + int vidx = maxmesh->GetNormalVertexIndex(i); 1.206 + normals[vidx * 3] = norm.x; 1.207 + normals[vidx * 3 + 1] = norm.y; 1.208 + normals[vidx * 3 + 2] = norm.z; 1.209 + } 1.210 + 1.211 + for(int i=0; i<maxmesh->GetNumberOfTexVerts(); i++) { 1.212 + Point3 tex = maxmesh->GetTexVertex(i); 1.213 + 1.214 + texcoords[i * 2] = tex.x; 1.215 + texcoords[i * 2 + 1] = tex.y; 1.216 + } 1.217 + 1.218 + goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts); 1.219 + goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts); 1.220 + goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts); 1.221 + 1.222 + delete [] vertices; 1.223 + delete [] normals; 1.224 + delete [] texcoords; 1.225 +} 1.226 + 1.227 +void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj) 1.228 +{ 1.229 +} 1.230 + 1.231 +void GoatExporter::process_camera(goat3d *goat, goat3d_camera *cam, IGameObject *maxobj) 1.232 +{ 1.233 +} 1.234 + 1.235 + 1.236 // ------------------------------------------ 1.237 1.238 class GoatClassDesc : public ClassDesc2 {