PageRenderTime 53ms CodeModel.GetById 3ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcharacter/llcharacter.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 503 lines | 319 code | 56 blank | 128 comment | 43 complexity | e0c3317be36170fd6ba4f687dc9cbda1 MD5 | raw file
  1/** 
  2 * @file llcharacter.cpp
  3 * @brief Implementation of LLCharacter class.
  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//-----------------------------------------------------------------------------
 28// Header Files
 29//-----------------------------------------------------------------------------
 30
 31#include "linden_common.h"
 32
 33#include "llcharacter.h"
 34#include "llstring.h"
 35
 36#define SKEL_HEADER "Linden Skeleton 1.0"
 37
 38LLStringTable LLCharacter::sVisualParamNames(1024);
 39
 40std::vector< LLCharacter* > LLCharacter::sInstances;
 41BOOL LLCharacter::sAllowInstancesChange = TRUE ;
 42
 43//-----------------------------------------------------------------------------
 44// LLCharacter()
 45// Class Constructor
 46//-----------------------------------------------------------------------------
 47LLCharacter::LLCharacter()
 48	: 
 49	mPreferredPelvisHeight( 0.f ),
 50	mSex( SEX_FEMALE ),
 51	mAppearanceSerialNum( 0 ),
 52	mSkeletonSerialNum( 0 )
 53{
 54	llassert_always(sAllowInstancesChange) ;
 55	sInstances.push_back(this);
 56
 57	mMotionController.setCharacter( this );	
 58	mPauseRequest = new LLPauseRequestHandle();
 59}
 60
 61
 62//-----------------------------------------------------------------------------
 63// ~LLCharacter()
 64// Class Destructor
 65//-----------------------------------------------------------------------------
 66LLCharacter::~LLCharacter()
 67{	
 68	for (LLVisualParam *param = getFirstVisualParam(); 
 69		param;
 70		param = getNextVisualParam())
 71	{
 72		delete param;
 73	}
 74
 75	U32 i ;
 76	U32 size = sInstances.size() ;
 77	for(i = 0 ; i < size ; i++)
 78	{
 79		if(sInstances[i] == this)
 80		{
 81			break ;
 82		}
 83	}
 84
 85	llassert_always(i < size) ;
 86
 87	llassert_always(sAllowInstancesChange) ;
 88	sInstances[i] = sInstances[size - 1] ;
 89	sInstances.pop_back() ;
 90}
 91
 92
 93//-----------------------------------------------------------------------------
 94// getJoint()
 95//-----------------------------------------------------------------------------
 96LLJoint *LLCharacter::getJoint( const std::string &name )
 97{
 98	LLJoint* joint = NULL;
 99
100	LLJoint *root = getRootJoint();
101	if (root)
102	{
103		joint = root->findJoint(name);
104	}
105
106	if (!joint)
107	{
108		llwarns << "Failed to find joint." << llendl;
109	}
110	return joint;
111}
112
113//-----------------------------------------------------------------------------
114// registerMotion()
115//-----------------------------------------------------------------------------
116BOOL LLCharacter::registerMotion( const LLUUID& id, LLMotionConstructor create )
117{
118	return mMotionController.registerMotion(id, create);
119}
120
121//-----------------------------------------------------------------------------
122// removeMotion()
123//-----------------------------------------------------------------------------
124void LLCharacter::removeMotion( const LLUUID& id )
125{
126	mMotionController.removeMotion(id);
127}
128
129//-----------------------------------------------------------------------------
130// findMotion()
131//-----------------------------------------------------------------------------
132LLMotion* LLCharacter::findMotion( const LLUUID &id )
133{
134	return mMotionController.findMotion( id );
135}
136
137//-----------------------------------------------------------------------------
138// createMotion()
139// NOTE: Always assign the result to a LLPointer!
140//-----------------------------------------------------------------------------
141LLMotion* LLCharacter::createMotion( const LLUUID &id )
142{
143	return mMotionController.createMotion( id );
144}
145
146//-----------------------------------------------------------------------------
147// startMotion()
148//-----------------------------------------------------------------------------
149BOOL LLCharacter::startMotion(const LLUUID &id, F32 start_offset)
150{
151	return mMotionController.startMotion(id, start_offset);
152}
153
154
155//-----------------------------------------------------------------------------
156// stopMotion()
157//-----------------------------------------------------------------------------
158BOOL LLCharacter::stopMotion(const LLUUID& id, BOOL stop_immediate)
159{
160	return mMotionController.stopMotionLocally(id, stop_immediate);
161}
162
163//-----------------------------------------------------------------------------
164// isMotionActive()
165//-----------------------------------------------------------------------------
166BOOL LLCharacter::isMotionActive(const LLUUID& id)
167{
168	LLMotion *motionp = mMotionController.findMotion(id);
169	if (motionp)
170	{
171		return mMotionController.isMotionActive(motionp);
172	}
173
174	return FALSE;
175}
176
177
178//-----------------------------------------------------------------------------
179// onDeactivateMotion()
180//-----------------------------------------------------------------------------
181void LLCharacter::requestStopMotion( LLMotion* motion)
182{
183//	llinfos << "DEBUG: Char::onDeactivateMotion( " << motionName << " )" << llendl;
184}
185
186
187//-----------------------------------------------------------------------------
188// updateMotions()
189//-----------------------------------------------------------------------------
190static LLFastTimer::DeclareTimer FTM_UPDATE_ANIMATION("Update Animation");
191static LLFastTimer::DeclareTimer FTM_UPDATE_HIDDEN_ANIMATION("Update Hidden Anim");
192
193void LLCharacter::updateMotions(e_update_t update_type)
194{
195	if (update_type == HIDDEN_UPDATE)
196	{
197		LLFastTimer t(FTM_UPDATE_HIDDEN_ANIMATION);
198		mMotionController.updateMotionsMinimal();
199	}
200	else
201	{
202		LLFastTimer t(FTM_UPDATE_ANIMATION);
203		// unpause if the number of outstanding pause requests has dropped to the initial one
204		if (mMotionController.isPaused() && mPauseRequest->getNumRefs() == 1)
205		{
206			mMotionController.unpauseAllMotions();
207		}
208		bool force_update = (update_type == FORCE_UPDATE);
209		mMotionController.updateMotions(force_update);
210	}
211}
212
213
214//-----------------------------------------------------------------------------
215// deactivateAllMotions()
216//-----------------------------------------------------------------------------
217void LLCharacter::deactivateAllMotions()
218{
219	mMotionController.deactivateAllMotions();
220}
221
222
223//-----------------------------------------------------------------------------
224// flushAllMotions()
225//-----------------------------------------------------------------------------
226void LLCharacter::flushAllMotions()
227{
228	mMotionController.flushAllMotions();
229}
230
231
232//-----------------------------------------------------------------------------
233// dumpCharacter()
234//-----------------------------------------------------------------------------
235void LLCharacter::dumpCharacter( LLJoint* joint )
236{
237	// handle top level entry into recursion
238	if (joint == NULL)
239	{
240		llinfos << "DEBUG: Dumping Character @" << this << llendl;
241		dumpCharacter( getRootJoint() );
242		llinfos << "DEBUG: Done." << llendl;
243		return;
244	}
245
246	// print joint info
247	llinfos << "DEBUG: " << joint->getName() << " (" << (joint->getParent()?joint->getParent()->getName():std::string("ROOT")) << ")" << llendl;
248
249	// recurse
250	for (LLJoint::child_list_t::iterator iter = joint->mChildren.begin();
251		 iter != joint->mChildren.end(); ++iter)
252	{
253		LLJoint* child_joint = *iter;
254		dumpCharacter(child_joint);
255	}
256}
257
258//-----------------------------------------------------------------------------
259// setAnimationData()
260//-----------------------------------------------------------------------------
261void LLCharacter::setAnimationData(std::string name, void *data)
262{
263	mAnimationData[name] = data;
264}
265
266//-----------------------------------------------------------------------------
267// getAnimationData()
268//-----------------------------------------------------------------------------
269void* LLCharacter::getAnimationData(std::string name)
270{
271	return get_if_there(mAnimationData, name, (void*)NULL);
272}
273
274//-----------------------------------------------------------------------------
275// removeAnimationData()
276//-----------------------------------------------------------------------------
277void LLCharacter::removeAnimationData(std::string name)
278{
279	mAnimationData.erase(name);
280}
281
282//-----------------------------------------------------------------------------
283// setVisualParamWeight()
284//-----------------------------------------------------------------------------
285BOOL LLCharacter::setVisualParamWeight(LLVisualParam* which_param, F32 weight, BOOL upload_bake)
286{
287	S32 index = which_param->getID();
288	visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
289	if (index_iter != mVisualParamIndexMap.end())
290	{
291		index_iter->second->setWeight(weight, upload_bake);
292		return TRUE;
293	}
294	return FALSE;
295}
296
297//-----------------------------------------------------------------------------
298// setVisualParamWeight()
299//-----------------------------------------------------------------------------
300BOOL LLCharacter::setVisualParamWeight(const char* param_name, F32 weight, BOOL upload_bake)
301{
302	std::string tname(param_name);
303	LLStringUtil::toLower(tname);
304	char *tableptr = sVisualParamNames.checkString(tname);
305	visual_param_name_map_t::iterator name_iter = mVisualParamNameMap.find(tableptr);
306	if (name_iter != mVisualParamNameMap.end())
307	{
308		name_iter->second->setWeight(weight, upload_bake);
309		return TRUE;
310	}
311	llwarns << "LLCharacter::setVisualParamWeight() Invalid visual parameter: " << param_name << llendl;
312	return FALSE;
313}
314
315//-----------------------------------------------------------------------------
316// setVisualParamWeight()
317//-----------------------------------------------------------------------------
318BOOL LLCharacter::setVisualParamWeight(S32 index, F32 weight, BOOL upload_bake)
319{
320	visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
321	if (index_iter != mVisualParamIndexMap.end())
322	{
323		index_iter->second->setWeight(weight, upload_bake);
324		return TRUE;
325	}
326	llwarns << "LLCharacter::setVisualParamWeight() Invalid visual parameter index: " << index << llendl;
327	return FALSE;
328}
329
330//-----------------------------------------------------------------------------
331// getVisualParamWeight()
332//-----------------------------------------------------------------------------
333F32 LLCharacter::getVisualParamWeight(LLVisualParam *which_param)
334{
335	S32 index = which_param->getID();
336	visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
337	if (index_iter != mVisualParamIndexMap.end())
338	{
339		return index_iter->second->getWeight();
340	}
341	else
342	{
343		llwarns << "LLCharacter::getVisualParamWeight() Invalid visual parameter*, index= " << index << llendl;
344		return 0.f;
345	}
346}
347
348//-----------------------------------------------------------------------------
349// getVisualParamWeight()
350//-----------------------------------------------------------------------------
351F32 LLCharacter::getVisualParamWeight(const char* param_name)
352{
353	std::string tname(param_name);
354	LLStringUtil::toLower(tname);
355	char *tableptr = sVisualParamNames.checkString(tname);
356	visual_param_name_map_t::iterator name_iter = mVisualParamNameMap.find(tableptr);
357	if (name_iter != mVisualParamNameMap.end())
358	{
359		return name_iter->second->getWeight();
360	}
361	llwarns << "LLCharacter::getVisualParamWeight() Invalid visual parameter: " << param_name << llendl;
362	return 0.f;
363}
364
365//-----------------------------------------------------------------------------
366// getVisualParamWeight()
367//-----------------------------------------------------------------------------
368F32 LLCharacter::getVisualParamWeight(S32 index)
369{
370	visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
371	if (index_iter != mVisualParamIndexMap.end())
372	{
373		return index_iter->second->getWeight();
374	}
375	else
376	{
377		llwarns << "LLCharacter::getVisualParamWeight() Invalid visual parameter index: " << index << llendl;
378		return 0.f;
379	}
380}
381
382//-----------------------------------------------------------------------------
383// clearVisualParamWeights()
384//-----------------------------------------------------------------------------
385void LLCharacter::clearVisualParamWeights()
386{
387	for (LLVisualParam *param = getFirstVisualParam(); 
388		param;
389		param = getNextVisualParam())
390	{
391		if (param->isTweakable())
392		{
393			param->setWeight( param->getDefaultWeight(), FALSE );
394		}
395	}
396}
397
398//-----------------------------------------------------------------------------
399// getVisualParam()
400//-----------------------------------------------------------------------------
401LLVisualParam*	LLCharacter::getVisualParam(const char *param_name)
402{
403	std::string tname(param_name);
404	LLStringUtil::toLower(tname);
405	char *tableptr = sVisualParamNames.checkString(tname);
406	visual_param_name_map_t::iterator name_iter = mVisualParamNameMap.find(tableptr);
407	if (name_iter != mVisualParamNameMap.end())
408	{
409		return name_iter->second;
410	}
411	llwarns << "LLCharacter::getVisualParam() Invalid visual parameter: " << param_name << llendl;
412	return NULL;
413}
414
415//-----------------------------------------------------------------------------
416// addSharedVisualParam()
417//-----------------------------------------------------------------------------
418void LLCharacter::addSharedVisualParam(LLVisualParam *param)
419{
420	S32 index = param->getID();
421	visual_param_index_map_t::iterator index_iter = mVisualParamIndexMap.find(index);
422	LLVisualParam* current_param = 0;
423	if (index_iter != mVisualParamIndexMap.end())
424		current_param = index_iter->second;
425	if( current_param )
426	{
427		LLVisualParam* next_param = current_param;
428		while(next_param->getNextParam())
429		{
430			next_param = next_param->getNextParam();
431		}
432		next_param->setNextParam(param);
433	}
434	else
435	{
436		llwarns << "Shared visual parameter " << param->getName() << " does not already exist with ID " << 
437			param->getID() << llendl;
438	}
439}
440
441//-----------------------------------------------------------------------------
442// addVisualParam()
443//-----------------------------------------------------------------------------
444void LLCharacter::addVisualParam(LLVisualParam *param)
445{
446	S32 index = param->getID();
447	// Add Index map
448	std::pair<visual_param_index_map_t::iterator, bool> idxres;
449	idxres = mVisualParamIndexMap.insert(visual_param_index_map_t::value_type(index, param));
450	if (!idxres.second)
451	{
452		llwarns << "Visual parameter " << param->getName() << " already exists with same ID as " << 
453			param->getName() << llendl;
454		visual_param_index_map_t::iterator index_iter = idxres.first;
455		index_iter->second = param;
456	}
457
458	if (param->getInfo())
459	{
460		// Add name map
461		std::string tname(param->getName());
462		LLStringUtil::toLower(tname);
463		char *tableptr = sVisualParamNames.addString(tname);
464		std::pair<visual_param_name_map_t::iterator, bool> nameres;
465		nameres = mVisualParamNameMap.insert(visual_param_name_map_t::value_type(tableptr, param));
466		if (!nameres.second)
467		{
468			// Already exists, copy param
469			visual_param_name_map_t::iterator name_iter = nameres.first;
470			name_iter->second = param;
471		}
472	}
473	//llinfos << "Adding Visual Param '" << param->getName() << "' ( " << index << " )" << llendl;
474}
475
476//-----------------------------------------------------------------------------
477// updateVisualParams()
478//-----------------------------------------------------------------------------
479void LLCharacter::updateVisualParams()
480{
481	for (LLVisualParam *param = getFirstVisualParam(); 
482		param;
483		param = getNextVisualParam())
484	{
485		if (param->isAnimating())
486		{
487			continue;
488		}
489		// only apply parameters whose effective weight has changed
490		F32 effective_weight = ( param->getSex() & mSex ) ? param->getWeight() : param->getDefaultWeight();
491		if (effective_weight != param->getLastWeight())
492		{
493			param->apply( mSex );
494		}
495	}
496}
497 
498LLAnimPauseRequest LLCharacter::requestPause()
499{
500	mMotionController.pauseAllMotions();
501	return mPauseRequest;
502}
503