Newer
Older
Andrey Kleshchev
committed
/**
* @file llmodelpreview.cpp
* @brief LLModelPreview class implementation
*
* $LicenseInfo:firstyear=2020&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2020, 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 "llmodelpreview.h"
Andrey Kleshchev
committed
#include "llmodelloader.h"
#include "lldaeloader.h"
#include "llfloatermodelpreview.h"
#include "llagent.h"
#include "llanimationstates.h"
#include "llcallbacklist.h"
#include "lldatapacker.h"
#include "lldrawable.h"
#include "llface.h"
#include "lliconctrl.h"
#include "llmatrix4a.h"
#include "llmeshrepository.h"
Andrey Kleshchev
committed
#include "llrender.h"
#include "llsdutil_math.h"
#include "llskinningutil.h"
#include "llstring.h"
#include "llsdserialize.h"
#include "lltoolmgr.h"
#include "llui.h"
#include "llvector4a.h"
#include "llviewercamera.h"
#include "llviewercontrol.h"
#include "llviewerobjectlist.h"
#include "llviewernetwork.h"
#include "llviewershadermgr.h"
#include "llviewertexteditor.h"
#include "llviewertexturelist.h"
#include "llvoavatar.h"
#include "pipeline.h"
// ui controls (from floater)
#include "llbutton.h"
#include "llcombobox.h"
#include "llspinctrl.h"
#include "lltabcontainer.h"
#include "lltextbox.h"
#include <boost/algorithm/string.hpp>
bool LLModelPreview::sIgnoreLoadedCallback = false;
// Extra configurability, to be exposed later in xml (LLModelPreview probably
// should become UI control at some point or get split into preview control)
static const LLColor4 PREVIEW_CANVAS_COL(0.169f, 0.169f, 0.169f, 1.f);
static const LLColor4 PREVIEW_EDGE_COL(0.4f, 0.4f, 0.4f, 1.0);
static const LLColor4 PREVIEW_BASE_COL(1.f, 1.f, 1.f, 1.f);
static const LLColor3 PREVIEW_BRIGHTNESS(0.9f, 0.9f, 0.9f);
static const F32 PREVIEW_EDGE_WIDTH(1.f);
static const LLColor4 PREVIEW_PSYH_EDGE_COL(0.f, 0.25f, 0.5f, 0.25f);
static const LLColor4 PREVIEW_PSYH_FILL_COL(0.f, 0.5f, 1.0f, 0.5f);
static const F32 PREVIEW_PSYH_EDGE_WIDTH(1.f);
static const LLColor4 PREVIEW_DEG_EDGE_COL(1.f, 0.f, 0.f, 1.f);
static const LLColor4 PREVIEW_DEG_FILL_COL(1.f, 0.f, 0.f, 0.5f);
static const F32 PREVIEW_DEG_EDGE_WIDTH(3.f);
static const F32 PREVIEW_DEG_POINT_SIZE(8.f);
static const F32 PREVIEW_ZOOM_LIMIT(10.f);
Howard Stearns
committed
static const std::string DEFAULT_PHYSICS_MESH_NAME = "default_physics_shape";
Andrey Kleshchev
committed
const F32 SKIN_WEIGHT_CAMERA_DISTANCE = 16.f;
LLViewerFetchedTexture* bindMaterialDiffuseTexture(const LLImportMaterial& material)
{
LLViewerFetchedTexture *texture = LLViewerTextureManager::getFetchedTexture(material.getDiffuseMap(), FTT_DEFAULT, TRUE, LLGLTexture::BOOST_PREVIEW);
if (texture)
{
if (texture->getDiscardLevel() > -1)
{
gGL.getTexUnit(0)->bind(texture, true);
return texture;
}
}
return NULL;
}
std::string stripSuffix(std::string name)
{
if ((name.find("_LOD") != -1) || (name.find("_PHYS") != -1))
{
return name.substr(0, name.rfind('_'));
}
return name;
}
std::string getLodSuffix(S32 lod)
{
std::string suffix;
switch (lod)
{
case LLModel::LOD_IMPOSTOR: suffix = "_LOD0"; break;
case LLModel::LOD_LOW: suffix = "_LOD1"; break;
case LLModel::LOD_MEDIUM: suffix = "_LOD2"; break;
case LLModel::LOD_PHYSICS: suffix = "_PHYS"; break;
case LLModel::LOD_HIGH: break;
}
return suffix;
}
Andrey Kleshchev
committed
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
void FindModel(LLModelLoader::scene& scene, const std::string& name_to_match, LLModel*& baseModelOut, LLMatrix4& matOut)
{
LLModelLoader::scene::iterator base_iter = scene.begin();
bool found = false;
while (!found && (base_iter != scene.end()))
{
matOut = base_iter->first;
LLModelLoader::model_instance_list::iterator base_instance_iter = base_iter->second.begin();
while (!found && (base_instance_iter != base_iter->second.end()))
{
LLModelInstance& base_instance = *base_instance_iter++;
LLModel* base_model = base_instance.mModel;
if (base_model && (base_model->mLabel == name_to_match))
{
baseModelOut = base_model;
return;
}
}
base_iter++;
}
}
//-----------------------------------------------------------------------------
// LLModelPreview
//-----------------------------------------------------------------------------
LLModelPreview::LLModelPreview(S32 width, S32 height, LLFloater* fmp)
: LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE), LLMutex()
, mLodsQuery()
, mLodsWithParsingError()
, mPelvisZOffset(0.0f)
, mLegacyRigFlags(U32_MAX)
, mRigValidJointUpload(false)
, mPhysicsSearchLOD(LLModel::LOD_PHYSICS)
, mResetJoints(false)
, mModelNoErrors(true)
, mLastJointUpdate(false)
Andrey Kleshchev
committed
, mHasDegenerate(false)
, mImporterDebug(LLCachedControl<bool>(gSavedSettings, "ImporterDebug", false))
{
mNeedsUpdate = TRUE;
mCameraDistance = 0.f;
mCameraYaw = 0.f;
mCameraPitch = 0.f;
mCameraZoom = 1.f;
mTextureName = 0;
mPreviewLOD = 0;
mModelLoader = NULL;
mMaxTriangleLimit = 0;
mDirty = false;
mGenLOD = false;
mLoading = false;
mLookUpLodFiles = false;
Andrey Kleshchev
committed
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
mLoadState = LLModelLoader::STARTING;
mGroup = 0;
mLODFrozen = false;
for (U32 i = 0; i < LLModel::NUM_LODS; ++i)
{
mRequestedTriangleCount[i] = 0;
mRequestedCreaseAngle[i] = -1.f;
mRequestedLoDMode[i] = 0;
mRequestedErrorThreshold[i] = 0.f;
}
mViewOption["show_textures"] = false;
mFMP = fmp;
mHasPivot = false;
mModelPivot = LLVector3(0.0f, 0.0f, 0.0f);
createPreviewAvatar();
}
LLModelPreview::~LLModelPreview()
{
if (mModelLoader)
{
mModelLoader->shutdown();
}
if (mPreviewAvatar)
{
mPreviewAvatar->markDead();
mPreviewAvatar = NULL;
}
Andrey Kleshchev
committed
}
void LLModelPreview::updateDimentionsAndOffsets()
Andrey Kleshchev
committed
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
{
assert_main_thread();
rebuildUploadData();
std::set<LLModel*> accounted;
mPelvisZOffset = mFMP ? mFMP->childGetValue("pelvis_offset").asReal() : 3.0f;
if (mFMP && mFMP->childGetValue("upload_joints").asBoolean())
{
// FIXME if preview avatar ever gets reused, this fake mesh ID stuff will fail.
// see also call to addAttachmentPosOverride.
LLUUID fake_mesh_id;
fake_mesh_id.generate();
getPreviewAvatar()->addPelvisFixup(mPelvisZOffset, fake_mesh_id);
}
for (U32 i = 0; i < mUploadData.size(); ++i)
{
LLModelInstance& instance = mUploadData[i];
if (accounted.find(instance.mModel) == accounted.end())
{
accounted.insert(instance.mModel);
//update instance skin info for each lods pelvisZoffset
for (int j = 0; j<LLModel::NUM_LODS; ++j)
{
if (instance.mLOD[j])
{
instance.mLOD[j]->mSkinInfo.mPelvisOffset = mPelvisZOffset;
}
}
}
}
F32 scale = mFMP ? mFMP->childGetValue("import_scale").asReal()*2.f : 2.f;
mDetailsSignal((F32)(mPreviewScale[0] * scale), (F32)(mPreviewScale[1] * scale), (F32)(mPreviewScale[2] * scale));
Andrey Kleshchev
committed
updateStatusMessages();
}
void LLModelPreview::rebuildUploadData()
{
assert_main_thread();
mDefaultPhysicsShapeP = NULL;
Andrey Kleshchev
committed
mUploadData.clear();
mTextureSet.clear();
//fill uploaddata instance vectors from scene data
std::string requested_name = mFMP->getChild<LLUICtrl>("description_form")->getValue().asString();
LLSpinCtrl* scale_spinner = mFMP->getChild<LLSpinCtrl>("import_scale");
F32 scale = scale_spinner->getValue().asReal();
LLMatrix4 scale_mat;
scale_mat.initScale(LLVector3(scale, scale, scale));
F32 max_scale = 0.f;
BOOL legacyMatching = gSavedSettings.getBOOL("ImporterLegacyMatching");
Andrey Kleshchev
committed
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
for (LLModelLoader::scene::iterator iter = mBaseScene.begin(); iter != mBaseScene.end(); ++iter)
{ //for each transform in scene
LLMatrix4 mat = iter->first;
// compute position
LLVector3 position = LLVector3(0, 0, 0) * mat;
// compute scale
LLVector3 x_transformed = LLVector3(1, 0, 0) * mat - position;
LLVector3 y_transformed = LLVector3(0, 1, 0) * mat - position;
LLVector3 z_transformed = LLVector3(0, 0, 1) * mat - position;
F32 x_length = x_transformed.normalize();
F32 y_length = y_transformed.normalize();
F32 z_length = z_transformed.normalize();
max_scale = llmax(llmax(llmax(max_scale, x_length), y_length), z_length);
mat *= scale_mat;
for (LLModelLoader::model_instance_list::iterator model_iter = iter->second.begin(); model_iter != iter->second.end();)
{ //for each instance with said transform applied
LLModelInstance instance = *model_iter++;
LLModel* base_model = instance.mModel;
if (base_model && !requested_name.empty())
{
base_model->mRequestedLabel = requested_name;
}
for (int i = LLModel::NUM_LODS - 1; i >= LLModel::LOD_IMPOSTOR; i--)
{
LLModel* lod_model = NULL;
if (!legacyMatching)
{
// Fill LOD slots by finding matching meshes by label with name extensions
// in the appropriate scene for each LOD. This fixes all kinds of issues
// where the indexed method below fails in spectacular fashion.
// If you don't take the time to name your LOD and PHYS meshes
// with the name of their corresponding mesh in the HIGH LOD,
// then the indexed method will be attempted below.
LLMatrix4 transform;
std::string name_to_match = instance.mLabel;
llassert(!name_to_match.empty());
int extensionLOD;
if (i != LLModel::LOD_PHYSICS || mModel[LLModel::LOD_PHYSICS].empty())
{
extensionLOD = i;
}
else
{
//Physics can be inherited from other LODs or loaded, so we need to adjust what extension we are searching for
extensionLOD = mPhysicsSearchLOD;
}
std::string toAdd = getLodSuffix(extensionLOD);
Andrey Kleshchev
committed
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
if (name_to_match.find(toAdd) == -1)
{
name_to_match += toAdd;
}
FindModel(mScene[i], name_to_match, lod_model, transform);
if (!lod_model && i != LLModel::LOD_PHYSICS)
{
if (mImporterDebug)
{
std::ostringstream out;
out << "Search of" << name_to_match;
out << " in LOD" << i;
out << " list failed. Searching for alternative among LOD lists.";
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, false);
}
int searchLOD = (i > LLModel::LOD_HIGH) ? LLModel::LOD_HIGH : i;
while ((searchLOD <= LLModel::LOD_HIGH) && !lod_model)
{
std::string name_to_match = instance.mLabel;
llassert(!name_to_match.empty());
std::string toAdd = getLodSuffix(searchLOD);
Andrey Kleshchev
committed
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
if (name_to_match.find(toAdd) == -1)
{
name_to_match += toAdd;
}
// See if we can find an appropriately named model in LOD 'searchLOD'
//
FindModel(mScene[searchLOD], name_to_match, lod_model, transform);
searchLOD++;
}
}
}
else
{
// Use old method of index-based association
U32 idx = 0;
for (idx = 0; idx < mBaseModel.size(); ++idx)
{
// find reference instance for this model
if (mBaseModel[idx] == base_model)
{
if (mImporterDebug)
{
std::ostringstream out;
out << "Attempting to use model index " << idx;
out << " for LOD" << i;
out << " of " << instance.mLabel;
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, false);
}
break;
}
}
// If the model list for the current LOD includes that index...
//
if (mModel[i].size() > idx)
{
// Assign that index from the model list for our LOD as the LOD model for this instance
//
lod_model = mModel[i][idx];
if (mImporterDebug)
{
std::ostringstream out;
out << "Indexed match of model index " << idx << " at LOD " << i << " to model named " << lod_model->mLabel;
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, false);
}
}
else if (mImporterDebug)
{
std::ostringstream out;
out << "List of models does not include index " << idx;
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, false);
}
}
Howard Stearns
committed
if (mWarnOfUnmatchedPhyicsMeshes && !lod_model && (i == LLModel::LOD_PHYSICS))
Howard Stearns
committed
{
// Despite the various strategies above, if we don't now have a physics model, we're going to end up with decomposition.
Howard Stearns
committed
// That's ok, but might not what they wanted. Use default_physics_shape if found.
Howard Stearns
committed
std::ostringstream out;
Howard Stearns
committed
out << "No physics model specified for " << instance.mLabel;
if (mDefaultPhysicsShapeP)
{
out << " - using: " << DEFAULT_PHYSICS_MESH_NAME;
lod_model = mDefaultPhysicsShapeP;
}
Howard Stearns
committed
LLFloaterModelPreview::addStringToLog(out, !mDefaultPhysicsShapeP); // Flash log tab if no default.
Howard Stearns
committed
}
Andrey Kleshchev
committed
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
if (lod_model)
{
if (mImporterDebug)
{
std::ostringstream out;
if (i == LLModel::LOD_PHYSICS)
{
out << "Assigning collision for " << instance.mLabel << " to match " << lod_model->mLabel;
}
else
{
out << "Assigning LOD" << i << " for " << instance.mLabel << " to found match " << lod_model->mLabel;
}
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, false);
}
instance.mLOD[i] = lod_model;
}
else
{
if (i < LLModel::LOD_HIGH && !lodsReady())
{
// assign a placeholder from previous LOD until lod generation is complete.
// Note: we might need to assign it regardless of conditions like named search does, to prevent crashes.
instance.mLOD[i] = instance.mLOD[i + 1];
}
if (mImporterDebug)
{
std::ostringstream out;
out << "List of models does not include " << instance.mLabel;
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, false);
}
}
}
LLModel* high_lod_model = instance.mLOD[LLModel::LOD_HIGH];
if (!high_lod_model)
{
LLFloaterModelPreview::addStringToLog("Model " + instance.mLabel + " has no High Lod (LOD3).", true);
Andrey Kleshchev
committed
mFMP->childDisable("calculate_btn");
}
else
{
for (U32 i = 0; i < LLModel::NUM_LODS - 1; i++)
{
int refFaceCnt = 0;
int modelFaceCnt = 0;
llassert(instance.mLOD[i]);
if (instance.mLOD[i] && !instance.mLOD[i]->matchMaterialOrder(high_lod_model, refFaceCnt, modelFaceCnt))
{
LLFloaterModelPreview::addStringToLog("Model " + instance.mLabel + " has mismatching materials between lods." , true);
Andrey Kleshchev
committed
mFMP->childDisable("calculate_btn");
}
}
LLFloaterModelPreview* fmp = (LLFloaterModelPreview*)mFMP;
bool upload_skinweights = fmp && fmp->childGetValue("upload_skin").asBoolean();
if (upload_skinweights && high_lod_model->mSkinInfo.mJointNames.size() > 0)
{
LLQuaternion bind_rot = LLSkinningUtil::getUnscaledQuaternion(LLMatrix4(high_lod_model->mSkinInfo.mBindShapeMatrix));
Andrey Kleshchev
committed
LLQuaternion identity;
if (!bind_rot.isEqualEps(identity, 0.01))
{
// Bind shape matrix is not in standard X-forward orientation.
// Might be good idea to only show this once. It can be spammy.
Andrey Kleshchev
committed
std::ostringstream out;
out << "non-identity bind shape rot. mat is ";
out << high_lod_model->mSkinInfo.mBindShapeMatrix;
out << " bind_rot ";
out << bind_rot;
LL_WARNS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, getLoadState() != LLModelLoader::WARNING_BIND_SHAPE_ORIENTATION);
load_state = LLModelLoader::WARNING_BIND_SHAPE_ORIENTATION;
Andrey Kleshchev
committed
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
}
}
}
instance.mTransform = mat;
mUploadData.push_back(instance);
}
}
for (U32 lod = 0; lod < LLModel::NUM_LODS - 1; lod++)
{
// Search for models that are not included into upload data
// If we found any, that means something we loaded is not a sub-model.
for (U32 model_ind = 0; model_ind < mModel[lod].size(); ++model_ind)
{
bool found_model = false;
for (LLMeshUploadThread::instance_list::iterator iter = mUploadData.begin(); iter != mUploadData.end(); ++iter)
{
LLModelInstance& instance = *iter;
if (instance.mLOD[lod] == mModel[lod][model_ind])
{
found_model = true;
break;
}
}
if (!found_model && mModel[lod][model_ind] && !mModel[lod][model_ind]->mSubmodelID)
{
if (mImporterDebug)
{
std::ostringstream out;
out << "Model " << mModel[lod][model_ind]->mLabel << " was not used - mismatching lod models.";
LL_INFOS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, true);
Andrey Kleshchev
committed
}
Andrey Kleshchev
committed
mFMP->childDisable("calculate_btn");
}
}
}
// Update state for notifications
if (load_state > 0)
{
// encountered issues
setLoadState(load_state);
}
else if (getLoadState() == LLModelLoader::ERROR_MATERIALS
|| getLoadState() == LLModelLoader::WARNING_BIND_SHAPE_ORIENTATION)
{
// This is only valid for these two error types because they are
// only used inside rebuildUploadData() and updateStatusMessages()
// updateStatusMessages() is called after rebuildUploadData()
setLoadState(LLModelLoader::DONE);
}
Andrey Kleshchev
committed
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
F32 max_import_scale = (DEFAULT_MAX_PRIM_SCALE - 0.1f) / max_scale;
F32 max_axis = llmax(mPreviewScale.mV[0], mPreviewScale.mV[1]);
max_axis = llmax(max_axis, mPreviewScale.mV[2]);
max_axis *= 2.f;
//clamp scale so that total imported model bounding box is smaller than 240m on a side
max_import_scale = llmin(max_import_scale, 240.f / max_axis);
scale_spinner->setMaxValue(max_import_scale);
if (max_import_scale < scale)
{
scale_spinner->setValue(max_import_scale);
}
}
void LLModelPreview::saveUploadData(bool save_skinweights, bool save_joint_positions, bool lock_scale_if_joint_position)
{
if (!mLODFile[LLModel::LOD_HIGH].empty())
{
std::string filename = mLODFile[LLModel::LOD_HIGH];
std::string slm_filename;
if (LLModelLoader::getSLMFilename(filename, slm_filename))
{
saveUploadData(slm_filename, save_skinweights, save_joint_positions, lock_scale_if_joint_position);
}
}
}
void LLModelPreview::saveUploadData(const std::string& filename,
bool save_skinweights, bool save_joint_positions, bool lock_scale_if_joint_position)
{
std::set<LLPointer<LLModel> > meshes;
std::map<LLModel*, std::string> mesh_binary;
LLModel::hull empty_hull;
LLSD data;
data["version"] = SLM_SUPPORTED_VERSION;
if (!mBaseModel.empty())
{
data["name"] = mBaseModel[0]->getName();
}
S32 mesh_id = 0;
//build list of unique models and initialize local id
for (U32 i = 0; i < mUploadData.size(); ++i)
{
LLModelInstance& instance = mUploadData[i];
if (meshes.find(instance.mModel) == meshes.end())
{
instance.mModel->mLocalID = mesh_id++;
meshes.insert(instance.mModel);
std::stringstream str;
LLModel::Decomposition& decomp =
instance.mLOD[LLModel::LOD_PHYSICS].notNull() ?
instance.mLOD[LLModel::LOD_PHYSICS]->mPhysics :
instance.mModel->mPhysics;
LLModel::writeModel(str,
instance.mLOD[LLModel::LOD_PHYSICS],
instance.mLOD[LLModel::LOD_HIGH],
instance.mLOD[LLModel::LOD_MEDIUM],
instance.mLOD[LLModel::LOD_LOW],
instance.mLOD[LLModel::LOD_IMPOSTOR],
decomp,
save_skinweights,
save_joint_positions,
lock_scale_if_joint_position,
FALSE, TRUE, instance.mModel->mSubmodelID);
data["mesh"][instance.mModel->mLocalID] = str.str();
}
data["instance"][i] = instance.asLLSD();
}
llofstream out(filename.c_str(), std::ios_base::out | std::ios_base::binary);
LLSDSerialize::toBinary(data, out);
out.flush();
out.close();
}
void LLModelPreview::clearModel(S32 lod)
{
if (lod < 0 || lod > LLModel::LOD_PHYSICS)
{
return;
}
mVertexBuffer[lod].clear();
mModel[lod].clear();
mScene[lod].clear();
}
void LLModelPreview::getJointAliases(JointMap& joint_map)
{
// Get all standard skeleton joints from the preview avatar.
LLVOAvatar *av = getPreviewAvatar();
//Joint names and aliases come from avatar_skeleton.xml
joint_map = av->getJointAliases();
std::vector<std::string> cv_names, attach_names;
av->getSortedJointNames(1, cv_names);
av->getSortedJointNames(2, attach_names);
for (std::vector<std::string>::iterator it = cv_names.begin(); it != cv_names.end(); ++it)
{
joint_map[*it] = *it;
}
for (std::vector<std::string>::iterator it = attach_names.begin(); it != attach_names.end(); ++it)
{
joint_map[*it] = *it;
}
}
void LLModelPreview::loadModel(std::string filename, S32 lod, bool force_disable_slm)
{
assert_main_thread();
LLMutexLock lock(this);
if (lod < LLModel::LOD_IMPOSTOR || lod > LLModel::NUM_LODS - 1)
{
std::ostringstream out;
out << "Invalid level of detail: ";
out << lod;
LL_WARNS() << out.str() << LL_ENDL;
LLFloaterModelPreview::addStringToLog(out, true);
assert(lod >= LLModel::LOD_IMPOSTOR && lod < LLModel::NUM_LODS);
return;
}
// This triggers if you bring up the file picker and then hit CANCEL.
// Just use the previous model (if any) and ignore that you brought up
// the file picker.
if (filename.empty())
{
if (mBaseModel.empty())
{
// this is the initial file picking. Close the whole floater
// if we don't have a base model to show for high LOD.
mFMP->closeFloater(false);
}
mLoading = false;
return;
}
if (mModelLoader)
{
LL_WARNS() << "Incompleted model load operation pending." << LL_ENDL;
return;
}
mLODFile[lod] = filename;
std::map<std::string, std::string> joint_alias_map;
getJointAliases(joint_alias_map);
mModelLoader = new LLDAELoader(
filename,
lod,
&LLModelPreview::loadedCallback,
&LLModelPreview::lookupJointByName,
&LLModelPreview::loadTextures,
&LLModelPreview::stateChangedCallback,
this,
mJointTransformMap,
mJointsFromNode,
joint_alias_map,
LLSkinningUtil::getMaxJointCount(),
gSavedSettings.getU32("ImporterModelLimit"),
gSavedSettings.getBOOL("ImporterPreprocessDAE"));
if (force_disable_slm)
{
mModelLoader->mTrySLM = false;
}
else
{
// For MAINT-6647, we have set force_disable_slm to true,
// which means this code path will never be taken. Trying to
// re-use SLM files has never worked properly; in particular,
// it tends to force the UI into strange checkbox options
// which cannot be altered.
//only try to load from slm if viewer is configured to do so and this is the
//initial model load (not an LoD or physics shape)
mModelLoader->mTrySLM = gSavedSettings.getBOOL("MeshImportUseSLM") && mUploadData.empty();
}
mModelLoader->start();
mFMP->childSetTextArg("status", "[STATUS]", mFMP->getString("status_reading_file"));
setPreviewLOD(lod);
if (getLoadState() >= LLModelLoader::ERROR_PARSING)
{
mFMP->childDisable("ok_btn");
mFMP->childDisable("calculate_btn");
}
if (lod == mPreviewLOD)
{
mFMP->childSetValue("lod_file_" + lod_name[lod], mLODFile[lod]);
}
else if (lod == LLModel::LOD_PHYSICS)
{
mFMP->childSetValue("physics_file", mLODFile[lod]);
}
mFMP->openFloater();
}
void LLModelPreview::setPhysicsFromLOD(S32 lod)
{
assert_main_thread();
if (lod >= 0 && lod <= 3)
{
mPhysicsSearchLOD = lod;
mModel[LLModel::LOD_PHYSICS] = mModel[lod];
mScene[LLModel::LOD_PHYSICS] = mScene[lod];
mLODFile[LLModel::LOD_PHYSICS].clear();
mFMP->childSetValue("physics_file", mLODFile[LLModel::LOD_PHYSICS]);
mVertexBuffer[LLModel::LOD_PHYSICS].clear();
rebuildUploadData();
refresh();
updateStatusMessages();
}
}
void LLModelPreview::clearIncompatible(S32 lod)
{
//Don't discard models if specified model is the physic rep
if (lod == LLModel::LOD_PHYSICS)
{
return;
}
// at this point we don't care about sub-models,
// different amount of sub-models means face count mismatch, not incompatibility
U32 lod_size = countRootModels(mModel[lod]);
Andrey Kleshchev
committed
bool replaced_base_model = (lod == LLModel::LOD_HIGH);
Andrey Kleshchev
committed
for (U32 i = 0; i <= LLModel::LOD_HIGH; i++)
Andrey Kleshchev
committed
{
// Clear out any entries that aren't compatible with this model
Andrey Kleshchev
committed
if (i != lod)
{
if (countRootModels(mModel[i]) != lod_size)
{
mModel[i].clear();
mScene[i].clear();
mVertexBuffer[i].clear();
if (i == LLModel::LOD_HIGH)
{
mBaseModel = mModel[lod];
mBaseScene = mScene[lod];
mVertexBuffer[5].clear();
Andrey Kleshchev
committed
replaced_base_model = true;
}
}
}
}
if (replaced_base_model && !mGenLOD)
Andrey Kleshchev
committed
{
// In case base was replaced, we might need to restart generation
// Check if already started
Andrey Kleshchev
committed
bool subscribe_for_generation = mLodsQuery.empty();
// Remove previously scheduled work
mLodsQuery.clear();
Andrey Kleshchev
committed
LLFloaterModelPreview* fmp = LLFloaterModelPreview::sInstance;
if (!fmp) return;
// Schedule new work
Andrey Kleshchev
committed
for (S32 i = LLModel::LOD_HIGH; i >= 0; --i)
{
if (mModel[i].empty())
{
// Base model was replaced, regenerate this lod if applicable
LLComboBox* lod_combo = mFMP->findChild<LLComboBox>("lod_source_" + lod_name[i]);
if (!lod_combo) return;
S32 lod_mode = lod_combo->getCurrentIndex();
if (lod_mode != LOD_FROM_FILE)
{
mLodsQuery.push_back(i);
Andrey Kleshchev
committed
}
}
}
Andrey Kleshchev
committed
// Subscribe if we have pending work and not subscribed yet
Andrey Kleshchev
committed
if (!mLodsQuery.empty() && subscribe_for_generation)
{
doOnIdleRepeating(lodQueryCallback);
}
Andrey Kleshchev
committed
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
}
}
void LLModelPreview::loadModelCallback(S32 loaded_lod)
{
assert_main_thread();
LLMutexLock lock(this);
if (!mModelLoader)
{
mLoading = false;
return;
}
if (getLoadState() >= LLModelLoader::ERROR_PARSING)
{
mLoading = false;
mModelLoader = NULL;
mLodsWithParsingError.push_back(loaded_lod);
return;
}
mLodsWithParsingError.erase(std::remove(mLodsWithParsingError.begin(), mLodsWithParsingError.end(), loaded_lod), mLodsWithParsingError.end());
if (mLodsWithParsingError.empty())
{
mFMP->childEnable("calculate_btn");
}
// Copy determinations about rig so UI will reflect them
//
setRigValidForJointPositionUpload(mModelLoader->isRigValidForJointPositionUpload());
setLegacyRigFlags(mModelLoader->getLegacyRigFlags());
mModelLoader->loadTextures();
if (loaded_lod == -1)
{ //populate all LoDs from model loader scene
mBaseModel.clear();
mBaseScene.clear();
bool skin_weights = false;
bool joint_overrides = false;
bool lock_scale_if_joint_position = false;
for (S32 lod = 0; lod < LLModel::NUM_LODS; ++lod)
{ //for each LoD
//clear scene and model info
mScene[lod].clear();
mModel[lod].clear();
mVertexBuffer[lod].clear();
if (mModelLoader->mScene.begin()->second[0].mLOD[lod].notNull())
{ //if this LoD exists in the loaded scene
//copy scene to current LoD
mScene[lod] = mModelLoader->mScene;
//touch up copied scene to look like current LoD
for (LLModelLoader::scene::iterator iter = mScene[lod].begin(); iter != mScene[lod].end(); ++iter)
{
LLModelLoader::model_instance_list& list = iter->second;
for (LLModelLoader::model_instance_list::iterator list_iter = list.begin(); list_iter != list.end(); ++list_iter)
{
//override displayed model with current LoD
list_iter->mModel = list_iter->mLOD[lod];
if (!list_iter->mModel)
{
continue;
}
//add current model to current LoD's model list (LLModel::mLocalID makes a good vector index)
S32 idx = list_iter->mModel->mLocalID;
if (mModel[lod].size() <= idx)
{ //stretch model list to fit model at given index
mModel[lod].resize(idx + 1);
}
mModel[lod][idx] = list_iter->mModel;
if (!list_iter->mModel->mSkinWeights.empty())
{
skin_weights = true;
if (!list_iter->mModel->mSkinInfo.mAlternateBindMatrix.empty())
{
joint_overrides = true;
}
if (list_iter->mModel->mSkinInfo.mLockScaleIfJointPosition)
{
lock_scale_if_joint_position = true;
}
}
}
}
}
}
if (mFMP)
{
LLFloaterModelPreview* fmp = (LLFloaterModelPreview*)mFMP;
if (skin_weights)
{ //enable uploading/previewing of skin weights if present in .slm file
fmp->enableViewOption("show_skin_weight");
mViewOption["show_skin_weight"] = true;
fmp->childSetValue("upload_skin", true);