PageRenderTime 64ms CodeModel.GetById 17ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llhudeffectbeam.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 348 lines | 256 code | 51 blank | 41 comment | 29 complexity | 8ebd476908fb2bfbd1af05e6b7908def MD5 | raw file
  1/** 
  2 * @file llhudeffectbeam.cpp
  3 * @brief LLHUDEffectBeam 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 "llhudeffectbeam.h"
 30#include "message.h"
 31
 32#include "llviewerobjectlist.h"
 33
 34#include "llagent.h"
 35#include "lldrawable.h"
 36#include "llfontgl.h"
 37#include "llgl.h"
 38#include "llglheaders.h"
 39#include "llhudrender.h"
 40#include "llrendersphere.h"
 41#include "llviewercamera.h"
 42#include "llvoavatar.h"
 43#include "llviewercontrol.h"
 44
 45const F32 BEAM_SPACING = 0.075f;
 46
 47LLHUDEffectBeam::LLHUDEffectBeam(const U8 type) : LLHUDEffect(type)
 48{
 49	mKillTime = mDuration;
 50
 51	// Initialize all of these to defaults
 52	S32 i;
 53	for (i = 0; i < NUM_POINTS; i++)
 54	{
 55		mInterp[i].setStartTime(BEAM_SPACING*i);
 56		mInterp[i].setEndTime(BEAM_SPACING*NUM_POINTS + BEAM_SPACING*i);
 57		mInterp[i].start();
 58		mInterpFade[i].setStartTime(BEAM_SPACING*NUM_POINTS + BEAM_SPACING*i - 0.5f*NUM_POINTS*BEAM_SPACING);
 59		mInterpFade[i].setEndTime(BEAM_SPACING*NUM_POINTS + BEAM_SPACING*i);
 60		mInterpFade[i].setStartVal(1.f);
 61		mInterpFade[i].setEndVal(0.f);
 62	}
 63
 64	// Setup default timeouts and fade animations.
 65	F32 fade_length;
 66	fade_length = llmin(0.5f, mDuration);
 67	mFadeInterp.setStartTime(mKillTime - fade_length);
 68	mFadeInterp.setEndTime(mKillTime);
 69	mFadeInterp.setStartVal(1.f);
 70	mFadeInterp.setEndVal(0.f);
 71}
 72
 73LLHUDEffectBeam::~LLHUDEffectBeam()
 74{
 75}
 76
 77void LLHUDEffectBeam::packData(LLMessageSystem *mesgsys)
 78{
 79	if (!mSourceObject)
 80	{
 81		llwarns << "Missing source object!" << llendl;
 82	}
 83
 84	// Pack the default data
 85	LLHUDEffect::packData(mesgsys);
 86
 87	// Pack the type-specific data.  Uses a fun packed binary format.  Whee!
 88	// 16 + 24 + 1 = 41
 89	U8 packed_data[41];
 90	memset(packed_data, 0, 41);
 91	if (mSourceObject)
 92	{
 93		htonmemcpy(packed_data, mSourceObject->mID.mData, MVT_LLUUID, 16);
 94	}
 95
 96	if (mTargetObject)
 97	{
 98		packed_data[16] = 1;
 99	}
100	else
101	{
102		packed_data[16] = 0;
103	}
104
105	if (mTargetObject)
106	{
107		htonmemcpy(&(packed_data[17]), mTargetObject->mID.mData, MVT_LLUUID, 16);
108	}
109	else
110	{
111		htonmemcpy(&(packed_data[17]), mTargetPos.mdV, MVT_LLVector3d, 24);
112	}
113	mesgsys->addBinaryDataFast(_PREHASH_TypeData, packed_data, 41);
114}
115
116void LLHUDEffectBeam::unpackData(LLMessageSystem *mesgsys, S32 blocknum)
117{
118	llerrs << "Got beam!" << llendl;
119	BOOL use_target_object;
120	LLVector3d new_target;
121	U8 packed_data[41];
122
123	LLHUDEffect::unpackData(mesgsys, blocknum);
124	LLUUID source_id;
125	LLUUID target_id;
126	S32 size = mesgsys->getSizeFast(_PREHASH_Effect, blocknum, _PREHASH_TypeData);
127	if (size != 41)
128	{
129		llwarns << "Beam effect with bad size " << size << llendl;
130		return;
131	}
132	mesgsys->getBinaryDataFast(_PREHASH_Effect, _PREHASH_TypeData, packed_data, 41, blocknum);
133	
134	htonmemcpy(source_id.mData, packed_data, MVT_LLUUID, 16);
135
136	LLViewerObject *objp = gObjectList.findObject(source_id);
137	if (objp)
138	{
139		setSourceObject(objp);
140	}
141
142	use_target_object = packed_data[16];
143
144	if (use_target_object)
145	{
146		htonmemcpy(target_id.mData, &packed_data[17], MVT_LLUUID, 16);
147
148		LLViewerObject *objp = gObjectList.findObject(target_id);
149		if (objp)
150		{
151			setTargetObject(objp);
152		}
153	}
154	else
155	{
156		htonmemcpy(new_target.mdV, &(packed_data[17]), MVT_LLVector3d, 24);
157		setTargetPos(new_target);
158	}
159
160	// We've received an update for the effect, update the various timeouts
161	// and fade animations.
162	mKillTime = mTimer.getElapsedTimeF32() + mDuration;
163	F32 fade_length;
164	fade_length = llmin(0.5f, mDuration);
165	mFadeInterp.setStartTime(mKillTime - fade_length);
166	mFadeInterp.setEndTime(mKillTime);
167	mFadeInterp.setStartVal(1.f);
168	mFadeInterp.setEndVal(0.f);
169}
170
171void LLHUDEffectBeam::setSourceObject(LLViewerObject *objp)
172{
173	if (objp->isDead())
174	{
175		llwarns << "HUDEffectBeam: Source object is dead!" << llendl;
176		mSourceObject = NULL;
177		return;
178	}
179
180	if (mSourceObject == objp)
181	{
182		return;
183	}
184
185	mSourceObject = objp;
186	if (mSourceObject)
187	{
188		S32 i;
189		for (i = 0; i < NUM_POINTS; i++)
190		{
191			if (mSourceObject->isAvatar())
192			{
193				LLViewerObject *objp = mSourceObject;
194				LLVOAvatar *avatarp = (LLVOAvatar *)objp;
195				LLVector3d hand_pos_global = gAgent.getPosGlobalFromAgent(avatarp->mWristLeftp->getWorldPosition());
196				mInterp[i].setStartVal(hand_pos_global);
197				mInterp[i].start();
198			}
199			else
200			{
201				mInterp[i].setStartVal(mSourceObject->getPositionGlobal());
202				mInterp[i].start();
203			}
204		}
205	}
206}
207
208
209void LLHUDEffectBeam::setTargetObject(LLViewerObject *objp)
210{
211	if (mTargetObject->isDead())
212	{
213		llwarns << "HUDEffectBeam: Target object is dead!" << llendl;
214	}
215
216	mTargetObject = objp;
217}
218
219void LLHUDEffectBeam::setTargetPos(const LLVector3d &pos_global)
220{
221	mTargetPos = pos_global;
222	mTargetObject = NULL;
223}
224
225void LLHUDEffectBeam::render()
226{
227	if (!mSourceObject)
228	{
229		markDead();
230		return;
231	}
232	if (mSourceObject->isDead())
233	{
234		markDead();
235		return;
236	}
237
238	F32 time = mTimer.getElapsedTimeF32();
239
240	// Kill us if our time is over...
241	if (mKillTime < time)
242	{
243		markDead();
244		return;
245	}
246
247	LLGLSPipelineAlpha gls_pipeline_alpha;
248	gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
249
250
251	// Interpolate the global fade alpha
252	mFadeInterp.update(time);
253
254	if (mTargetObject.notNull() && mTargetObject->mDrawable.notNull())
255	{
256		// use viewer object position on freshly created objects
257		if (mTargetObject->mDrawable->getGeneration() == -1)
258		{
259			mTargetPos = mTargetObject->getPositionGlobal();
260		}
261		// otherwise use drawable
262		else
263		{
264			mTargetPos = gAgent.getPosGlobalFromAgent(mTargetObject->mDrawable->getPositionAgent());
265		}
266	}
267
268
269	// Init the color of the particles
270	LLColor4U coloru = mColor;
271
272	// Draw the particles
273	S32 i;
274	for (i = 0; i < NUM_POINTS; i++)
275	{
276		mInterp[i].update(time);
277		if (!mInterp[i].isActive())
278		{
279			continue;
280		}
281		mInterpFade[i].update(time);
282
283		if (mInterp[i].isDone())
284		{
285			// Reinitialize the particle when the particle has finished its animation.
286			setupParticle(i);
287		}
288
289		F32 frac = mInterp[i].getCurFrac();
290		F32 scale = 0.025f + fabs(0.05f*sin(2.f*F_PI*(frac - time)));
291		scale *= mInterpFade[i].getCurVal();
292
293		LLVector3 pos_agent = gAgent.getPosAgentFromGlobal(mInterp[i].getCurVal());
294
295		F32 alpha = mFadeInterp.getCurVal()*mColor.mV[3];
296		alpha *= mInterpFade[i].getCurVal();
297		coloru.mV[3] = (U8)alpha;
298		gGL.color4ubv(coloru.mV);
299
300		gGL.pushMatrix();
301		gGL.translatef(pos_agent.mV[0], pos_agent.mV[1], pos_agent.mV[2]);
302		gGL.scalef(scale, scale, scale);
303		gSphere.render();
304		gGL.popMatrix();
305	}
306}
307
308void LLHUDEffectBeam::renderForTimer()
309{
310	render();
311}
312
313void LLHUDEffectBeam::setupParticle(const S32 i)
314{
315	LLVector3d start_pos_global;
316	if (mSourceObject->getPCode() == LL_PCODE_LEGACY_AVATAR)
317	{
318		LLViewerObject *objp = mSourceObject;
319		LLVOAvatar *avatarp = (LLVOAvatar *)objp;
320		start_pos_global = gAgent.getPosGlobalFromAgent(avatarp->mWristLeftp->getWorldPosition());
321	}
322	else
323	{
324		start_pos_global = mSourceObject->getPositionGlobal();
325	}
326
327	// Generate a random offset for the target point.
328	const F32 SCALE = 0.5f;
329	F32 x, y, z;
330	x = ll_frand(SCALE) - 0.5f*SCALE;
331	y = ll_frand(SCALE) - 0.5f*SCALE;
332	z = ll_frand(SCALE) - 0.5f*SCALE;
333
334	LLVector3d target_pos_global(mTargetPos);
335	target_pos_global += LLVector3d(x, y, z);
336
337	mInterp[i].setStartTime(mInterp[i].getEndTime());
338	mInterp[i].setEndTime(mInterp[i].getStartTime() + BEAM_SPACING*NUM_POINTS);
339	mInterp[i].setStartVal(start_pos_global);
340	mInterp[i].setEndVal(target_pos_global);
341	mInterp[i].start();
342
343
344	// Setup the interpolator that fades out the alpha.
345	mInterpFade[i].setStartTime(mInterp[i].getStartTime() + BEAM_SPACING*NUM_POINTS - 0.5f*NUM_POINTS*BEAM_SPACING);
346	mInterpFade[i].setEndTime(mInterp[i].getStartTime() + BEAM_SPACING*NUM_POINTS - 0.05f);
347	mInterpFade[i].start();
348}