export.cpp
Go to the documentation of this file.
1 
7 /*
8 * Copyright 2006 Sony Computer Entertainment Inc.
9 *
10 * Licensed under the MIT Open Source License, for details please see license.txt or the website
11 * http://www.opensource.org/licenses/mit-license.php
12 *
13 */
14 #include <dae.h>
15 #include <dae/daeUtils.h>
16 #include <dom/domCOLLADA.h>
17 
18 using namespace std;
19 using namespace ColladaDOM150;
20 using namespace cdom;
21 
22 // Demonstrates how to use the DOM to create a simple, textured Collada model
23 // and save it to disk.
24 
25 
26 
27 template<typename T>
28 daeTArray<T> rawArrayToDaeArray(T rawArray[], size_t count) {
29  daeTArray<T> result;
30  for (size_t i = 0; i < count; i++)
31  result.append(rawArray[i]);
32  return result;
33 }
34 
35 // "myGeom" --> "#myGeom"
36 string makeUriRef(const string& id) {
37  return string("#") + id;
38 }
39 
40 void my_addSource(daeElement* mesh,
41  const std::string& srcID,
42  const std::string& paramNames,
43  domFloat values[],
44  int valueCount) {
45 
46  daeElement* src = mesh->add("source");
47  //SafeAdd(mesh, "source", src);
48 
49  src->setAttribute("id", srcID.c_str());
50 
51  domFloat_array* fa = daeSafeCast<domFloat_array>(src->add("float_array"));
52  //CheckResult(fa);
53  fa->setId((src->getAttribute("id") + "-array").c_str());
54  fa->setCount(valueCount);
55  fa->getValue() = rawArrayToDaeArray(values, valueCount);
56 
57  domAccessor* acc = daeSafeCast<domAccessor>(src->add("technique_common accessor"));
58  //CheckResult(acc);
59  acc->setSource(makeUriRef(fa->getId()).c_str());
60 
61  list<string> params = tokenize(paramNames, " ");
62  acc->setStride(params.size());
63  acc->setCount(valueCount/params.size());
64  for (tokenIter iter = params.begin(); iter != params.end(); iter++) {
65  //SafeAdd(acc, "param", p);
66  daeElement* p = acc->add("param");
67  p->setAttribute("name", iter->c_str());
68  p->setAttribute("type", "float");
69  }
70 
71  //return testResult(true);
72 }
73 
74 
75 void my_addInput(daeElement* triangles,
76  const string& semantic,
77  const string& srcID,
78  int offset) {
79  domInput_local_offset* input = daeSafeCast<domInput_local_offset>(triangles->add("input"));
80  //CheckResult(input);
81  input->setSemantic(semantic.c_str());
82  input->setOffset(offset);
83  domUrifragment source(*triangles->getDAE(), srcID);
84  input->setSource(source);
85  if (semantic == "TEXCOORD")
86  input->setSet(0);
87  //return testResult(true);
88 }
89 
90 void my_addGeometry(daeElement* root) {
91 
92  daeElement* geomLib = root->add("library_geometries");
93  daeElement* geom = geomLib->add("geometry");
94  string geomID = "cubeGeom";
95  geom->setAttribute("id", geomID.c_str());
96  daeElement* mesh = geom->add("mesh");
97 
98  // Add the position data
99  domFloat posArray[] = { -10, -10, -10,
100  -10, -10, 10,
101  -10, 10, -10,
102  -10, 10, 10,
103  10, -10, -10,
104  10, -10, 10,
105  10, 10, -10,
106  10, 10, 10 };
107  int count = sizeof(posArray)/sizeof(posArray[0]);
108  my_addSource(mesh, geomID + "-positions", "X Y Z", posArray, count);
109 
110  // Add the normal data
111  domFloat normalArray[] = { 1, 0, 0,
112  -1, 0, 0,
113  0, 1, 0,
114  0, -1, 0,
115  0, 0, 1,
116  0, 0, -1 };
117  count = sizeof(normalArray)/sizeof(normalArray[0]);
118  my_addSource(mesh, geomID + "-normals", "X Y Z", normalArray, count);
119 
120  // Add the tex coord data
121  domFloat uvArray[] = { 0, 0,
122  0, 1,
123  1, 0,
124  1, 1 };
125  count = sizeof(uvArray)/sizeof(uvArray[0]);
126  my_addSource(mesh, geomID + "-uv", "S T", uvArray, count);
127 
128  // Add the <vertices> element
129  daeElement* vertices = mesh->add("vertices");
130  vertices->setAttribute("id", (geomID + "-vertices").c_str());
131  daeElement* verticesInput = vertices->add("input");
132  verticesInput->setAttribute("semantic", "POSITION");
133  verticesInput->setAttribute("source", makeUriRef(geomID + "-positions").c_str());
134 
135  // Add the <triangles> element.
136  // Each line is one triangle.
137  domUint indices[] = { 0, 1, 0, 1, 1, 1, 2, 1, 2,
138  1, 1, 1, 3, 1, 3, 2, 1, 2,
139  0, 2, 0, 4, 2, 1, 1, 2, 2,
140  4, 2, 1, 5, 2, 3, 1, 2, 2,
141  1, 4, 0, 5, 4, 1, 3, 4, 2,
142  5, 4, 1, 7, 4, 3, 3, 4, 2,
143  5, 0, 0, 4, 0, 1, 7, 0, 2,
144  4, 0, 1, 6, 0, 3, 7, 0, 2,
145  4, 5, 0, 0, 5, 1, 6, 5, 2,
146  0, 5, 1, 2, 5, 3, 6, 5, 2,
147  3, 3, 0, 7, 3, 1, 2, 3, 2,
148  7, 3, 1, 6, 3, 3, 2, 3, 2 };
149  count = sizeof(indices)/sizeof(indices[0]);
150 
151  domTriangles* triangles = daeSafeCast<domTriangles>(mesh->add("triangles"));
152  triangles->setCount(count/(3*3)); // 3 indices per vertex, 3 vertices per triangle
153  triangles->setMaterial("mtl");
154 
155  my_addInput(triangles, "VERTEX", geomID + "-vertices", 0);
156  my_addInput(triangles, "NORMAL", geomID + "-normals", 1);
157  my_addInput(triangles, "TEXCOORD", geomID + "-uv", 2);
158 
159  domP* p = daeSafeCast<domP>(triangles->add("p"));
160  p->getValue() = rawArrayToDaeArray(indices, count);
161 
162 }
163 
164 void my_addImage(daeElement* root) {
165  daeElement* imageLib = root->add("library_images");
166  daeElement* image = imageLib->add("image");
167  image->setAttribute("id", "img");
168  image->setAttribute("name", "myimage");
169  image->add("init_from")->setCharData("./texture.bmp");
170 
171 
172  return;
173 
174 
175  //daeElement* tmp = image->add("init_from");
176  daeElement* tmp = image->add("init_from");
177  //image->add("init_from")->setCharData("./texture.bmp");
178  std::string str("file://home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture1.png");
179  //std::string str("http://dubinko.info/writing/xforms/");
180  //const char* date = "file:///home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture.bmp";
181  //std::string str("MAX");
182  daeBool res = tmp->setCharData(str);
183  printf("result is =%d",res);
184 }
185 
186 void my_addEffect(daeElement* root) {
187  daeElement* effectLib = root->add("library_effects");
188  daeElement* effect = effectLib->add("effect");
189  effect->setAttribute("id", "cubeEffect");
190  daeElement* profile = effect->add("profile_COMMON");
191 
192  // Add a <sampler2D>
193  daeElement* newparam = profile->add("newparam");
194  newparam->setAttribute("sid", "sampler");
195 
196 
197  daeElement* sampler = newparam->add("sampler2D");
198  daeSafeCast<domInstance_image>(sampler->add("instance_image"))->setUrl("#img");
199  sampler->add("minfilter")->setCharData("LINEAR");
200  sampler->add("magfilter")->setCharData("LINEAR");
201 
202  daeElement* technique = profile->add("technique");
203  technique->setAttribute("sid", "common");
204  daeElement* texture = technique->add("phong diffuse texture");
205  texture->setAttribute("texture", "sampler");
206  texture->setAttribute("texcoord", "uv0");
207 }
208 
209 void my_addMaterial(daeElement* root) {
210  //SafeAdd(root, "library_materials", materialLib);
211  daeElement* materialLib = root->add("library_materials");
212  //SafeAdd(materialLib, "material", material);
213  daeElement* material = materialLib->add("material");
214  material->setAttribute("id", "cubeMaterial");
215  material->add("instance_effect")->setAttribute("url", makeUriRef("cubeEffect").c_str());
216 
217 }
218 
219 void my_addVisualScene(daeElement* root) {
220 
221  daeElement* visualSceneLib = root->add("library_visual_scenes");
222  daeElement* visualScene = visualSceneLib->add("visual_scene");
223  visualScene->setAttribute("id", "cubeScene");
224 
225  // Add a <node> with a simple transformation
226  daeElement* node = visualScene->add("node");
227  node->setAttribute("id", "cubeNode");
228  node->add("rotate")->setCharData("1 0 0 45");
229  node->add("translate")->setCharData("0 10 0");
230 
231  // Instantiate the <geometry>
232  daeElement* instanceGeom = node->add("instance_geometry");
233  instanceGeom->setAttribute("url", makeUriRef("cubeGeom").c_str());
234 
235  // Bind material parameters
236  daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
237  instanceMaterial->setAttribute("symbol", "mtl");
238  instanceMaterial->setAttribute("target", makeUriRef("cubeMaterial").c_str());
239 
240  daeElement* bindVertexInput = instanceMaterial->add("bind_vertex_input");
241  bindVertexInput->setAttribute("semantic", "uv0");
242  bindVertexInput->setAttribute("input_semantic", "TEXCOORD");
243  bindVertexInput->setAttribute("input_set", "0");
244 
245  // Add a <scene>
246  root->add("scene instance_visual_scene")->setAttribute("url", makeUriRef("cubeScene").c_str());
247 
248 }
void my_addVisualScene(daeElement *root)
Definition: export.cpp:219
void my_addInput(daeElement *triangles, const string &semantic, const string &srcID, int offset)
Definition: export.cpp:75
void my_addMaterial(daeElement *root)
Definition: export.cpp:209
string makeUriRef(const string &id)
Definition: export.cpp:36
void my_addSource(daeElement *mesh, const std::string &srcID, const std::string &paramNames, domFloat values[], int valueCount)
Definition: export.cpp:40
void my_addGeometry(daeElement *root)
Definition: export.cpp:90
void my_addEffect(daeElement *root)
Definition: export.cpp:186
void my_addImage(daeElement *root)
Definition: export.cpp:164
daeTArray< T > rawArrayToDaeArray(T rawArray[], size_t count)
Definition: export.cpp:28


wrapper_collada
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:33:01