-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathWriterNodeVisitor.cpp
More file actions
717 lines (631 loc) · 24.7 KB
/
WriterNodeVisitor.cpp
File metadata and controls
717 lines (631 loc) · 24.7 KB
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
// -*-c++-*-
/*
* FBX writer for Open Scene Graph
*
* Copyright (C) 2009
*
* Writing support added 2009 by Thibault Caporal and Sukender (Benoit Neil - http://sukender.free.fr)
*
* The Open Scene Graph (OSG) is a cross platform C++/OpenGL library for
* real-time rendering of large 3D photo-realistic models.
* The OSG homepage is http://www.openscenegraph.org/
*/
#include <climits> // required for UINT_MAX
#include <cassert>
#include <osg/CullFace>
#include <osg/MatrixTransform>
#include <osg/NodeVisitor>
#include <osg/PrimitiveSet>
#include <osgDB/FileUtils>
#include <osgDB/WriteFile>
#include "WriterNodeVisitor.h"
// Use namespace qualification to avoid static-link symbol collisions
// from multiply defined symbols.
namespace pluginfbx
{
/** writes all primitives of a primitive-set out to a stream, decomposes quads to triangles, line-strips to lines etc */
class PrimitiveIndexWriter : public osg::PrimitiveIndexFunctor
{
public:
PrimitiveIndexWriter(const osg::Geometry* geo,
ListTriangle& listTriangles,
unsigned int drawable_n,
unsigned int material) :
_drawable_n(drawable_n),
_listTriangles(listTriangles),
_modeCache(0),
_hasNormalCoords(geo->getNormalArray() != NULL),
_hasTexCoords(geo->getTexCoordArray(0) != NULL),
_geo(geo),
_lastFaceIndex(0),
_material(material),
_curNormalIndex(0),
_normalBinding(osg::Geometry::BIND_OFF),
_mesh(0)
{
_normalBinding = geo->getNormalBinding();
if (!geo->getNormalArray() || geo->getNormalArray()->getNumElements()==0)
{
_normalBinding = osg::Geometry::BIND_OFF; // Turn off binding if there is no normal data
}
reset();
}
void reset() { _curNormalIndex = 0; }
unsigned int getNextFaceIndex() { return _lastFaceIndex; }
virtual void setVertexArray(unsigned int, const osg::Vec2*) {}
virtual void setVertexArray(unsigned int, const osg::Vec3*) {}
virtual void setVertexArray(unsigned int, const osg::Vec4*) {}
virtual void setVertexArray(unsigned int, const osg::Vec2d*) {}
virtual void setVertexArray(unsigned int, const osg::Vec3d*) {}
virtual void setVertexArray(unsigned int, const osg::Vec4d*) {}
// operator for triangles
void writeTriangle(unsigned int i1, unsigned int i2, unsigned int i3)
{
Triangle triangle;
triangle.t1 = i1;
triangle.t2 = i2;
triangle.t3 = i3;
if (_normalBinding == osg::Geometry::BIND_PER_VERTEX){
triangle.normalIndex1 = i1;
triangle.normalIndex2 = i2;
triangle.normalIndex3 = i3;
}
else{
triangle.normalIndex1 = _curNormalIndex;
triangle.normalIndex2 = _curNormalIndex;
triangle.normalIndex3 = _curNormalIndex;
}
triangle.material = _material;
_listTriangles.push_back(std::make_pair(triangle, _drawable_n));
}
virtual void begin(GLenum mode)
{
_modeCache = mode;
_indexCache.clear();
}
virtual void vertex(unsigned int vert)
{
_indexCache.push_back(vert);
}
virtual void end()
{
if (!_indexCache.empty())
{
drawElements(_modeCache, _indexCache.size(), &_indexCache.front());
}
}
virtual void drawArrays(GLenum mode, GLint first, GLsizei count);
virtual void drawElements(GLenum mode, GLsizei count, const GLubyte* indices)
{
drawElementsImplementation<GLubyte>(mode, count, indices);
}
virtual void drawElements(GLenum mode, GLsizei count, const GLushort* indices)
{
drawElementsImplementation<GLushort>(mode, count, indices);
}
virtual void drawElements(GLenum mode, GLsizei count, const GLuint* indices)
{
drawElementsImplementation<GLuint>(mode, count, indices);
}
protected:
template <typename T> void drawElementsImplementation(GLenum mode, GLsizei count, const T* indices)
{
if (indices==0 || count==0) return;
typedef const T* IndexPointer;
switch (mode)
{
case GL_TRIANGLES:
{
IndexPointer ilast = indices + count;
for (IndexPointer iptr = indices; iptr < ilast; iptr+=3)
{
writeTriangle(iptr[0], iptr[1], iptr[2]);
}
break;
}
case GL_TRIANGLE_STRIP:
{
IndexPointer iptr = indices;
for (GLsizei i = 2; i < count; ++i, ++iptr)
{
if (i & 1) writeTriangle(iptr[0], iptr[2], iptr[1]);
else writeTriangle(iptr[0], iptr[1], iptr[2]);
}
break;
}
case GL_QUADS:
{
IndexPointer iptr = indices;
for (GLsizei i = 3; i < count; i += 4, iptr += 4)
{
writeTriangle(iptr[0], iptr[1], iptr[2]);
writeTriangle(iptr[0], iptr[2], iptr[3]);
}
break;
}
case GL_QUAD_STRIP:
{
IndexPointer iptr = indices;
for (GLsizei i = 3; i < count; i += 2, iptr += 2)
{
writeTriangle(iptr[0], iptr[1], iptr[2]);
writeTriangle(iptr[1], iptr[3], iptr[2]);
}
break;
}
case GL_POLYGON: // treat polygons as GL_TRIANGLE_FAN
case GL_TRIANGLE_FAN:
{
IndexPointer iptr = indices;
unsigned int first = *iptr;
++iptr;
for (GLsizei i = 2; i < count; ++i, ++iptr)
{
writeTriangle(first, iptr[0], iptr[1]);
}
break;
}
case GL_POINTS:
case GL_LINES:
case GL_LINE_STRIP:
case GL_LINE_LOOP:
// Not handled
break;
default:
// uhm should never come to this point :)
break;
}
if (_normalBinding == osg::Geometry::BIND_PER_PRIMITIVE_SET) ++_curNormalIndex;
}
private:
PrimitiveIndexWriter& operator = (const PrimitiveIndexWriter&); // { return *this; }
unsigned int _drawable_n;
ListTriangle& _listTriangles;
GLenum _modeCache;
std::vector<GLuint> _indexCache;
bool _hasNormalCoords, _hasTexCoords;
const osg::Geometry* _geo;
unsigned int _lastFaceIndex;
int _material;
unsigned int _curNormalIndex;
osg::Geometry::AttributeBinding _normalBinding;
FbxMesh* _mesh;
};
void PrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count)
{
unsigned int pos=first;
switch (mode)
{
case GL_TRIANGLES:
for (GLsizei i = 2; i < count; i += 3, pos += 3)
{
writeTriangle(pos, pos + 1, pos + 2);
}
break;
case GL_TRIANGLE_STRIP:
for (GLsizei i = 2; i < count; ++i, ++pos)
{
if (i & 1) writeTriangle(pos, pos + 2, pos + 1);
else writeTriangle(pos, pos + 1, pos + 2);
}
break;
case GL_QUADS:
for (GLsizei i = 3; i < count; i += 4, pos += 4)
{
writeTriangle(pos, pos + 1, pos + 2);
writeTriangle(pos, pos + 2, pos + 3);
}
break;
case GL_QUAD_STRIP:
for (GLsizei i = 3; i < count; i += 2, pos += 2)
{
writeTriangle(pos, pos + 1, pos + 2);
writeTriangle(pos + 1, pos + 3, pos + 2);
}
break;
case GL_POLYGON: // treat polygons as GL_TRIANGLE_FAN
case GL_TRIANGLE_FAN:
pos = first + 1;
for (GLsizei i = 2; i < count; ++i, ++pos)
{
writeTriangle(first, pos, pos+1);
}
break;
case GL_POINTS:
case GL_LINES:
case GL_LINE_STRIP:
case GL_LINE_LOOP:
default:
OSG_WARN << "WriterNodeVisitor :: can't handle mode " << mode << std::endl;
break;
}
if (_normalBinding == osg::Geometry::BIND_PER_PRIMITIVE_SET) ++_curNormalIndex;
}
WriterNodeVisitor::Material::Material(WriterNodeVisitor& writerNodeVisitor,
osgDB::ExternalFileWriter & externalWriter,
const osg::StateSet* stateset,
const osg::Material* mat,
const osg::Texture* tex,
FbxManager* pSdkManager,
const osgDB::ReaderWriter::Options * options,
int index) :
_fbxMaterial(NULL),
_fbxTexture(NULL),
_index(index),
_osgImage(NULL)
{
osg::Vec4 diffuse(1,1,1,1),
ambient(0.2,0.2,0.2,1),
specular(0,0,0,1),
emission(1,1,1,1);
float shininess(0);
float transparency(0);
if (mat)
{
assert(stateset);
diffuse = mat->getDiffuse(osg::Material::FRONT);
ambient = mat->getAmbient(osg::Material::FRONT);
specular = mat->getSpecular(osg::Material::FRONT);
shininess = mat->getShininess(osg::Material::FRONT);
emission = mat->getEmission(osg::Material::FRONT);
transparency = 1 - diffuse.w();
const osg::StateAttribute* attribute = stateset->getAttribute(osg::StateAttribute::CULLFACE);
if (attribute)
{
assert(dynamic_cast<const osg::CullFace*>(attribute));
osg::CullFace::Mode mode = static_cast<const osg::CullFace*>(attribute)->getMode();
if (mode == osg::CullFace::FRONT)
{
OSG_WARN << "FBX Writer: Reversed face (culled FRONT) not supported yet." << std::endl;
}
else if (mode != osg::CullFace::BACK)
{
assert(mode == osg::CullFace::FRONT_AND_BACK);
OSG_WARN << "FBX Writer: Invisible face (culled FRONT_AND_BACK) not supported yet." << std::endl;
}
}
_fbxMaterial = FbxSurfacePhong::Create(pSdkManager, mat->getName().c_str());
if (_fbxMaterial)
{
_fbxMaterial->DiffuseFactor.Set(1);
_fbxMaterial->Diffuse.Set(FbxDouble3(
diffuse.x(),
diffuse.y(),
diffuse.z()));
_fbxMaterial->TransparencyFactor.Set(transparency);
_fbxMaterial->Ambient.Set(FbxDouble3(
ambient.x(),
ambient.y(),
ambient.z()));
_fbxMaterial->Emissive.Set(FbxDouble3(
emission.x(),
emission.y(),
emission.z()));
_fbxMaterial->Specular.Set(FbxDouble3(
specular.x(),
specular.y(),
specular.z()));
_fbxMaterial->Shininess.Set(shininess);
}
}
if (tex && tex->getImage(0))
{
_osgImage = tex->getImage(0);
std::string relativePath;
externalWriter.write(*_osgImage, options, NULL, &relativePath);
_fbxTexture = FbxFileTexture::Create(pSdkManager, relativePath.c_str());
_fbxTexture->SetFileName(relativePath.c_str());
// Create a FBX material if needed
if (!_fbxMaterial)
{
_fbxMaterial = FbxSurfacePhong::Create(pSdkManager, relativePath.c_str());
}
// Connect texture to material's diffuse
// Note there should be no reason FbxSurfacePhong::Create() would return NULL, but as previous code made this security test, here we keep the same way.
if (_fbxMaterial)
{
_fbxMaterial->Diffuse.ConnectSrcObject(_fbxTexture);
}
}
}
int WriterNodeVisitor::processStateSet(const osg::StateSet* ss)
{
//OSG_ALWAYS << "Trying Adding " << ss->getAttribute(osg::StateAttribute::MATERIAL)->getName() << std::endl;
MaterialMap::iterator itr = _materialMap.find(MaterialMap::key_type(ss));
if (itr != _materialMap.end())
{
if (itr->second.getIndex() < 0)
itr->second.setIndex(_lastMaterialIndex++);
return itr->second.getIndex();
}
const osg::Material* mat = dynamic_cast<const osg::Material*>(ss->getAttribute(osg::StateAttribute::MATERIAL));
const osg::Texture* tex = dynamic_cast<const osg::Texture*>(ss->getTextureAttribute(0, osg::StateAttribute::TEXTURE));
if (mat || tex)
{
int matNum = _lastMaterialIndex;
_materialMap.insert(MaterialMap::value_type(MaterialMap::key_type(ss),
Material(*this, _externalWriter, ss, mat, tex, _pSdkManager, _options, matNum)));
++_lastMaterialIndex;
return matNum;
}
return -1;
}
unsigned int addPolygon(MapIndices & index_vert, unsigned int vertIndex, unsigned int normIndex, unsigned int drawableNum)
{
VertexIndex vert(vertIndex, drawableNum, normIndex);
MapIndices::iterator itIndex = index_vert.find(vert);
if (itIndex == index_vert.end())
{
unsigned int indexMesh = index_vert.size();
index_vert.insert(std::make_pair(vert, indexMesh));
return indexMesh;
}
return itIndex->second;
}
void addPolygon(FbxMesh * mesh, MapIndices & index_vert, const Triangle & tri, unsigned int drawableNum)
{
mesh->AddPolygon(addPolygon(index_vert, tri.t1, tri.normalIndex1, drawableNum));
mesh->AddPolygon(addPolygon(index_vert, tri.t2, tri.normalIndex2, drawableNum));
mesh->AddPolygon(addPolygon(index_vert, tri.t3, tri.normalIndex3, drawableNum));
}
void
WriterNodeVisitor::setLayerTextureAndMaterial(FbxMesh* mesh)
{
FbxLayerElementTexture* lTextureDiffuseLayer = FbxLayerElementTexture::Create(mesh, "Diffuse");
lTextureDiffuseLayer->SetMappingMode(FbxLayerElement::eByPolygon);
lTextureDiffuseLayer->SetReferenceMode(FbxLayerElement::eIndexToDirect);
FbxLayerElementMaterial* lMaterialLayer = FbxLayerElementMaterial::Create(mesh, "materialLayer");
lMaterialLayer->SetMappingMode(FbxLayerElement::eByPolygon);
lMaterialLayer->SetReferenceMode(FbxLayerElement::eIndexToDirect);
lTextureDiffuseLayer->GetDirectArray().SetCount(_lastMaterialIndex);
lMaterialLayer->mDirectArray->SetCount(_lastMaterialIndex);
for (MaterialMap::iterator it = _materialMap.begin(); it != _materialMap.end(); ++it)
{
if (it->second.getIndex() != -1)
{
FbxSurfaceMaterial* lMaterial = it->second.getFbxMaterial();
FbxFileTexture* lTexture = it->second.getFbxTexture();
lTextureDiffuseLayer->GetDirectArray().SetAt(it->second.getIndex(), lTexture);
lMaterialLayer->mDirectArray->SetAt(it->second.getIndex(), lMaterial);
}
}
mesh->GetLayer(0)->SetMaterials(lMaterialLayer);
mesh->GetLayer(0)->SetTextures(FbxLayerElement::eTextureDiffuse, lTextureDiffuseLayer);
}
void
WriterNodeVisitor::setControlPointAndNormalsAndUV(const GeometryList& geometryList,
MapIndices& index_vert,
bool texcoords,
FbxMesh* mesh)
{
mesh->InitControlPoints(index_vert.size());
FbxLayerElementNormal* lLayerElementNormal= FbxLayerElementNormal::Create(mesh, "");
// For now, FBX writer only supports normals bound per vertices
lLayerElementNormal->SetMappingMode(FbxLayerElement::eByControlPoint);
lLayerElementNormal->SetReferenceMode(FbxLayerElement::eDirect);
lLayerElementNormal->GetDirectArray().SetCount(index_vert.size());
mesh->GetLayer(0)->SetNormals(lLayerElementNormal);
FbxLayerElementUV* lUVDiffuseLayer = FbxLayerElementUV::Create(mesh, "DiffuseUV");
if (texcoords)
{
lUVDiffuseLayer->SetMappingMode(FbxLayerElement::eByControlPoint);
lUVDiffuseLayer->SetReferenceMode(FbxLayerElement::eDirect);
lUVDiffuseLayer->GetDirectArray().SetCount(index_vert.size());
mesh->GetLayer(0)->SetUVs(lUVDiffuseLayer, FbxLayerElement::eTextureDiffuse);
}
for (MapIndices::iterator it = index_vert.begin(); it != index_vert.end(); ++it)
{
const osg::Geometry* pGeometry = geometryList[it->first.drawableIndex];
unsigned int vertexIndex = it->first.vertexIndex;
unsigned int normalIndex = it->first.normalIndex;
const osg::Array * basevecs = pGeometry->getVertexArray();
assert(basevecs);
if (!basevecs || basevecs->getNumElements()==0)
{
//OSG_NOTIFY()
continue;
}
FbxVector4 vertex;
if (basevecs->getType() == osg::Array::Vec3ArrayType)
{
const osg::Vec3 & vec = (*static_cast<const osg::Vec3Array *>(basevecs))[vertexIndex];
vertex.Set(vec.x(), vec.y(), vec.z());
}
else if (basevecs->getType() == osg::Array::Vec3dArrayType)
{
const osg::Vec3d & vec = (*static_cast<const osg::Vec3dArray *>(basevecs))[vertexIndex];
vertex.Set(vec.x(), vec.y(), vec.z());
}
else
{
OSG_NOTIFY(osg::FATAL) << "Vertex array is not Vec3 or Vec3d. Not implemented" << std::endl;
throw "Vertex array is not Vec3 or Vec3d. Not implemented";
//_succeeded = false;
//return;
}
mesh->SetControlPointAt(vertex, it->second);
const osg::Array * basenormals = pGeometry->getNormalArray();
if (basenormals && basenormals->getNumElements()>0)
{
FbxVector4 normal;
if (basenormals->getType() == osg::Array::Vec3ArrayType)
{
const osg::Vec3 & vec = (*static_cast<const osg::Vec3Array *>(basenormals))[normalIndex];
normal.Set(vec.x(), vec.y(), vec.z(), 0);
}
else if (basenormals->getType() == osg::Array::Vec3dArrayType)
{
const osg::Vec3d & vec = (*static_cast<const osg::Vec3dArray *>(basenormals))[normalIndex];
normal.Set(vec.x(), vec.y(), vec.z(), 0);
}
else
{
OSG_NOTIFY(osg::FATAL) << "Normal array is not Vec3 or Vec3d. Not implemented" << std::endl;
throw "Normal array is not Vec3 or Vec3d. Not implemented";
//_succeeded = false;
//return;
}
//switch (pGeometry->getNormalBinding())
//{
//case osg::Geometry::BIND_PER_PRIMITIVE_SET:
//case osg::Geometry::BIND_PER_VERTEX:
// break;
//}
lLayerElementNormal->GetDirectArray().SetAt(it->second, normal);
}
if (texcoords)
{
const osg::Array * basetexcoords = pGeometry->getTexCoordArray(0);
if (basetexcoords && basetexcoords->getNumElements()>0)
{
FbxVector2 texcoord;
if (basetexcoords->getType() == osg::Array::Vec2ArrayType)
{
const osg::Vec2 & vec = (*static_cast<const osg::Vec2Array *>(basetexcoords))[vertexIndex];
texcoord.Set(vec.x(), vec.y());
}
else if (basetexcoords->getType() == osg::Array::Vec2dArrayType)
{
const osg::Vec2d & vec = (*static_cast<const osg::Vec2dArray *>(basetexcoords))[vertexIndex];
texcoord.Set(vec.x(), vec.y());
}
else
{
OSG_NOTIFY(osg::FATAL) << "Texture coords array is not Vec2 or Vec2d. Not implemented" << std::endl;
throw "Texture coords array is not Vec2 or Vec2d. Not implemented";
//_succeeded = false;
//return;
}
lUVDiffuseLayer->GetDirectArray().SetAt(it->second, texcoord);
}
}
}
}
void WriterNodeVisitor::buildFaces(const std::string& name,
const GeometryList& geometryList,
ListTriangle& listTriangles,
bool texcoords)
{
MapIndices index_vert;
FbxMesh* mesh = FbxMesh::Create(_pSdkManager, name.c_str());
_curFbxNode->AddNodeAttribute(mesh);
_curFbxNode->SetShadingMode(FbxNode::eTextureShading);
FbxLayer* lLayer = mesh->GetLayer(0);
if (lLayer == NULL)
{
mesh->CreateLayer();
lLayer = mesh->GetLayer(0);
}
setLayerTextureAndMaterial(mesh);
lLayer->GetTextures(FbxLayerElement::eTextureDiffuse)->GetIndexArray().SetCount(listTriangles.size());
lLayer->GetMaterials()->GetIndexArray().SetCount(listTriangles.size());
unsigned int i = 0;
for (ListTriangle::iterator it = listTriangles.begin(); it != listTriangles.end(); ++it, ++i) //Go through the triangle list to define meshs
{
if (it->first.material == -1)
{
mesh->BeginPolygon();
}
else
{
mesh->BeginPolygon(i);
lLayer->GetTextures(FbxLayerElement::eTextureDiffuse)->GetIndexArray().SetAt(i, it->first.material);
lLayer->GetMaterials()->GetIndexArray().SetAt(i, it->first.material);
}
addPolygon(mesh, index_vert, it->first, it->second);
mesh->EndPolygon();
}
setControlPointAndNormalsAndUV(geometryList, index_vert, texcoords, mesh);
_geometryList.clear();
_listTriangles.clear();
_texcoords = false;
_drawableNum = 0;
for (MaterialMap::iterator it = _materialMap.begin(); it != _materialMap.end(); ++it)
it->second.setIndex(-1);
_lastMaterialIndex = 0;
}
void WriterNodeVisitor::createListTriangle(const osg::Geometry* geo,
ListTriangle& listTriangles,
bool& texcoords,
unsigned int drawable_n)
{
unsigned int nbVertices = 0;
{
const osg::Array * vecs = geo->getVertexArray();
if (vecs)
{
nbVertices = vecs->getNumElements();
// Texture coords
const osg::Array * texvec = geo->getTexCoordArray(0);
if (texvec)
{
unsigned int nb = texvec->getNumElements();
if (nb == nbVertices) texcoords = true;
else
{
OSG_WARN << "There are more/less texture coords than vertices! Ignoring texture coords.";
}
}
}
}
if (nbVertices==0) return;
int material = processStateSet(_currentStateSet.get());
PrimitiveIndexWriter pif(geo, listTriangles, drawable_n, material);
for (unsigned int iPrimSet = 0; iPrimSet < geo->getNumPrimitiveSets(); ++iPrimSet) //Fill the Triangle List
{
const osg::PrimitiveSet* ps = geo->getPrimitiveSet(iPrimSet);
const_cast<osg::PrimitiveSet*>(ps)->accept(pif);
}
}
void WriterNodeVisitor::apply(osg::Geometry& geometry)
{
// retrieved from the geometry.
_geometryList.push_back(&geometry);
pushStateSet(geometry.getStateSet());
createListTriangle(&geometry, _listTriangles, _texcoords, _drawableNum++);
popStateSet(geometry.getStateSet());
osg::NodeVisitor::traverse(geometry);
if (getNodePath().size() == 1)
buildFaces(geometry.getName(), _geometryList, _listTriangles, _texcoords);
}
void WriterNodeVisitor::apply(osg::Group& node)
{
if (_firstNodeProcessed)
{
FbxNode* parent = _curFbxNode;
FbxNode* nodeFBX = FbxNode::Create(_pSdkManager, node.getName().empty() ? "DefaultName" : node.getName().c_str());
_curFbxNode->AddChild(nodeFBX);
_curFbxNode = nodeFBX;
traverse(node);
if (_listTriangles.size() > 0)
buildFaces(node.getName(), _geometryList, _listTriangles, _texcoords);
_curFbxNode = parent;
}
else
{
//ignore the root node to maintain same hierarchy
_firstNodeProcessed = true;
traverse(node);
}
}
void WriterNodeVisitor::apply(osg::MatrixTransform& node)
{
FbxNode* parent = _curFbxNode;
_curFbxNode = FbxNode::Create(_pSdkManager, node.getName().empty() ? "DefaultName" : node.getName().c_str());
parent->AddChild(_curFbxNode);
const osg::Matrix& matrix = node.getMatrix();
osg::Vec3d pos, scl;
osg::Quat rot, so;
matrix.decompose(pos, rot, scl, so);
_curFbxNode->LclTranslation.Set(FbxDouble3(pos.x(), pos.y(), pos.z()));
_curFbxNode->LclScaling.Set(FbxDouble3(scl.x(), scl.y(), scl.z()));
FbxAMatrix mat;
FbxQuaternion q(rot.x(), rot.y(), rot.z(), rot.w());
mat.SetQ(q);
FbxVector4 vec4 = mat.GetR();
_curFbxNode->LclRotation.Set(FbxDouble3(vec4[0], vec4[1], vec4[2]));
traverse(node);
_curFbxNode = parent;
}
// end namespace pluginfbx
}