PageRenderTime 237ms CodeModel.GetById 20ms app.highlight 160ms RepoModel.GetById 31ms app.codeStats 1ms

/indra/newview/llviewerjointmesh.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 847 lines | 541 code | 140 blank | 166 comment | 86 complexity | 3249125e53b57486c1be3cdc66521062 MD5 | raw file
  1/** 
  2 * @file llviewerjointmesh.cpp
  3 * @brief Implementation of LLViewerJointMesh 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#include "llviewerprecompiledheaders.h"
 31
 32#include "imageids.h"
 33#include "llfasttimer.h"
 34#include "llrender.h"
 35
 36#include "llapr.h"
 37#include "llbox.h"
 38#include "lldrawable.h"
 39#include "lldrawpoolavatar.h"
 40#include "lldrawpoolbump.h"
 41#include "lldynamictexture.h"
 42#include "llface.h"
 43#include "llgldbg.h"
 44#include "llglheaders.h"
 45#include "lltexlayer.h"
 46#include "llviewercamera.h"
 47#include "llviewercontrol.h"
 48#include "llviewertexturelist.h"
 49#include "llviewerjointmesh.h"
 50#include "llvoavatar.h"
 51#include "llsky.h"
 52#include "pipeline.h"
 53#include "llviewershadermgr.h"
 54#include "llmath.h"
 55#include "v4math.h"
 56#include "m3math.h"
 57#include "m4math.h"
 58#include "llmatrix4a.h"
 59
 60#if !LL_DARWIN && !LL_LINUX && !LL_SOLARIS
 61extern PFNGLWEIGHTPOINTERARBPROC glWeightPointerARB;
 62extern PFNGLWEIGHTFVARBPROC glWeightfvARB;
 63extern PFNGLVERTEXBLENDARBPROC glVertexBlendARB;
 64#endif
 65
 66static LLPointer<LLVertexBuffer> sRenderBuffer = NULL;
 67static const U32 sRenderMask = LLVertexBuffer::MAP_VERTEX |
 68							   LLVertexBuffer::MAP_NORMAL |
 69							   LLVertexBuffer::MAP_TEXCOORD0;
 70
 71
 72//-----------------------------------------------------------------------------
 73//-----------------------------------------------------------------------------
 74// LLViewerJointMesh::LLSkinJoint
 75//-----------------------------------------------------------------------------
 76//-----------------------------------------------------------------------------
 77
 78//-----------------------------------------------------------------------------
 79// LLSkinJoint
 80//-----------------------------------------------------------------------------
 81LLSkinJoint::LLSkinJoint()
 82{
 83	mJoint       = NULL;
 84}
 85
 86//-----------------------------------------------------------------------------
 87// ~LLSkinJoint
 88//-----------------------------------------------------------------------------
 89LLSkinJoint::~LLSkinJoint()
 90{
 91	mJoint = NULL;
 92}
 93
 94
 95//-----------------------------------------------------------------------------
 96// LLSkinJoint::setupSkinJoint()
 97//-----------------------------------------------------------------------------
 98BOOL LLSkinJoint::setupSkinJoint( LLViewerJoint *joint)
 99{
100	// find the named joint
101	mJoint = joint;
102	if ( !mJoint )
103	{
104		llinfos << "Can't find joint" << llendl;
105	}
106
107	// compute the inverse root skin matrix
108	mRootToJointSkinOffset.clearVec();
109
110	LLVector3 rootSkinOffset;
111	while (joint)
112	{
113		rootSkinOffset += joint->getSkinOffset();
114		joint = (LLViewerJoint*)joint->getParent();
115	}
116
117	mRootToJointSkinOffset = -rootSkinOffset;
118	mRootToParentJointSkinOffset = mRootToJointSkinOffset;
119	mRootToParentJointSkinOffset += mJoint->getSkinOffset();
120
121	return TRUE;
122}
123
124
125//-----------------------------------------------------------------------------
126//-----------------------------------------------------------------------------
127// LLViewerJointMesh
128//-----------------------------------------------------------------------------
129//-----------------------------------------------------------------------------
130
131BOOL LLViewerJointMesh::sPipelineRender = FALSE;
132EAvatarRenderPass LLViewerJointMesh::sRenderPass = AVATAR_RENDER_PASS_SINGLE;
133U32 LLViewerJointMesh::sClothingMaskImageName = 0;
134LLColor4 LLViewerJointMesh::sClothingInnerColor;
135
136//-----------------------------------------------------------------------------
137// LLViewerJointMesh()
138//-----------------------------------------------------------------------------
139LLViewerJointMesh::LLViewerJointMesh()
140	:
141	mTexture( NULL ),
142	mLayerSet( NULL ),
143	mTestImageName( 0 ),
144	mFaceIndexCount(0),
145	mIsTransparent(FALSE)
146{
147
148	mColor[0] = 1.0f;
149	mColor[1] = 1.0f;
150	mColor[2] = 1.0f;
151	mColor[3] = 1.0f;
152	mShiny = 0.0f;
153	mCullBackFaces = TRUE;
154
155	mMesh = NULL;
156
157	mNumSkinJoints = 0;
158	mSkinJoints = NULL;
159
160	mFace = NULL;
161
162	mMeshID = 0;
163	mUpdateXform = FALSE;
164
165	mValid = FALSE;
166}
167
168
169//-----------------------------------------------------------------------------
170// ~LLViewerJointMesh()
171// Class Destructor
172//-----------------------------------------------------------------------------
173LLViewerJointMesh::~LLViewerJointMesh()
174{
175	mMesh = NULL;
176	mTexture = NULL;
177	freeSkinData();
178}
179
180
181//-----------------------------------------------------------------------------
182// LLViewerJointMesh::allocateSkinData()
183//-----------------------------------------------------------------------------
184BOOL LLViewerJointMesh::allocateSkinData( U32 numSkinJoints )
185{
186	mSkinJoints = new LLSkinJoint[ numSkinJoints ];
187	mNumSkinJoints = numSkinJoints;
188	return TRUE;
189}
190
191//-----------------------------------------------------------------------------
192// LLViewerJointMesh::freeSkinData()
193//-----------------------------------------------------------------------------
194void LLViewerJointMesh::freeSkinData()
195{
196	mNumSkinJoints = 0;
197	delete [] mSkinJoints;
198	mSkinJoints = NULL;
199}
200
201//--------------------------------------------------------------------
202// LLViewerJointMesh::getColor()
203//--------------------------------------------------------------------
204void LLViewerJointMesh::getColor( F32 *red, F32 *green, F32 *blue, F32 *alpha )
205{
206	*red   = mColor[0];
207	*green = mColor[1];
208	*blue  = mColor[2];
209	*alpha = mColor[3];
210}
211
212//--------------------------------------------------------------------
213// LLViewerJointMesh::setColor()
214//--------------------------------------------------------------------
215void LLViewerJointMesh::setColor( F32 red, F32 green, F32 blue, F32 alpha )
216{
217	mColor[0] = red;
218	mColor[1] = green;
219	mColor[2] = blue;
220	mColor[3] = alpha;
221}
222
223
224//--------------------------------------------------------------------
225// LLViewerJointMesh::getTexture()
226//--------------------------------------------------------------------
227//LLViewerTexture *LLViewerJointMesh::getTexture()
228//{
229//	return mTexture;
230//}
231
232//--------------------------------------------------------------------
233// LLViewerJointMesh::setTexture()
234//--------------------------------------------------------------------
235void LLViewerJointMesh::setTexture( LLViewerTexture *texture )
236{
237	mTexture = texture;
238
239	// texture and dynamic_texture are mutually exclusive
240	if( texture )
241	{
242		mLayerSet = NULL;
243		//texture->bindTexture(0);
244		//texture->setClamp(TRUE, TRUE);
245	}
246}
247
248//--------------------------------------------------------------------
249// LLViewerJointMesh::setLayerSet()
250// Sets the shape texture (takes precedence over normal texture)
251//--------------------------------------------------------------------
252void LLViewerJointMesh::setLayerSet( LLTexLayerSet* layer_set )
253{
254	mLayerSet = layer_set;
255	
256	// texture and dynamic_texture are mutually exclusive
257	if( layer_set )
258	{
259		mTexture = NULL;
260	}
261}
262
263
264
265//--------------------------------------------------------------------
266// LLViewerJointMesh::getMesh()
267//--------------------------------------------------------------------
268LLPolyMesh *LLViewerJointMesh::getMesh()
269{
270	return mMesh;
271}
272
273//-----------------------------------------------------------------------------
274// LLViewerJointMesh::setMesh()
275//-----------------------------------------------------------------------------
276void LLViewerJointMesh::setMesh( LLPolyMesh *mesh )
277{
278	// set the mesh pointer
279	mMesh = mesh;
280
281	// release any existing skin joints
282	freeSkinData();
283
284	if ( mMesh == NULL )
285	{
286		return;
287	}
288
289	// acquire the transform from the mesh object
290	setPosition( mMesh->getPosition() );
291	setRotation( mMesh->getRotation() );
292	setScale( mMesh->getScale() );
293
294	// create skin joints if necessary
295	if ( mMesh->hasWeights() && !mMesh->isLOD())
296	{
297		U32 numJointNames = mMesh->getNumJointNames();
298		
299		allocateSkinData( numJointNames );
300		std::string *jointNames = mMesh->getJointNames();
301
302		U32 jn;
303		for (jn = 0; jn < numJointNames; jn++)
304		{
305			//llinfos << "Setting up joint " << jointNames[jn] << llendl;
306			LLViewerJoint* joint = (LLViewerJoint*)(getRoot()->findJoint(jointNames[jn]) );
307			mSkinJoints[jn].setupSkinJoint( joint );
308		}
309	}
310
311	// setup joint array
312	if (!mMesh->isLOD())
313	{
314		setupJoint((LLViewerJoint*)getRoot());
315	}
316
317//	llinfos << "joint render entries: " << mMesh->mJointRenderData.count() << llendl;
318}
319
320//-----------------------------------------------------------------------------
321// setupJoint()
322//-----------------------------------------------------------------------------
323void LLViewerJointMesh::setupJoint(LLViewerJoint* current_joint)
324{
325//	llinfos << "Mesh: " << getName() << llendl;
326
327//	S32 joint_count = 0;
328	U32 sj;
329	for (sj=0; sj<mNumSkinJoints; sj++)
330	{
331		LLSkinJoint &js = mSkinJoints[sj];
332
333		if (js.mJoint != current_joint)
334		{
335			continue;
336		}
337
338		// we've found a skinjoint for this joint..
339
340		// is the last joint in the array our parent?
341		if(mMesh->mJointRenderData.count() && mMesh->mJointRenderData[mMesh->mJointRenderData.count() - 1]->mWorldMatrix == &current_joint->getParent()->getWorldMatrix())
342		{
343			// ...then just add ourselves
344			LLViewerJoint* jointp = js.mJoint;
345			mMesh->mJointRenderData.put(new LLJointRenderData(&jointp->getWorldMatrix(), &js));
346//			llinfos << "joint " << joint_count << js.mJoint->getName() << llendl;
347//			joint_count++;
348		}
349		// otherwise add our parent and ourselves
350		else
351		{
352			mMesh->mJointRenderData.put(new LLJointRenderData(&current_joint->getParent()->getWorldMatrix(), NULL));
353//			llinfos << "joint " << joint_count << current_joint->getParent()->getName() << llendl;
354//			joint_count++;
355			mMesh->mJointRenderData.put(new LLJointRenderData(&current_joint->getWorldMatrix(), &js));
356//			llinfos << "joint " << joint_count << current_joint->getName() << llendl;
357//			joint_count++;
358		}
359	}
360
361	// depth-first traversal
362	for (LLJoint::child_list_t::iterator iter = current_joint->mChildren.begin();
363		 iter != current_joint->mChildren.end(); ++iter)
364	{
365		LLViewerJoint* child_joint = (LLViewerJoint*)(*iter);
366		setupJoint(child_joint);
367	}
368}
369
370const S32 NUM_AXES = 3;
371
372// register layoud
373// rotation X 0-n
374// rotation Y 0-n
375// rotation Z 0-n
376// pivot parent 0-n -- child = n+1
377
378static LLMatrix4	gJointMatUnaligned[32];
379static LLMatrix4a	gJointMatAligned[32];
380static LLMatrix3	gJointRotUnaligned[32];
381static LLVector4	gJointPivot[32];
382
383//-----------------------------------------------------------------------------
384// uploadJointMatrices()
385//-----------------------------------------------------------------------------
386void LLViewerJointMesh::uploadJointMatrices()
387{
388	S32 joint_num;
389	LLPolyMesh *reference_mesh = mMesh->getReferenceMesh();
390	LLDrawPool *poolp = mFace ? mFace->getPool() : NULL;
391	BOOL hardware_skinning = (poolp && poolp->getVertexShaderLevel() > 0) ? TRUE : FALSE;
392
393	//calculate joint matrices
394	for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); joint_num++)
395	{
396		LLMatrix4 joint_mat = *reference_mesh->mJointRenderData[joint_num]->mWorldMatrix;
397
398		if (hardware_skinning)
399		{
400			joint_mat *= LLDrawPoolAvatar::getModelView();
401		}
402		gJointMatUnaligned[joint_num] = joint_mat;
403		gJointRotUnaligned[joint_num] = joint_mat.getMat3();
404	}
405
406	BOOL last_pivot_uploaded = FALSE;
407	S32 j = 0;
408
409	//upload joint pivots
410	for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); joint_num++)
411	{
412		LLSkinJoint *sj = reference_mesh->mJointRenderData[joint_num]->mSkinJoint;
413		if (sj)
414		{
415			if (!last_pivot_uploaded)
416			{
417				LLVector4 parent_pivot(sj->mRootToParentJointSkinOffset);
418				parent_pivot.mV[VW] = 0.f;
419				gJointPivot[j++] = parent_pivot;
420			}
421
422			LLVector4 child_pivot(sj->mRootToJointSkinOffset);
423			child_pivot.mV[VW] = 0.f;
424
425			gJointPivot[j++] = child_pivot;
426
427			last_pivot_uploaded = TRUE;
428		}
429		else
430		{
431			last_pivot_uploaded = FALSE;
432		}
433	}
434
435	//add pivot point into transform
436	for (S32 i = 0; i < j; i++)
437	{
438		LLVector3 pivot;
439		pivot = LLVector3(gJointPivot[i]);
440		pivot = pivot * gJointRotUnaligned[i];
441		gJointMatUnaligned[i].translate(pivot);
442	}
443
444	// upload matrices
445	if (hardware_skinning)
446	{
447		GLfloat mat[45*4];
448		memset(mat, 0, sizeof(GLfloat)*45*4);
449
450		for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); joint_num++)
451		{
452			gJointMatUnaligned[joint_num].transpose();
453
454			for (S32 axis = 0; axis < NUM_AXES; axis++)
455			{
456				F32* vector = gJointMatUnaligned[joint_num].mMatrix[axis];
457				U32 offset = LL_CHARACTER_MAX_JOINTS_PER_MESH*axis+joint_num;
458				memcpy(mat+offset*4, vector, sizeof(GLfloat)*4);
459			}
460		}
461		stop_glerror();
462		if (LLGLSLShader::sCurBoundShaderPtr)
463		{
464			LLGLSLShader::sCurBoundShaderPtr->uniform4fv(LLViewerShaderMgr::AVATAR_MATRIX, 45, mat);
465		}
466		stop_glerror();
467	}
468	else
469	{
470		//load gJointMatUnaligned into gJointMatAligned
471		for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); ++joint_num)
472		{
473			gJointMatAligned[joint_num].loadu(gJointMatUnaligned[joint_num]);
474		}
475	}
476}
477
478//--------------------------------------------------------------------
479// LLViewerJointMesh::drawBone()
480//--------------------------------------------------------------------
481void LLViewerJointMesh::drawBone()
482{
483}
484
485//--------------------------------------------------------------------
486// LLViewerJointMesh::isTransparent()
487//--------------------------------------------------------------------
488BOOL LLViewerJointMesh::isTransparent()
489{
490	return mIsTransparent;
491}
492
493//--------------------------------------------------------------------
494// DrawElementsBLEND and utility code
495//--------------------------------------------------------------------
496
497// compate_int is used by the qsort function to sort the index array
498int compare_int(const void *a, const void *b)
499{
500	if (*(U32*)a < *(U32*)b)
501	{
502		return -1;
503	}
504	else if (*(U32*)a > *(U32*)b)
505	{
506		return 1;
507	}
508	else return 0;
509}
510
511//--------------------------------------------------------------------
512// LLViewerJointMesh::drawShape()
513//--------------------------------------------------------------------
514U32 LLViewerJointMesh::drawShape( F32 pixelArea, BOOL first_pass, BOOL is_dummy)
515{
516	if (!mValid || !mMesh || !mFace || !mVisible || 
517		!mFace->getVertexBuffer() ||
518		mMesh->getNumFaces() == 0 ||
519		(LLGLSLShader::sNoFixedFunction && LLGLSLShader::sCurBoundShaderPtr == NULL))
520	{
521		return 0;
522	}
523
524	U32 triangle_count = 0;
525
526	S32 diffuse_channel = LLDrawPoolAvatar::sDiffuseChannel;
527
528	stop_glerror();
529	
530	//----------------------------------------------------------------
531	// setup current color
532	//----------------------------------------------------------------
533	if (is_dummy)
534		gGL.diffuseColor4fv(LLVOAvatar::getDummyColor().mV);
535	else
536		gGL.diffuseColor4fv(mColor.mV);
537
538	stop_glerror();
539	
540	LLGLSSpecular specular(LLColor4(1.f,1.f,1.f,1.f), (mFace->getPool()->getVertexShaderLevel() > 0 || LLGLSLShader::sNoFixedFunction) ? 0.f : mShiny);
541
542	//----------------------------------------------------------------
543	// setup current texture
544	//----------------------------------------------------------------
545	llassert( !(mTexture.notNull() && mLayerSet) );  // mutually exclusive
546
547	LLTexUnit::eTextureAddressMode old_mode = LLTexUnit::TAM_WRAP;
548	if (mTestImageName)
549	{
550		gGL.getTexUnit(diffuse_channel)->bindManual(LLTexUnit::TT_TEXTURE, mTestImageName);
551
552		if (mIsTransparent)
553		{
554			gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
555		}
556		else
557		{
558			gGL.diffuseColor4f(0.7f, 0.6f, 0.3f, 1.f);
559			gGL.getTexUnit(diffuse_channel)->setTextureColorBlend(LLTexUnit::TBO_LERP_TEX_ALPHA, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_PREV_COLOR);
560		}
561	}
562	else if( !is_dummy && mLayerSet )
563	{
564		if(	mLayerSet->hasComposite() )
565		{
566			gGL.getTexUnit(diffuse_channel)->bind(mLayerSet->getComposite());
567		}
568		else
569		{
570			gGL.getTexUnit(diffuse_channel)->bind(LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT));
571		}
572	}
573	else
574	if ( !is_dummy && mTexture.notNull() )
575	{
576		if(mTexture->hasGLTexture())
577		{
578			old_mode = mTexture->getAddressMode();
579		}
580		gGL.getTexUnit(diffuse_channel)->bind(mTexture.get());
581		gGL.getTexUnit(diffuse_channel)->bind(mTexture);
582		gGL.getTexUnit(diffuse_channel)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
583	}
584	else
585	{
586		gGL.getTexUnit(diffuse_channel)->bind(LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT));
587	}
588	
589	
590	U32 mask = sRenderMask;
591
592	U32 start = mMesh->mFaceVertexOffset;
593	U32 end = start + mMesh->mFaceVertexCount - 1;
594	U32 count = mMesh->mFaceIndexCount;
595	U32 offset = mMesh->mFaceIndexOffset;
596
597	LLVertexBuffer* buff = mFace->getVertexBuffer();
598
599	if (mMesh->hasWeights())
600	{
601		if ((mFace->getPool()->getVertexShaderLevel() > 0))
602		{
603			if (first_pass)
604			{
605				uploadJointMatrices();
606			}
607			mask = mask | LLVertexBuffer::MAP_WEIGHT;
608			if (mFace->getPool()->getVertexShaderLevel() > 1)
609			{
610				mask = mask | LLVertexBuffer::MAP_CLOTHWEIGHT;
611			}
612		}
613		
614		buff->setBuffer(mask);
615		buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
616	}
617	else
618	{
619		gGL.pushMatrix();
620		LLMatrix4 jointToWorld = getWorldMatrix();
621		gGL.multMatrix((GLfloat*)jointToWorld.mMatrix);
622		buff->setBuffer(mask);
623		buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
624		gGL.popMatrix();
625	}
626	gPipeline.addTrianglesDrawn(count);
627
628	triangle_count += count;
629	
630	if (mTestImageName)
631	{
632		gGL.getTexUnit(diffuse_channel)->setTextureBlendType(LLTexUnit::TB_MULT);
633	}
634
635	if (mTexture.notNull() && !is_dummy)
636	{
637		gGL.getTexUnit(diffuse_channel)->bind(mTexture);
638		gGL.getTexUnit(diffuse_channel)->setTextureAddressMode(old_mode);
639	}
640
641	return triangle_count;
642}
643
644//-----------------------------------------------------------------------------
645// updateFaceSizes()
646//-----------------------------------------------------------------------------
647void LLViewerJointMesh::updateFaceSizes(U32 &num_vertices, U32& num_indices, F32 pixel_area)
648{
649	//bump num_vertices to next multiple of 4
650	num_vertices = (num_vertices + 0x3) & ~0x3;
651
652	// Do a pre-alloc pass to determine sizes of data.
653	if (mMesh && mValid)
654	{
655		mMesh->mFaceVertexOffset = num_vertices;
656		mMesh->mFaceVertexCount = mMesh->getNumVertices();
657		mMesh->mFaceIndexOffset = num_indices;
658		mMesh->mFaceIndexCount = mMesh->getSharedData()->mNumTriangleIndices;
659
660		mMesh->getReferenceMesh()->mCurVertexCount = mMesh->mFaceVertexCount;
661
662		num_vertices += mMesh->getNumVertices();
663		num_indices += mMesh->mFaceIndexCount;
664	}
665}
666
667//-----------------------------------------------------------------------------
668// updateFaceData()
669//-----------------------------------------------------------------------------
670static LLFastTimer::DeclareTimer FTM_AVATAR_FACE("Avatar Face");
671
672void LLViewerJointMesh::updateFaceData(LLFace *face, F32 pixel_area, BOOL damp_wind, bool terse_update)
673{
674	//IF THIS FUNCTION BREAKS, SEE LLPOLYMESH CONSTRUCTOR AND CHECK ALIGNMENT OF INPUT ARRAYS
675
676	mFace = face;
677
678	if (!mFace->getVertexBuffer())
679	{
680		return;
681	}
682
683	LLDrawPool *poolp = mFace->getPool();
684	BOOL hardware_skinning = (poolp && poolp->getVertexShaderLevel() > 0) ? TRUE : FALSE;
685
686	if (!hardware_skinning && terse_update)
687	{ //no need to do terse updates if we're doing software vertex skinning
688	 // since mMesh is being copied into mVertexBuffer every frame
689		return;
690	}
691
692
693	LLFastTimer t(FTM_AVATAR_FACE);
694
695	LLStrider<LLVector3> verticesp;
696	LLStrider<LLVector3> normalsp;
697	LLStrider<LLVector2> tex_coordsp;
698	LLStrider<F32>		 vertex_weightsp;
699	LLStrider<LLVector4> clothing_weightsp;
700	LLStrider<U16> indicesp;
701
702	// Copy data into the faces from the polymesh data.
703	if (mMesh && mValid)
704	{
705		const U32 num_verts = mMesh->getNumVertices();
706
707		if (num_verts)
708		{
709			face->getVertexBuffer()->getIndexStrider(indicesp);
710			face->getGeometryAvatar(verticesp, normalsp, tex_coordsp, vertex_weightsp, clothing_weightsp);
711			
712			verticesp += mMesh->mFaceVertexOffset;
713			normalsp += mMesh->mFaceVertexOffset;
714			
715			F32* v = (F32*) verticesp.get();
716			F32* n = (F32*) normalsp.get();
717			
718			U32 words = num_verts*4;
719
720			LLVector4a::memcpyNonAliased16(v, (F32*) mMesh->getCoords(), words*sizeof(F32));
721			LLVector4a::memcpyNonAliased16(n, (F32*) mMesh->getNormals(), words*sizeof(F32));
722						
723			
724			if (!terse_update)
725			{
726				vertex_weightsp += mMesh->mFaceVertexOffset;
727				clothing_weightsp += mMesh->mFaceVertexOffset;
728				tex_coordsp += mMesh->mFaceVertexOffset;
729		
730				F32* tc = (F32*) tex_coordsp.get();
731				F32* vw = (F32*) vertex_weightsp.get();
732				F32* cw = (F32*) clothing_weightsp.get();	
733
734				LLVector4a::memcpyNonAliased16(tc, (F32*) mMesh->getTexCoords(), num_verts*2*sizeof(F32));
735				LLVector4a::memcpyNonAliased16(vw, (F32*) mMesh->getWeights(), num_verts*sizeof(F32));	
736				LLVector4a::memcpyNonAliased16(cw, (F32*) mMesh->getClothingWeights(), num_verts*4*sizeof(F32));	
737			}
738
739			const U32 idx_count = mMesh->getNumFaces()*3;
740
741			indicesp += mMesh->mFaceIndexOffset;
742
743			U16* __restrict idx = indicesp.get();
744			S32* __restrict src_idx = (S32*) mMesh->getFaces();	
745
746			const S32 offset = (S32) mMesh->mFaceVertexOffset;
747
748			for (S32 i = 0; i < idx_count; ++i)
749			{
750				*(idx++) = *(src_idx++)+offset;
751			}
752		}
753	}
754}
755
756
757
758//-----------------------------------------------------------------------------
759// updateLOD()
760//-----------------------------------------------------------------------------
761BOOL LLViewerJointMesh::updateLOD(F32 pixel_area, BOOL activate)
762{
763	BOOL valid = mValid;
764	setValid(activate, TRUE);
765	return (valid != activate);
766}
767
768// static
769void LLViewerJointMesh::updateGeometry(LLFace *mFace, LLPolyMesh *mMesh)
770{
771	LLStrider<LLVector3> o_vertices;
772	LLStrider<LLVector3> o_normals;
773
774	//get vertex and normal striders
775	LLVertexBuffer* buffer = mFace->getVertexBuffer();
776	buffer->getVertexStrider(o_vertices,  0);
777	buffer->getNormalStrider(o_normals,   0);
778
779	F32* __restrict vert = o_vertices[0].mV;
780	F32* __restrict norm = o_normals[0].mV;
781
782	const F32* __restrict weights = mMesh->getWeights();
783	const LLVector4a* __restrict coords = (LLVector4a*) mMesh->getCoords();
784	const LLVector4a* __restrict normals = (LLVector4a*) mMesh->getNormals();
785
786	U32 offset = mMesh->mFaceVertexOffset*4;
787	vert += offset;
788	norm += offset;
789
790	for (U32 index = 0; index < mMesh->getNumVertices(); index++)
791	{
792		// equivalent to joint = floorf(weights[index]);
793		S32 joint = _mm_cvtt_ss2si(_mm_load_ss(weights+index));
794		F32 w = weights[index] - joint;		
795
796		LLMatrix4a gBlendMat;
797
798		if (w != 0.f)
799		{
800			// blend between matrices and apply
801			gBlendMat.setLerp(gJointMatAligned[joint+0],
802							  gJointMatAligned[joint+1], w);
803
804			LLVector4a res;
805			gBlendMat.affineTransform(coords[index], res);
806			res.store4a(vert+index*4);
807			gBlendMat.rotate(normals[index], res);
808			res.store4a(norm+index*4);
809		}
810		else
811		{  // No lerp required in this case.
812			LLVector4a res;
813			gJointMatAligned[joint].affineTransform(coords[index], res);
814			res.store4a(vert+index*4);
815			gJointMatAligned[joint].rotate(normals[index], res);
816			res.store4a(norm+index*4);
817		}
818	}
819
820	buffer->flush();
821}
822
823void LLViewerJointMesh::updateJointGeometry()
824{
825	if (!(mValid
826		  && mMesh
827		  && mFace
828		  && mMesh->hasWeights()
829		  && mFace->getVertexBuffer()
830		  && LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_AVATAR) == 0))
831	{
832		return;
833	}
834
835	uploadJointMatrices();
836	updateGeometry(mFace, mMesh);
837}
838
839void LLViewerJointMesh::dump()
840{
841	if (mValid)
842	{
843		llinfos << "Usable LOD " << mName << llendl;
844	}
845}
846
847// End