00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <list>
00027 #include <vector>
00028 #include <iostream>
00029 #include <dae.h>
00030 #include <dom/domMaterial.h>
00031 #include <dom/domGeometry.h>
00032 #include <dom/domNode.h>
00033 #include <dom/domCOLLADA.h>
00034 #include "domTest.h"
00035
00036 using namespace std;
00037
00038 #define Check(val) if (!(val)) throw exception();
00039
00040
00041
00042 class Material {
00043 public:
00044 vector<float> diffuseColor;
00045 string diffuseTexture;
00046
00047
00048 Material(domMaterial& mtl) {
00049
00050 }
00051 };
00052
00053
00054
00055 class Mesh {
00056 public:
00057 Material* mtl;
00058
00059
00060 Mesh(domGeometry& geom) {
00061
00062 }
00063 };
00064
00065
00066
00067 class Node {
00068 public:
00069 list<Mesh*> meshes;
00070 list<Node*> childNodes;
00071
00072
00073 Node(domNode& node);
00074 };
00075
00076
00077
00078
00079
00080
00081 template<typename MyType, typename DomType>
00082 MyType& lookup(DomType& domObject) {
00083 if (!domObject.getUserData())
00084 domObject.setUserData(new MyType(domObject));
00085 return *(MyType*)(domObject.getUserData());
00086 }
00087
00088
00089
00090 template<typename MyType, typename DomType>
00091 void freeConversionObjects(DAE& dae) {
00092 vector<daeElement*> elts = dae.getDatabase()->typeLookup(DomType::ID());
00093 for (size_t i = 0; i < elts.size(); i++)
00094 delete (MyType*)elts[i]->getUserData();
00095 }
00096
00097
00098 Node::Node(domNode& node) {
00099
00100 for (size_t i = 0; i < node.getNode_array().getCount(); i++)
00101 childNodes.push_back(&lookup<Node, domNode>(*node.getNode_array()[i]));
00102
00103
00104 for (size_t i = 0; i < node.getInstance_node_array().getCount(); i++) {
00105 domNode* child = daeSafeCast<domNode>(
00106 node.getInstance_node_array()[i]->getUrl().getElement());
00107 Check(child);
00108 childNodes.push_back(&lookup<Node, domNode>(*child));
00109 }
00110
00111
00112 for (size_t i = 0; i < node.getInstance_geometry_array().getCount(); i++) {
00113 domInstance_geometry* instanceGeom = node.getInstance_geometry_array()[i];
00114 domGeometry* geom = daeSafeCast<domGeometry>(instanceGeom->getUrl().getElement());
00115 Check(geom);
00116
00117
00118
00119
00120 domInstance_material* instanceMtl = daeSafeCast<domInstance_material>(
00121 instanceGeom->getDescendant("instance_material"));
00122 Check(instanceMtl);
00123 domMaterial* mtl = daeSafeCast<domMaterial>(instanceMtl->getTarget().getElement());
00124 Check(mtl);
00125 Material& convertedMtl = lookup<Material, domMaterial>(*mtl);
00126
00127
00128
00129 meshes.push_back(&lookup<Mesh, domGeometry>(*geom));
00130 meshes.back()->mtl = &convertedMtl;
00131 }
00132 }
00133
00134
00135 void convertModel(domCOLLADA& root) {
00136
00137
00138
00139
00140 domVisual_scene* visualScene = daeSafeCast<domVisual_scene>(root.getDescendant("visual_scene"));
00141 Check(visualScene);
00142
00143
00144
00145 domNode_Array& nodes = visualScene->getNode_array();
00146 for (size_t i = 0; i < nodes.getCount(); i++)
00147 lookup<Node, domNode>(*nodes[i]);
00148 }
00149
00150
00151 DefineTest(integration) {
00152
00153 string file = lookupTestFile("cube.dae");
00154 DAE dae;
00155 domCOLLADA* root = dae.open(file);
00156 CheckResult(root);
00157
00158
00159
00160 try {
00161 convertModel(*root);
00162 }
00163 catch (const exception&) {
00164 return testResult(false);
00165 }
00166
00167
00168 freeConversionObjects<Node, domNode>(dae);
00169 freeConversionObjects<Mesh, domGeometry>(dae);
00170 freeConversionObjects<Material, domMaterial>(dae);
00171
00172 return testResult(true);
00173 }