PageRenderTime 147ms CodeModel.GetById 20ms app.highlight 76ms RepoModel.GetById 15ms app.codeStats 0ms

/indra/newview/llhudeffectlookat.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 671 lines | 463 code | 90 blank | 118 comment | 109 complexity | 058b700f5f821c053c24ae1d45bcec6b MD5 | raw file
  1/** 
  2 * @file llhudeffectlookat.cpp
  3 * @brief LLHUDEffectLookAt class implementation
  4 *
  5 * $LicenseInfo:firstyear=2002&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#include "llviewerprecompiledheaders.h"
 28
 29#include "llhudeffectlookat.h"
 30
 31#include "llrender.h"
 32
 33#include "message.h"
 34#include "llagent.h"
 35#include "llagentcamera.h"
 36#include "llvoavatar.h"
 37#include "lldrawable.h"
 38#include "llviewerobjectlist.h"
 39#include "llrendersphere.h"
 40#include "llselectmgr.h"
 41#include "llglheaders.h"
 42
 43
 44#include "llxmltree.h"
 45
 46
 47BOOL LLHUDEffectLookAt::sDebugLookAt = FALSE;
 48
 49// packet layout
 50const S32 SOURCE_AVATAR = 0;
 51const S32 TARGET_OBJECT = 16;
 52const S32 TARGET_POS = 32;
 53const S32 LOOKAT_TYPE = 56;
 54const S32 PKT_SIZE = 57;
 55
 56// throttle
 57const F32 MAX_SENDS_PER_SEC = 4.f;
 58
 59const F32 MIN_DELTAPOS_FOR_UPDATE_SQUARED = 0.05f * 0.05f;
 60const F32 MIN_TARGET_OFFSET_SQUARED = 0.0001f;
 61
 62
 63// can't use actual F32_MAX, because we add this to the current frametime
 64const F32 MAX_TIMEOUT = F32_MAX / 2.f;
 65
 66/**
 67 * Simple data class holding values for a particular type of attention.
 68 */
 69class LLAttention
 70{
 71public:
 72	LLAttention()
 73		: mTimeout(0.f),
 74		  mPriority(0.f)
 75	{}
 76	LLAttention(F32 timeout, F32 priority, const std::string& name, LLColor3 color) :
 77	  mTimeout(timeout), mPriority(priority), mName(name), mColor(color)
 78	{
 79	}
 80	F32 mTimeout, mPriority;
 81	std::string mName;
 82	LLColor3 mColor;
 83};
 84
 85/**
 86 * Simple data class holding a list of attentions, one for every type.
 87 */
 88class LLAttentionSet
 89{
 90public:
 91	LLAttentionSet(const LLAttention attentions[])
 92	{
 93		for(int i=0; i<LOOKAT_NUM_TARGETS; i++)
 94		{
 95			mAttentions[i] = attentions[i];
 96		}
 97	}
 98	LLAttention mAttentions[LOOKAT_NUM_TARGETS];
 99	LLAttention& operator[](int idx) { return mAttentions[idx]; }
100};
101
102// Default attribute set data.
103// Used to initialize the global attribute set objects, one of which will be
104// refered to by the hud object at any given time.
105// Note that the values below are only the default values and that any or all of them
106// can be overwritten with customizing data from the XML file. The actual values below
107// are those that will give exactly the same look-at behavior as before the ability
108// to customize was added. - MG
109static const 
110	LLAttention 
111		BOY_ATTS[] = { // default set of masculine attentions
112			LLAttention(MAX_TIMEOUT, 0, "None",			 LLColor3(0.3f, 0.3f, 0.3f)), // LOOKAT_TARGET_NONE
113			LLAttention(3.f,         1, "Idle",		     LLColor3(0.5f, 0.5f, 0.5f)), // LOOKAT_TARGET_IDLE
114			LLAttention(4.f,         3, "AutoListen",	 LLColor3(0.5f, 0.5f, 0.5f)), // LOOKAT_TARGET_AUTO_LISTEN
115			LLAttention(2.f,         2, "FreeLook",		 LLColor3(0.5f, 0.5f, 0.9f)), // LOOKAT_TARGET_FREELOOK
116			LLAttention(4.f,         3, "Respond",	     LLColor3(0.0f, 0.0f, 0.0f)), // LOOKAT_TARGET_RESPOND
117			LLAttention(1.f,         4, "Hover",		 LLColor3(0.5f, 0.9f, 0.5f)), // LOOKAT_TARGET_HOVER
118			LLAttention(MAX_TIMEOUT, 0, "Conversation",  LLColor3(0.1f, 0.1f, 0.5f)), // LOOKAT_TARGET_CONVERSATION
119			LLAttention(MAX_TIMEOUT, 6, "Select",		 LLColor3(0.9f, 0.5f, 0.5f)), // LOOKAT_TARGET_SELECT
120			LLAttention(MAX_TIMEOUT, 6, "Focus",		 LLColor3(0.9f, 0.5f, 0.9f)), // LOOKAT_TARGET_FOCUS
121			LLAttention(MAX_TIMEOUT, 7, "Mouselook",	 LLColor3(0.9f, 0.9f, 0.5f)), // LOOKAT_TARGET_MOUSELOOK
122			LLAttention(0.f,         8, "Clear",		 LLColor3(1.0f, 1.0f, 1.0f)), // LOOKAT_TARGET_CLEAR
123		},																				
124		GIRL_ATTS[] = { // default set of feminine attentions													
125			LLAttention(MAX_TIMEOUT, 0, "None",			 LLColor3(0.3f, 0.3f, 0.3f)), // LOOKAT_TARGET_NONE
126			LLAttention(3.f,         1, "Idle",		     LLColor3(0.5f, 0.5f, 0.5f)), // LOOKAT_TARGET_IDLE
127			LLAttention(4.f,         3, "AutoListen",	 LLColor3(0.5f, 0.5f, 0.5f)), // LOOKAT_TARGET_AUTO_LISTEN
128			LLAttention(2.f,         2, "FreeLook",		 LLColor3(0.5f, 0.5f, 0.9f)), // LOOKAT_TARGET_FREELOOK
129			LLAttention(4.f,         3, "Respond",	     LLColor3(0.0f, 0.0f, 0.0f)), // LOOKAT_TARGET_RESPOND
130			LLAttention(1.f,         4, "Hover",		 LLColor3(0.5f, 0.9f, 0.5f)), // LOOKAT_TARGET_HOVER
131			LLAttention(MAX_TIMEOUT, 0, "Conversation",  LLColor3(0.1f, 0.1f, 0.5f)), // LOOKAT_TARGET_CONVERSATION
132			LLAttention(MAX_TIMEOUT, 6, "Select",		 LLColor3(0.9f, 0.5f, 0.5f)), // LOOKAT_TARGET_SELECT
133			LLAttention(MAX_TIMEOUT, 6, "Focus",		 LLColor3(0.9f, 0.5f, 0.9f)), // LOOKAT_TARGET_FOCUS
134			LLAttention(MAX_TIMEOUT, 7, "Mouselook",	 LLColor3(0.9f, 0.9f, 0.5f)), // LOOKAT_TARGET_MOUSELOOK
135			LLAttention(0.f,         8, "Clear",		 LLColor3(1.0f, 1.0f, 1.0f)), // LOOKAT_TARGET_CLEAR
136		};
137
138static LLAttentionSet
139	gBoyAttentions(BOY_ATTS),
140	gGirlAttentions(GIRL_ATTS);
141
142
143static BOOL loadGender(LLXmlTreeNode* gender)
144{
145	if( !gender)
146	{
147		return FALSE;
148	}
149	std::string str;
150	gender->getAttributeString("name", str);
151	LLAttentionSet& attentions = (str.compare("Masculine") == 0) ? gBoyAttentions : gGirlAttentions;
152	for (LLXmlTreeNode* attention_node = gender->getChildByName( "param" );
153		 attention_node;
154		 attention_node = gender->getNextNamedChild())
155	{
156		attention_node->getAttributeString("attention", str);
157		LLAttention* attention;
158		if     (str == "idle")         attention = &attentions[LOOKAT_TARGET_IDLE];
159		else if(str == "auto_listen")  attention = &attentions[LOOKAT_TARGET_AUTO_LISTEN];
160		else if(str == "freelook")     attention = &attentions[LOOKAT_TARGET_FREELOOK];
161		else if(str == "respond")      attention = &attentions[LOOKAT_TARGET_RESPOND];
162		else if(str == "hover")        attention = &attentions[LOOKAT_TARGET_HOVER];
163		else if(str == "conversation") attention = &attentions[LOOKAT_TARGET_CONVERSATION];
164		else if(str == "select")       attention = &attentions[LOOKAT_TARGET_SELECT];
165		else if(str == "focus")        attention = &attentions[LOOKAT_TARGET_FOCUS];
166		else if(str == "mouselook")    attention = &attentions[LOOKAT_TARGET_MOUSELOOK];
167		else return FALSE;
168
169		F32 priority, timeout;
170		attention_node->getAttributeF32("priority", priority);
171		attention_node->getAttributeF32("timeout", timeout);
172		if(timeout < 0) timeout = MAX_TIMEOUT;
173		attention->mPriority = priority;
174		attention->mTimeout = timeout;
175	}	
176	return TRUE;
177}
178
179static BOOL loadAttentions()
180{
181	static BOOL first_time = TRUE;
182	if( ! first_time) 
183	{
184		return TRUE; // maybe not ideal but otherwise it can continue to fail forever.
185	}
186	first_time = FALSE;
187	
188	std::string filename;
189	filename = gDirUtilp->getExpandedFilename(LL_PATH_CHARACTER,"attentions.xml");
190	LLXmlTree xml_tree;
191	BOOL success = xml_tree.parseFile( filename, FALSE );
192	if( !success )
193	{
194		return FALSE;
195	}
196	LLXmlTreeNode* root = xml_tree.getRoot();
197	if( !root )
198	{
199		return FALSE;
200	}
201
202	//-------------------------------------------------------------------------
203	// <linden_attentions version="1.0"> (root)
204	//-------------------------------------------------------------------------
205	if( !root->hasName( "linden_attentions" ) )
206	{
207		llwarns << "Invalid linden_attentions file header: " << filename << llendl;
208		return FALSE;
209	}
210
211	std::string version;
212	static LLStdStringHandle version_string = LLXmlTree::addAttributeString("version");
213	if( !root->getFastAttributeString( version_string, version ) || (version != "1.0") )
214	{
215		llwarns << "Invalid linden_attentions file version: " << version << llendl;
216		return FALSE;
217	}
218
219	//-------------------------------------------------------------------------
220	// <gender>
221	//-------------------------------------------------------------------------
222	for (LLXmlTreeNode* child = root->getChildByName( "gender" );
223		 child;
224		 child = root->getNextNamedChild())
225	{
226		if( !loadGender( child ) )
227		{
228			return FALSE;
229		}
230	}
231
232	return TRUE;
233}
234
235
236
237
238//-----------------------------------------------------------------------------
239// LLHUDEffectLookAt()
240//-----------------------------------------------------------------------------
241LLHUDEffectLookAt::LLHUDEffectLookAt(const U8 type) : 
242	LLHUDEffect(type), 
243	mKillTime(0.f),
244	mLastSendTime(0.f)
245{
246	clearLookAtTarget();
247	// parse the default sets
248	loadAttentions();
249	// initialize current attention set. switches when avatar sex changes.
250	mAttentions = &gGirlAttentions;
251}
252
253//-----------------------------------------------------------------------------
254// ~LLHUDEffectLookAt()
255//-----------------------------------------------------------------------------
256LLHUDEffectLookAt::~LLHUDEffectLookAt()
257{
258}
259
260//-----------------------------------------------------------------------------
261// packData()
262//-----------------------------------------------------------------------------
263void LLHUDEffectLookAt::packData(LLMessageSystem *mesgsys)
264{
265	// Pack the default data
266	LLHUDEffect::packData(mesgsys);
267
268	// Pack the type-specific data.  Uses a fun packed binary format.  Whee!
269	U8 packed_data[PKT_SIZE];
270	memset(packed_data, 0, PKT_SIZE);
271
272	if (mSourceObject)
273	{
274		htonmemcpy(&(packed_data[SOURCE_AVATAR]), mSourceObject->mID.mData, MVT_LLUUID, 16);
275	}
276	else
277	{
278		htonmemcpy(&(packed_data[SOURCE_AVATAR]), LLUUID::null.mData, MVT_LLUUID, 16);
279	}
280
281	// pack both target object and position
282	// position interpreted as offset if target object is non-null
283	if (mTargetObject)
284	{
285		htonmemcpy(&(packed_data[TARGET_OBJECT]), mTargetObject->mID.mData, MVT_LLUUID, 16);
286	}
287	else
288	{
289		htonmemcpy(&(packed_data[TARGET_OBJECT]), LLUUID::null.mData, MVT_LLUUID, 16);
290	}
291
292	htonmemcpy(&(packed_data[TARGET_POS]), mTargetOffsetGlobal.mdV, MVT_LLVector3d, 24);
293
294	U8 lookAtTypePacked = (U8)mTargetType;
295	
296	htonmemcpy(&(packed_data[LOOKAT_TYPE]), &lookAtTypePacked, MVT_U8, 1);
297
298	mesgsys->addBinaryDataFast(_PREHASH_TypeData, packed_data, PKT_SIZE);
299
300	mLastSendTime = mTimer.getElapsedTimeF32();
301}
302
303//-----------------------------------------------------------------------------
304// unpackData()
305//-----------------------------------------------------------------------------
306void LLHUDEffectLookAt::unpackData(LLMessageSystem *mesgsys, S32 blocknum)
307{
308	LLVector3d new_target;
309	U8 packed_data[PKT_SIZE];
310
311	LLUUID dataId;
312	mesgsys->getUUIDFast(_PREHASH_Effect, _PREHASH_ID, dataId, blocknum);
313
314	if (!gAgentCamera.mLookAt.isNull() && dataId == gAgentCamera.mLookAt->getID())
315	{
316		return;
317	}
318
319	LLHUDEffect::unpackData(mesgsys, blocknum);
320	LLUUID source_id;
321	LLUUID target_id;
322	S32 size = mesgsys->getSizeFast(_PREHASH_Effect, blocknum, _PREHASH_TypeData);
323	if (size != PKT_SIZE)
324	{
325		llwarns << "LookAt effect with bad size " << size << llendl;
326		return;
327	}
328	mesgsys->getBinaryDataFast(_PREHASH_Effect, _PREHASH_TypeData, packed_data, PKT_SIZE, blocknum);
329	
330	htonmemcpy(source_id.mData, &(packed_data[SOURCE_AVATAR]), MVT_LLUUID, 16);
331
332	LLViewerObject *objp = gObjectList.findObject(source_id);
333	if (objp && objp->isAvatar())
334	{
335		setSourceObject(objp);
336	}
337	else
338	{
339		//llwarns << "Could not find source avatar for lookat effect" << llendl;
340		return;
341	}
342
343	htonmemcpy(target_id.mData, &(packed_data[TARGET_OBJECT]), MVT_LLUUID, 16);
344
345	objp = gObjectList.findObject(target_id);
346
347	htonmemcpy(new_target.mdV, &(packed_data[TARGET_POS]), MVT_LLVector3d, 24);
348
349	if (objp)
350	{
351		setTargetObjectAndOffset(objp, new_target);
352	}
353	else if (target_id.isNull())
354	{
355		setTargetPosGlobal(new_target);
356	}
357	else
358	{
359		//llwarns << "Could not find target object for lookat effect" << llendl;
360	}
361
362	U8 lookAtTypeUnpacked = 0;
363	htonmemcpy(&lookAtTypeUnpacked, &(packed_data[LOOKAT_TYPE]), MVT_U8, 1);
364	mTargetType = (ELookAtType)lookAtTypeUnpacked;
365
366	if (mTargetType == LOOKAT_TARGET_NONE)
367	{
368		clearLookAtTarget();
369	}
370}
371
372//-----------------------------------------------------------------------------
373// setTargetObjectAndOffset()
374//-----------------------------------------------------------------------------
375void LLHUDEffectLookAt::setTargetObjectAndOffset(LLViewerObject *objp, LLVector3d offset)
376{
377	mTargetObject = objp;
378	mTargetOffsetGlobal = offset;
379}
380
381//-----------------------------------------------------------------------------
382// setTargetPosGlobal()
383//-----------------------------------------------------------------------------
384void LLHUDEffectLookAt::setTargetPosGlobal(const LLVector3d &target_pos_global)
385{
386	mTargetObject = NULL;
387	mTargetOffsetGlobal = target_pos_global;
388}
389
390//-----------------------------------------------------------------------------
391// setLookAt()
392// called by agent logic to set look at behavior locally, and propagate to sim
393//-----------------------------------------------------------------------------
394BOOL LLHUDEffectLookAt::setLookAt(ELookAtType target_type, LLViewerObject *object, LLVector3 position)
395{
396	if (!mSourceObject)
397	{
398		return FALSE;
399	}
400	
401	if (target_type >= LOOKAT_NUM_TARGETS)
402	{
403		llwarns << "Bad target_type " << (int)target_type << " - ignoring." << llendl;
404		return FALSE;
405	}
406
407	// must be same or higher priority than existing effect
408	if ((*mAttentions)[target_type].mPriority < (*mAttentions)[mTargetType].mPriority)
409	{
410		return FALSE;
411	}
412
413	F32 current_time  = mTimer.getElapsedTimeF32();
414
415	// type of lookat behavior or target object has changed
416	BOOL lookAtChanged = (target_type != mTargetType) || (object != mTargetObject);
417
418	// lookat position has moved a certain amount and we haven't just sent an update
419	lookAtChanged = lookAtChanged || ((dist_vec_squared(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE_SQUARED) && 
420		((current_time - mLastSendTime) > (1.f / MAX_SENDS_PER_SEC)));
421
422	if (lookAtChanged)
423	{
424		mLastSentOffsetGlobal = position;
425		F32 timeout = (*mAttentions)[target_type].mTimeout;
426		setDuration(timeout);
427		setNeedsSendToSim(TRUE);
428	}
429 
430	if (target_type == LOOKAT_TARGET_CLEAR)
431	{
432		clearLookAtTarget();
433	}
434	else
435	{
436		mTargetType = target_type;
437		mTargetObject = object;
438		if (object)
439		{
440			mTargetOffsetGlobal.setVec(position);
441		}
442		else
443		{
444			mTargetOffsetGlobal = gAgent.getPosGlobalFromAgent(position);
445		}
446		mKillTime = mTimer.getElapsedTimeF32() + mDuration;
447
448		update();
449	}
450	return TRUE;
451}
452
453//-----------------------------------------------------------------------------
454// clearLookAtTarget()
455//-----------------------------------------------------------------------------
456void LLHUDEffectLookAt::clearLookAtTarget()
457{
458	mTargetObject = NULL;
459	mTargetOffsetGlobal.clearVec();
460	mTargetType = LOOKAT_TARGET_NONE;
461	if (mSourceObject.notNull())
462	{
463		((LLVOAvatar*)(LLViewerObject*)mSourceObject)->stopMotion(ANIM_AGENT_HEAD_ROT);
464	}
465}
466
467//-----------------------------------------------------------------------------
468// markDead()
469//-----------------------------------------------------------------------------
470void LLHUDEffectLookAt::markDead()
471{
472	if (mSourceObject.notNull())
473	{
474		((LLVOAvatar*)(LLViewerObject*)mSourceObject)->removeAnimationData("LookAtPoint");
475	}
476
477	mSourceObject = NULL;
478	clearLookAtTarget();
479	LLHUDEffect::markDead();
480}
481
482void LLHUDEffectLookAt::setSourceObject(LLViewerObject* objectp)
483{
484	// restrict source objects to avatars
485	if (objectp && objectp->isAvatar())
486	{
487		LLHUDEffect::setSourceObject(objectp);
488	}
489}
490
491//-----------------------------------------------------------------------------
492// render()
493//-----------------------------------------------------------------------------
494void LLHUDEffectLookAt::render()
495{
496	if (sDebugLookAt && mSourceObject.notNull())
497	{
498		gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
499
500		LLVector3 target = mTargetPos + ((LLVOAvatar*)(LLViewerObject*)mSourceObject)->mHeadp->getWorldPosition();
501		gGL.matrixMode(LLRender::MM_MODELVIEW);
502		gGL.pushMatrix();
503		gGL.translatef(target.mV[VX], target.mV[VY], target.mV[VZ]);
504		gGL.scalef(0.3f, 0.3f, 0.3f);
505		gGL.begin(LLRender::LINES);
506		{
507			LLColor3 color = (*mAttentions)[mTargetType].mColor;
508			gGL.color3f(color.mV[VRED], color.mV[VGREEN], color.mV[VBLUE]);
509			gGL.vertex3f(-1.f, 0.f, 0.f);
510			gGL.vertex3f(1.f, 0.f, 0.f);
511
512			gGL.vertex3f(0.f, -1.f, 0.f);
513			gGL.vertex3f(0.f, 1.f, 0.f);
514
515			gGL.vertex3f(0.f, 0.f, -1.f);
516			gGL.vertex3f(0.f, 0.f, 1.f);
517		} gGL.end();
518		gGL.popMatrix();
519	}
520}
521
522//-----------------------------------------------------------------------------
523// update()
524//-----------------------------------------------------------------------------
525void LLHUDEffectLookAt::update()
526{
527	// If the target object is dead, set the target object to NULL
528	if (!mTargetObject.isNull() && mTargetObject->isDead())
529	{
530		clearLookAtTarget();
531	}
532
533	// if source avatar is null or dead, mark self as dead and return
534	if (mSourceObject.isNull() || mSourceObject->isDead())
535	{
536		markDead();
537		return;
538	}
539
540	// make sure the proper set of avatar attention are currently being used.
541	LLVOAvatar* source_avatar = (LLVOAvatar*)(LLViewerObject*)mSourceObject;
542	// for now the first cut will just switch on sex. future development could adjust 
543	// timeouts according to avatar age and/or other features. 
544	mAttentions = (source_avatar->getSex() == SEX_MALE) ? &gBoyAttentions : &gGirlAttentions;
545	//printf("updated to %s\n", (source_avatar->getSex() == SEX_MALE) ? "male" : "female");
546
547	F32 time = mTimer.getElapsedTimeF32();
548
549	// clear out the effect if time is up
550	if (mKillTime != 0.f && time > mKillTime)
551	{
552		if (mTargetType != LOOKAT_TARGET_NONE)
553		{
554			clearLookAtTarget();
555			// look at timed out (only happens on own avatar), so tell everyone
556			setNeedsSendToSim(TRUE);
557		}
558	}
559
560	if (mTargetType != LOOKAT_TARGET_NONE)
561	{
562		if (calcTargetPosition())
563		{
564			LLMotion* head_motion = ((LLVOAvatar*)(LLViewerObject*)mSourceObject)->findMotion(ANIM_AGENT_HEAD_ROT);
565			if (!head_motion || head_motion->isStopped())
566			{
567				((LLVOAvatar*)(LLViewerObject*)mSourceObject)->startMotion(ANIM_AGENT_HEAD_ROT);
568			}
569		}
570	}
571
572	if (sDebugLookAt)
573	{
574		((LLVOAvatar*)(LLViewerObject*)mSourceObject)->addDebugText((*mAttentions)[mTargetType].mName);
575	}
576}
577
578/**
579 * Initializes the mTargetPos member from the current mSourceObjec and mTargetObject
580 * (and possibly mTargetOffsetGlobal).
581 * When mTargetObject is another avatar, it sets mTargetPos to be their eyes.
582 * 
583 * Has the side-effect of also calling setAnimationData("LookAtPoint") with the new
584 * mTargetPos on the source object which is assumed to be an avatar.
585 *
586 * Returns whether we successfully calculated a finite target position.
587 */
588bool LLHUDEffectLookAt::calcTargetPosition()
589{
590	LLViewerObject *target_obj = (LLViewerObject *)mTargetObject;
591	LLVector3 local_offset;
592	
593	if (target_obj)
594	{
595		local_offset.setVec(mTargetOffsetGlobal);
596	}
597	else
598	{
599		local_offset = gAgent.getPosAgentFromGlobal(mTargetOffsetGlobal);
600	}
601
602	LLVOAvatar* source_avatar = (LLVOAvatar*)(LLViewerObject*)mSourceObject;
603	if (!source_avatar->isBuilt())
604		return false;
605	
606	if (target_obj && target_obj->mDrawable.notNull())
607	{
608		LLQuaternion target_rot;
609		if (target_obj->isAvatar())
610		{
611			LLVOAvatar *target_av = (LLVOAvatar *)target_obj;
612
613			BOOL looking_at_self = source_avatar->isSelf() && target_av->isSelf();
614
615			// if selecting self, stare forward
616			if (looking_at_self && mTargetOffsetGlobal.magVecSquared() < MIN_TARGET_OFFSET_SQUARED)
617			{
618				//sets the lookat point in front of the avatar
619				mTargetOffsetGlobal.setVec(5.0, 0.0, 0.0);
620				local_offset.setVec(mTargetOffsetGlobal);
621			}
622
623			// look the other avatar in the eye. note: what happens if target is self? -MG
624			mTargetPos = target_av->mHeadp->getWorldPosition();
625			if (mTargetType == LOOKAT_TARGET_MOUSELOOK || mTargetType == LOOKAT_TARGET_FREELOOK)
626			{
627				// mouselook and freelook target offsets are absolute
628				target_rot = LLQuaternion::DEFAULT;
629			}
630			else if (looking_at_self && gAgentCamera.cameraCustomizeAvatar())
631			{
632				// *NOTE: We have to do this because animation
633				// overrides do not set lookat behavior.
634				// *TODO: animation overrides for lookat behavior.
635				target_rot = target_av->mPelvisp->getWorldRotation();
636			}
637			else
638			{
639				target_rot = target_av->mRoot.getWorldRotation();
640			}
641		}
642		else // target obj is not an avatar
643		{
644			if (target_obj->mDrawable->getGeneration() == -1)
645			{
646				mTargetPos = target_obj->getPositionAgent();
647				target_rot = target_obj->getWorldRotation();
648			}
649			else
650			{
651				mTargetPos = target_obj->getRenderPosition();
652				target_rot = target_obj->getRenderRotation();
653			}
654		}
655
656		mTargetPos += (local_offset * target_rot);
657	}
658	else // no target obj or it's not drawable
659	{
660		mTargetPos = local_offset;
661	}
662
663	mTargetPos -= source_avatar->mHeadp->getWorldPosition();
664
665	if (!mTargetPos.isFinite())
666		return false;
667
668	source_avatar->setAnimationData("LookAtPoint", (void *)&mTargetPos);
669
670	return true;
671}