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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
|
/**
* @file asset.cpp
* @brief LL GLTF Implementation
*
* $LicenseInfo:firstyear=2024&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2024, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "../llviewerprecompiledheaders.h"
#include "asset.h"
#include "llvolumeoctree.h"
#include "../llviewershadermgr.h"
#include "../llviewercontrol.h"
#include "../llviewertexturelist.h"
using namespace LL::GLTF;
namespace LL
{
namespace GLTF
{
template <typename T, typename U>
void copy(const std::vector<T>& src, std::vector<U>& dst)
{
dst.resize(src.size());
for (U32 i = 0; i < src.size(); ++i)
{
copy(src[i], dst[i]);
}
}
void copy(const Node& src, tinygltf::Node& dst)
{
if (src.mMatrixValid)
{
if (!src.mMatrix.asMatrix4().isIdentity())
{
dst.matrix.resize(16);
for (U32 i = 0; i < 16; ++i)
{
dst.matrix[i] = src.mMatrix.getF32ptr()[i];
}
}
}
else if (src.mTRSValid)
{
if (!src.mRotation.equals(glh::quaternionf::identity(), FLT_EPSILON))
{
dst.rotation.resize(4);
for (U32 i = 0; i < 4; ++i)
{
dst.rotation[i] = src.mRotation.get_value()[i];
}
}
if (src.mTranslation != glh::vec3f(0.f, 0.f, 0.f))
{
dst.translation.resize(3);
for (U32 i = 0; i < 3; ++i)
{
dst.translation[i] = src.mTranslation.v[i];
}
}
if (src.mScale != glh::vec3f(1.f, 1.f, 1.f))
{
dst.scale.resize(3);
for (U32 i = 0; i < 3; ++i)
{
dst.scale[i] = src.mScale.v[i];
}
}
}
dst.children = src.mChildren;
dst.mesh = src.mMesh;
dst.skin = src.mSkin;
dst.name = src.mName;
}
void copy(const Scene& src, tinygltf::Scene& dst)
{
dst.nodes = src.mNodes;
dst.name = src.mName;
}
void copy(const Primitive& src, tinygltf::Primitive& dst)
{
for (auto& attrib : src.mAttributes)
{
dst.attributes[attrib.first] = attrib.second;
}
dst.indices = src.mIndices;
dst.material = src.mMaterial;
dst.mode = src.mMode;
}
void copy(const Mesh& src, tinygltf::Mesh& mesh)
{
copy(src.mPrimitives, mesh.primitives);
mesh.weights = src.mWeights;
mesh.name = src.mName;
}
void copy(const Material::TextureInfo& src, tinygltf::TextureInfo& dst)
{
dst.index = src.mIndex;
dst.texCoord = src.mTexCoord;
}
void copy(const Material::OcclusionTextureInfo& src, tinygltf::OcclusionTextureInfo& dst)
{
dst.index = src.mIndex;
dst.texCoord = src.mTexCoord;
dst.strength = src.mStrength;
}
void copy(const Material::NormalTextureInfo& src, tinygltf::NormalTextureInfo& dst)
{
dst.index = src.mIndex;
dst.texCoord = src.mTexCoord;
dst.scale = src.mScale;
}
void copy(const Material::PbrMetallicRoughness& src, tinygltf::PbrMetallicRoughness& dst)
{
dst.baseColorFactor = { src.mBaseColorFactor.v[0], src.mBaseColorFactor.v[1], src.mBaseColorFactor.v[2], src.mBaseColorFactor.v[3] };
copy(src.mBaseColorTexture, dst.baseColorTexture);
dst.metallicFactor = src.mMetallicFactor;
dst.roughnessFactor = src.mRoughnessFactor;
copy(src.mMetallicRoughnessTexture, dst.metallicRoughnessTexture);
}
void copy(const Material& src, tinygltf::Material& material)
{
material.name = src.mName;
material.emissiveFactor = { src.mEmissiveFactor.v[0], src.mEmissiveFactor.v[1], src.mEmissiveFactor.v[2] };
copy(src.mPbrMetallicRoughness, material.pbrMetallicRoughness);
copy(src.mNormalTexture, material.normalTexture);
copy(src.mEmissiveTexture, material.emissiveTexture);
}
void copy(const Texture& src, tinygltf::Texture& texture)
{
texture.sampler = src.mSampler;
texture.source = src.mSource;
texture.name = src.mName;
}
void copy(const Sampler& src, tinygltf::Sampler& sampler)
{
sampler.magFilter = src.mMagFilter;
sampler.minFilter = src.mMinFilter;
sampler.wrapS = src.mWrapS;
sampler.wrapT = src.mWrapT;
sampler.name = src.mName;
}
void copy(const Skin& src, tinygltf::Skin& skin)
{
skin.joints = src.mJoints;
skin.inverseBindMatrices = src.mInverseBindMatrices;
skin.skeleton = src.mSkeleton;
skin.name = src.mName;
}
void copy(const Accessor& src, tinygltf::Accessor& accessor)
{
accessor.bufferView = src.mBufferView;
accessor.byteOffset = src.mByteOffset;
accessor.componentType = src.mComponentType;
accessor.minValues = src.mMin;
accessor.maxValues = src.mMax;
accessor.count = src.mCount;
accessor.type = src.mType;
accessor.normalized = src.mNormalized;
accessor.name = src.mName;
}
void copy(const Animation::Sampler& src, tinygltf::AnimationSampler& sampler)
{
sampler.input = src.mInput;
sampler.output = src.mOutput;
sampler.interpolation = src.mInterpolation;
}
void copy(const Animation::Channel& src, tinygltf::AnimationChannel& channel)
{
channel.sampler = src.mSampler;
channel.target_node = src.mTarget.mNode;
channel.target_path = src.mTarget.mPath;
}
void copy(const Animation& src, tinygltf::Animation& animation)
{
animation.name = src.mName;
copy(src.mSamplers, animation.samplers);
U32 channel_count = src.mRotationChannels.size() + src.mTranslationChannels.size() + src.mScaleChannels.size();
animation.channels.resize(channel_count);
U32 idx = 0;
for (U32 i = 0; i < src.mTranslationChannels.size(); ++i)
{
copy(src.mTranslationChannels[i], animation.channels[idx++]);
}
for (U32 i = 0; i < src.mRotationChannels.size(); ++i)
{
copy(src.mRotationChannels[i], animation.channels[idx++]);
}
for (U32 i = 0; i < src.mScaleChannels.size(); ++i)
{
copy(src.mScaleChannels[i], animation.channels[idx++]);
}
}
void copy(const Buffer& src, tinygltf::Buffer& buffer)
{
buffer.uri = src.mUri;
buffer.data = src.mData;
buffer.name = src.mName;
}
void copy(const BufferView& src, tinygltf::BufferView& bufferView)
{
bufferView.buffer = src.mBuffer;
bufferView.byteOffset = src.mByteOffset;
bufferView.byteLength = src.mByteLength;
bufferView.byteStride = src.mByteStride;
bufferView.target = src.mTarget;
bufferView.name = src.mName;
}
void copy(const Image& src, tinygltf::Image& image)
{
image.name = src.mName;
image.width = src.mWidth;
image.height = src.mHeight;
image.component = src.mComponent;
image.bits = src.mBits;
image.pixel_type = src.mPixelType;
image.image = src.mData;
image.bufferView = src.mBufferView;
image.mimeType = src.mMimeType;
image.uri = src.mUri;
}
void copy(const Asset & src, tinygltf::Model& dst)
{
dst.defaultScene = src.mDefaultScene;
dst.asset.copyright = src.mCopyright;
dst.asset.version = src.mVersion;
dst.asset.minVersion = src.mMinVersion;
dst.asset.generator = "Linden Lab Experimental GLTF Export";
dst.asset.extras = src.mExtras;
copy(src.mScenes, dst.scenes);
copy(src.mNodes, dst.nodes);
copy(src.mMeshes, dst.meshes);
copy(src.mMaterials, dst.materials);
copy(src.mBuffers, dst.buffers);
copy(src.mBufferViews, dst.bufferViews);
copy(src.mTextures, dst.textures);
copy(src.mSamplers, dst.samplers);
copy(src.mImages, dst.images);
copy(src.mAccessors, dst.accessors);
copy(src.mAnimations, dst.animations);
copy(src.mSkins, dst.skins);
}
}
}
void Scene::updateTransforms(Asset& asset)
{
LLMatrix4a identity;
identity.setIdentity();
for (auto& nodeIndex : mNodes)
{
Node& node = asset.mNodes[nodeIndex];
node.updateTransforms(asset, identity);
}
}
void Scene::updateRenderTransforms(Asset& asset, const LLMatrix4a& modelview)
{
for (auto& nodeIndex : mNodes)
{
Node& node = asset.mNodes[nodeIndex];
node.updateRenderTransforms(asset, modelview);
}
}
void Node::updateRenderTransforms(Asset& asset, const LLMatrix4a& modelview)
{
matMul(mMatrix, modelview, mRenderMatrix);
for (auto& childIndex : mChildren)
{
Node& child = asset.mNodes[childIndex];
child.updateRenderTransforms(asset, mRenderMatrix);
}
}
LLMatrix4a inverse(const LLMatrix4a& mat);
void Node::updateTransforms(Asset& asset, const LLMatrix4a& parentMatrix)
{
makeMatrixValid();
matMul(mMatrix, parentMatrix, mAssetMatrix);
mAssetMatrixInv = inverse(mAssetMatrix);
S32 my_index = this - &asset.mNodes[0];
for (auto& childIndex : mChildren)
{
Node& child = asset.mNodes[childIndex];
child.mParent = my_index;
child.updateTransforms(asset, mAssetMatrix);
}
}
void Asset::updateTransforms()
{
for (auto& scene : mScenes)
{
scene.updateTransforms(*this);
}
}
void Asset::updateRenderTransforms(const LLMatrix4a& modelview)
{
#if 0
// traverse hierarchy and update render transforms from scratch
for (auto& scene : mScenes)
{
scene.updateRenderTransforms(*this, modelview);
}
#else
// use mAssetMatrix to update render transforms from node list
for (auto& node : mNodes)
{
//if (node.mMesh != INVALID_INDEX)
{
matMul(node.mAssetMatrix, modelview, node.mRenderMatrix);
}
}
#endif
}
S32 Asset::lineSegmentIntersect(const LLVector4a& start, const LLVector4a& end,
LLVector4a* intersection, // return the intersection point
LLVector2* tex_coord, // return the texture coordinates of the intersection point
LLVector4a* normal, // return the surface normal at the intersection point
LLVector4a* tangent, // return the surface tangent at the intersection point
S32* primitive_hitp
)
{
S32 node_hit = -1;
S32 primitive_hit = -1;
LLVector4a local_start;
LLVector4a asset_end = end;
LLVector4a local_end;
LLVector4a p;
for (auto& node : mNodes)
{
if (node.mMesh != INVALID_INDEX)
{
bool newHit = false;
// transform start and end to this node's local space
node.mAssetMatrixInv.affineTransform(start, local_start);
node.mAssetMatrixInv.affineTransform(asset_end, local_end);
Mesh& mesh = mMeshes[node.mMesh];
for (auto& primitive : mesh.mPrimitives)
{
const LLVolumeTriangle* tri = primitive.lineSegmentIntersect(local_start, local_end, &p, tex_coord, normal, tangent);
if (tri)
{
newHit = true;
local_end = p;
// pointer math to get the node index
node_hit = &node - &mNodes[0];
llassert(&mNodes[node_hit] == &node);
//pointer math to get the primitive index
primitive_hit = &primitive - &mesh.mPrimitives[0];
llassert(&mesh.mPrimitives[primitive_hit] == &primitive);
}
}
if (newHit)
{
// shorten line segment on hit
node.mAssetMatrix.affineTransform(p, asset_end);
// transform results back to asset space
if (intersection)
{
*intersection = asset_end;
}
if (normal || tangent)
{
LLMatrix4 normalMatrix(node.mAssetMatrixInv.getF32ptr());
normalMatrix.transpose();
LLMatrix4a norm_mat;
norm_mat.loadu((F32*)normalMatrix.mMatrix);
if (normal)
{
LLVector4a n = *normal;
F32 w = n.getF32ptr()[3];
n.getF32ptr()[3] = 0.0f;
norm_mat.affineTransform(n, *normal);
normal->getF32ptr()[3] = w;
}
if (tangent)
{
LLVector4a t = *tangent;
F32 w = t.getF32ptr()[3];
t.getF32ptr()[3] = 0.0f;
norm_mat.affineTransform(t, *tangent);
tangent->getF32ptr()[3] = w;
}
}
}
}
}
if (node_hit != -1)
{
if (primitive_hitp)
{
*primitive_hitp = primitive_hit;
}
}
return node_hit;
}
void Node::makeMatrixValid()
{
if (!mMatrixValid && mTRSValid)
{
glh::matrix4f rot;
mRotation.get_value(rot);
glh::matrix4f trans;
trans.set_translate(mTranslation);
glh::matrix4f sc;
sc.set_scale(mScale);
glh::matrix4f t;
//t = sc * rot * trans;
//t = trans * rot * sc; // best so far, still wrong on negative scale
//t = sc * trans * rot;
t = trans * sc * rot;
mMatrix.loadu(t.m);
mMatrixValid = true;
}
llassert(mMatrixValid);
}
void Node::makeTRSValid()
{
if (!mTRSValid && mMatrixValid)
{
glh::matrix4f t(mMatrix.getF32ptr());
glh::vec4f p = t.get_column(3);
mTranslation.set_value(p.v[0], p.v[1], p.v[2]);
mScale.set_value(t.get_column(0).length(), t.get_column(1).length(), t.get_column(2).length());
mRotation.set_value(t);
mTRSValid = true;
}
llassert(mTRSValid);
}
void Node::setRotation(const glh::quaternionf& q)
{
makeTRSValid();
mRotation = q;
mMatrixValid = false;
}
void Node::setTranslation(const glh::vec3f& t)
{
makeTRSValid();
mTranslation = t;
mMatrixValid = false;
}
void Node::setScale(const glh::vec3f& s)
{
makeTRSValid();
mScale = s;
mMatrixValid = false;
}
const Node& Node::operator=(const tinygltf::Node& src)
{
F32* dstMatrix = mMatrix.getF32ptr();
if (src.matrix.size() == 16)
{
// Node has a transformation matrix, just copy it
for (U32 i = 0; i < 16; ++i)
{
dstMatrix[i] = (F32)src.matrix[i];
}
mMatrixValid = true;
}
else if (!src.rotation.empty() || !src.translation.empty() || !src.scale.empty())
{
// node has rotation/translation/scale, convert to matrix
if (src.rotation.size() == 4)
{
mRotation = glh::quaternionf((F32)src.rotation[0], (F32)src.rotation[1], (F32)src.rotation[2], (F32)src.rotation[3]);
}
if (src.translation.size() == 3)
{
mTranslation = glh::vec3f((F32)src.translation[0], (F32)src.translation[1], (F32)src.translation[2]);
}
glh::vec3f scale;
if (src.scale.size() == 3)
{
mScale = glh::vec3f((F32)src.scale[0], (F32)src.scale[1], (F32)src.scale[2]);
}
else
{
mScale.set_value(1.f, 1.f, 1.f);
}
mTRSValid = true;
}
else
{
// node specifies no transformation, set to identity
mMatrix.setIdentity();
mMatrixValid = true;
}
mChildren = src.children;
mMesh = src.mesh;
mSkin = src.skin;
mName = src.name;
return *this;
}
void Asset::render(bool opaque, bool rigged)
{
if (rigged)
{
gGL.loadIdentity();
}
for (auto& node : mNodes)
{
if (node.mSkin != INVALID_INDEX)
{
if (rigged)
{
Skin& skin = mSkins[node.mSkin];
skin.uploadMatrixPalette(*this, node);
}
else
{
//skip static nodes if we're rendering rigged
continue;
}
}
else if (rigged)
{
// skip rigged nodes if we're not rendering rigged
continue;
}
if (node.mMesh != INVALID_INDEX)
{
Mesh& mesh = mMeshes[node.mMesh];
for (auto& primitive : mesh.mPrimitives)
{
if (!rigged)
{
gGL.loadMatrix((F32*)node.mRenderMatrix.mMatrix);
}
bool cull = true;
if (primitive.mMaterial != INVALID_INDEX)
{
Material& material = mMaterials[primitive.mMaterial];
if ((material.mMaterial->mAlphaMode == LLGLTFMaterial::ALPHA_MODE_BLEND) == opaque)
{
continue;
}
material.mMaterial->bind();
cull = !material.mMaterial->mDoubleSided;
}
else
{
if (!opaque)
{
continue;
}
LLFetchedGLTFMaterial::sDefault.bind();
}
LLGLDisable cull_face(!cull ? GL_CULL_FACE : 0);
primitive.mVertexBuffer->setBuffer();
if (primitive.mVertexBuffer->getNumIndices() > 0)
{
primitive.mVertexBuffer->draw(primitive.mGLMode, primitive.mVertexBuffer->getNumIndices(), 0);
}
else
{
primitive.mVertexBuffer->drawArrays(primitive.mGLMode, 0, primitive.mVertexBuffer->getNumVerts());
}
}
}
}
}
void Asset::renderOpaque()
{
render(true);
}
void Asset::renderTransparent()
{
render(false);
}
void Asset::update()
{
F32 dt = gFrameTimeSeconds - mLastUpdateTime;
if (dt > 0.f)
{
mLastUpdateTime = gFrameTimeSeconds;
if (mAnimations.size() > 0)
{
static LLCachedControl<U32> anim_idx(gSavedSettings, "GLTFAnimationIndex", 0);
static LLCachedControl<F32> anim_speed(gSavedSettings, "GLTFAnimationSpeed", 1.f);
U32 idx = llclamp(anim_idx(), 0U, mAnimations.size() - 1);
mAnimations[idx].update(*this, dt*anim_speed);
}
updateTransforms();
}
}
void Asset::allocateGLResources(const std::string& filename, const tinygltf::Model& model)
{
// do images first as materials may depend on images
for (auto& image : mImages)
{
image.allocateGLResources();
}
// do materials before meshes as meshes may depend on materials
for (U32 i = 0; i < mMaterials.size(); ++i)
{
mMaterials[i].allocateGLResources(*this);
LLTinyGLTFHelper::getMaterialFromModel(filename, model, i, mMaterials[i].mMaterial, mMaterials[i].mName, true);
}
for (auto& mesh : mMeshes)
{
mesh.allocateGLResources(*this);
}
for (auto& animation : mAnimations)
{
animation.allocateGLResources(*this);
}
for (auto& skin : mSkins)
{
skin.allocateGLResources(*this);
}
}
const Asset& Asset::operator=(const tinygltf::Model& src)
{
mVersion = src.asset.version;
mMinVersion = src.asset.minVersion;
mGenerator = src.asset.generator;
mCopyright = src.asset.copyright;
mExtras = src.asset.extras;
mDefaultScene = src.defaultScene;
mScenes.resize(src.scenes.size());
for (U32 i = 0; i < src.scenes.size(); ++i)
{
mScenes[i] = src.scenes[i];
}
mNodes.resize(src.nodes.size());
for (U32 i = 0; i < src.nodes.size(); ++i)
{
mNodes[i] = src.nodes[i];
}
mMeshes.resize(src.meshes.size());
for (U32 i = 0; i < src.meshes.size(); ++i)
{
mMeshes[i] = src.meshes[i];
}
mMaterials.resize(src.materials.size());
for (U32 i = 0; i < src.materials.size(); ++i)
{
mMaterials[i] = src.materials[i];
}
mBuffers.resize(src.buffers.size());
for (U32 i = 0; i < src.buffers.size(); ++i)
{
mBuffers[i] = src.buffers[i];
}
mBufferViews.resize(src.bufferViews.size());
for (U32 i = 0; i < src.bufferViews.size(); ++i)
{
mBufferViews[i] = src.bufferViews[i];
}
mTextures.resize(src.textures.size());
for (U32 i = 0; i < src.textures.size(); ++i)
{
mTextures[i] = src.textures[i];
}
mSamplers.resize(src.samplers.size());
for (U32 i = 0; i < src.samplers.size(); ++i)
{
mSamplers[i] = src.samplers[i];
}
mImages.resize(src.images.size());
for (U32 i = 0; i < src.images.size(); ++i)
{
mImages[i] = src.images[i];
}
mAccessors.resize(src.accessors.size());
for (U32 i = 0; i < src.accessors.size(); ++i)
{
mAccessors[i] = src.accessors[i];
}
mAnimations.resize(src.animations.size());
for (U32 i = 0; i < src.animations.size(); ++i)
{
mAnimations[i] = src.animations[i];
}
mSkins.resize(src.skins.size());
for (U32 i = 0; i < src.skins.size(); ++i)
{
mSkins[i] = src.skins[i];
}
return *this;
}
void Asset::save(tinygltf::Model& dst)
{
LL::GLTF::copy(*this, dst);
}
void Asset::decompose(const std::string& filename)
{
// get folder path
std::string folder = gDirUtilp->getDirName(filename);
// decompose images
for (auto& image : mImages)
{
image.decompose(*this, folder);
}
}
void Asset::eraseBufferView(S32 bufferView)
{
mBufferViews.erase(mBufferViews.begin() + bufferView);
for (auto& accessor : mAccessors)
{
if (accessor.mBufferView > bufferView)
{
accessor.mBufferView--;
}
}
for (auto& image : mImages)
{
if (image.mBufferView > bufferView)
{
image.mBufferView--;
}
}
}
void Image::decompose(Asset& asset, const std::string& folder)
{
std::string name = mName;
if (name.empty())
{
S32 idx = this - asset.mImages.data();
name = llformat("image_%d", idx);
}
if (mBufferView != INVALID_INDEX)
{
// save original image
BufferView& bufferView = asset.mBufferViews[mBufferView];
Buffer& buffer = asset.mBuffers[bufferView.mBuffer];
std::string extension;
if (mMimeType == "image/jpeg")
{
extension = ".jpg";
}
else if (mMimeType == "image/png")
{
extension = ".png";
}
else
{
extension = ".bin";
}
std::string filename = folder + "/" + name + "." + extension;
// set URI to non-j2c file for now, but later we'll want to reference the j2c hash
mUri = name + "." + extension;
std::ofstream file(filename, std::ios::binary);
file.write((const char*)buffer.mData.data() + bufferView.mByteOffset, bufferView.mByteLength);
buffer.erase(asset, bufferView.mByteOffset, bufferView.mByteLength);
asset.eraseBufferView(mBufferView);
}
if (!mData.empty())
{
// save j2c image
std::string filename = folder + "/" + name + ".j2c";
LLPointer<LLImageRaw> raw = new LLImageRaw(mWidth, mHeight, mComponent);
U8* data = raw->allocateData();
llassert(mData.size() == raw->getDataSize());
memcpy(data, mData.data(), mData.size());
LLViewerTextureList::createUploadFile(raw, filename, 4096);
mData.clear();
}
mWidth = -1;
mHeight = -1;
mComponent = -1;
mBits = -1;
mPixelType = -1;
mMimeType = "";
}
const Material::TextureInfo& Material::TextureInfo::operator=(const tinygltf::TextureInfo& src)
{
mIndex = src.index;
mTexCoord = src.texCoord;
return *this;
}
const Material::OcclusionTextureInfo& Material::OcclusionTextureInfo::operator=(const tinygltf::OcclusionTextureInfo& src)
{
mIndex = src.index;
mTexCoord = src.texCoord;
mStrength = src.strength;
return *this;
}
const Material::NormalTextureInfo& Material::NormalTextureInfo::operator=(const tinygltf::NormalTextureInfo& src)
{
mIndex = src.index;
mTexCoord = src.texCoord;
mScale = src.scale;
return *this;
}
const Material::PbrMetallicRoughness& Material::PbrMetallicRoughness::operator=(const tinygltf::PbrMetallicRoughness& src)
{
if (src.baseColorFactor.size() == 4)
{
mBaseColorFactor.set_value(src.baseColorFactor[0], src.baseColorFactor[1], src.baseColorFactor[2], src.baseColorFactor[3]);
}
mBaseColorTexture = src.baseColorTexture;
mMetallicFactor = src.metallicFactor;
mRoughnessFactor = src.roughnessFactor;
mMetallicRoughnessTexture = src.metallicRoughnessTexture;
return *this;
}
const Material& Material::operator=(const tinygltf::Material& src)
{
mName = src.name;
if (src.emissiveFactor.size() == 3)
{
mEmissiveFactor.set_value(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2]);
}
mPbrMetallicRoughness = src.pbrMetallicRoughness;
mNormalTexture = src.normalTexture;
mOcclusionTexture = src.occlusionTexture;
mEmissiveTexture = src.emissiveTexture;
mAlphaMode = src.alphaMode;
mAlphaCutoff = src.alphaCutoff;
mDoubleSided = src.doubleSided;
return *this;
}
void Material::allocateGLResources(Asset& asset)
{
// allocate material
mMaterial = new LLFetchedGLTFMaterial();
}
const Mesh& Mesh::operator=(const tinygltf::Mesh& src)
{
mPrimitives.resize(src.primitives.size());
for (U32 i = 0; i < src.primitives.size(); ++i)
{
mPrimitives[i] = src.primitives[i];
}
mWeights = src.weights;
mName = src.name;
return *this;
}
void Mesh::allocateGLResources(Asset& asset)
{
for (auto& primitive : mPrimitives)
{
primitive.allocateGLResources(asset);
}
}
const Scene& Scene::operator=(const tinygltf::Scene& src)
{
mNodes = src.nodes;
mName = src.name;
return *this;
}
const Texture& Texture::operator=(const tinygltf::Texture& src)
{
mSampler = src.sampler;
mSource = src.source;
mName = src.name;
return *this;
}
const Sampler& Sampler::operator=(const tinygltf::Sampler& src)
{
mMagFilter = src.magFilter;
mMinFilter = src.minFilter;
mWrapS = src.wrapS;
mWrapT = src.wrapT;
mName = src.name;
return *this;
}
void Skin::uploadMatrixPalette(Asset& asset, Node& node)
{
// prepare matrix palette
// modelview will be applied by the shader, so assume matrix palette is in asset space
std::vector<glh::matrix4f> t_mp;
t_mp.resize(mJoints.size());
for (U32 i = 0; i < mJoints.size(); ++i)
{
Node& joint = asset.mNodes[mJoints[i]];
//t_mp[i].set_value(joint.mRenderMatrix.getF32ptr());
//t_mp[i] = t_mp[i] * mInverseBindMatricesData[i];
//t_mp[i].set_value(joint.mRenderMatrix.getF32ptr());
//t_mp[i] = mInverseBindMatricesData[i] * t_mp[i];
t_mp[i].set_value(joint.mRenderMatrix.getF32ptr());
t_mp[i] = t_mp[i] * mInverseBindMatricesData[i];
}
std::vector<F32> glmp;
glmp.resize(mJoints.size() * 12);
F32* mp = glmp.data();
for (U32 i = 0; i < mJoints.size(); ++i)
{
F32* m = (F32*)t_mp[i].m;
U32 idx = i * 12;
mp[idx + 0] = m[0];
mp[idx + 1] = m[1];
mp[idx + 2] = m[2];
mp[idx + 3] = m[12];
mp[idx + 4] = m[4];
mp[idx + 5] = m[5];
mp[idx + 6] = m[6];
mp[idx + 7] = m[13];
mp[idx + 8] = m[8];
mp[idx + 9] = m[9];
mp[idx + 10] = m[10];
mp[idx + 11] = m[14];
}
LLGLSLShader::sCurBoundShaderPtr->uniformMatrix3x4fv(LLViewerShaderMgr::AVATAR_MATRIX,
mJoints.size(),
FALSE,
(GLfloat*)glmp.data());
}
|