PageRenderTime 234ms CodeModel.GetById 15ms app.highlight 197ms RepoModel.GetById 1ms app.codeStats 2ms

/indra/newview/llvoavatarself.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2172 lines | 1699 code | 240 blank | 233 comment | 341 complexity | b27d8f34f13dc9cf3682a2cd0c9b2009 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/** 
   2 * @file llvoavatar.cpp
   3 * @brief Implementation of LLVOAvatar class which is a derivation fo LLViewerObject
   4 *
   5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
   6 * Second Life Viewer Source Code
   7 * Copyright (C) 2010, Linden Research, Inc.
   8 * 
   9 * This library is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU Lesser General Public
  11 * License as published by the Free Software Foundation;
  12 * version 2.1 of the License only.
  13 * 
  14 * This library is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 * Lesser General Public License for more details.
  18 * 
  19 * You should have received a copy of the GNU Lesser General Public
  20 * License along with this library; if not, write to the Free Software
  21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  22 * 
  23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
  24 * $/LicenseInfo$
  25 */
  26
  27#if LL_MSVC
  28// disable warning about boost::lexical_cast returning uninitialized data
  29// when it fails to parse the string
  30#pragma warning (disable:4701)
  31#endif
  32
  33#include "llviewerprecompiledheaders.h"
  34
  35#include "llvoavatarself.h"
  36#include "llvoavatar.h"
  37
  38#include "pipeline.h"
  39
  40#include "llagent.h" //  Get state values from here
  41#include "llagentcamera.h"
  42#include "llagentwearables.h"
  43#include "llhudeffecttrail.h"
  44#include "llhudmanager.h"
  45#include "llinventoryfunctions.h"
  46#include "llnotificationsutil.h"
  47#include "llselectmgr.h"
  48#include "lltoolgrab.h"	// for needsRenderBeam
  49#include "lltoolmgr.h" // for needsRenderBeam
  50#include "lltoolmorph.h"
  51#include "lltrans.h"
  52#include "llviewercamera.h"
  53#include "llviewercontrol.h"
  54#include "llviewermenu.h"
  55#include "llviewerobjectlist.h"
  56#include "llviewerstats.h"
  57#include "llviewerregion.h"
  58#include "llappearancemgr.h"
  59#include "llmeshrepository.h"
  60#include "llvovolume.h"
  61
  62#if LL_MSVC
  63// disable boost::lexical_cast warning
  64#pragma warning (disable:4702)
  65#endif
  66
  67#include <boost/lexical_cast.hpp>
  68
  69LLVOAvatarSelf *gAgentAvatarp = NULL;
  70BOOL isAgentAvatarValid()
  71{
  72	return (gAgentAvatarp &&
  73			(gAgentAvatarp->getRegion() != NULL) &&
  74			(!gAgentAvatarp->isDead()));
  75}
  76
  77using namespace LLVOAvatarDefines;
  78
  79/*********************************************************************************
  80 **                                                                             **
  81 ** Begin private LLVOAvatarSelf Support classes
  82 **
  83 **/
  84
  85struct LocalTextureData
  86{
  87	LocalTextureData() : 
  88		mIsBakedReady(false), 
  89		mDiscard(MAX_DISCARD_LEVEL+1), 
  90		mImage(NULL), 
  91		mWearableID(IMG_DEFAULT_AVATAR),
  92		mTexEntry(NULL)
  93	{}
  94	LLPointer<LLViewerFetchedTexture> mImage;
  95	bool mIsBakedReady;
  96	S32 mDiscard;
  97	LLUUID mWearableID;	// UUID of the wearable that this texture belongs to, not of the image itself
  98	LLTextureEntry *mTexEntry;
  99};
 100
 101//-----------------------------------------------------------------------------
 102// Callback data
 103//-----------------------------------------------------------------------------
 104
 105
 106/**
 107 **
 108 ** End LLVOAvatarSelf Support classes
 109 **                                                                             **
 110 *********************************************************************************/
 111
 112
 113//-----------------------------------------------------------------------------
 114// Static Data
 115//-----------------------------------------------------------------------------
 116S32 LLVOAvatarSelf::sScratchTexBytes = 0;
 117LLMap< LLGLenum, LLGLuint*> LLVOAvatarSelf::sScratchTexNames;
 118LLMap< LLGLenum, F32*> LLVOAvatarSelf::sScratchTexLastBindTime;
 119
 120
 121/*********************************************************************************
 122 **                                                                             **
 123 ** Begin LLVOAvatarSelf Constructor routines
 124 **
 125 **/
 126
 127LLVOAvatarSelf::LLVOAvatarSelf(const LLUUID& id,
 128							   const LLPCode pcode,
 129							   LLViewerRegion* regionp) :
 130	LLVOAvatar(id, pcode, regionp),
 131	mScreenp(NULL),
 132	mLastRegionHandle(0),
 133	mRegionCrossingCount(0)
 134{
 135	gAgentWearables.setAvatarObject(this);
 136
 137	mMotionController.mIsSelf = TRUE;
 138
 139	lldebugs << "Marking avatar as self " << id << llendl;
 140}
 141
 142void LLVOAvatarSelf::initInstance()
 143{
 144	BOOL status = TRUE;
 145	// creates hud joint(mScreen) among other things
 146	status &= loadAvatarSelf();
 147
 148	// adds attachment points to mScreen among other things
 149	LLVOAvatar::initInstance();
 150
 151	llinfos << "Self avatar object created. Starting timer." << llendl;
 152	mDebugSelfLoadTimer.reset();
 153	// clear all times to -1 for debugging
 154	for (U32 i =0; i < LLVOAvatarDefines::TEX_NUM_INDICES; ++i)
 155	{
 156		for (U32 j = 0; j <= MAX_DISCARD_LEVEL; ++j)
 157		{
 158			mDebugTextureLoadTimes[i][j] = -1.0f;
 159		}
 160	}
 161
 162	for (U32 i =0; i < LLVOAvatarDefines::BAKED_NUM_INDICES; ++i)
 163	{
 164		mDebugBakedTextureTimes[i][0] = -1.0f;
 165		mDebugBakedTextureTimes[i][1] = -1.0f;
 166	}
 167
 168	status &= buildMenus();
 169	if (!status)
 170	{
 171		llerrs << "Unable to load user's avatar" << llendl;
 172		return;
 173	}
 174}
 175
 176// virtual
 177void LLVOAvatarSelf::markDead()
 178{
 179	mBeam = NULL;
 180	LLVOAvatar::markDead();
 181}
 182
 183/*virtual*/ BOOL LLVOAvatarSelf::loadAvatar()
 184{
 185	BOOL success = LLVOAvatar::loadAvatar();
 186
 187	// set all parameters sotred directly in the avatar to have
 188	// the isSelfParam to be TRUE - this is used to prevent
 189	// them from being animated or trigger accidental rebakes
 190	// when we copy params from the wearable to the base avatar.
 191	for (LLViewerVisualParam* param = (LLViewerVisualParam*) getFirstVisualParam(); 
 192		 param;
 193		 param = (LLViewerVisualParam*) getNextVisualParam())
 194	{
 195		if (param->getWearableType() != LLWearableType::WT_INVALID)
 196		{
 197			param->setIsDummy(TRUE);
 198		}
 199	}
 200
 201	return success;
 202}
 203
 204
 205BOOL LLVOAvatarSelf::loadAvatarSelf()
 206{
 207	BOOL success = TRUE;
 208	// avatar_skeleton.xml
 209	if (!buildSkeletonSelf(sAvatarSkeletonInfo))
 210	{
 211		llwarns << "avatar file: buildSkeleton() failed" << llendl;
 212		return FALSE;
 213	}
 214	// TODO: make loadLayersets() called only by self.
 215	//success &= loadLayersets();
 216
 217	return success;
 218}
 219
 220BOOL LLVOAvatarSelf::buildSkeletonSelf(const LLVOAvatarSkeletonInfo *info)
 221{
 222	LLMemType mt(LLMemType::MTYPE_AVATAR);
 223
 224	// add special-purpose "screen" joint
 225	mScreenp = new LLViewerJoint("mScreen", NULL);
 226	// for now, put screen at origin, as it is only used during special
 227	// HUD rendering mode
 228	F32 aspect = LLViewerCamera::getInstance()->getAspect();
 229	LLVector3 scale(1.f, aspect, 1.f);
 230	mScreenp->setScale(scale);
 231	mScreenp->setWorldPosition(LLVector3::zero);
 232	// need to update screen agressively when sidebar opens/closes, for example
 233	mScreenp->mUpdateXform = TRUE;
 234	return TRUE;
 235}
 236
 237BOOL LLVOAvatarSelf::buildMenus()
 238{
 239	//-------------------------------------------------------------------------
 240	// build the attach and detach menus
 241	//-------------------------------------------------------------------------
 242	gAttachBodyPartPieMenus[0] = NULL;
 243
 244	LLContextMenu::Params params;
 245	params.label(LLTrans::getString("BodyPartsRightArm"));
 246	params.name(params.label);
 247	params.visible(false);
 248	gAttachBodyPartPieMenus[1] = LLUICtrlFactory::create<LLContextMenu> (params);
 249
 250	params.label(LLTrans::getString("BodyPartsHead"));
 251	params.name(params.label);
 252	gAttachBodyPartPieMenus[2] = LLUICtrlFactory::create<LLContextMenu> (params);
 253
 254	params.label(LLTrans::getString("BodyPartsLeftArm"));
 255	params.name(params.label);
 256	gAttachBodyPartPieMenus[3] = LLUICtrlFactory::create<LLContextMenu> (params);
 257
 258	gAttachBodyPartPieMenus[4] = NULL;
 259
 260	params.label(LLTrans::getString("BodyPartsLeftLeg"));
 261	params.name(params.label);
 262	gAttachBodyPartPieMenus[5] = LLUICtrlFactory::create<LLContextMenu> (params);
 263
 264	params.label(LLTrans::getString("BodyPartsTorso"));
 265	params.name(params.label);
 266	gAttachBodyPartPieMenus[6] = LLUICtrlFactory::create<LLContextMenu> (params);
 267
 268	params.label(LLTrans::getString("BodyPartsRightLeg"));
 269	params.name(params.label);
 270	gAttachBodyPartPieMenus[7] = LLUICtrlFactory::create<LLContextMenu> (params);
 271
 272	gDetachBodyPartPieMenus[0] = NULL;
 273
 274	params.label(LLTrans::getString("BodyPartsRightArm"));
 275	params.name(params.label);
 276	gDetachBodyPartPieMenus[1] = LLUICtrlFactory::create<LLContextMenu> (params);
 277
 278	params.label(LLTrans::getString("BodyPartsHead"));
 279	params.name(params.label);
 280	gDetachBodyPartPieMenus[2] = LLUICtrlFactory::create<LLContextMenu> (params);
 281
 282	params.label(LLTrans::getString("BodyPartsLeftArm"));
 283	params.name(params.label);
 284	gDetachBodyPartPieMenus[3] = LLUICtrlFactory::create<LLContextMenu> (params);
 285
 286	gDetachBodyPartPieMenus[4] = NULL;
 287
 288	params.label(LLTrans::getString("BodyPartsLeftLeg"));
 289	params.name(params.label);
 290	gDetachBodyPartPieMenus[5] = LLUICtrlFactory::create<LLContextMenu> (params);
 291
 292	params.label(LLTrans::getString("BodyPartsTorso"));
 293	params.name(params.label);
 294	gDetachBodyPartPieMenus[6] = LLUICtrlFactory::create<LLContextMenu> (params);
 295
 296	params.label(LLTrans::getString("BodyPartsRightLeg"));
 297	params.name(params.label);
 298	gDetachBodyPartPieMenus[7] = LLUICtrlFactory::create<LLContextMenu> (params);
 299
 300	for (S32 i = 0; i < 8; i++)
 301	{
 302		if (gAttachBodyPartPieMenus[i])
 303		{
 304			gAttachPieMenu->appendContextSubMenu( gAttachBodyPartPieMenus[i] );
 305		}
 306		else
 307		{
 308			BOOL attachment_found = FALSE;
 309			for (attachment_map_t::iterator iter = mAttachmentPoints.begin(); 
 310				 iter != mAttachmentPoints.end();
 311				 ++iter)
 312			{
 313				LLViewerJointAttachment* attachment = iter->second;
 314				if (attachment->getGroup() == i)
 315				{
 316					LLMenuItemCallGL::Params item_params;
 317						
 318					std::string sub_piemenu_name = attachment->getName();
 319					if (LLTrans::getString(sub_piemenu_name) != "")
 320					{
 321						item_params.label = LLTrans::getString(sub_piemenu_name);
 322					}
 323					else
 324					{
 325						item_params.label = sub_piemenu_name;
 326					}
 327					item_params.name =(item_params.label );
 328					item_params.on_click.function_name = "Object.AttachToAvatar";
 329					item_params.on_click.parameter = iter->first;
 330					item_params.on_enable.function_name = "Object.EnableWear";
 331					item_params.on_enable.parameter = iter->first;
 332					LLMenuItemCallGL* item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 333
 334					gAttachPieMenu->addChild(item);
 335
 336					attachment_found = TRUE;
 337					break;
 338
 339				}
 340			}
 341		}
 342
 343		if (gDetachBodyPartPieMenus[i])
 344		{
 345			gDetachPieMenu->appendContextSubMenu( gDetachBodyPartPieMenus[i] );
 346		}
 347		else
 348		{
 349			BOOL attachment_found = FALSE;
 350			for (attachment_map_t::iterator iter = mAttachmentPoints.begin(); 
 351				 iter != mAttachmentPoints.end();
 352				 ++iter)
 353			{
 354				LLViewerJointAttachment* attachment = iter->second;
 355				if (attachment->getGroup() == i)
 356				{
 357					LLMenuItemCallGL::Params item_params;
 358					std::string sub_piemenu_name = attachment->getName();
 359					if (LLTrans::getString(sub_piemenu_name) != "")
 360					{
 361						item_params.label = LLTrans::getString(sub_piemenu_name);
 362					}
 363					else
 364					{
 365						item_params.label = sub_piemenu_name;
 366					}
 367					item_params.name =(item_params.label );
 368					item_params.on_click.function_name = "Attachment.Detach";
 369					item_params.on_click.parameter = iter->first;
 370					item_params.on_enable.function_name = "Attachment.EnableDetach";
 371					item_params.on_enable.parameter = iter->first;
 372					LLMenuItemCallGL* item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 373
 374					gDetachPieMenu->addChild(item);
 375						
 376					attachment_found = TRUE;
 377					break;
 378				}
 379			}
 380		}
 381	}
 382
 383	// add screen attachments
 384	for (attachment_map_t::iterator iter = mAttachmentPoints.begin(); 
 385		 iter != mAttachmentPoints.end();
 386		 ++iter)
 387	{
 388		LLViewerJointAttachment* attachment = iter->second;
 389		if (attachment->getGroup() == 8)
 390		{
 391			LLMenuItemCallGL::Params item_params;
 392			std::string sub_piemenu_name = attachment->getName();
 393			if (LLTrans::getString(sub_piemenu_name) != "")
 394			{
 395				item_params.label = LLTrans::getString(sub_piemenu_name);
 396			}
 397			else
 398			{
 399				item_params.label = sub_piemenu_name;
 400			}
 401			item_params.name =(item_params.label );
 402			item_params.on_click.function_name = "Object.AttachToAvatar";
 403			item_params.on_click.parameter = iter->first;
 404			item_params.on_enable.function_name = "Object.EnableWear";
 405			item_params.on_enable.parameter = iter->first;
 406			LLMenuItemCallGL* item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 407			gAttachScreenPieMenu->addChild(item);
 408
 409			item_params.on_click.function_name = "Attachment.DetachFromPoint";
 410			item_params.on_click.parameter = iter->first;
 411			item_params.on_enable.function_name = "Attachment.PointFilled";
 412			item_params.on_enable.parameter = iter->first;
 413			item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 414			gDetachScreenPieMenu->addChild(item);
 415		}
 416	}
 417
 418	for (S32 pass = 0; pass < 2; pass++)
 419	{
 420		// *TODO: Skinning - gAttachSubMenu is an awful, awful hack
 421		if (!gAttachSubMenu)
 422		{
 423			break;
 424		}
 425		for (attachment_map_t::iterator iter = mAttachmentPoints.begin(); 
 426			 iter != mAttachmentPoints.end();
 427			 ++iter)
 428		{
 429			LLViewerJointAttachment* attachment = iter->second;
 430			if (attachment->getIsHUDAttachment() != (pass == 1))
 431			{
 432				continue;
 433			}
 434			LLMenuItemCallGL::Params item_params;
 435			std::string sub_piemenu_name = attachment->getName();
 436			if (LLTrans::getString(sub_piemenu_name) != "")
 437			{
 438				item_params.label = LLTrans::getString(sub_piemenu_name);
 439			}
 440			else
 441			{
 442				item_params.label = sub_piemenu_name;
 443			}
 444			item_params.name =(item_params.label );
 445			item_params.on_click.function_name = "Object.AttachToAvatar";
 446			item_params.on_click.parameter = iter->first;
 447			item_params.on_enable.function_name = "Object.EnableWear";
 448			item_params.on_enable.parameter = iter->first;
 449			//* TODO: Skinning:
 450			//LLSD params;
 451			//params["index"] = iter->first;
 452			//params["label"] = attachment->getName();
 453			//item->addEventHandler("on_enable", LLMenuItemCallGL::MenuCallback().function_name("Attachment.Label").parameter(params));
 454				
 455			LLMenuItemCallGL* item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 456			gAttachSubMenu->addChild(item);
 457
 458			item_params.on_click.function_name = "Attachment.DetachFromPoint";
 459			item_params.on_click.parameter = iter->first;
 460			item_params.on_enable.function_name = "Attachment.PointFilled";
 461			item_params.on_enable.parameter = iter->first;
 462			//* TODO: Skinning: item->addEventHandler("on_enable", LLMenuItemCallGL::MenuCallback().function_name("Attachment.Label").parameter(params));
 463
 464			item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 465			gDetachSubMenu->addChild(item);
 466		}
 467		if (pass == 0)
 468		{
 469			// put separator between non-hud and hud attachments
 470			gAttachSubMenu->addSeparator();
 471			gDetachSubMenu->addSeparator();
 472		}
 473	}
 474
 475	for (S32 group = 0; group < 8; group++)
 476	{
 477		// skip over groups that don't have sub menus
 478		if (!gAttachBodyPartPieMenus[group] || !gDetachBodyPartPieMenus[group])
 479		{
 480			continue;
 481		}
 482
 483		std::multimap<S32, S32> attachment_pie_menu_map;
 484
 485		// gather up all attachment points assigned to this group, and throw into map sorted by pie slice number
 486		for (attachment_map_t::iterator iter = mAttachmentPoints.begin(); 
 487			 iter != mAttachmentPoints.end();
 488			 ++iter)
 489		{
 490			LLViewerJointAttachment* attachment = iter->second;
 491			if(attachment->getGroup() == group)
 492			{
 493				// use multimap to provide a partial order off of the pie slice key
 494				S32 pie_index = attachment->getPieSlice();
 495				attachment_pie_menu_map.insert(std::make_pair(pie_index, iter->first));
 496			}
 497		}
 498
 499		// add in requested order to pie menu, inserting separators as necessary
 500		for (std::multimap<S32, S32>::iterator attach_it = attachment_pie_menu_map.begin();
 501			 attach_it != attachment_pie_menu_map.end(); ++attach_it)
 502		{
 503			S32 attach_index = attach_it->second;
 504
 505			LLViewerJointAttachment* attachment = get_if_there(mAttachmentPoints, attach_index, (LLViewerJointAttachment*)NULL);
 506			if (attachment)
 507			{
 508				LLMenuItemCallGL::Params item_params;
 509				item_params.name = attachment->getName();
 510				item_params.label = LLTrans::getString(attachment->getName());
 511				item_params.on_click.function_name = "Object.AttachToAvatar";
 512				item_params.on_click.parameter = attach_index;
 513				item_params.on_enable.function_name = "Object.EnableWear";
 514				item_params.on_enable.parameter = attach_index;
 515
 516				LLMenuItemCallGL* item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 517				gAttachBodyPartPieMenus[group]->addChild(item);
 518					
 519				item_params.on_click.function_name = "Attachment.DetachFromPoint";
 520				item_params.on_click.parameter = attach_index;
 521				item_params.on_enable.function_name = "Attachment.PointFilled";
 522				item_params.on_enable.parameter = attach_index;
 523				item = LLUICtrlFactory::create<LLMenuItemCallGL>(item_params);
 524				gDetachBodyPartPieMenus[group]->addChild(item);
 525			}
 526		}
 527	}
 528	return TRUE;
 529}
 530
 531void LLVOAvatarSelf::cleanup()
 532{
 533	markDead();
 534 	delete mScreenp;
 535 	mScreenp = NULL;
 536	mRegionp = NULL;
 537}
 538
 539LLVOAvatarSelf::~LLVOAvatarSelf()
 540{
 541	cleanup();
 542}
 543
 544/**
 545 **
 546 ** End LLVOAvatarSelf Constructor routines
 547 **                                                                             **
 548 *********************************************************************************/
 549
 550//virtual
 551BOOL LLVOAvatarSelf::loadLayersets()
 552{
 553	BOOL success = TRUE;
 554	for (LLVOAvatarXmlInfo::layer_info_list_t::const_iterator iter = sAvatarXmlInfo->mLayerInfoList.begin();
 555		 iter != sAvatarXmlInfo->mLayerInfoList.end(); 
 556		 ++iter)
 557	{
 558		// Construct a layerset for each one specified in avatar_lad.xml and initialize it as such.
 559		const LLTexLayerSetInfo *info = *iter;
 560		LLTexLayerSet* layer_set = new LLTexLayerSet( this );
 561		
 562		if (!layer_set->setInfo(info))
 563		{
 564			stop_glerror();
 565			delete layer_set;
 566			llwarns << "avatar file: layer_set->parseData() failed" << llendl;
 567			return FALSE;
 568		}
 569
 570		// scan baked textures and associate the layerset with the appropriate one
 571		EBakedTextureIndex baked_index = BAKED_NUM_INDICES;
 572		for (LLVOAvatarDictionary::BakedTextures::const_iterator baked_iter = LLVOAvatarDictionary::getInstance()->getBakedTextures().begin();
 573			 baked_iter != LLVOAvatarDictionary::getInstance()->getBakedTextures().end();
 574			 ++baked_iter)
 575		{
 576			const LLVOAvatarDictionary::BakedEntry *baked_dict = baked_iter->second;
 577			if (layer_set->isBodyRegion(baked_dict->mName))
 578			{
 579				baked_index = baked_iter->first;
 580				// ensure both structures are aware of each other
 581				mBakedTextureDatas[baked_index].mTexLayerSet = layer_set;
 582				layer_set->setBakedTexIndex(baked_index);
 583				break;
 584			}
 585		}
 586		// if no baked texture was found, warn and cleanup
 587		if (baked_index == BAKED_NUM_INDICES)
 588		{
 589			llwarns << "<layer_set> has invalid body_region attribute" << llendl;
 590			delete layer_set;
 591			return FALSE;
 592		}
 593
 594		// scan morph masks and let any affected layers know they have an associated morph
 595		for (LLVOAvatar::morph_list_t::const_iterator morph_iter = mBakedTextureDatas[baked_index].mMaskedMorphs.begin();
 596			morph_iter != mBakedTextureDatas[baked_index].mMaskedMorphs.end();
 597			 ++morph_iter)
 598		{
 599			LLMaskedMorph *morph = *morph_iter;
 600			LLTexLayerInterface* layer = layer_set->findLayerByName(morph->mLayer);
 601			if (layer)
 602			{
 603				layer->setHasMorph(TRUE);
 604			}
 605			else
 606			{
 607				llwarns << "Could not find layer named " << morph->mLayer << " to set morph flag" << llendl;
 608				success = FALSE;
 609			}
 610		}
 611	}
 612	return success;
 613}
 614// virtual
 615BOOL LLVOAvatarSelf::updateCharacter(LLAgent &agent)
 616{
 617	LLMemType mt(LLMemType::MTYPE_AVATAR);
 618
 619	// update screen joint size
 620	if (mScreenp)
 621	{
 622		F32 aspect = LLViewerCamera::getInstance()->getAspect();
 623		LLVector3 scale(1.f, aspect, 1.f);
 624		mScreenp->setScale(scale);
 625		mScreenp->updateWorldMatrixChildren();
 626		resetHUDAttachments();
 627	}
 628	
 629	return LLVOAvatar::updateCharacter(agent);
 630}
 631
 632// virtual
 633BOOL LLVOAvatarSelf::idleUpdate(LLAgent &agent, LLWorld &world, const F64 &time)
 634{
 635	if (!isAgentAvatarValid())
 636	{
 637		return TRUE;
 638	}
 639	LLVOAvatar::idleUpdate(agent, world, time);
 640	idleUpdateTractorBeam();
 641	return TRUE;
 642}
 643
 644// virtual
 645LLJoint *LLVOAvatarSelf::getJoint(const std::string &name)
 646{
 647	if (mScreenp)
 648	{
 649		LLJoint* jointp = mScreenp->findJoint(name);
 650		if (jointp) return jointp;
 651	}
 652	return LLVOAvatar::getJoint(name);
 653}
 654//virtual
 655void LLVOAvatarSelf::resetJointPositions( void )
 656{
 657	return LLVOAvatar::resetJointPositions();
 658}
 659// virtual
 660BOOL LLVOAvatarSelf::setVisualParamWeight(LLVisualParam *which_param, F32 weight, BOOL upload_bake )
 661{
 662	if (!which_param)
 663	{
 664		return FALSE;
 665	}
 666	LLViewerVisualParam *param = (LLViewerVisualParam*) LLCharacter::getVisualParam(which_param->getID());
 667	return setParamWeight(param,weight,upload_bake);
 668}
 669
 670// virtual
 671BOOL LLVOAvatarSelf::setVisualParamWeight(const char* param_name, F32 weight, BOOL upload_bake )
 672{
 673	if (!param_name)
 674	{
 675		return FALSE;
 676	}
 677	LLViewerVisualParam *param = (LLViewerVisualParam*) LLCharacter::getVisualParam(param_name);
 678	return setParamWeight(param,weight,upload_bake);
 679}
 680
 681// virtual
 682BOOL LLVOAvatarSelf::setVisualParamWeight(S32 index, F32 weight, BOOL upload_bake )
 683{
 684	LLViewerVisualParam *param = (LLViewerVisualParam*) LLCharacter::getVisualParam(index);
 685	return setParamWeight(param,weight,upload_bake);
 686}
 687
 688BOOL LLVOAvatarSelf::setParamWeight(LLViewerVisualParam *param, F32 weight, BOOL upload_bake )
 689{
 690	if (!param)
 691	{
 692		return FALSE;
 693	}
 694
 695	if (param->getCrossWearable())
 696	{
 697		LLWearableType::EType type = (LLWearableType::EType)param->getWearableType();
 698		U32 size = gAgentWearables.getWearableCount(type);
 699		for (U32 count = 0; count < size; ++count)
 700		{
 701			LLWearable *wearable = gAgentWearables.getWearable(type,count);
 702			if (wearable)
 703			{
 704				wearable->setVisualParamWeight(param->getID(), weight, upload_bake);
 705			}
 706		}
 707	}
 708
 709	return LLCharacter::setVisualParamWeight(param,weight,upload_bake);
 710}
 711
 712/*virtual*/ 
 713void LLVOAvatarSelf::updateVisualParams()
 714{
 715	LLVOAvatar::updateVisualParams();
 716}
 717
 718/*virtual*/
 719void LLVOAvatarSelf::idleUpdateAppearanceAnimation()
 720{
 721	// Animate all top-level wearable visual parameters
 722	gAgentWearables.animateAllWearableParams(calcMorphAmount(), FALSE);
 723
 724	// apply wearable visual params to avatar
 725	for (U32 type = 0; type < LLWearableType::WT_COUNT; type++)
 726	{
 727		LLWearable *wearable = gAgentWearables.getTopWearable((LLWearableType::EType)type);
 728		if (wearable)
 729		{
 730			wearable->writeToAvatar();
 731		}
 732	}
 733
 734	//allow avatar to process updates
 735	LLVOAvatar::idleUpdateAppearanceAnimation();
 736
 737}
 738
 739// virtual
 740void LLVOAvatarSelf::requestStopMotion(LLMotion* motion)
 741{
 742	// Only agent avatars should handle the stop motion notifications.
 743
 744	// Notify agent that motion has stopped
 745	gAgent.requestStopMotion(motion);
 746}
 747
 748// virtual
 749void LLVOAvatarSelf::stopMotionFromSource(const LLUUID& source_id)
 750{
 751	for (AnimSourceIterator motion_it = mAnimationSources.find(source_id); motion_it != mAnimationSources.end(); )
 752	{
 753		gAgent.sendAnimationRequest(motion_it->second, ANIM_REQUEST_STOP);
 754		mAnimationSources.erase(motion_it++);
 755	}
 756
 757	LLViewerObject* object = gObjectList.findObject(source_id);
 758	if (object)
 759	{
 760		object->mFlags &= ~FLAGS_ANIM_SOURCE;
 761	}
 762}
 763
 764void LLVOAvatarSelf::setLocalTextureTE(U8 te, LLViewerTexture* image, U32 index)
 765{
 766	if (te >= TEX_NUM_INDICES)
 767	{
 768		llassert(0);
 769		return;
 770	}
 771
 772	if (getTEImage(te)->getID() == image->getID())
 773	{
 774		return;
 775	}
 776
 777	if (isIndexBakedTexture((ETextureIndex)te))
 778	{
 779		llassert(0);
 780		return;
 781	}
 782
 783	setTEImage(te, image);
 784}
 785
 786//virtual
 787void LLVOAvatarSelf::removeMissingBakedTextures()
 788{	
 789	BOOL removed = FALSE;
 790	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
 791	{
 792		const S32 te = mBakedTextureDatas[i].mTextureIndex;
 793		const LLViewerTexture* tex = getTEImage(te);
 794
 795		// Replace with default if we can't find the asset, assuming the
 796		// default is actually valid (which it should be unless something
 797		// is seriously wrong).
 798		if (!tex || tex->isMissingAsset())
 799		{
 800			LLViewerTexture *imagep = LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT_AVATAR);
 801			if (imagep)
 802			{
 803				setTEImage(te, imagep);
 804				removed = TRUE;
 805			}
 806		}
 807	}
 808
 809	if (removed)
 810	{
 811		for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
 812		{
 813			mBakedTextureDatas[i].mTexLayerSet->setUpdatesEnabled(TRUE);
 814			invalidateComposite(mBakedTextureDatas[i].mTexLayerSet, FALSE);
 815		}
 816		updateMeshTextures();
 817		requestLayerSetUploads();
 818	}
 819}
 820
 821//virtual
 822void LLVOAvatarSelf::updateRegion(LLViewerRegion *regionp)
 823{
 824	// Save the global position
 825	LLVector3d global_pos_from_old_region = getPositionGlobal();
 826
 827	// Change the region
 828	setRegion(regionp);
 829
 830	if (regionp)
 831	{	// Set correct region-relative position from global coordinates
 832		setPositionGlobal(global_pos_from_old_region);
 833
 834		// Diagnostic info
 835		//LLVector3d pos_from_new_region = getPositionGlobal();
 836		//llinfos << "pos_from_old_region is " << global_pos_from_old_region
 837		//	<< " while pos_from_new_region is " << pos_from_new_region
 838		//	<< llendl;
 839	}
 840
 841	if (!regionp || (regionp->getHandle() != mLastRegionHandle))
 842	{
 843		if (mLastRegionHandle != 0)
 844		{
 845			++mRegionCrossingCount;
 846			F64 delta = (F64)mRegionCrossingTimer.getElapsedTimeF32();
 847			F64 avg = (mRegionCrossingCount == 1) ? 0 : LLViewerStats::getInstance()->getStat(LLViewerStats::ST_CROSSING_AVG);
 848			F64 delta_avg = (delta + avg*(mRegionCrossingCount-1)) / mRegionCrossingCount;
 849			LLViewerStats::getInstance()->setStat(LLViewerStats::ST_CROSSING_AVG, delta_avg);
 850			
 851			F64 max = (mRegionCrossingCount == 1) ? 0 : LLViewerStats::getInstance()->getStat(LLViewerStats::ST_CROSSING_MAX);
 852			max = llmax(delta, max);
 853			LLViewerStats::getInstance()->setStat(LLViewerStats::ST_CROSSING_MAX, max);
 854
 855			// Diagnostics
 856			llinfos << "Region crossing took " << (F32)(delta * 1000.0) << " ms " << llendl;
 857		}
 858		if (regionp)
 859		{
 860			mLastRegionHandle = regionp->getHandle();
 861		}
 862	}
 863	mRegionCrossingTimer.reset();
 864	LLViewerObject::updateRegion(regionp);
 865}
 866
 867//--------------------------------------------------------------------
 868// draw tractor beam when editing objects
 869//--------------------------------------------------------------------
 870//virtual
 871void LLVOAvatarSelf::idleUpdateTractorBeam()
 872{
 873	// This is only done for yourself (maybe it should be in the agent?)
 874	if (!needsRenderBeam() || !mIsBuilt)
 875	{
 876		mBeam = NULL;
 877	}
 878	else if (!mBeam || mBeam->isDead())
 879	{
 880		// VEFFECT: Tractor Beam
 881		mBeam = (LLHUDEffectSpiral *)LLHUDManager::getInstance()->createViewerEffect(LLHUDObject::LL_HUD_EFFECT_BEAM);
 882		mBeam->setColor(LLColor4U(gAgent.getEffectColor()));
 883		mBeam->setSourceObject(this);
 884		mBeamTimer.reset();
 885	}
 886
 887	if (!mBeam.isNull())
 888	{
 889		LLObjectSelectionHandle selection = LLSelectMgr::getInstance()->getSelection();
 890
 891		if (gAgentCamera.mPointAt.notNull())
 892		{
 893			// get point from pointat effect
 894			mBeam->setPositionGlobal(gAgentCamera.mPointAt->getPointAtPosGlobal());
 895			mBeam->triggerLocal();
 896		}
 897		else if (selection->getFirstRootObject() && 
 898				selection->getSelectType() != SELECT_TYPE_HUD)
 899		{
 900			LLViewerObject* objectp = selection->getFirstRootObject();
 901			mBeam->setTargetObject(objectp);
 902		}
 903		else
 904		{
 905			mBeam->setTargetObject(NULL);
 906			LLTool *tool = LLToolMgr::getInstance()->getCurrentTool();
 907			if (tool->isEditing())
 908			{
 909				if (tool->getEditingObject())
 910				{
 911					mBeam->setTargetObject(tool->getEditingObject());
 912				}
 913				else
 914				{
 915					mBeam->setPositionGlobal(tool->getEditingPointGlobal());
 916				}
 917			}
 918			else
 919			{
 920				const LLPickInfo& pick = gViewerWindow->getLastPick();
 921				mBeam->setPositionGlobal(pick.mPosGlobal);
 922			}
 923
 924		}
 925		if (mBeamTimer.getElapsedTimeF32() > 0.25f)
 926		{
 927			mBeam->setColor(LLColor4U(gAgent.getEffectColor()));
 928			mBeam->setNeedsSendToSim(TRUE);
 929			mBeamTimer.reset();
 930		}
 931	}
 932}
 933
 934//-----------------------------------------------------------------------------
 935// restoreMeshData()
 936//-----------------------------------------------------------------------------
 937// virtual
 938void LLVOAvatarSelf::restoreMeshData()
 939{
 940	LLMemType mt(LLMemType::MTYPE_AVATAR);
 941	
 942	//llinfos << "Restoring" << llendl;
 943	mMeshValid = TRUE;
 944	updateJointLODs();
 945	updateAttachmentVisibility(gAgentCamera.getCameraMode());
 946
 947	// force mesh update as LOD might not have changed to trigger this
 948	gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_GEOMETRY, TRUE);
 949}
 950
 951
 952
 953//-----------------------------------------------------------------------------
 954// updateAttachmentVisibility()
 955//-----------------------------------------------------------------------------
 956void LLVOAvatarSelf::updateAttachmentVisibility(U32 camera_mode)
 957{
 958	for (attachment_map_t::iterator iter = mAttachmentPoints.begin(); 
 959		 iter != mAttachmentPoints.end();
 960		 ++iter)
 961	{
 962		LLViewerJointAttachment* attachment = iter->second;
 963		if (attachment->getIsHUDAttachment())
 964		{
 965			attachment->setAttachmentVisibility(TRUE);
 966		}
 967		else
 968		{
 969			switch (camera_mode)
 970			{
 971				case CAMERA_MODE_MOUSELOOK:
 972					if (LLVOAvatar::sVisibleInFirstPerson && attachment->getVisibleInFirstPerson())
 973					{
 974						attachment->setAttachmentVisibility(TRUE);
 975					}
 976					else
 977					{
 978						attachment->setAttachmentVisibility(FALSE);
 979					}
 980					break;
 981				default:
 982					attachment->setAttachmentVisibility(TRUE);
 983					break;
 984			}
 985		}
 986	}
 987}
 988
 989/*virtual*/ BOOL LLVOAvatarSelf::isWearingWearableType(LLWearableType::EType type ) const
 990{
 991	return gAgentWearables.getWearableCount(type) > 0;
 992}
 993
 994//-----------------------------------------------------------------------------
 995// updatedWearable( LLWearableType::EType type )
 996// forces an update to any baked textures relevant to type.
 997// will force an upload of the resulting bake if the second parameter is TRUE
 998//-----------------------------------------------------------------------------
 999void LLVOAvatarSelf::wearableUpdated( LLWearableType::EType type, BOOL upload_result )
