From ff4d9d011d831932ceddb359470a3e833e7ca511 Mon Sep 17 00:00:00 2001 From: granzuglia Date: Fri, 7 Jul 2006 06:55:41 +0000 Subject: [PATCH] updated the importer and added the first version of the exporter --- wrap/io_trimesh/export_dae.h | 134 +++++++++++++++++++++++++++++++++++ wrap/io_trimesh/import_dae.h | 10 ++- 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 wrap/io_trimesh/export_dae.h diff --git a/wrap/io_trimesh/export_dae.h b/wrap/io_trimesh/export_dae.h new file mode 100644 index 00000000..59d22532 --- /dev/null +++ b/wrap/io_trimesh/export_dae.h @@ -0,0 +1,134 @@ +#ifndef __VCGLIB_EXPORTERDAE +#define __VCGLIB_EXPORTERDAE + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace vcg { +namespace tri { +namespace io { + +//FCollada Library assumes that SaveMeshType::ScalarType is always a float + +template +class ExporterDAE +{ +public: + static int Save(SaveMeshType &m, const char * filename) + { + unsigned int ncomp = sizeof(SaveMeshType::CoordType) / sizeof(SaveMeshType::ScalarType); + FCDocument* doc = new FCDocument(); + FCDGeometryLibrary* geolib = doc->GetGeometryLibrary(); + FCDGeometry* geo = geolib->AddEntity(); + geo->SetDaeId("vcgmesh"); + FCDGeometryMesh* mesh = geo->CreateMesh(); + + FCDGeometrySource* vsource = mesh->AddVertexSource(); + vsource->SetDaeId("vcg-position"); + vsource->SetSourceType(FUDaeGeometryInput::POSITION); + FCDGeometryPolygons* polyset = mesh->AddPolygons(); + //for(UInt32List::iterator it = polyinput->indices.begin();it != polyinput->indices.end();++it) + // *it = 100; + + FloatList plist(m.vert.size() * ncomp); + + SaveMeshType::CoordType point; + unsigned int ii = 0; + for(FloatList::iterator it = plist.begin();it != plist.end();++it) + { + if ((ii % ncomp) == 0) + { + point = m.vert[ii / ncomp].P(); + } + (*it) = point[ii % ncomp]; + ++ii; + } + + vsource->SetSourceData(plist,ncomp); + + unsigned int jj = 0; + FCDGeometryPolygonsInput* ps = mesh->GetPolygons(0)->FindInput(FUDaeGeometryInput::POSITION); + //ps->indices.resize(m.face.size() * 3); + for(SaveMeshType::FaceIterator itf = m.face.begin();itf != m.face.end();++itf) + { + if( !(*itf).IsD() ) + { + polyset->AddFace(3); + ps->indices[jj] = (itf->V(0) - &(m.vert[0])); + ps->indices[jj + 1] = (itf->V(1) - &(m.vert[0])); + ps->indices[jj + 2] = (itf->V(2) - &(m.vert[0])); + jj += 3; + } + } + + FloatList nlist; + + FCDGeometrySource* nsource = mesh->AddSource(); + nsource->SetDaeId("vcg-wedgenormal"); + nsource->SetSourceType(FUDaeGeometryInput::NORMAL); + //in the oldmesh format of vcg library normal is always a Point3 + nlist.resize(m.face.size() * 3 * 3); + + unsigned int cc = 0; + jj = 0; + + FCDGeometryPolygonsInput* nm = polyset->AddInput(nsource,1); + //nm->semantic = FUDaeGeometryInput::NORMAL; + nm->indices.resize(m.face.size() * 3); + + for(SaveMeshType::FaceIterator fit = m.face.begin();fit != m.face.end();++fit) + { + if( !(*fit).IsD() ) + { + vcg::Point3 norm = (((fit->V(1))->P() - (fit->V(0))->P()) ^ ((fit->V(2))->P() - (fit->V(0))->P())).Normalize(); + for(unsigned int vv = 0; vv < 3;++vv) + { + nm->indices[jj + vv] = cc / 3; + for(unsigned int hh = 0; hh < 3;++hh) + { + nlist[cc] = norm[hh]; + ++cc; + } + } + jj += 3; + } + } + nsource->SetSourceData(nlist,3); + + doc->WriteToFile(FUStringConversion::ToFString(filename)); + return 0; + } + + static int GetExportMaskCapability() + { + int capability = 0; + + //camera + //capability |= MeshModel::IOM_CAMERA; + + //vert + //capability |= MeshModel::IOM_VERTTEXCOORD; + + //face + capability |= MeshModel::IOM_FACEFLAGS; + //capability |= MeshModel::IOM_FACECOLOR; + capability |= MeshModel::IOM_FACENORMAL; + + //wedg + //capability |= MeshModel::IOM_WEDGTEXCOORD; + capability |= MeshModel::IOM_WEDGNORMAL; + + return capability; + } +}; +} +} +} + +#endif \ No newline at end of file diff --git a/wrap/io_trimesh/import_dae.h b/wrap/io_trimesh/import_dae.h index cb5e6312..c3ee5b61 100644 --- a/wrap/io_trimesh/import_dae.h +++ b/wrap/io_trimesh/import_dae.h @@ -12,10 +12,12 @@ #include #include + //#include #include #include +//#include namespace vcg { namespace tri { @@ -56,6 +58,7 @@ public: static int Open(OpenMeshType& m,const char* filename) { + assert(filename!=0); FCDocument* doc = new FCDocument(); @@ -81,6 +84,10 @@ public: if (geomsh[ii]->GetFaceCount() > 0) { geomsh[ii]->Triangulate(); + /*std::vector< std::vector > vt(m.face.size()); + for(std::vector< std::vector >::iterator + HGRD::Triangulate(*/ + //geomsh[ii]->Get size_t dim = geomsh[ii]->GetFaceVertexCount() / geomsh[ii]->GetFaceCount(); assert(dim == 3); @@ -101,6 +108,7 @@ public: vi->N() = vcg::Point3f(0.0,0.0,0.0); } } + else return E_NOVERTEXPOSITION; //a single mesh may be composed by a variable numbers of polygons' subsets @@ -231,7 +239,7 @@ public: return E_NOERROR; } }; - + } } }