2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21 * ***** END GPL LICENSE BLOCK *****
24 /** \file blender/collada/GeometryExporter.cpp
31 #include "COLLADASWPrimitves.h"
32 #include "COLLADASWSource.h"
33 #include "COLLADASWVertices.h"
34 #include "COLLADABUUtils.h"
36 #include "GeometryExporter.h"
38 #include "DNA_meshdata_types.h"
41 #include "BLI_utildefines.h"
43 #include "BKE_DerivedMesh.h"
45 #include "BKE_global.h"
46 #include "BKE_library.h"
47 #include "BKE_customdata.h"
48 #include "BKE_material.h"
52 #include "collada_internal.h"
53 #include "collada_utils.h"
55 // TODO: optimize UV sets by making indexed list with duplicates removed
56 GeometryExporter::GeometryExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryGeometries(sw), export_settings(export_settings)
61 void GeometryExporter::exportGeom(Scene *sce)
67 gf.forEachMeshObjectInExportSet<GeometryExporter>(sce, *this, this->export_settings->export_set);
72 void GeometryExporter::operator()(Object *ob)
74 // XXX don't use DerivedMesh, Mesh instead?
76 DerivedMesh *dm = mesh_get_derived_final(mScene, ob, CD_MASK_BAREMESH);
79 bool use_instantiation = this->export_settings->use_object_instantiation;
81 if (this->export_settings->apply_modifiers) {
82 me = bc_to_mesh_apply_modifiers(mScene, ob, this->export_settings->export_mesh_type);
85 me = (Mesh *)ob->data;
87 BKE_mesh_tessface_ensure(me);
89 std::string geom_id = get_geometry_id(ob, use_instantiation);
90 std::vector<Normal> nor;
91 std::vector<Face> norind;
93 // Skip if linked geometry was already exported from another reference
94 if (use_instantiation &&
95 exportedGeometry.find(geom_id) != exportedGeometry.end())
100 std::string geom_name = (use_instantiation) ? id_name(ob->data) : id_name(ob);
102 exportedGeometry.insert(geom_id);
104 bool has_color = (bool)CustomData_has_layer(&me->fdata, CD_MCOL);
106 create_normals(nor, norind, me);
108 // openMesh(geoId, geoName, meshId)
109 openMesh(geom_id, geom_name);
111 // writes <source> for vertex coords
112 createVertsSource(geom_id, me);
114 // writes <source> for normal coords
115 createNormalsSource(geom_id, me, nor);
117 bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
119 // writes <source> for uv coords if mesh has uv coords
121 createTexcoordsSource(geom_id, me);
124 createVertexColorSource(geom_id, me);
128 COLLADASW::Vertices verts(mSW);
129 verts.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX));
130 COLLADASW::InputList &input_list = verts.getInputList();
131 COLLADASW::Input input(COLLADASW::InputSemantic::POSITION, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
132 input_list.push_back(input);
135 createLooseEdgeList(ob, me, geom_id, norind);
137 // Only create Polylists if number of faces > 0
138 if (me->totface > 0) {
141 for (int a = 0; a < ob->totcol; a++) {
142 createPolylist(a, has_uvs, has_color, ob, me, geom_id, norind);
146 createPolylist(0, has_uvs, has_color, ob, me, geom_id, norind);
152 if (me->flag & ME_TWOSIDED) {
153 mSW->appendTextBlock("<extra><technique profile=\"MAYA\"><double_sided>1</double_sided></technique></extra>");
158 if (this->export_settings->apply_modifiers) {
159 BKE_libblock_free_us(&(G.main->mesh), me);
162 if (this->export_settings->include_shapekeys) {
163 Key * key = BKE_key_from_object(ob);
165 KeyBlock * kb = (KeyBlock*)key->block.first;
168 for (; kb; kb = kb->next) {
169 BKE_key_convert_to_mesh(kb, me);
170 export_key_mesh(ob, me, kb);
179 void GeometryExporter::export_key_mesh(Object *ob, Mesh *me, KeyBlock *kb)
181 std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
182 std::vector<Normal> nor;
183 std::vector<Face> norind;
185 if (exportedGeometry.find(geom_id) != exportedGeometry.end())
190 std::string geom_name = id_name(ob) + "_morph_" + kb->name;
192 exportedGeometry.insert(geom_id);
194 bool has_color = (bool)CustomData_has_layer(&me->fdata, CD_MCOL);
196 create_normals(nor, norind, me);
198 // openMesh(geoId, geoName, meshId)
199 openMesh(geom_id, geom_name);
201 // writes <source> for vertex coords
202 createVertsSource(geom_id, me);
204 // writes <source> for normal coords
205 createNormalsSource(geom_id, me, nor);
207 bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
209 // writes <source> for uv coords if mesh has uv coords
211 createTexcoordsSource(geom_id, me);
214 createVertexColorSource(geom_id, me);
218 COLLADASW::Vertices verts(mSW);
219 verts.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX));
220 COLLADASW::InputList &input_list = verts.getInputList();
221 COLLADASW::Input input(COLLADASW::InputSemantic::POSITION, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
222 input_list.push_back(input);
225 //createLooseEdgeList(ob, me, geom_id, norind);
229 for (int a = 0; a < ob->totcol; a++) {
230 createPolylist(a, has_uvs, has_color, ob, me, geom_id, norind);
234 createPolylist(0, has_uvs, has_color, ob, me, geom_id, norind);
239 if (me->flag & ME_TWOSIDED) {
240 mSW->appendTextBlock("<extra><technique profile=\"MAYA\"><double_sided>1</double_sided></technique></extra>");
246 void GeometryExporter::createLooseEdgeList(Object *ob,
248 std::string& geom_id,
249 std::vector<Face>& norind)
252 MEdge *medges = me->medge;
253 int totedges = me->totedge;
254 int edges_in_linelist = 0;
255 std::vector<unsigned int> edge_list;
258 // Find all loose edges in Mesh
259 // and save vertex indices in edge_list
260 for (index = 0; index < totedges; index++)
262 MEdge *edge = &medges[index];
264 if (edge->flag & ME_LOOSEEDGE)
266 edges_in_linelist += 1;
267 edge_list.push_back(edge->v1);
268 edge_list.push_back(edge->v2);
272 if (edges_in_linelist > 0)
274 // Create the list of loose edges
275 COLLADASW::Lines lines(mSW);
277 lines.setCount(edges_in_linelist);
280 COLLADASW::InputList &til = lines.getInputList();
282 // creates <input> in <lines> for vertices
283 COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX), 0);
284 til.push_back(input1);
286 lines.prepareToAppendValues();
288 for (index = 0; index < edges_in_linelist; index++)
290 lines.appendValues(edge_list[2 * index + 1]);
291 lines.appendValues(edge_list[2 * index]);
298 // powerful because it handles both cases when there is material and when there's not
299 void GeometryExporter::createPolylist(short material_index,
304 std::string& geom_id,
305 std::vector<Face>& norind)
307 MFace *mfaces = me->mface;
308 int totfaces = me->totface;
312 int faces_in_polylist = 0;
313 std::vector<unsigned long> vcount_list;
315 // count faces with this material
316 for (i = 0; i < totfaces; i++) {
317 MFace *f = &mfaces[i];
319 if (f->mat_nr == material_index) {
322 vcount_list.push_back(3);
325 vcount_list.push_back(4);
330 // no faces using this material
331 if (faces_in_polylist == 0) {
332 fprintf(stderr, "%s: material with index %d is not used.\n", id_name(ob).c_str(), material_index);
336 Material *ma = ob->totcol ? give_current_material(ob, material_index + 1) : NULL;
337 COLLADASW::Polylist polylist(mSW);
339 // sets count attribute in <polylist>
340 polylist.setCount(faces_in_polylist);
342 // sets material name
344 std::string material_id = get_material_id(ma);
345 std::ostringstream ostr;
346 ostr << translate_id(material_id);
347 polylist.setMaterial(ostr.str());
350 COLLADASW::InputList &til = polylist.getInputList();
352 // creates <input> in <polylist> for vertices
353 COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX), 0);
355 // creates <input> in <polylist> for normals
356 COLLADASW::Input input2(COLLADASW::InputSemantic::NORMAL, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL), 1);
358 til.push_back(input1);
359 til.push_back(input2);
361 // if mesh has uv coords writes <input> for TEXCOORD
362 int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
363 int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE)-1;
364 for (i = 0; i < num_layers; i++) {
365 if (!this->export_settings->active_uv_only || i == active_uv_index) {
367 // char *name = CustomData_get_layer_name(&me->fdata, CD_MTFACE, i);
368 COLLADASW::Input input3(COLLADASW::InputSemantic::TEXCOORD,
369 makeUrl(makeTexcoordSourceId(geom_id, i)),
370 2, // offset always 2, this is only until we have optimized UV sets
371 i // set number equals UV map index
373 til.push_back(input3);
378 COLLADASW::Input input4(COLLADASW::InputSemantic::COLOR, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::COLOR), has_uvs ? 3 : 2);
379 til.push_back(input4);
383 polylist.setVCountList(vcount_list);
385 // performs the actual writing
386 polylist.prepareToAppendValues();
392 for (i = 0; i < totfaces; i++) {
393 MFace *f = &mfaces[i];
395 if (f->mat_nr == material_index) {
397 unsigned int *v = &f->v1;
398 unsigned int *n = &norind[i].v1;
399 for (int j = 0; j < (f->v4 == 0 ? 3 : 4); j++) {
400 polylist.appendValues(v[j]);
401 polylist.appendValues(n[j]);
404 polylist.appendValues(texindex + j);
407 polylist.appendValues(texindex + j);
419 // creates <source> for positions
420 void GeometryExporter::createVertsSource(std::string geom_id, Mesh *me)
423 int totverts = dm->getNumVerts(dm);
424 MVert *verts = dm->getVertArray(dm);
426 int totverts = me->totvert;
427 MVert *verts = me->mvert;
429 COLLADASW::FloatSourceF source(mSW);
430 source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
431 source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::POSITION) +
433 source.setAccessorCount(totverts);
434 source.setAccessorStride(3);
435 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
436 param.push_back("X");
437 param.push_back("Y");
438 param.push_back("Z");
439 /* main function, it creates <source id = "">, <float_array id = ""
441 source.prepareToAppendValues();
442 //appends data to <float_array>
444 for (i = 0; i < totverts; i++) {
445 source.appendValues(verts[i].co[0], verts[i].co[1], verts[i].co[2]);
452 void GeometryExporter::createVertexColorSource(std::string geom_id, Mesh *me)
454 if (!CustomData_has_layer(&me->fdata, CD_MCOL))
458 int totcolor = 0, i, j;
460 for (i = 0, f = me->mface; i < me->totface; i++, f++)
461 totcolor += f->v4 ? 4 : 3;
463 COLLADASW::FloatSourceF source(mSW);
464 source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::COLOR));
465 source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::COLOR) + ARRAY_ID_SUFFIX);
466 source.setAccessorCount(totcolor);
467 source.setAccessorStride(3);
469 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
470 param.push_back("R");
471 param.push_back("G");
472 param.push_back("B");
474 source.prepareToAppendValues();
476 int index = CustomData_get_active_layer_index(&me->fdata, CD_MCOL);
478 MCol *mcol = (MCol *)me->fdata.layers[index].data;
481 for (i = 0, f = me->mface; i < me->totface; i++, c += 4, f++)
482 for (j = 0; j < (f->v4 ? 4 : 3); j++)
483 source.appendValues(c[j].b / 255.0f, c[j].g / 255.0f, c[j].r / 255.0f);
488 std::string GeometryExporter::makeTexcoordSourceId(std::string& geom_id, int layer_index)
491 sprintf(suffix, "-%d", layer_index);
492 return getIdBySemantics(geom_id, COLLADASW::InputSemantic::TEXCOORD) + suffix;
495 //creates <source> for texcoords
496 void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *me)
499 int totfaces = dm->getNumTessFaces(dm);
500 MFace *mfaces = dm->getTessFaceArray(dm);
502 int totfaces = me->totface;
503 MFace *mfaces = me->mface;
509 for (i = 0; i < totfaces; i++) {
510 MFace *f = &mfaces[i];
519 int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
521 // write <source> for each layer
522 // each <source> will get id like meshName + "map-channel-1"
524 int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE)-1;
525 for (int a = 0; a < num_layers; a++) {
527 if (!this->export_settings->active_uv_only || a == active_uv_index) {
528 MTFace *tface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, a);
529 // char *name = CustomData_get_layer_name(&me->fdata, CD_MTFACE, a);
531 COLLADASW::FloatSourceF source(mSW);
532 std::string layer_id = makeTexcoordSourceId(geom_id, map_index++);
533 source.setId(layer_id);
534 source.setArrayId(layer_id + ARRAY_ID_SUFFIX);
536 source.setAccessorCount(totuv);
537 source.setAccessorStride(2);
538 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
539 param.push_back("S");
540 param.push_back("T");
542 source.prepareToAppendValues();
544 for (i = 0; i < totfaces; i++) {
545 MFace *f = &mfaces[i];
547 for (int j = 0; j < (f->v4 == 0 ? 3 : 4); j++) {
548 source.appendValues(tface[i].uv[j][0],
559 //creates <source> for normals
560 void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal>& nor)
563 int totverts = dm->getNumVerts(dm);
564 MVert *verts = dm->getVertArray(dm);
567 COLLADASW::FloatSourceF source(mSW);
568 source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL));
569 source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL) +
571 source.setAccessorCount((unsigned long)nor.size());
572 source.setAccessorStride(3);
573 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
574 param.push_back("X");
575 param.push_back("Y");
576 param.push_back("Z");
578 source.prepareToAppendValues();
580 std::vector<Normal>::iterator it;
581 for (it = nor.begin(); it != nor.end(); it++) {
583 source.appendValues(n.x, n.y, n.z);
589 void GeometryExporter::create_normals(std::vector<Normal> &nor, std::vector<Face> &ind, Mesh *me)
592 MVert *vert = me->mvert;
593 std::map<unsigned int, unsigned int> nshar;
595 for (i = 0; i < me->totface; i++) {
596 MFace *fa = &me->mface[i];
598 unsigned int *nn = &f.v1;
599 unsigned int *vv = &fa->v1;
601 memset(&f, 0, sizeof(f));
602 v = fa->v4 == 0 ? 3 : 4;
604 if (!(fa->flag & ME_SMOOTH)) {
607 normal_quad_v3(&n.x, vert[fa->v1].co, vert[fa->v2].co, vert[fa->v3].co, vert[fa->v4].co);
609 normal_tri_v3(&n.x, vert[fa->v1].co, vert[fa->v2].co, vert[fa->v3].co);
613 for (j = 0; j < v; j++) {
614 if (fa->flag & ME_SMOOTH) {
615 if (nshar.find(*vv) != nshar.end())
619 (float)vert[*vv].no[0] / 32767.0f,
620 (float)vert[*vv].no[1] / 32767.0f,
621 (float)vert[*vv].no[2] / 32767.0f
624 *nn = (unsigned int)nor.size() - 1;
630 *nn = (unsigned int)nor.size() - 1;
639 std::string GeometryExporter::getIdBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix)
641 return geom_id + getSuffixBySemantic(type) + other_suffix;
645 COLLADASW::URI GeometryExporter::getUrlBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix)
648 std::string id(getIdBySemantics(geom_id, type, other_suffix));
649 return COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, id);
653 COLLADASW::URI GeometryExporter::makeUrl(std::string id)
655 return COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, id);
659 int GeometryExporter::getTriCount(MFace *faces, int totface)
663 for (i = 0; i < totface; i++) {
665 if (faces[i].v4 != 0)