00001
00007
00008
00009
00010
00011
00012
00013
00014 #include <dae.h>
00015 #include <dae/daeUtils.h>
00016 #include <dom/domCOLLADA.h>
00017
00018 using namespace std;
00019 using namespace ColladaDOM150;
00020 using namespace cdom;
00021
00022
00023
00024
00025
00026
00027 template<typename T>
00028 daeTArray<T> rawArrayToDaeArray(T rawArray[], size_t count) {
00029 daeTArray<T> result;
00030 for (size_t i = 0; i < count; i++)
00031 result.append(rawArray[i]);
00032 return result;
00033 }
00034
00035
00036 string makeUriRef(const string& id) {
00037 return string("#") + id;
00038 }
00039
00040 void my_addSource(daeElement* mesh,
00041 const std::string& srcID,
00042 const std::string& paramNames,
00043 domFloat values[],
00044 int valueCount) {
00045
00046 daeElement* src = mesh->add("source");
00047
00048
00049 src->setAttribute("id", srcID.c_str());
00050
00051 domFloat_array* fa = daeSafeCast<domFloat_array>(src->add("float_array"));
00052
00053 fa->setId((src->getAttribute("id") + "-array").c_str());
00054 fa->setCount(valueCount);
00055 fa->getValue() = rawArrayToDaeArray(values, valueCount);
00056
00057 domAccessor* acc = daeSafeCast<domAccessor>(src->add("technique_common accessor"));
00058
00059 acc->setSource(makeUriRef(fa->getId()).c_str());
00060
00061 list<string> params = tokenize(paramNames, " ");
00062 acc->setStride(params.size());
00063 acc->setCount(valueCount/params.size());
00064 for (tokenIter iter = params.begin(); iter != params.end(); iter++) {
00065
00066 daeElement* p = acc->add("param");
00067 p->setAttribute("name", iter->c_str());
00068 p->setAttribute("type", "float");
00069 }
00070
00071
00072 }
00073
00074
00075 void my_addInput(daeElement* triangles,
00076 const string& semantic,
00077 const string& srcID,
00078 int offset) {
00079 domInput_local_offset* input = daeSafeCast<domInput_local_offset>(triangles->add("input"));
00080
00081 input->setSemantic(semantic.c_str());
00082 input->setOffset(offset);
00083 domUrifragment source(*triangles->getDAE(), srcID);
00084 input->setSource(source);
00085 if (semantic == "TEXCOORD")
00086 input->setSet(0);
00087
00088 }
00089
00090 void my_addGeometry(daeElement* root) {
00091
00092 daeElement* geomLib = root->add("library_geometries");
00093 daeElement* geom = geomLib->add("geometry");
00094 string geomID = "cubeGeom";
00095 geom->setAttribute("id", geomID.c_str());
00096 daeElement* mesh = geom->add("mesh");
00097
00098
00099 domFloat posArray[] = { -10, -10, -10,
00100 -10, -10, 10,
00101 -10, 10, -10,
00102 -10, 10, 10,
00103 10, -10, -10,
00104 10, -10, 10,
00105 10, 10, -10,
00106 10, 10, 10 };
00107 int count = sizeof(posArray)/sizeof(posArray[0]);
00108 my_addSource(mesh, geomID + "-positions", "X Y Z", posArray, count);
00109
00110
00111 domFloat normalArray[] = { 1, 0, 0,
00112 -1, 0, 0,
00113 0, 1, 0,
00114 0, -1, 0,
00115 0, 0, 1,
00116 0, 0, -1 };
00117 count = sizeof(normalArray)/sizeof(normalArray[0]);
00118 my_addSource(mesh, geomID + "-normals", "X Y Z", normalArray, count);
00119
00120
00121 domFloat uvArray[] = { 0, 0,
00122 0, 1,
00123 1, 0,
00124 1, 1 };
00125 count = sizeof(uvArray)/sizeof(uvArray[0]);
00126 my_addSource(mesh, geomID + "-uv", "S T", uvArray, count);
00127
00128
00129 daeElement* vertices = mesh->add("vertices");
00130 vertices->setAttribute("id", (geomID + "-vertices").c_str());
00131 daeElement* verticesInput = vertices->add("input");
00132 verticesInput->setAttribute("semantic", "POSITION");
00133 verticesInput->setAttribute("source", makeUriRef(geomID + "-positions").c_str());
00134
00135
00136
00137 domUint indices[] = { 0, 1, 0, 1, 1, 1, 2, 1, 2,
00138 1, 1, 1, 3, 1, 3, 2, 1, 2,
00139 0, 2, 0, 4, 2, 1, 1, 2, 2,
00140 4, 2, 1, 5, 2, 3, 1, 2, 2,
00141 1, 4, 0, 5, 4, 1, 3, 4, 2,
00142 5, 4, 1, 7, 4, 3, 3, 4, 2,
00143 5, 0, 0, 4, 0, 1, 7, 0, 2,
00144 4, 0, 1, 6, 0, 3, 7, 0, 2,
00145 4, 5, 0, 0, 5, 1, 6, 5, 2,
00146 0, 5, 1, 2, 5, 3, 6, 5, 2,
00147 3, 3, 0, 7, 3, 1, 2, 3, 2,
00148 7, 3, 1, 6, 3, 3, 2, 3, 2 };
00149 count = sizeof(indices)/sizeof(indices[0]);
00150
00151 domTriangles* triangles = daeSafeCast<domTriangles>(mesh->add("triangles"));
00152 triangles->setCount(count/(3*3));
00153 triangles->setMaterial("mtl");
00154
00155 my_addInput(triangles, "VERTEX", geomID + "-vertices", 0);
00156 my_addInput(triangles, "NORMAL", geomID + "-normals", 1);
00157 my_addInput(triangles, "TEXCOORD", geomID + "-uv", 2);
00158
00159 domP* p = daeSafeCast<domP>(triangles->add("p"));
00160 p->getValue() = rawArrayToDaeArray(indices, count);
00161
00162 }
00163
00164 void my_addImage(daeElement* root) {
00165 daeElement* imageLib = root->add("library_images");
00166 daeElement* image = imageLib->add("image");
00167 image->setAttribute("id", "img");
00168 image->setAttribute("name", "myimage");
00169 image->add("init_from")->setCharData("./texture.bmp");
00170
00171
00172 return;
00173
00174
00175
00176 daeElement* tmp = image->add("init_from");
00177
00178 std::string str("file://home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture1.png");
00179
00180
00181
00182 daeBool res = tmp->setCharData(str);
00183 printf("result is =%d",res);
00184 }
00185
00186 void my_addEffect(daeElement* root) {
00187 daeElement* effectLib = root->add("library_effects");
00188 daeElement* effect = effectLib->add("effect");
00189 effect->setAttribute("id", "cubeEffect");
00190 daeElement* profile = effect->add("profile_COMMON");
00191
00192
00193 daeElement* newparam = profile->add("newparam");
00194 newparam->setAttribute("sid", "sampler");
00195
00196
00197 daeElement* sampler = newparam->add("sampler2D");
00198 daeSafeCast<domInstance_image>(sampler->add("instance_image"))->setUrl("#img");
00199 sampler->add("minfilter")->setCharData("LINEAR");
00200 sampler->add("magfilter")->setCharData("LINEAR");
00201
00202 daeElement* technique = profile->add("technique");
00203 technique->setAttribute("sid", "common");
00204 daeElement* texture = technique->add("phong diffuse texture");
00205 texture->setAttribute("texture", "sampler");
00206 texture->setAttribute("texcoord", "uv0");
00207 }
00208
00209 void my_addMaterial(daeElement* root) {
00210
00211 daeElement* materialLib = root->add("library_materials");
00212
00213 daeElement* material = materialLib->add("material");
00214 material->setAttribute("id", "cubeMaterial");
00215 material->add("instance_effect")->setAttribute("url", makeUriRef("cubeEffect").c_str());
00216
00217 }
00218
00219 void my_addVisualScene(daeElement* root) {
00220
00221 daeElement* visualSceneLib = root->add("library_visual_scenes");
00222 daeElement* visualScene = visualSceneLib->add("visual_scene");
00223 visualScene->setAttribute("id", "cubeScene");
00224
00225
00226 daeElement* node = visualScene->add("node");
00227 node->setAttribute("id", "cubeNode");
00228 node->add("rotate")->setCharData("1 0 0 45");
00229 node->add("translate")->setCharData("0 10 0");
00230
00231
00232 daeElement* instanceGeom = node->add("instance_geometry");
00233 instanceGeom->setAttribute("url", makeUriRef("cubeGeom").c_str());
00234
00235
00236 daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
00237 instanceMaterial->setAttribute("symbol", "mtl");
00238 instanceMaterial->setAttribute("target", makeUriRef("cubeMaterial").c_str());
00239
00240 daeElement* bindVertexInput = instanceMaterial->add("bind_vertex_input");
00241 bindVertexInput->setAttribute("semantic", "uv0");
00242 bindVertexInput->setAttribute("input_semantic", "TEXCOORD");
00243 bindVertexInput->setAttribute("input_set", "0");
00244
00245
00246 root->add("scene instance_visual_scene")->setAttribute("url", makeUriRef("cubeScene").c_str());
00247
00248 }