1000{
1001	for (LLVOAvatarDictionary::BakedTextures::const_iterator baked_iter = LLVOAvatarDictionary::getInstance()->getBakedTextures().begin();
1002		 baked_iter != LLVOAvatarDictionary::getInstance()->getBakedTextures().end();
1003		 ++baked_iter)
1004	{
1005		const LLVOAvatarDictionary::BakedEntry *baked_dict = baked_iter->second;
1006		const LLVOAvatarDefines::EBakedTextureIndex index = baked_iter->first;
1007
1008		if (baked_dict)
1009		{
1010			for (LLVOAvatarDefines::wearables_vec_t::const_iterator type_iter = baked_dict->mWearables.begin();
1011				type_iter != baked_dict->mWearables.end();
1012				 ++type_iter)
1013			{
1014				const LLWearableType::EType comp_type = *type_iter;
1015				if (comp_type == type)
1016				{
1017					if (mBakedTextureDatas[index].mTexLayerSet)
1018					{
1019						mBakedTextureDatas[index].mTexLayerSet->setUpdatesEnabled(true);
1020						invalidateComposite(mBakedTextureDatas[index].mTexLayerSet, upload_result);
1021					}
1022					break;
1023				}
1024			}
1025		}
1026	}
1027	
1028	// Physics type has no associated baked textures, but change of params needs to be sent to
1029	// other avatars.
1030	if (type == LLWearableType::WT_PHYSICS)
1031	  {
1032	    gAgent.sendAgentSetAppearance();
1033	  }
1034}
1035
1036//-----------------------------------------------------------------------------
1037// isWearingAttachment()
1038//-----------------------------------------------------------------------------
1039BOOL LLVOAvatarSelf::isWearingAttachment(const LLUUID& inv_item_id) const
1040{
1041	const LLUUID& base_inv_item_id = gInventory.getLinkedItemID(inv_item_id);
1042	for (attachment_map_t::const_iterator iter = mAttachmentPoints.begin(); 
1043		 iter != mAttachmentPoints.end();
1044		 ++iter)
1045	{
1046		const LLViewerJointAttachment* attachment = iter->second;
1047		if (attachment->getAttachedObject(base_inv_item_id))
1048		{
1049			return TRUE;
1050		}
1051	}
1052	return FALSE;
1053}
1054
1055//-----------------------------------------------------------------------------
1056BOOL LLVOAvatarSelf::attachmentWasRequested(const LLUUID& inv_item_id) const
1057{
1058	const F32 REQUEST_EXPIRATION_SECONDS = 5.0;  // any request older than this is ignored/removed.
1059	std::map<LLUUID,LLTimer>::iterator it = mAttachmentRequests.find(inv_item_id);
1060	if (it != mAttachmentRequests.end())
1061	{
1062		const LLTimer& request_time = it->second;
1063		F32 request_time_elapsed = request_time.getElapsedTimeF32();
1064		if (request_time_elapsed > REQUEST_EXPIRATION_SECONDS)
1065		{
1066			mAttachmentRequests.erase(it);
1067			return FALSE;
1068		}
1069		else
1070		{
1071			return TRUE;
1072		}
1073	}
1074	else
1075	{
1076		return FALSE;
1077	}
1078}
1079
1080//-----------------------------------------------------------------------------
1081void LLVOAvatarSelf::addAttachmentRequest(const LLUUID& inv_item_id)
1082{
1083	LLTimer current_time;
1084	mAttachmentRequests[inv_item_id] = current_time;
1085}
1086
1087//-----------------------------------------------------------------------------
1088void LLVOAvatarSelf::removeAttachmentRequest(const LLUUID& inv_item_id)
1089{
1090	mAttachmentRequests.erase(inv_item_id);
1091}
1092
1093//-----------------------------------------------------------------------------
1094// getWornAttachment()
1095//-----------------------------------------------------------------------------
1096LLViewerObject* LLVOAvatarSelf::getWornAttachment(const LLUUID& inv_item_id)
1097{
1098	const LLUUID& base_inv_item_id = gInventory.getLinkedItemID(inv_item_id);
1099	for (attachment_map_t::const_iterator iter = mAttachmentPoints.begin(); 
1100		 iter != mAttachmentPoints.end();
1101		 ++iter)
1102	{
1103		LLViewerJointAttachment* attachment = iter->second;
1104 		if (LLViewerObject *attached_object = attachment->getAttachedObject(base_inv_item_id))
1105		{
1106			return attached_object;
1107		}
1108	}
1109	return NULL;
1110}
1111
1112const std::string LLVOAvatarSelf::getAttachedPointName(const LLUUID& inv_item_id) const
1113{
1114	const LLUUID& base_inv_item_id = gInventory.getLinkedItemID(inv_item_id);
1115	for (attachment_map_t::const_iterator iter = mAttachmentPoints.begin(); 
1116		 iter != mAttachmentPoints.end(); 
1117		 ++iter)
1118	{
1119		const LLViewerJointAttachment* attachment = iter->second;
1120		if (attachment->getAttachedObject(base_inv_item_id))
1121		{
1122			return attachment->getName();
1123		}
1124	}
1125
1126	return LLStringUtil::null;
1127}
1128
1129//virtual
1130const LLViewerJointAttachment *LLVOAvatarSelf::attachObject(LLViewerObject *viewer_object)
1131{
1132	const LLViewerJointAttachment *attachment = LLVOAvatar::attachObject(viewer_object);
1133	if (!attachment)
1134	{
1135		return 0;
1136	}
1137
1138	updateAttachmentVisibility(gAgentCamera.getCameraMode());
1139	
1140	// Then make sure the inventory is in sync with the avatar.
1141
1142	// Should just be the last object added
1143	if (attachment->isObjectAttached(viewer_object))
1144	{
1145		const LLUUID& attachment_id = viewer_object->getAttachmentItemID();
1146		LLAppearanceMgr::instance().registerAttachment(attachment_id);
1147		// Clear any pending requests once the attachment arrives.
1148		removeAttachmentRequest(attachment_id);
1149		updateLODRiggedAttachments();		
1150	}
1151
1152	return attachment;
1153}
1154
1155//virtual
1156BOOL LLVOAvatarSelf::detachObject(LLViewerObject *viewer_object)
1157{
1158	const LLUUID attachment_id = viewer_object->getAttachmentItemID();
1159	if ( LLVOAvatar::detachObject(viewer_object) )
1160	{
1161		LLVOAvatar::cleanupAttachedMesh( viewer_object );
1162		
1163		// the simulator should automatically handle permission revocation
1164		
1165		stopMotionFromSource(attachment_id);
1166		LLFollowCamMgr::setCameraActive(viewer_object->getID(), FALSE);
1167		
1168		LLViewerObject::const_child_list_t& child_list = viewer_object->getChildren();
1169		for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
1170			 iter != child_list.end(); 
1171			 ++iter)
1172		{
1173			LLViewerObject* child_objectp = *iter;
1174			// the simulator should automatically handle
1175			// permissions revocation
1176			
1177			stopMotionFromSource(child_objectp->getID());
1178			LLFollowCamMgr::setCameraActive(child_objectp->getID(), FALSE);
1179		}
1180		
1181		// Make sure the inventory is in sync with the avatar.
1182
1183		// Update COF contents, don't trigger appearance update.
1184		if (!isAgentAvatarValid())
1185		{
1186			llinfos << "removeItemLinks skipped, avatar is under destruction" << llendl;
1187		}
1188		else
1189		{
1190			LLAppearanceMgr::instance().unregisterAttachment(attachment_id);
1191		}
1192		
1193		return TRUE;
1194	}
1195	return FALSE;
1196}
1197
1198// static
1199BOOL LLVOAvatarSelf::detachAttachmentIntoInventory(const LLUUID &item_id)
1200{
1201	LLInventoryItem* item = gInventory.getItem(item_id);
1202	if (item)
1203	{
1204		gMessageSystem->newMessageFast(_PREHASH_DetachAttachmentIntoInv);
1205		gMessageSystem->nextBlockFast(_PREHASH_ObjectData);
1206		gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
1207		gMessageSystem->addUUIDFast(_PREHASH_ItemID, item_id);
1208		gMessageSystem->sendReliable(gAgent.getRegion()->getHost());
1209		
1210		// This object might have been selected, so let the selection manager know it's gone now
1211		LLViewerObject *found_obj = gObjectList.findObject(item_id);
1212		if (found_obj)
1213		{
1214			LLSelectMgr::getInstance()->remove(found_obj);
1215		}
1216
1217		// Error checking in case this object was attached to an invalid point
1218		// In that case, just remove the item from COF preemptively since detach 
1219		// will fail.
1220		if (isAgentAvatarValid())
1221		{
1222			const LLViewerObject *attached_obj = gAgentAvatarp->getWornAttachment(item_id);
1223			if (!attached_obj)
1224			{
1225				LLAppearanceMgr::instance().removeCOFItemLinks(item_id, false);
1226			}
1227		}
1228		return TRUE;
1229	}
1230	return FALSE;
1231}
1232
1233U32 LLVOAvatarSelf::getNumWearables(LLVOAvatarDefines::ETextureIndex i) const
1234{
1235	LLWearableType::EType type = LLVOAvatarDictionary::getInstance()->getTEWearableType(i);
1236	return gAgentWearables.getWearableCount(type);
1237}
1238
1239// virtual
1240void LLVOAvatarSelf::localTextureLoaded(BOOL success, LLViewerFetchedTexture *src_vi, LLImageRaw* src_raw, LLImageRaw* aux_src, S32 discard_level, BOOL final, void* userdata)
1241{	
1242
1243	const LLUUID& src_id = src_vi->getID();
1244	LLAvatarTexData *data = (LLAvatarTexData *)userdata;
1245	ETextureIndex index = data->mIndex;
1246	if (!isIndexLocalTexture(index)) return;
1247
1248	LLLocalTextureObject *local_tex_obj = getLocalTextureObject(index, 0);
1249
1250	// fix for EXT-268. Preventing using of NULL pointer
1251	if(NULL == local_tex_obj)
1252	{
1253		LL_WARNS("TAG") << "There is no Local Texture Object with index: " << index 
1254			<< ", final: " << final
1255			<< LL_ENDL;
1256		return;
1257	}
1258	if (success)
1259	{
1260		if (!local_tex_obj->getBakedReady() &&
1261			local_tex_obj->getImage() != NULL &&
1262			(local_tex_obj->getID() == src_id) &&
1263			discard_level < local_tex_obj->getDiscard())
1264		{
1265			local_tex_obj->setDiscard(discard_level);
1266			if (isUsingBakedTextures())
1267			{
1268				requestLayerSetUpdate(index);
1269			}
1270			else
1271			{
1272				LLVisualParamHint::requestHintUpdates();
1273			}
1274			updateMeshTextures();
1275		}
1276	}
1277	else if (final)
1278	{
1279		// Failed: asset is missing
1280		if (!local_tex_obj->getBakedReady() &&
1281			local_tex_obj->getImage() != NULL &&
1282			local_tex_obj->getImage()->getID() == src_id)
1283		{
1284			local_tex_obj->setDiscard(0);
1285			requestLayerSetUpdate(index);
1286			updateMeshTextures();
1287		}
1288	}
1289}
1290
1291// virtual
1292BOOL LLVOAvatarSelf::getLocalTextureGL(ETextureIndex type, LLViewerTexture** tex_pp, U32 index) const
1293{
1294	*tex_pp = NULL;
1295
1296	if (!isIndexLocalTexture(type)) return FALSE;
1297	if (getLocalTextureID(type, index) == IMG_DEFAULT_AVATAR) return TRUE;
1298
1299	const LLLocalTextureObject *local_tex_obj = getLocalTextureObject(type, index);
1300	if (!local_tex_obj)
1301	{
1302		return FALSE;
1303	}
1304	*tex_pp = local_tex_obj->getImage();
1305	return TRUE;
1306}
1307
1308LLViewerFetchedTexture* LLVOAvatarSelf::getLocalTextureGL(LLVOAvatarDefines::ETextureIndex type, U32 index) const
1309{
1310	if (!isIndexLocalTexture(type))
1311	{
1312		return NULL;
1313	}
1314
1315	const LLLocalTextureObject *local_tex_obj = getLocalTextureObject(type, index);
1316	if (!local_tex_obj)
1317	{
1318		return NULL;
1319	}
1320	if (local_tex_obj->getID() == IMG_DEFAULT_AVATAR)
1321	{
1322		return LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT_AVATAR);
1323	}
1324	return local_tex_obj->getImage();
1325}
1326
1327const LLUUID& LLVOAvatarSelf::getLocalTextureID(ETextureIndex type, U32 index) const
1328{
1329	if (!isIndexLocalTexture(type)) return IMG_DEFAULT_AVATAR;
1330
1331	const LLLocalTextureObject *local_tex_obj = getLocalTextureObject(type, index);
1332	if (local_tex_obj && local_tex_obj->getImage() != NULL)
1333	{
1334		return local_tex_obj->getImage()->getID();
1335	}
1336	return IMG_DEFAULT_AVATAR;
1337} 
1338
1339
1340//-----------------------------------------------------------------------------
1341// isLocalTextureDataAvailable()
1342// Returns true if at least the lowest quality discard level exists for every texture
1343// in the layerset.
1344//-----------------------------------------------------------------------------
1345BOOL LLVOAvatarSelf::isLocalTextureDataAvailable(const LLTexLayerSet* layerset) const
1346{
1347	/* if (layerset == mBakedTextureDatas[BAKED_HEAD].mTexLayerSet)
1348	   return getLocalDiscardLevel(TEX_HEAD_BODYPAINT) >= 0; */
1349	for (LLVOAvatarDictionary::BakedTextures::const_iterator baked_iter = LLVOAvatarDictionary::getInstance()->getBakedTextures().begin();
1350		 baked_iter != LLVOAvatarDictionary::getInstance()->getBakedTextures().end();
1351		 ++baked_iter)
1352	{
1353		const EBakedTextureIndex baked_index = baked_iter->first;
1354		if (layerset == mBakedTextureDatas[baked_index].mTexLayerSet)
1355		{
1356			BOOL ret = true;
1357			const LLVOAvatarDictionary::BakedEntry *baked_dict = baked_iter->second;
1358			for (texture_vec_t::const_iterator local_tex_iter = baked_dict->mLocalTextures.begin();
1359				 local_tex_iter != baked_dict->mLocalTextures.end();
1360				 ++local_tex_iter)
1361			{
1362				const ETextureIndex tex_index = *local_tex_iter;
1363				const LLWearableType::EType wearable_type = LLVOAvatarDictionary::getTEWearableType(tex_index);
1364				const U32 wearable_count = gAgentWearables.getWearableCount(wearable_type);
1365				for (U32 wearable_index = 0; wearable_index < wearable_count; wearable_index++)
1366				{
1367					ret &= (getLocalDiscardLevel(tex_index, wearable_index) >= 0);
1368				}
1369			}
1370			return ret;
1371		}
1372	}
1373	llassert(0);
1374	return FALSE;
1375}
1376
1377//-----------------------------------------------------------------------------
1378// virtual
1379// isLocalTextureDataFinal()
1380// Returns true if the highest quality discard level exists for every texture
1381// in the layerset.
1382//-----------------------------------------------------------------------------
1383BOOL LLVOAvatarSelf::isLocalTextureDataFinal(const LLTexLayerSet* layerset) const
1384{
1385	const U32 desired_tex_discard_level = gSavedSettings.getU32("TextureDiscardLevel"); 
1386	// const U32 desired_tex_discard_level = 0; // hack to not bake textures on lower discard levels.
1387
1388	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1389	{
1390		if (layerset == mBakedTextureDatas[i].mTexLayerSet)
1391		{
1392			const LLVOAvatarDictionary::BakedEntry *baked_dict = LLVOAvatarDictionary::getInstance()->getBakedTexture((EBakedTextureIndex)i);
1393			for (texture_vec_t::const_iterator local_tex_iter = baked_dict->mLocalTextures.begin();
1394				 local_tex_iter != baked_dict->mLocalTextures.end();
1395				 ++local_tex_iter)
1396			{
1397				const ETextureIndex tex_index = *local_tex_iter;
1398				const LLWearableType::EType wearable_type = LLVOAvatarDictionary::getTEWearableType(tex_index);
1399				const U32 wearable_count = gAgentWearables.getWearableCount(wearable_type);
1400				for (U32 wearable_index = 0; wearable_index < wearable_count; wearable_index++)
1401				{
1402					if (getLocalDiscardLevel(*local_tex_iter, wearable_index) > (S32)(desired_tex_discard_level))
1403					{
1404						return FALSE;
1405					}
1406				}
1407			}
1408			return TRUE;
1409		}
1410	}
1411	llassert(0);
1412	return FALSE;
1413}
1414
1415BOOL LLVOAvatarSelf::isAllLocalTextureDataFinal() const
1416{
1417	const U32 desired_tex_discard_level = gSavedSettings.getU32("TextureDiscardLevel"); 
1418	// const U32 desired_tex_discard_level = 0; // hack to not bake textures on lower discard levels
1419
1420	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1421	{
1422		const LLVOAvatarDictionary::BakedEntry *baked_dict = LLVOAvatarDictionary::getInstance()->getBakedTexture((EBakedTextureIndex)i);
1423		for (texture_vec_t::const_iterator local_tex_iter = baked_dict->mLocalTextures.begin();
1424			 local_tex_iter != baked_dict->mLocalTextures.end();
1425			 ++local_tex_iter)
1426		{
1427			const ETextureIndex tex_index = *local_tex_iter;
1428			const LLWearableType::EType wearable_type = LLVOAvatarDictionary::getTEWearableType(tex_index);
1429			const U32 wearable_count = gAgentWearables.getWearableCount(wearable_type);
1430			for (U32 wearable_index = 0; wearable_index < wearable_count; wearable_index++)
1431			{
1432				if (getLocalDiscardLevel(*local_tex_iter, wearable_index) > (S32)(desired_tex_discard_level))
1433				{
1434					return FALSE;
1435				}
1436			}
1437		}
1438	}
1439	return TRUE;
1440}
1441
1442BOOL LLVOAvatarSelf::isBakedTextureFinal(const LLVOAvatarDefines::EBakedTextureIndex index) const
1443{
1444	const LLTexLayerSet *layerset = mBakedTextureDatas[index].mTexLayerSet;
1445	if (!layerset) return FALSE;
1446	const LLTexLayerSetBuffer *layerset_buffer = layerset->getComposite();
1447	if (!layerset_buffer) return FALSE;
1448	return !layerset_buffer->uploadNeeded();
1449}
1450
1451BOOL LLVOAvatarSelf::isTextureDefined(LLVOAvatarDefines::ETextureIndex type, U32 index) const
1452{
1453	LLUUID id;
1454	BOOL isDefined = TRUE;
1455	if (isIndexLocalTexture(type))
1456	{
1457		const LLWearableType::EType wearable_type = LLVOAvatarDictionary::getTEWearableType(type);
1458		const U32 wearable_count = gAgentWearables.getWearableCount(wearable_type);
1459		if (index >= wearable_count)
1460		{
1461			// invalid index passed in. check all textures of a given type
1462			for (U32 wearable_index = 0; wearable_index < wearable_count; wearable_index++)
1463			{
1464				id = getLocalTextureID(type, wearable_index);
1465				isDefined &= (id != IMG_DEFAULT_AVATAR && id != IMG_DEFAULT);
1466			}
1467		}
1468		else
1469		{
1470			id = getLocalTextureID(type, index);
1471			isDefined &= (id != IMG_DEFAULT_AVATAR && id != IMG_DEFAULT);
1472		}
1473	}
1474	else
1475	{
1476		id = getTEImage(type)->getID();
1477		isDefined &= (id != IMG_DEFAULT_AVATAR && id != IMG_DEFAULT);
1478	}
1479	
1480	return isDefined;
1481}
1482
1483//virtual
1484BOOL LLVOAvatarSelf::isTextureVisible(LLVOAvatarDefines::ETextureIndex type, U32 index) const
1485{
1486	if (isIndexBakedTexture(type))
1487	{
1488		return LLVOAvatar::isTextureVisible(type, (U32)0);
1489	}
1490
1491	LLUUID tex_id = getLocalTextureID(type,index);
1492	return (tex_id != IMG_INVISIBLE) 
1493			|| (LLDrawPoolAlpha::sShowDebugAlpha);
1494}
1495
1496//virtual
1497BOOL LLVOAvatarSelf::isTextureVisible(LLVOAvatarDefines::ETextureIndex type, LLWearable *wearable) const
1498{
1499	if (isIndexBakedTexture(type))
1500	{
1501		return LLVOAvatar::isTextureVisible(type);
1502	}
1503
1504	U32 index = gAgentWearables.getWearableIndex(wearable);
1505	return isTextureVisible(type,index);
1506}
1507
1508
1509//-----------------------------------------------------------------------------
1510// requestLayerSetUploads()
1511//-----------------------------------------------------------------------------
1512void LLVOAvatarSelf::requestLayerSetUploads()
1513{
1514	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1515	{
1516		requestLayerSetUpload((EBakedTextureIndex)i);
1517	}
1518}
1519
1520void LLVOAvatarSelf::requestLayerSetUpload(LLVOAvatarDefines::EBakedTextureIndex i)
1521{
1522	ETextureIndex tex_index = mBakedTextureDatas[i].mTextureIndex;
1523	const BOOL layer_baked = isTextureDefined(tex_index, gAgentWearables.getWearableCount(tex_index));
1524	if (!layer_baked && mBakedTextureDatas[i].mTexLayerSet)
1525	{
1526		mBakedTextureDatas[i].mTexLayerSet->requestUpload();
1527	}
1528}
1529
1530bool LLVOAvatarSelf::areTexturesCurrent() const
1531{
1532	return !hasPendingBakedUploads() && gAgentWearables.areWearablesLoaded();
1533}
1534
1535// virtual
1536bool LLVOAvatarSelf::hasPendingBakedUploads() const
1537{
1538	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1539	{
1540		LLTexLayerSet* layerset = mBakedTextureDatas[i].mTexLayerSet;
1541		if (layerset && layerset->getComposite() && layerset->getComposite()->uploadPending())
1542		{
1543			return true;
1544		}
1545	}
1546	return false;
1547}
1548
1549void LLVOAvatarSelf::invalidateComposite( LLTexLayerSet* layerset, BOOL upload_result )
1550{
1551	if( !layerset || !layerset->getUpdatesEnabled() )
1552	{
1553		return;
1554	}
1555	// llinfos << "LLVOAvatar::invalidComposite() " << layerset->getBodyRegionName() << llendl;
1556
1557	layerset->requestUpdate();
1558	layerset->invalidateMorphMasks();
1559
1560	if( upload_result )
1561	{
1562		llassert(isSelf());
1563
1564		ETextureIndex baked_te = getBakedTE( layerset );
1565		setTEImage( baked_te, LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT_AVATAR) );
1566		layerset->requestUpload();
1567		updateMeshTextures();
1568	}
1569}
1570
1571void LLVOAvatarSelf::invalidateAll()
1572{
1573	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1574	{
1575		invalidateComposite(mBakedTextureDatas[i].mTexLayerSet, TRUE);
1576	}
1577	mDebugSelfLoadTimer.reset();
1578}
1579
1580//-----------------------------------------------------------------------------
1581// setCompositeUpdatesEnabled()
1582//-----------------------------------------------------------------------------
1583void LLVOAvatarSelf::setCompositeUpdatesEnabled( bool b )
1584{
1585	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1586	{
1587		setCompositeUpdatesEnabled(i, b);
1588	}
1589}
1590
1591void LLVOAvatarSelf::setCompositeUpdatesEnabled(U32 index, bool b)
1592{
1593	if (mBakedTextureDatas[index].mTexLayerSet )
1594	{
1595		mBakedTextureDatas[index].mTexLayerSet->setUpdatesEnabled( b );
1596	}
1597}
1598
1599bool LLVOAvatarSelf::isCompositeUpdateEnabled(U32 index)
1600{
1601	if (mBakedTextureDatas[index].mTexLayerSet)
1602	{
1603		return mBakedTextureDatas[index].mTexLayerSet->getUpdatesEnabled();
1604	}
1605	return false;
1606}
1607
1608void LLVOAvatarSelf::setupComposites()
1609{
1610	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1611	{
1612		ETextureIndex tex_index = mBakedTextureDatas[i].mTextureIndex;
1613		BOOL layer_baked = isTextureDefined(tex_index, gAgentWearables.getWearableCount(tex_index));
1614		if (mBakedTextureDatas[i].mTexLayerSet)
1615		{
1616			mBakedTextureDatas[i].mTexLayerSet->setUpdatesEnabled(!layer_baked);
1617		}
1618	}
1619}
1620
1621void LLVOAvatarSelf::updateComposites()
1622{
1623	for (U32 i = 0; i < mBakedTextureDatas.size(); i++)
1624	{
1625		if (mBakedTextureDatas[i].mTexLayerSet 
1626			&& ((i != BAKED_SKIRT) || isWearingWearableType(LLWearableType::WT_SKIRT)))
1627		{
1628			mBakedTextureDatas[i].mTexLayerSet->updateComposite();
1629		}
1630	}
1631}
1632
1633// virtual
1634S32 LLVOAvatarSelf::getLocalDiscardLevel(ETextureIndex type, U32 wearable_index) const
1635{
1636	if (!isIndexLocalTexture(type)) return FALSE;
1637
1638	const LLLocalTextureObject *local_tex_obj = getLocalTextureObject(type, wearable_index);
1639	if (local_tex_obj)
1640	{
1641		if (type >= 0
1642			&& local_tex_obj->getID() != IMG_DEFAULT_AVATAR
1643			&& !local_tex_obj->getImage()->isMissingAsset())
1644		{
1645			return local_tex_obj->getImage()->getDiscardLevel();
1646		}
1647		else
1648		{
1649			// We don't care about this (no image associated with the layer) treat as fully loaded.
1650			return 0;
1651		}
1652	}
1653	return 0;
1654}
1655
1656// virtual
1657// Counts the memory footprint of local textures.
1658void LLVOAvatarSelf::getLocalTextureByteCount(S32* gl_bytes) const
1659{
1660	*gl_bytes = 0;
1661	for (S32 type = 0; type < TEX_NUM_INDICES; type++)
1662	{
1663		if (!isIndexLocalTexture((ETextureIndex)type)) continue;
1664		U32 max_tex = getNumWearables((ETextureIndex) type);
1665		for (U32 num = 0; num < max_tex; num++)
1666		{
1667			const LLLocalTextureObject *local_tex_obj = getLocalTextureObject((ETextureIndex) type, num);
1668			if (local_tex_obj)
1669			{
1670				const LLViewerFetchedTexture* image_gl = local_tex_obj->getImage();
1671				if (image_gl)
1672				{
1673					S32 bytes = (S32)image_gl->getWidth() * 

Large files files are truncated, but you can click here to view the full file