int main(int argc, char **argv ) { // describe command-line options std::string scene_file; // input collada scene filename std::string hdf5_file; // input hdf5 filename std::string hdf5_group; std::string file_out; // output collada animation filename po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("hdf5-file", po::value<std::string>(&hdf5_file), "HDF5 input data file") ("hdf5-group", po::value<std::string>(&hdf5_group)->default_value("/"), "subgroup within the HDF5 file") ("scene-file", po::value<std::string>(&scene_file), "collada input scene file") ("output-file,o", po::value<std::string>(&file_out)->default_value("out.dae"), "output file") ; po::positional_options_description p; p.add("hdf5-file", 1); p.add("scene-file", 1); p.add("output-file", 1); po::variables_map vm; po::store(po::command_line_parser(argc, argv). options(desc).positional(p).run(), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << "\n"; return 1; } std::vector<std::string> elements; DAE *dae = new DAE; //daeElement *root = (daeElement*)dae->open(scene_file); domCOLLADA *root = (domCOLLADA*)dae->open(scene_file); H5RandomReader myReader(hdf5_file, hdf5_group); FrameData fd = myReader.getFrame(0); std::map <std::string, Matrix >::iterator it; for (it = fd.matrices.begin(); it != fd.matrices.end(); ++it){ elements.push_back(std::string(it->first)); } //printf("Found %i elements... \n", elements.size() ); domLibrary_animations *lib_anim; if (elements.size()!=0) { lib_anim = (domLibrary_animations*)root->add("library_animations"); } //printf("Found %i steps...\n", (int)myReader.getNbSteps()); for (int j=0; j<elements.size(); j++){ AnimationExporter *matrix = new AnimationExporter(lib_anim, 16, elements[j].c_str(), "matrix"); for (int i=0; i<myReader.getNbSteps(); i++){ //printf("Step %i\n", i); fd = myReader.getFrame(i); std::map <std::string, Matrix >::iterator it; for (it = fd.matrices.begin(); it != fd.matrices.end(); ++it){ if (elements[j] == (it->first).c_str()) { //std::cout << "Key : " << it->first << std::endl; //td::cout << ax << "\t" << ay << "\t" << az << std::endl; matrix->add(myReader.getTime(i), it->second[0 + 0*4], it->second[1 + 0*4], it->second[2 + 0*4], it->second[3 + 0*4], it->second[0 + 1*4], it->second[1 + 1*4], it->second[2 + 1*4], it->second[3 + 1*4], it->second[0 + 2*4], it->second[1 + 2*4], it->second[2 + 2*4], it->second[3 + 2*4], it->second[0 + 3*4], it->second[1 + 3*4], it->second[2 + 3*4], it->second[3 + 3*4]); } } } } dae->add(file_out); dae->setRoot(file_out, (domCOLLADA*)root); dae->write(file_out); return 0; }
using namespace cdom; // Demonstrates how to use the DOM to create a simple, textured Collada model // and save it to disk. testResult addAsset(daeElement* root); testResult addGeometry(daeElement* root); testResult addImage(daeElement* root); testResult addEffect(daeElement* root); testResult addMaterial(daeElement* root); testResult addVisualScene(daeElement* root); DefineTest(export) { DAE dae; string file = getTmpFile("export.dae"); domCOLLADA* root = dae.add(file); CheckResult(root); CheckTestResult(addAsset(root)); CheckTestResult(addGeometry(root)); CheckTestResult(addImage(root)); CheckTestResult(addEffect(root)); CheckTestResult(addMaterial(root)); CheckTestResult(addVisualScene(root)); dae.writeAll(); // As a very simple check for possible errors, make sure the document loads // back in successfully. dae.clear(); CheckResult(dae.open(file));
void FrameBuffer::write(DAE &dae, const char *fn) { const Matrix4 m_trans = Matrix4(0.707107, 0.408248, -0.57735, -100.f, -0.707107, 0.408248, -0.57735, -100.f, 0.f, 0.816497, 0.57735, 100.f, 0.f, 0.f, 0.f, 1.f)*rotX_mat(3.14159265358979323846264338327950288419716939937510582097494459230781640628620); daeElement *root = dae.add(fn); if (!root) { if (fprintf(stderr, "Problem preparing to write to temporary file.\n") < 0) perror("fprintf"); exit(0); } time_t now; char date[256]; if (time(&now) == -1 || strftime(date, 256, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now)) == 0) { if (fprintf(stderr, "Getting time failed.\n") < 0) perror("fprintf"); exit(0); } daeElement *asset = root->add("asset"); asset->add("contributor"); asset->add("created")->setCharData(date); asset->add("modified")->setCharData(date); daeElement *visualScene = root->add("library_visual_scenes visual_scene"); visualScene->setAttribute("id", "myscene"); daeElement *node = visualScene->add("node"); node->setAttribute("id", "mynode"); daeElement *instanceGeom = node->add("instance_geometry"); instanceGeom->setAttribute("url", "#mygeom"); daeElement *matbindings = instanceGeom->add("bind_material technique_common"); root->add("scene instance_visual_scene")->setAttribute("url", "#myscene"); daeElement *libMats = root->add("library_materials"); daeElement *libFx = root->add("library_effects"); for (size_t y = 0; y < dim; y++) for (size_t x = 0; x < dim; x++) { ostringstream matid; matid << "mat" << dim * y + x; ostringstream maturl; maturl << "#mat" << dim * y + x; ostringstream fxid; fxid << "fx" << dim * y + x; ostringstream fxurl; fxurl << "#fx" << dim * y + x; ostringstream matsymbol; matsymbol << "MATSYMBOL" << dim * y + x; ostringstream color; color << R[dim * y + x] << ' ' << G[dim * y + x] << ' ' << B[dim * y + x] << " 1"; daeElement *mat = libMats->add("material"); mat->setAttribute("id", matid.str().c_str()); mat->add("instance_effect")->setAttribute("url", fxurl.str().c_str()); daeElement *fx = libFx->add("effect"); fx->setAttribute("id", fxid.str().c_str()); daeElement *technique = fx->add("profile_COMMON technique"); technique->setAttribute("sid", "common"); daeElement *phong = technique->add("phong"); phong->add("diffuse color")->setCharData(color.str()); phong->add("specular color")->setCharData("0 0 0 1"); daeElement *matbinding = matbindings->add("instance_material"); matbinding->setAttribute("symbol", matsymbol.str().c_str()); matbinding->setAttribute("target", maturl.str().c_str()); } daeElement *geom = root->add("library_geometries geometry"); geom->setAttribute("id", "mygeom"); daeElement *mesh = geom->add("mesh"); daeElement *elt; domFloat_array *fa; domAccessor *acc; domListOfFloats floatarray; elt = mesh->add("source"); elt->setAttribute("id", "mypositions"); fa = daeSafeCast<domFloat_array>(elt->add("float_array")); fa->setId("mypositions-array"); fa->setCount(12 * dim * dim); floatarray.clear(); for (size_t y = 0; y < dim; y++) for (size_t x = 0; x < dim; x++) { Point3 p1 = m_trans * Point3((double)x /(double)dim, (double)y /(double)dim, depth[dim * y + x]); Point3 p2 = m_trans * Point3((double)x /(double)dim, (double)(y+1)/(double)dim, depth[dim * y + x]); Point3 p3 = m_trans * Point3((double)(x+1)/(double)dim, (double)(y+1)/(double)dim, depth[dim * y + x]); Point3 p4 = m_trans * Point3((double)(x+1)/(double)dim, (double)y /(double)dim, depth[dim * y + x]); floatarray.append3(p1.x,p1.y,p1.z); floatarray.append3(p2.x,p2.y,p2.z); floatarray.append3(p3.x,p3.y,p3.z); floatarray.append3(p4.x,p4.y,p4.z); } fa->getValue() = floatarray; acc = daeSafeCast<domAccessor>(elt->add("technique_common accessor")); acc->setSource("#mypositions"); acc->setStride(3); acc->setCount(4 * dim * dim); elt = acc->add("param"); elt->setAttribute("name", "X"); elt->setAttribute("type", "float"); elt = acc->add("param"); elt->setAttribute("name", "Y"); elt->setAttribute("type", "float"); elt = acc->add("param"); elt->setAttribute("name", "Z"); elt->setAttribute("type", "float"); elt = mesh->add("source"); elt->setAttribute("id", "mynormals"); fa = daeSafeCast<domFloat_array>(elt->add("float_array")); fa->setId("mynormals-array"); fa->setCount(3); floatarray.clear(); Vector3 back = normalize(inverse(transpose(m_trans)) * Vector3(0.f, 0.f, -1.f)); floatarray.append3(back.x, back.y, back.z); fa->getValue() = floatarray; acc = daeSafeCast<domAccessor>(elt->add("technique_common accessor")); acc->setSource("#mynormals"); acc->setStride(3); acc->setCount(1); elt = acc->add("param"); elt->setAttribute("name", "X"); elt->setAttribute("type", "float"); elt = acc->add("param"); elt->setAttribute("name", "Y"); elt->setAttribute("type", "float"); elt = acc->add("param"); elt->setAttribute("name", "Z"); elt->setAttribute("type", "float"); elt = mesh->add("vertices"); elt->setAttribute("id", "myvertices"); elt = elt->add("input"); elt->setAttribute("semantic", "POSITION"); elt->setAttribute("source", "#mypositions"); for (size_t y = 0; y < dim; y++) for (size_t x = 0; x < dim; x++) { ostringstream matsymbol; matsymbol << "MATSYMBOL" << dim * y + x; domInputLocalOffset *input; domTriangles *triangles = daeSafeCast<domTriangles>(mesh->add("triangles")); triangles->setCount(2); triangles->setMaterial(matsymbol.str().c_str()); input = daeSafeCast<domInputLocalOffset>(triangles->add("input")); input->setSemantic("VERTEX"); input->setOffset(0); input->setSource("#myvertices"); input = daeSafeCast<domInputLocalOffset>(triangles->add("input")); input->setSemantic("NORMAL"); input->setOffset(1); input->setSource("#mynormals"); domP *p = daeSafeCast<domP>(triangles->add("p")); domListOfUInts inds; inds.append2(4 * (dim * y + x) , 0); inds.append2(4 * (dim * y + x) + 1, 0); inds.append2(4 * (dim * y + x) + 3, 0); inds.append2(4 * (dim * y + x) + 1, 0); inds.append2(4 * (dim * y + x) + 2, 0); inds.append2(4 * (dim * y + x) + 3, 0); p->getValue() = inds; } if (!dae.writeAll()) { if (fprintf(stderr, "Problem writing to temporary file.\n") < 0) perror("fprintf"); exit(0); } dae.clear(); }