1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file blender/collada/GeometryExporter.cpp
* \ingroup collada
*/
#include <sstream>
#include "COLLADASWPrimitves.h"
#include "COLLADASWSource.h"
#include "COLLADASWVertices.h"
#include "COLLADABUUtils.h"
#include "GeometryExporter.h"
#include "DNA_meshdata_types.h"
extern "C" {
#include "BLI_utildefines.h"
#include "BKE_DerivedMesh.h"
#include "BKE_main.h"
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_customdata.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
}
#include "collada_internal.h"
#include "collada_utils.h"
// TODO: optimize UV sets by making indexed list with duplicates removed
GeometryExporter::GeometryExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryGeometries(sw), export_settings(export_settings)
{
}
void GeometryExporter::exportGeom(Main *bmain, Scene *sce)
{
openLibrary();
m_bmain = bmain;
mScene = sce;
GeometryFunctor gf;
gf.forEachMeshObjectInExportSet<GeometryExporter>(sce, *this, this->export_settings->export_set);
closeLibrary();
}
void GeometryExporter::operator()(Object *ob)
{
// XXX don't use DerivedMesh, Mesh instead?
#if 0
DerivedMesh *dm = mesh_get_derived_final(mScene, ob, CD_MASK_BAREMESH);
#endif
bool use_instantiation = this->export_settings->use_object_instantiation;
Mesh *me = bc_get_mesh_copy(
m_bmain,
mScene,
ob,
this->export_settings->export_mesh_type,
this->export_settings->apply_modifiers,
this->export_settings->triangulate);
std::string geom_id = get_geometry_id(ob, use_instantiation);
std::vector<Normal> nor;
std::vector<BCPolygonNormalsIndices> norind;
// Skip if linked geometry was already exported from another reference
if (use_instantiation &&
exportedGeometry.find(geom_id) != exportedGeometry.end())
{
return;
}
std::string geom_name = (use_instantiation) ? id_name(ob->data) : id_name(ob);
exportedGeometry.insert(geom_id);
bool has_color = (bool)CustomData_has_layer(&me->fdata, CD_MCOL);
create_normals(nor, norind, me);
// openMesh(geoId, geoName, meshId)
openMesh(geom_id, geom_name);
// writes <source> for vertex coords
createVertsSource(geom_id, me);
// writes <source> for normal coords
createNormalsSource(geom_id, me, nor);
bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
// writes <source> for uv coords if mesh has uv coords
if (has_uvs) {
createTexcoordsSource(geom_id, me);
}
if (has_color) {
createVertexColorSource(geom_id, me);
}
// <vertices>
COLLADASW::Vertices verts(mSW);
verts.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX));
COLLADASW::InputList &input_list = verts.getInputList();
COLLADASW::Input input(COLLADASW::InputSemantic::POSITION, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
input_list.push_back(input);
verts.add();
createLooseEdgeList(ob, me, geom_id);
// Only create Polylists if number of faces > 0
if (me->totface > 0) {
// XXX slow
std::set<Image *> uv_images = bc_getUVImages(ob, !this->export_settings->active_uv_only);
if (this->export_settings->export_texture_type == BC_TEXTURE_TYPE_MAT || uv_images.size() == 0) {
if (ob->totcol) {
for (int a = 0; a < ob->totcol; a++) {
createPolylist(a, has_uvs, has_color, ob, me, geom_id, norind);
}
}
else {
int i = 0;
createPolylist(i, has_uvs, has_color, ob, me, geom_id, norind);
}
}
else {
bool all_uv_layers = !this->export_settings->active_uv_only;
std::set<Image *> uv_image_set = bc_getUVImages(ob, all_uv_layers);
createPolylists(uv_image_set, has_uvs, has_color, ob, me, geom_id, norind);
}
}
closeMesh();
if (me->flag & ME_TWOSIDED) {
mSW->appendTextBlock("<extra><technique profile=\"MAYA\"><double_sided>1</double_sided></technique></extra>");
}
closeGeometry();
if (this->export_settings->include_shapekeys) {
Key *key = BKE_key_from_object(ob);
if (key) {
KeyBlock *kb = (KeyBlock *)key->block.first;
//skip the basis
kb = kb->next;
for (; kb; kb = kb->next) {
BKE_keyblock_convert_to_mesh(kb, me);
export_key_mesh(ob, me, kb);
}
}
}
BKE_libblock_free_us(m_bmain, me);
}
void GeometryExporter::export_key_mesh(Object *ob, Mesh *me, KeyBlock *kb)
{
std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
std::vector<Normal> nor;
std::vector<BCPolygonNormalsIndices> norind;
if (exportedGeometry.find(geom_id) != exportedGeometry.end())
{
return;
}
std::string geom_name = kb->name;
exportedGeometry.insert(geom_id);
bool has_color = (bool)CustomData_has_layer(&me->fdata, CD_MCOL);
create_normals(nor, norind, me);
// openMesh(geoId, geoName, meshId)
openMesh(geom_id, geom_name);
// writes <source> for vertex coords
createVertsSource(geom_id, me);
// writes <source> for normal coords
createNormalsSource(geom_id, me, nor);
bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE);
// writes <source> for uv coords if mesh has uv coords
if (has_uvs) {
createTexcoordsSource(geom_id, me);
}
if (has_color) {
createVertexColorSource(geom_id, me);
}
// <vertices>
COLLADASW::Vertices verts(mSW);
verts.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX));
COLLADASW::InputList &input_list = verts.getInputList();
COLLADASW::Input input(COLLADASW::InputSemantic::POSITION, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
input_list.push_back(input);
verts.add();
//createLooseEdgeList(ob, me, geom_id, norind);
// XXX slow
if (ob->totcol && this->export_settings->export_texture_type == BC_TEXTURE_TYPE_MAT) {
for (int a = 0; a < ob->totcol; a++) {
createPolylist(a, has_uvs, has_color, ob, me, geom_id, norind);
}
}
else {
bool all_uv_layers = !this->export_settings->active_uv_only;
std::set<Image *> uv_images = bc_getUVImages(ob, all_uv_layers);
createPolylists(uv_images, has_uvs, has_color, ob, me, geom_id, norind);
}
closeMesh();
if (me->flag & ME_TWOSIDED) {
mSW->appendTextBlock("<extra><technique profile=\"MAYA\"><double_sided>1</double_sided></technique></extra>");
}
closeGeometry();
}
void GeometryExporter::createLooseEdgeList(Object *ob,
Mesh *me,
std::string& geom_id)
{
MEdge *medges = me->medge;
int totedges = me->totedge;
int edges_in_linelist = 0;
std::vector<unsigned int> edge_list;
int index;
// Find all loose edges in Mesh
// and save vertex indices in edge_list
for (index = 0; index < totedges; index++)
{
MEdge *edge = &medges[index];
if (edge->flag & ME_LOOSEEDGE)
{
edges_in_linelist += 1;
edge_list.push_back(edge->v1);
edge_list.push_back(edge->v2);
}
}
if (edges_in_linelist > 0)
{
// Create the list of loose edges
COLLADASW::Lines lines(mSW);
lines.setCount(edges_in_linelist);
COLLADASW::InputList &til = lines.getInputList();
// creates <input> in <lines> for vertices
COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX), 0);
til.push_back(input1);
lines.prepareToAppendValues();
for (index = 0; index < edges_in_linelist; index++)
{
lines.appendValues(edge_list[2 * index + 1]);
lines.appendValues(edge_list[2 * index]);
}
lines.finish();
}
}
std::string GeometryExporter::makeVertexColorSourceId(std::string& geom_id, char *layer_name)
{
std::string result = getIdBySemantics(geom_id, COLLADASW::InputSemantic::COLOR) + "-" + layer_name;
return result;
}
static void prepareToAppendValues(bool is_triangulated, COLLADASW::PrimitivesBase *facelist, std::vector<unsigned long> &vcount_list)
{
// performs the actual writing
if (is_triangulated) {
((COLLADASW::Triangles *)facelist)->prepareToAppendValues();
}
else {
// sets <vcount>
facelist->setVCountList(vcount_list);
((COLLADASW::Polylist *)facelist)-> prepareToAppendValues();
}
}
static void finishList(bool is_triangulated, COLLADASW::PrimitivesBase *facelist)
{
if (is_triangulated) {
((COLLADASW::Triangles *)facelist)->finish();
}
else {
((COLLADASW::Polylist *)facelist)->finish();
}
}
COLLADASW::PrimitivesBase *getFacelist(bool is_triangulated, COLLADASW::StreamWriter *mSW)
{
COLLADASW::PrimitivesBase *facelist;
if (is_triangulated)
{
facelist = new COLLADASW::Triangles(mSW);
}
else {
facelist = new COLLADASW::Polylist(mSW);
}
return facelist;
}
// Export meshes with Materials
void GeometryExporter::createPolylist(short material_index,
bool has_uvs,
bool has_color,
Object *ob,
Mesh *me,
std::string& geom_id,
std::vector<BCPolygonNormalsIndices>& norind)
{
MPoly *mpolys = me->mpoly;
MLoop *mloops = me->mloop;
int totpolys = me->totpoly;
// <vcount>
int i;
int faces_in_polylist = 0;
std::vector<unsigned long> vcount_list;
bool is_triangulated = true;
// count faces with this material
for (i = 0; i < totpolys; i++) {
MPoly *p = &mpolys[i];
if (p->mat_nr == material_index) {
faces_in_polylist++;
vcount_list.push_back(p->totloop);
if (p->totloop != 3) {
is_triangulated = false;
}
}
}
// no faces using this material
if (faces_in_polylist == 0) {
fprintf(stderr, "%s: material with index %d is not used.\n", id_name(ob).c_str(), material_index);
return;
}
Material *ma = ob->totcol ? give_current_material(ob, material_index + 1) : NULL;
COLLADASW::PrimitivesBase *facelist = getFacelist(is_triangulated, mSW);
// sets count attribute in <polylist>
facelist->setCount(faces_in_polylist);
// sets material name
if (ma) {
std::string material_id = get_material_id(ma);
std::ostringstream ostr;
ostr << translate_id(material_id);
facelist->setMaterial(ostr.str());
}
COLLADASW::InputList &til = facelist->getInputList();
// creates <input> in <polylist> for vertices
COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX), 0);
// creates <input> in <polylist> for normals
COLLADASW::Input input2(COLLADASW::InputSemantic::NORMAL, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL), 1);
til.push_back(input1);
til.push_back(input2);
// if mesh has uv coords writes <input> for TEXCOORD
int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE)-1;
for (i = 0; i < num_layers; i++) {
if (!this->export_settings->active_uv_only || i == active_uv_index) {
std::string uv_name(bc_get_uvlayer_name(me, i));
std::string effective_id = geom_id; // (uv_name == "") ? geom_id : uv_name;
std::string layer_id = makeTexcoordSourceId(
effective_id,
i, this->export_settings->active_uv_only);
/* Note: the third parameter denotes the offset of TEXCOORD in polylist elements
For now this is always 2 (This may change sometime/maybe)
*/
COLLADASW::Input input3(COLLADASW::InputSemantic::TEXCOORD,
makeUrl(layer_id),
2, // this is only until we have optimized UV sets
(this->export_settings->active_uv_only) ? 0 : i // only_active_uv exported -> we have only one set
);
til.push_back(input3);
}
}
int totlayer_mcol = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
if (totlayer_mcol > 0) {
int map_index = 0;
for (int a = 0; a < totlayer_mcol; a++) {
char *layer_name = bc_CustomData_get_layer_name(&me->ldata, CD_MLOOPCOL, a);
COLLADASW::Input input4(COLLADASW::InputSemantic::COLOR,
makeUrl(makeVertexColorSourceId(geom_id, layer_name)),
(has_uvs) ? 3 : 2, // all color layers have same index order
map_index // set number equals color map index
);
til.push_back(input4);
map_index++;
}
}
// performs the actual writing
prepareToAppendValues(is_triangulated, facelist, vcount_list);
// <p>
int texindex = 0;
for (i = 0; i < totpolys; i++) {
MPoly *p = &mpolys[i];
int loop_count = p->totloop;
if (p->mat_nr == material_index) {
MLoop *l = &mloops[p->loopstart];
BCPolygonNormalsIndices normal_indices = norind[i];
for (int j = 0; j < loop_count; j++) {
facelist->appendValues(l[j].v);
facelist->appendValues(normal_indices[j]);
if (has_uvs)
facelist->appendValues(texindex + j);
if (has_color)
facelist->appendValues(texindex + j);
}
}
texindex += loop_count;
}
finishList(is_triangulated, facelist);
delete facelist;
}
void GeometryExporter::createPolylists(std::set<Image *> uv_images,
bool has_uvs,
bool has_color,
Object *ob,
Mesh *me,
std::string& geom_id,
std::vector<BCPolygonNormalsIndices>& norind)
{
std::set<Image *>::iterator uv_images_iter;
for (uv_images_iter = uv_images.begin();
uv_images_iter != uv_images.end();
uv_images_iter++)
{
Image *ima = *uv_images_iter;
std::string imageid(id_name(ima));
createPolylist(imageid, has_uvs,
has_color,
ob,
me,
geom_id,
norind);
}
/* We msut add an additional collector for the case when
* some parts of the object are not textured at all.
* The next call creates a polylist for all untextured polygons
*/
createPolylist("", has_uvs,
has_color,
ob,
me,
geom_id,
norind);
}
/* ===========================================================================
* Export Meshes with UV Textures (export as materials, see also in
* effectExporter and MaterialExporter)
*
* If imageid is the empty string, then collect only untextured polygons
* =========================================================================== */
void GeometryExporter::createPolylist(std::string imageid,
bool has_uvs,
bool has_color,
Object *ob,
Mesh *me,
std::string& geom_id,
std::vector<BCPolygonNormalsIndices>& norind)
{
MPoly *mpolys = me->mpoly;
MLoop *mloops = me->mloop;
MTexPoly *mtpolys = me->mtpoly;
int totpolys = me->totpoly;
// <vcount>
int i;
int faces_in_polylist = 0;
std::vector<unsigned long> vcount_list;
bool is_triangulated = true;
// count faces with this material
for (i = 0; i < totpolys; i++) {
MTexPoly *tp = &mtpolys[i];
MPoly *p = &mpolys[i];
std::string tpageid = (mtpolys && tp->tpage) ? id_name(tp->tpage) : "";
if (tpageid == imageid) {
faces_in_polylist++;
vcount_list.push_back(p->totloop);
if (p->totloop != 3) {
is_triangulated = false;
}
}
}
// no faces using this imageid
if (faces_in_polylist == 0) {
if (imageid != "")
fprintf(stderr, "%s: Image %s is not used.\n", id_name(ob).c_str(), imageid.c_str());
return;
}
COLLADASW::PrimitivesBase *facelist = getFacelist(is_triangulated, mSW);
// sets count attribute in <polylist>
facelist->setCount(faces_in_polylist);
if (imageid != "") {
// sets material name
std::string material_id = get_material_id_from_id(imageid);
std::ostringstream ostr;
ostr << translate_id(material_id);
facelist->setMaterial(ostr.str());
}
COLLADASW::InputList &til = facelist->getInputList();
// creates <input> in <polylist> for vertices
COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX), 0);
// creates <input> in <polylist> for normals
COLLADASW::Input input2(COLLADASW::InputSemantic::NORMAL, getUrlBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL), 1);
til.push_back(input1);
til.push_back(input2);
// if mesh has uv coords writes <input> for TEXCOORD
int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE) - 1;
for (i = 0; i < num_layers; i++) {
if (!this->export_settings->active_uv_only || i == active_uv_index) {
std::string uv_name(bc_get_uvlayer_name(me, i));
std::string effective_id = geom_id; // (uv_name == "") ? geom_id : uv_name;
std::string layer_id = makeTexcoordSourceId(
effective_id,
i, this->export_settings->active_uv_only);
/* Note: the third parameter denotes the offset of TEXCOORD in polylist elements
For now this is always 2 (This may change sometime/maybe)
*/
COLLADASW::Input input3(COLLADASW::InputSemantic::TEXCOORD,
makeUrl(layer_id),
2, // this is only until we have optimized UV sets
(this->export_settings->active_uv_only) ? 0 : i // only_active_uv exported -> we have only one set
);
til.push_back(input3);
}
}
int totlayer_mcol = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
if (totlayer_mcol > 0) {
int map_index = 0;
for (int a = 0; a < totlayer_mcol; a++) {
char *layer_name = bc_CustomData_get_layer_name(&me->ldata, CD_MLOOPCOL, a);
COLLADASW::Input input4(COLLADASW::InputSemantic::COLOR,
makeUrl(makeVertexColorSourceId(geom_id, layer_name)),
(has_uvs) ? 3 : 2, // all color layers have same index order
map_index // set number equals color map index
);
til.push_back(input4);
map_index++;
}
}
// performs the actual writing
prepareToAppendValues(is_triangulated, facelist, vcount_list);
// <p>
int texindex = 0;
for (i = 0; i < totpolys; i++) {
MTexPoly *tp = &mtpolys[i];
MPoly *p = &mpolys[i];
int loop_count = p->totloop;
std::string tpageid = (mtpolys && tp->tpage) ? id_name(tp->tpage) : "";
if (tpageid == imageid) {
MLoop *l = &mloops[p->loopstart];
BCPolygonNormalsIndices normal_indices = norind[i];
for (int j = 0; j < loop_count; j++) {
facelist->appendValues(l[j].v);
facelist->appendValues(normal_indices[j]);
if (has_uvs)
facelist->appendValues(texindex + j);
if (has_color)
facelist->appendValues(texindex + j);
}
}
texindex += loop_count;
}
finishList(is_triangulated, facelist);
delete facelist;
}
// creates <source> for positions
void GeometryExporter::createVertsSource(std::string geom_id, Mesh *me)
{
#if 0
int totverts = dm->getNumVerts(dm);
MVert *verts = dm->getVertArray(dm);
#endif
int totverts = me->totvert;
MVert *verts = me->mvert;
COLLADASW::FloatSourceF source(mSW);
source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::POSITION) +
ARRAY_ID_SUFFIX);
source.setAccessorCount(totverts);
source.setAccessorStride(3);
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
param.push_back("X");
param.push_back("Y");
param.push_back("Z");
/* main function, it creates <source id = "">, <float_array id = ""
* count = ""> */
source.prepareToAppendValues();
//appends data to <float_array>
int i = 0;
for (i = 0; i < totverts; i++) {
source.appendValues(verts[i].co[0], verts[i].co[1], verts[i].co[2]);
}
source.finish();
}
void GeometryExporter::createVertexColorSource(std::string geom_id, Mesh *me)
{
/* Find number of vertex color layers */
int totlayer_mcol = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
if (totlayer_mcol == 0)
return;
int map_index = 0;
for (int a = 0; a < totlayer_mcol; a++) {
map_index++;
MLoopCol *mloopcol = (MLoopCol *)CustomData_get_layer_n(&me->ldata, CD_MLOOPCOL, a);
COLLADASW::FloatSourceF source(mSW);
char *layer_name = bc_CustomData_get_layer_name(&me->ldata, CD_MLOOPCOL, a);
std::string layer_id = makeVertexColorSourceId(geom_id, layer_name);
source.setId(layer_id);
source.setNodeName(layer_name);
source.setArrayId(layer_id + ARRAY_ID_SUFFIX);
source.setAccessorCount(me->totloop);
source.setAccessorStride(4);
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
param.push_back("R");
param.push_back("G");
param.push_back("B");
param.push_back("A");
source.prepareToAppendValues();
MPoly *mpoly;
int i;
for (i = 0, mpoly = me->mpoly; i < me->totpoly; i++, mpoly++) {
MLoopCol *mlc = mloopcol + mpoly->loopstart;
for (int j = 0; j < mpoly->totloop; j++, mlc++) {
source.appendValues(
mlc->r / 255.0f,
mlc->g / 255.0f,
mlc->b / 255.0f,
mlc->a / 255.0f
);
}
}
source.finish();
}
}
std::string GeometryExporter::makeTexcoordSourceId(std::string& geom_id, int layer_index, bool is_single_layer)
{
char suffix[20];
if (is_single_layer) {
suffix[0] = '\0';
}
else {
sprintf(suffix, "-%d", layer_index);
}
return getIdBySemantics(geom_id, COLLADASW::InputSemantic::TEXCOORD) + suffix;
}
//creates <source> for texcoords
void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *me)
{
int totpoly = me->totpoly;
int totuv = me->totloop;
MPoly *mpolys = me->mpoly;
int num_layers = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
// write <source> for each layer
// each <source> will get id like meshName + "map-channel-1"
int active_uv_index = CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV);
for (int a = 0; a < num_layers; a++) {
int layer_index = CustomData_get_layer_index_n(&me->ldata, CD_MLOOPUV, a);
if (!this->export_settings->active_uv_only || layer_index == active_uv_index) {
MLoopUV *mloops = (MLoopUV *)CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, a);
COLLADASW::FloatSourceF source(mSW);
std::string active_uv_name(bc_get_active_uvlayer_name(me));
std::string effective_id = geom_id; // (active_uv_name == "") ? geom_id : active_uv_name;
std::string layer_id = makeTexcoordSourceId(
effective_id,
a,
this->export_settings->active_uv_only );
source.setId(layer_id);
source.setArrayId(layer_id + ARRAY_ID_SUFFIX);
source.setAccessorCount(totuv);
source.setAccessorStride(2);
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
param.push_back("S");
param.push_back("T");
source.prepareToAppendValues();
for (int index = 0; index < totpoly; index++) {
MPoly *mpoly = mpolys+index;
MLoopUV *mloop = mloops+mpoly->loopstart;
for (int j = 0; j < mpoly->totloop; j++) {
source.appendValues(mloop[j].uv[0],
mloop[j].uv[1]);
}
}
source.finish();
}
}
}
bool operator<(const Normal &a, const Normal &b)
{
/* only needed to sort normal vectors and find() them later in a map.*/
return a.x < b.x || (a.x == b.x && (a.y < b.y || (a.y == b.y && a.z < b.z)));
}
//creates <source> for normals
void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal>& nor)
{
#if 0
int totverts = dm->getNumVerts(dm);
MVert *verts = dm->getVertArray(dm);
#endif
COLLADASW::FloatSourceF source(mSW);
source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL));
source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL) +
ARRAY_ID_SUFFIX);
source.setAccessorCount((unsigned long)nor.size());
source.setAccessorStride(3);
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
param.push_back("X");
param.push_back("Y");
param.push_back("Z");
source.prepareToAppendValues();
std::vector<Normal>::iterator it;
for (it = nor.begin(); it != nor.end(); it++) {
Normal& n = *it;
source.appendValues(n.x, n.y, n.z);
}
source.finish();
}
void GeometryExporter::create_normals(std::vector<Normal> &normals, std::vector<BCPolygonNormalsIndices> &polygons_normals, Mesh *me)
{
std::map<Normal, unsigned int> shared_normal_indices;
int last_normal_index = -1;
MVert *verts = me->mvert;
MLoop *mloops = me->mloop;
float(*lnors)[3] = NULL;
bool use_custom_normals = false;
BKE_mesh_calc_normals_split(me);
if (CustomData_has_layer(&me->ldata, CD_NORMAL)) {
lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL);
use_custom_normals = true;
}
for (int poly_index = 0; poly_index < me->totpoly; poly_index++) {
MPoly *mpoly = &me->mpoly[poly_index];
bool use_vertex_normals = use_custom_normals || mpoly->flag & ME_SMOOTH;
if (!use_vertex_normals) {
// For flat faces use face normal as vertex normal:
float vector[3];
BKE_mesh_calc_poly_normal(mpoly, mloops+mpoly->loopstart, verts, vector);
Normal n = { vector[0], vector[1], vector[2] };
normals.push_back(n);
last_normal_index++;
}
BCPolygonNormalsIndices poly_indices;
for (int loop_index = 0; loop_index < mpoly->totloop; loop_index++) {
unsigned int loop_idx = mpoly->loopstart + loop_index;
if (use_vertex_normals) {
float normalized[3];
if (use_custom_normals) {
normalize_v3_v3(normalized, lnors[loop_idx]);
}
else {
normal_short_to_float_v3(normalized, verts[mloops[loop_index].v].no);
normalize_v3(normalized);
}
Normal n = { normalized[0], normalized[1], normalized[2] };
if (shared_normal_indices.find(n) != shared_normal_indices.end()) {
poly_indices.add_index(shared_normal_indices[n]);
}
else {
last_normal_index++;
poly_indices.add_index(last_normal_index);
shared_normal_indices[n] = last_normal_index;
normals.push_back(n);
}
}
else {
poly_indices.add_index(last_normal_index);
}
}
polygons_normals.push_back(poly_indices);
}
}
std::string GeometryExporter::getIdBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix)
{
return geom_id + getSuffixBySemantic(type) + other_suffix;
}
COLLADASW::URI GeometryExporter::getUrlBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix)
{
std::string id(getIdBySemantics(geom_id, type, other_suffix));
return COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, id);
}
COLLADASW::URI GeometryExporter::makeUrl(std::string id)
{
return COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, id);
}
|