PageRenderTime 576ms CodeModel.GetById 149ms app.highlight 184ms RepoModel.GetById 116ms app.codeStats 0ms

/indra/newview/llhudeffectpointat.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 461 lines | 310 code | 69 blank | 82 comment | 50 complexity | 3d1531c51843410b8c5bdaa2505dc39c MD5 | raw file
  1/** 
  2 * @file llhudeffectpointat.cpp
  3 * @brief LLHUDEffectPointAt 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 "llhudeffectpointat.h"
 30
 31#include "llgl.h"
 32#include "llrender.h"
 33
 34#include "llagent.h"
 35#include "llagentcamera.h"
 36#include "lldrawable.h"
 37#include "llviewerobjectlist.h"
 38#include "llvoavatar.h"
 39#include "message.h"
 40
 41// packet layout
 42const S32 SOURCE_AVATAR = 0;
 43const S32 TARGET_OBJECT = 16;
 44const S32 TARGET_POS = 32;
 45const S32 POINTAT_TYPE = 56;
 46const S32 PKT_SIZE = 57;
 47
 48// throttle
 49const F32 MAX_SENDS_PER_SEC = 4.f;
 50
 51const F32 MIN_DELTAPOS_FOR_UPDATE_SQUARED = 0.05f * 0.05f;
 52
 53// timeouts
 54// can't use actual F32_MAX, because we add this to the current frametime
 55const F32 MAX_TIMEOUT = F32_MAX / 4.f;
 56
 57const F32 POINTAT_TIMEOUTS[POINTAT_NUM_TARGETS] = 
 58{
 59	MAX_TIMEOUT, //POINTAT_TARGET_NONE
 60	MAX_TIMEOUT, //POINTAT_TARGET_SELECT
 61	MAX_TIMEOUT, //POINTAT_TARGET_GRAB
 62	0.f, //POINTAT_TARGET_CLEAR
 63};
 64
 65const S32 POINTAT_PRIORITIES[POINTAT_NUM_TARGETS] = 
 66{
 67	0, //POINTAT_TARGET_NONE
 68	1, //POINTAT_TARGET_SELECT
 69	2, //POINTAT_TARGET_GRAB
 70	3, //POINTAT_TARGET_CLEAR
 71};
 72
 73// statics
 74
 75BOOL LLHUDEffectPointAt::sDebugPointAt;
 76
 77
 78//-----------------------------------------------------------------------------
 79// LLHUDEffectPointAt()
 80//-----------------------------------------------------------------------------
 81LLHUDEffectPointAt::LLHUDEffectPointAt(const U8 type) : 
 82	LLHUDEffect(type), 
 83	mKillTime(0.f),
 84	mLastSendTime(0.f)
 85{
 86	clearPointAtTarget();
 87}
 88
 89//-----------------------------------------------------------------------------
 90// ~LLHUDEffectPointAt()
 91//-----------------------------------------------------------------------------
 92LLHUDEffectPointAt::~LLHUDEffectPointAt()
 93{
 94}
 95
 96//-----------------------------------------------------------------------------
 97// packData()
 98//-----------------------------------------------------------------------------
 99void LLHUDEffectPointAt::packData(LLMessageSystem *mesgsys)
100{
101	// Pack the default data
102	LLHUDEffect::packData(mesgsys);
103
104	// Pack the type-specific data.  Uses a fun packed binary format.  Whee!
105	U8 packed_data[PKT_SIZE];
106	memset(packed_data, 0, PKT_SIZE);
107
108	if (mSourceObject)
109	{
110		htonmemcpy(&(packed_data[SOURCE_AVATAR]), mSourceObject->mID.mData, MVT_LLUUID, 16);
111	}
112	else
113	{
114		htonmemcpy(&(packed_data[SOURCE_AVATAR]), LLUUID::null.mData, MVT_LLUUID, 16);
115	}
116
117	// pack both target object and position
118	// position interpreted as offset if target object is non-null
119	if (mTargetObject)
120	{
121		htonmemcpy(&(packed_data[TARGET_OBJECT]), mTargetObject->mID.mData, MVT_LLUUID, 16);
122	}
123	else
124	{
125		htonmemcpy(&(packed_data[TARGET_OBJECT]), LLUUID::null.mData, MVT_LLUUID, 16);
126	}
127
128	htonmemcpy(&(packed_data[TARGET_POS]), mTargetOffsetGlobal.mdV, MVT_LLVector3d, 24);
129
130	U8 pointAtTypePacked = (U8)mTargetType;
131	htonmemcpy(&(packed_data[POINTAT_TYPE]), &pointAtTypePacked, MVT_U8, 1);
132
133	mesgsys->addBinaryDataFast(_PREHASH_TypeData, packed_data, PKT_SIZE);
134
135	mLastSendTime = mTimer.getElapsedTimeF32();
136}
137
138//-----------------------------------------------------------------------------
139// unpackData()
140//-----------------------------------------------------------------------------
141void LLHUDEffectPointAt::unpackData(LLMessageSystem *mesgsys, S32 blocknum)
142{
143	LLVector3d new_target;
144	U8 packed_data[PKT_SIZE];
145
146	LLUUID dataId;
147	mesgsys->getUUIDFast(_PREHASH_Effect, _PREHASH_ID, dataId, blocknum);
148
149	// ignore messages from ourselves
150	if (!gAgentCamera.mPointAt.isNull() && dataId == gAgentCamera.mPointAt->getID())
151	{
152		return;
153	}
154
155	LLHUDEffect::unpackData(mesgsys, blocknum);
156	LLUUID source_id;
157	LLUUID target_id;
158	U8 pointAtTypeUnpacked = 0;
159	S32 size = mesgsys->getSizeFast(_PREHASH_Effect, blocknum, _PREHASH_TypeData);
160	if (size != PKT_SIZE)
161	{
162		llwarns << "PointAt effect with bad size " << size << llendl;
163		return;
164	}
165	mesgsys->getBinaryDataFast(_PREHASH_Effect, _PREHASH_TypeData, packed_data, PKT_SIZE, blocknum);
166	
167	htonmemcpy(source_id.mData, &(packed_data[SOURCE_AVATAR]), MVT_LLUUID, 16);
168	htonmemcpy(target_id.mData, &(packed_data[TARGET_OBJECT]), MVT_LLUUID, 16);
169	htonmemcpy(new_target.mdV, &(packed_data[TARGET_POS]), MVT_LLVector3d, 24);
170	htonmemcpy(&pointAtTypeUnpacked, &(packed_data[POINTAT_TYPE]), MVT_U8, 1);
171
172	LLViewerObject *objp = gObjectList.findObject(source_id);
173	if (objp && objp->isAvatar())
174	{
175		setSourceObject(objp);
176	}
177	else
178	{
179		//llwarns << "Could not find source avatar for pointat effect" << llendl;
180		return;
181	}
182
183	objp = gObjectList.findObject(target_id);
184
185	if (objp)
186	{
187		setTargetObjectAndOffset(objp, new_target);
188	}
189	else if (target_id.isNull())
190	{
191		setTargetPosGlobal(new_target);
192	}
193
194	mTargetType = (EPointAtType)pointAtTypeUnpacked;
195
196//	mKillTime = mTimer.getElapsedTimeF32() + mDuration;
197	update();
198}
199
200//-----------------------------------------------------------------------------
201// setTargetObjectAndOffset()
202//-----------------------------------------------------------------------------
203void LLHUDEffectPointAt::setTargetObjectAndOffset(LLViewerObject *objp, LLVector3d offset)
204{
205	mTargetObject = objp;
206	mTargetOffsetGlobal = offset;
207}
208
209//-----------------------------------------------------------------------------
210// setTargetPosGlobal()
211//-----------------------------------------------------------------------------
212void LLHUDEffectPointAt::setTargetPosGlobal(const LLVector3d &target_pos_global)
213{
214	mTargetObject = NULL;
215	mTargetOffsetGlobal = target_pos_global;
216}
217
218//-----------------------------------------------------------------------------
219// setPointAt()
220// called by agent logic to set look at behavior locally, and propagate to sim
221//-----------------------------------------------------------------------------
222BOOL LLHUDEffectPointAt::setPointAt(EPointAtType target_type, LLViewerObject *object, LLVector3 position)
223{
224	if (!mSourceObject)
225	{
226		return FALSE;
227	}
228	
229	if (target_type >= POINTAT_NUM_TARGETS)
230	{
231		llwarns << "Bad target_type " << (int)target_type << " - ignoring." << llendl;
232		return FALSE;
233	}
234
235	// must be same or higher priority than existing effect
236	if (POINTAT_PRIORITIES[target_type] < POINTAT_PRIORITIES[mTargetType])
237	{
238		return FALSE;
239	}
240
241	F32 current_time  = mTimer.getElapsedTimeF32();
242	
243	// type of pointat behavior or target object has changed
244	BOOL targetTypeChanged = (target_type != mTargetType) ||
245		(object != mTargetObject);
246
247	BOOL targetPosChanged = (dist_vec_squared(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE_SQUARED) && 
248		((current_time - mLastSendTime) > (1.f / MAX_SENDS_PER_SEC));
249
250	if (targetTypeChanged || targetPosChanged)
251	{
252		mLastSentOffsetGlobal = position;
253		setDuration(POINTAT_TIMEOUTS[target_type]);
254		setNeedsSendToSim(TRUE);
255//		llinfos << "Sending pointat data" << llendl;
256	}
257
258	if (target_type == POINTAT_TARGET_CLEAR)
259	{
260		clearPointAtTarget();
261	}
262	else
263	{
264		mTargetType = target_type;
265		mTargetObject = object;
266		if (object)
267		{
268			mTargetOffsetGlobal.setVec(position);
269		}
270		else
271		{
272			mTargetOffsetGlobal = gAgent.getPosGlobalFromAgent(position);
273		}
274
275		mKillTime = mTimer.getElapsedTimeF32() + mDuration;
276
277		//set up requisite animation data
278		update();
279	}
280
281	return TRUE;
282}
283
284//-----------------------------------------------------------------------------
285// clearPointAtTarget()
286//-----------------------------------------------------------------------------
287void LLHUDEffectPointAt::clearPointAtTarget()
288{
289	mTargetObject = NULL;
290	mTargetOffsetGlobal.clearVec();
291	mTargetType = POINTAT_TARGET_NONE;
292}
293
294//-----------------------------------------------------------------------------
295// markDead()
296//-----------------------------------------------------------------------------
297void LLHUDEffectPointAt::markDead()
298{
299	if (!mSourceObject.isNull() && mSourceObject->isAvatar())
300	{
301		((LLVOAvatar*)(LLViewerObject*)mSourceObject)->removeAnimationData("PointAtPoint");
302	}
303
304	clearPointAtTarget();
305	LLHUDEffect::markDead();
306}
307
308void LLHUDEffectPointAt::setSourceObject(LLViewerObject* objectp)
309{
310	// restrict source objects to avatars
311	if (objectp && objectp->isAvatar())
312	{
313		LLHUDEffect::setSourceObject(objectp);
314	}
315}
316
317//-----------------------------------------------------------------------------
318// render()
319//-----------------------------------------------------------------------------
320void LLHUDEffectPointAt::render()
321{
322	update();
323	if (sDebugPointAt && mTargetType != POINTAT_TARGET_NONE)
324	{
325		gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
326
327		LLVector3 target = mTargetPos + mSourceObject->getRenderPosition();
328		gGL.pushMatrix();
329		gGL.translatef(target.mV[VX], target.mV[VY], target.mV[VZ]);
330		gGL.scalef(0.3f, 0.3f, 0.3f);
331		gGL.begin(LLRender::LINES);
332		{
333			gGL.color3f(1.f, 0.f, 0.f);
334			gGL.vertex3f(-1.f, 0.f, 0.f);
335			gGL.vertex3f(1.f, 0.f, 0.f);
336
337			gGL.vertex3f(0.f, -1.f, 0.f);
338			gGL.vertex3f(0.f, 1.f, 0.f);
339
340			gGL.vertex3f(0.f, 0.f, -1.f);
341			gGL.vertex3f(0.f, 0.f, 1.f);
342		} gGL.end();
343		gGL.popMatrix();
344	}
345}
346
347//-----------------------------------------------------------------------------
348// update()
349//-----------------------------------------------------------------------------
350void LLHUDEffectPointAt::update()
351{
352	// If the target object is dead, set the target object to NULL
353	if (!mTargetObject.isNull() && mTargetObject->isDead())
354	{
355		clearPointAtTarget();
356	}
357
358	if (mSourceObject.isNull() || mSourceObject->isDead())
359	{
360		markDead();
361		return;
362	}
363	
364	F32 time = mTimer.getElapsedTimeF32();
365
366	// clear out the effect if time is up
367	if (mKillTime != 0.f && time > mKillTime)
368	{
369		mTargetType = POINTAT_TARGET_NONE;
370	}
371
372	if (mSourceObject->isAvatar())
373	{
374		if (mTargetType == POINTAT_TARGET_NONE)
375		{
376			((LLVOAvatar*)(LLViewerObject*)mSourceObject)->removeAnimationData("PointAtPoint");
377		}
378		else
379		{
380			if (calcTargetPosition())
381			{
382				((LLVOAvatar*)(LLViewerObject*)mSourceObject)->startMotion(ANIM_AGENT_EDITING);
383			}
384		}
385	}
386}
387
388//-----------------------------------------------------------------------------
389// calcTargetPosition()
390// returns whether we successfully calculated a finite target position.
391//-----------------------------------------------------------------------------
392bool LLHUDEffectPointAt::calcTargetPosition()
393{
394	LLViewerObject *targetObject = (LLViewerObject *)mTargetObject;
395	LLVector3 local_offset;
396	
397	if (targetObject)
398	{
399		local_offset.setVec(mTargetOffsetGlobal);
400	}
401	else
402	{
403		local_offset = gAgent.getPosAgentFromGlobal(mTargetOffsetGlobal);
404	}
405
406	if (targetObject && targetObject->mDrawable.notNull())
407	{
408		LLQuaternion objRot;
409		if (targetObject->isAvatar())
410		{
411			LLVOAvatar *avatarp = (LLVOAvatar *)targetObject;
412			mTargetPos = avatarp->mHeadp->getWorldPosition();
413			objRot = avatarp->mPelvisp->getWorldRotation();
414		}
415		else
416		{
417			if (targetObject->mDrawable->getGeneration() == -1)
418			{
419				mTargetPos = targetObject->getPositionAgent();
420				objRot = targetObject->getWorldRotation();
421			}
422			else
423			{
424				mTargetPos = targetObject->getRenderPosition();
425				objRot = targetObject->getRenderRotation();
426			}
427		}
428
429		mTargetPos += (local_offset * objRot);
430	}
431	else
432	{
433		mTargetPos = local_offset;
434	}
435
436	mTargetPos -= mSourceObject->getRenderPosition();
437
438	if (!llfinite(mTargetPos.lengthSquared()))
439	{
440		return false;
441	}
442
443	if (mSourceObject->isAvatar())
444	{
445		((LLVOAvatar*)(LLViewerObject*)mSourceObject)->setAnimationData("PointAtPoint", (void *)&mTargetPos);
446	}
447
448	return true;
449}
450
451const LLVector3d LLHUDEffectPointAt::getPointAtPosGlobal()
452{
453	LLVector3d global_pos;
454	global_pos.setVec(mTargetPos);
455	if (mSourceObject.notNull())
456	{
457		global_pos += mSourceObject->getPositionGlobal();
458	}
459	
460	return global_pos;
461}