goat3d
changeset 34:8471225a460c
merged
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Oct 2013 03:08:22 +0300 |
parents | f43f4849c86a d24f63e8031e |
children | 4f8383183d62 |
files | |
diffstat | 7 files changed, 127 insertions(+), 4 deletions(-) [+] |
line diff
1.1 --- a/exporters/maxgoat/src/maxgoat.cc Sat Oct 05 03:07:45 2013 +0300 1.2 +++ b/exporters/maxgoat/src/maxgoat.cc Sat Oct 05 03:08:22 2013 +0300 1.3 @@ -286,6 +286,7 @@ 1.4 } 1.5 1.6 process_mesh(goat, mesh, maxobj); 1.7 + goat3d_add_mesh(goat, mesh); 1.8 } 1.9 break; 1.10 1.11 @@ -296,6 +297,7 @@ 1.12 goat3d_set_node_object(node, GOAT3D_NODE_LIGHT, light); 1.13 1.14 process_light(goat, light, maxobj); 1.15 + goat3d_add_light(goat, light); 1.16 } 1.17 break; 1.18 1.19 @@ -306,6 +308,7 @@ 1.20 goat3d_set_node_object(node, GOAT3D_NODE_CAMERA, cam); 1.21 1.22 process_camera(goat, cam, maxobj); 1.23 + goat3d_add_camera(goat, cam); 1.24 } 1.25 break; 1.26 1.27 @@ -328,11 +331,13 @@ 1.28 maxobj->InitializeData(); 1.29 1.30 int num_verts = maxmesh->GetNumberOfVerts(); 1.31 + int num_faces = maxmesh->GetNumberOfFaces(); 1.32 //assert(maxmesh->GetNumberOfTexVerts() == num_verts); 1.33 1.34 float *vertices = new float[num_verts * 3]; 1.35 float *normals = new float[num_verts * 3]; 1.36 //float *texcoords = new float[num_verts * 2]; 1.37 + int *indices = new int[num_faces * 3]; 1.38 1.39 for(int i=0; i<num_verts; i++) { 1.40 Point3 v = maxmesh->GetVertex(i, true); 1.41 @@ -357,13 +362,24 @@ 1.42 texcoords[i * 2 + 1] = tex.y; 1.43 }*/ 1.44 1.45 + // get the faces 1.46 + for(int i=0; i<num_faces; i++) { 1.47 + FaceEx *face = maxmesh->GetFace(i); 1.48 + indices[i * 3] = face->vert[0]; 1.49 + indices[i * 3 + 1] = face->vert[1]; 1.50 + indices[i * 3 + 2] = face->vert[2]; 1.51 + // TODO at some point I'll have to split based on normal/texcoord indices 1.52 + } 1.53 + 1.54 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX, vertices, num_verts); 1.55 goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL, normals, num_verts); 1.56 //goat3d_set_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD, texcoords, num_verts); 1.57 + goat3d_set_mesh_faces(mesh, indices, num_faces); 1.58 1.59 delete [] vertices; 1.60 delete [] normals; 1.61 //delete [] texcoords; 1.62 + delete [] indices; 1.63 } 1.64 1.65 void GoatExporter::process_light(goat3d *goat, goat3d_light *light, IGameObject *maxobj)
2.1 --- a/goat3d.vcxproj Sat Oct 05 03:07:45 2013 +0300 2.2 +++ b/goat3d.vcxproj Sat Oct 05 03:08:22 2013 +0300 2.3 @@ -180,7 +180,7 @@ 2.4 <Optimization>Disabled</Optimization> 2.5 <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions);OPENCTM_STATIC;BUILD_MAXPLUGIN</PreprocessorDefinitions> 2.6 <DisableSpecificWarnings>4244;4305;4996</DisableSpecificWarnings> 2.7 - <AdditionalIncludeDirectories>$(SolutionDir)\src;$(SolutionDir)\libs\openctm;$(SolutionDir)\libs\openctm\liblzma;$(SolutionDir)\libs\tinyxml2</AdditionalIncludeDirectories> 2.8 + <AdditionalIncludeDirectories>$(SolutionDir)\src;$(SolutionDir)\libs\openctm;$(SolutionDir)\libs\openctm\liblzma;$(SolutionDir)\libs\tinyxml2;$(SolutionDir)\libs\anim;$(SolutionDir)\libs\vmath</AdditionalIncludeDirectories> 2.9 </ClCompile> 2.10 <Link> 2.11 <SubSystem>Windows</SubSystem> 2.12 @@ -219,7 +219,7 @@ 2.13 <IntrinsicFunctions>true</IntrinsicFunctions> 2.14 <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions);OPENCTM_STATIC</PreprocessorDefinitions> 2.15 <DisableSpecificWarnings>4244;4305;4996</DisableSpecificWarnings> 2.16 - <AdditionalIncludeDirectories>$(SolutionDir)\src;$(SolutionDir)\libs\openctm;$(SolutionDir)\libs\openctm\liblzma;$(SolutionDir)\libs\tinyxml2</AdditionalIncludeDirectories> 2.17 + <AdditionalIncludeDirectories>$(SolutionDir)\src;$(SolutionDir)\libs\openctm;$(SolutionDir)\libs\openctm\liblzma;$(SolutionDir)\libs\tinyxml2;$(SolutionDir)\libs\anim;$(SolutionDir)\libs\vmath</AdditionalIncludeDirectories> 2.18 </ClCompile> 2.19 <Link> 2.20 <SubSystem>Windows</SubSystem>
3.1 --- a/src/goat3d.cc Sat Oct 05 03:07:45 2013 +0300 3.2 +++ b/src/goat3d.cc Sat Oct 05 03:08:22 2013 +0300 3.3 @@ -1,12 +1,21 @@ 3.4 #include <string.h> 3.5 #include <errno.h> 3.6 +#include <ctype.h> 3.7 +#include <string> 3.8 #include "goat3d.h" 3.9 #include "goat3d_impl.h" 3.10 #include "log.h" 3.11 3.12 +#ifndef _MSC_VER 3.13 +#include <alloca.h> 3.14 +#else 3.15 +#include <malloc.h> 3.16 +#endif 3.17 + 3.18 struct goat3d { 3.19 Scene *scn; 3.20 unsigned int flags; 3.21 + char *search_path; 3.22 }; 3.23 3.24 struct goat3d_material : public Material {}; 3.25 @@ -20,12 +29,15 @@ 3.26 static long write_file(const void *buf, size_t bytes, void *uptr); 3.27 static long seek_file(long offs, int whence, void *uptr); 3.28 3.29 +static std::string clean_filename(const char *str); 3.30 + 3.31 extern "C" { 3.32 3.33 struct goat3d *goat3d_create(void) 3.34 { 3.35 goat3d *goat = new goat3d; 3.36 goat->flags = 0; 3.37 + goat->search_path = 0; 3.38 goat->scn = new Scene; 3.39 3.40 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1); 3.41 @@ -34,6 +46,7 @@ 3.42 3.43 void goat3d_free(struct goat3d *g) 3.44 { 3.45 + delete g->search_path; 3.46 delete g->scn; 3.47 delete g; 3.48 } 3.49 @@ -60,6 +73,24 @@ 3.50 return -1; 3.51 } 3.52 3.53 + /* if the filename contained any directory components, keep the prefix 3.54 + * to use it as a search path for external mesh file loading 3.55 + */ 3.56 + g->search_path = new char[strlen(fname) + 1]; 3.57 + strcpy(g->search_path, fname); 3.58 + 3.59 + char *slash = strrchr(g->search_path, '/'); 3.60 + if(slash) { 3.61 + *slash = 0; 3.62 + } else { 3.63 + if((slash = strrchr(g->search_path, '\\'))) { 3.64 + *slash = 0; 3.65 + } else { 3.66 + delete [] g->search_path; 3.67 + g->search_path = 0; 3.68 + } 3.69 + } 3.70 + 3.71 int res = goat3d_load_file(g, fp); 3.72 fclose(fp); 3.73 return res; 3.74 @@ -197,7 +228,7 @@ 3.75 3.76 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname) 3.77 { 3.78 - (*mtl)[attrib].map = std::string(mapname); 3.79 + (*mtl)[attrib].map = clean_filename(mapname); 3.80 } 3.81 3.82 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib) 3.83 @@ -706,3 +737,23 @@ 3.84 } 3.85 return ftell((FILE*)uptr); 3.86 } 3.87 + 3.88 +static std::string clean_filename(const char *str) 3.89 +{ 3.90 + const char *last_slash = strrchr(str, '/'); 3.91 + if(!last_slash) { 3.92 + last_slash = strrchr(str, '\\'); 3.93 + } 3.94 + 3.95 + if(last_slash) { 3.96 + str = last_slash + 1; 3.97 + } 3.98 + 3.99 + char *buf = (char*)alloca(strlen(str) + 1); 3.100 + char *dest = buf; 3.101 + while(*str) { 3.102 + char c = *str++; 3.103 + *dest++ = tolower(c); 3.104 + } 3.105 + return buf; 3.106 +}
4.1 --- a/src/goat3d_writexml.cc Sat Oct 05 03:07:45 2013 +0300 4.2 +++ b/src/goat3d_writexml.cc Sat Oct 05 03:08:22 2013 +0300 4.3 @@ -97,6 +97,46 @@ 4.4 4.5 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level) 4.6 { 4.7 + xmlout(io, level, "<node>\n"); 4.8 + xmlout(io, level + 1, "<name string=\"%s\"/>\n", node->get_name()); 4.9 + 4.10 + XFormNode *parent = node->get_parent(); 4.11 + if(parent) { 4.12 + xmlout(io, level + 1, "<parent string=\"%s\"/>\n", parent->get_name()); 4.13 + } 4.14 + 4.15 + const char *type = 0; 4.16 + const Object *obj = node->get_object(); 4.17 + if(dynamic_cast<const Mesh*>(obj)) { 4.18 + type = "mesh"; 4.19 + } else if(dynamic_cast<const Light*>(obj)) { 4.20 + type = "light"; 4.21 + } else if(dynamic_cast<const Camera*>(obj)) { 4.22 + type = "camera"; 4.23 + } 4.24 + 4.25 + if(type) { 4.26 + xmlout(io, level + 1, "<%s string=\"%s\"/>\n", type, obj->name.c_str()); 4.27 + } 4.28 + 4.29 + Vector3 pos = node->get_node_position(); 4.30 + Quaternion rot = node->get_node_rotation(); 4.31 + Vector3 scale = node->get_node_scaling(); 4.32 + Vector3 pivot = node->get_pivot(); 4.33 + 4.34 + Matrix4x4 xform; 4.35 + node->get_node_xform(0, &xform); 4.36 + 4.37 + xmlout(io, level + 1, "<pos float3=\"%g %g %g\"/>\n", pos.x, pos.y, pos.z); 4.38 + xmlout(io, level + 1, "<rot float4=\"%g %g %g %g\"/>\n", rot.v.x, rot.v.y, rot.v.z, rot.s); 4.39 + xmlout(io, level + 1, "<scale float3=\"%g %g %g\"/>\n", scale.x, scale.y, scale.z); 4.40 + xmlout(io, level + 1, "<pivot float3=\"%g %g %g\"/>\n", pivot.x, pivot.y, pivot.z); 4.41 + 4.42 + xmlout(io, level + 1, "<matrix0 float4=\"%g %g %g %g\"/>\n", xform[0][0], xform[0][1], xform[0][2], xform[0][3]); 4.43 + xmlout(io, level + 1, "<matrix1 float4=\"%g %g %g %g\"/>\n", xform[1][0], xform[1][1], xform[1][2], xform[1][3]); 4.44 + xmlout(io, level + 1, "<matrix2 float4=\"%g %g %g %g\"/>\n", xform[2][0], xform[2][1], xform[2][2], xform[2][3]); 4.45 + 4.46 + xmlout(io, level, "</node>\n"); 4.47 return true; 4.48 } 4.49
5.1 --- a/src/mesh.cc Sat Oct 05 03:07:45 2013 +0300 5.2 +++ b/src/mesh.cc Sat Oct 05 03:08:22 2013 +0300 5.3 @@ -110,11 +110,16 @@ 5.4 bool Mesh::save(const char *fname) const 5.5 { 5.6 int vnum = (int)vertices.size(); 5.7 + int fnum = (int)faces.size(); 5.8 + 5.9 + if(!vnum || !fnum) { 5.10 + return false; 5.11 + } 5.12 5.13 CTMcontext ctm = ctmNewContext(CTM_EXPORT); 5.14 5.15 // vertices, normals, and face-vertex indices 5.16 - ctmDefineMesh(ctm, &vertices[0].x, vnum, (CTMuint*)faces[0].v, faces.size(), 5.17 + ctmDefineMesh(ctm, &vertices[0].x, vnum, (CTMuint*)faces[0].v, fnum, 5.18 normals.empty() ? 0 : &normals[0].x); 5.19 5.20 // texture coordinates
6.1 --- a/src/xform_node.cc Sat Oct 05 03:07:45 2013 +0300 6.2 +++ b/src/xform_node.cc Sat Oct 05 03:08:22 2013 +0300 6.3 @@ -11,6 +11,8 @@ 6.4 { 6.5 anm = new anm_node; 6.6 anm_init_node(anm); 6.7 + 6.8 + parent = 0; 6.9 } 6.10 6.11 XFormNode::~XFormNode() 6.12 @@ -55,6 +57,7 @@ 6.13 { 6.14 children.push_back(child); 6.15 anm_link_node(anm, child->anm); 6.16 + child->parent = this; 6.17 } 6.18 6.19 void XFormNode::remove_child(XFormNode *child) 6.20 @@ -65,6 +68,7 @@ 6.21 children.erase(it); 6.22 anm_unlink_node(anm, child->anm); 6.23 } 6.24 + child->parent = 0; 6.25 } 6.26 6.27 int XFormNode::get_children_count() const 6.28 @@ -88,6 +92,11 @@ 6.29 return 0; 6.30 } 6.31 6.32 +XFormNode *XFormNode::get_parent() const 6.33 +{ 6.34 + return parent; 6.35 +} 6.36 + 6.37 void XFormNode::set_position(const Vector3 &pos, long tmsec) 6.38 { 6.39 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
7.1 --- a/src/xform_node.h Sat Oct 05 03:07:45 2013 +0300 7.2 +++ b/src/xform_node.h Sat Oct 05 03:08:22 2013 +0300 7.3 @@ -21,6 +21,7 @@ 7.4 private: 7.5 struct anm_node *anm; 7.6 std::vector<XFormNode*> children; 7.7 + XFormNode *parent; 7.8 7.9 Interp interp; 7.10 Extrap extrap; 7.11 @@ -51,6 +52,7 @@ 7.12 virtual XFormNode *get_child(int idx); 7.13 virtual const XFormNode *get_child(int idx) const; 7.14 7.15 + virtual XFormNode *get_parent() const; 7.16 7.17 virtual void set_position(const Vector3 &pos, long tmsec = 0); 7.18 virtual Vector3 get_node_position(long tmsec = 0) const;