PageRenderTime 143ms CodeModel.GetById 15ms app.highlight 115ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lldrawpoolavatar.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1721 lines | 1381 code | 262 blank | 78 comment | 198 complexity | 574bb64e79ec25cafb59b3a49cf84030 MD5 | raw file
   1/** 
   2 * @file lldrawpoolavatar.cpp
   3 * @brief LLDrawPoolAvatar 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 "lldrawpoolavatar.h"
  30#include "llrender.h"
  31
  32#include "llvoavatar.h"
  33#include "m3math.h"
  34#include "llmatrix4a.h"
  35
  36#include "llagent.h" //for gAgent.needsRenderAvatar()
  37#include "lldrawable.h"
  38#include "lldrawpoolbump.h"
  39#include "llface.h"
  40#include "llmeshrepository.h"
  41#include "llsky.h"
  42#include "llviewercamera.h"
  43#include "llviewerregion.h"
  44#include "noise.h"
  45#include "pipeline.h"
  46#include "llviewershadermgr.h"
  47#include "llvovolume.h"
  48#include "llvolume.h"
  49#include "llappviewer.h"
  50#include "llrendersphere.h"
  51#include "llviewerpartsim.h"
  52
  53static U32 sDataMask = LLDrawPoolAvatar::VERTEX_DATA_MASK;
  54static U32 sBufferUsage = GL_STREAM_DRAW_ARB;
  55static U32 sShaderLevel = 0;
  56
  57
  58LLGLSLShader* LLDrawPoolAvatar::sVertexProgram = NULL;
  59BOOL	LLDrawPoolAvatar::sSkipOpaque = FALSE;
  60BOOL	LLDrawPoolAvatar::sSkipTransparent = FALSE;
  61S32 LLDrawPoolAvatar::sDiffuseChannel = 0;
  62
  63
  64static bool is_deferred_render = false;
  65
  66extern BOOL gUseGLPick;
  67
  68F32 CLOTHING_GRAVITY_EFFECT = 0.7f;
  69F32 CLOTHING_ACCEL_FORCE_FACTOR = 0.2f;
  70const S32 NUM_TEST_AVATARS = 30;
  71const S32 MIN_PIXEL_AREA_2_PASS_SKINNING = 500000000;
  72
  73// Format for gAGPVertices
  74// vertex format for bumpmapping:
  75//  vertices   12
  76//  pad		    4
  77//  normals    12
  78//  pad		    4
  79//  texcoords0  8
  80//  texcoords1  8
  81// total       48
  82//
  83// for no bumpmapping
  84//  vertices	   12
  85//  texcoords	8
  86//  normals	   12
  87// total	   32
  88//
  89
  90S32 AVATAR_OFFSET_POS = 0;
  91S32 AVATAR_OFFSET_NORMAL = 16;
  92S32 AVATAR_OFFSET_TEX0 = 32;
  93S32 AVATAR_OFFSET_TEX1 = 40;
  94S32 AVATAR_VERTEX_BYTES = 48;
  95
  96BOOL gAvatarEmbossBumpMap = FALSE;
  97static BOOL sRenderingSkinned = FALSE;
  98S32 normal_channel = -1;
  99S32 specular_channel = -1;
 100S32 cube_channel = -1;
 101
 102static LLFastTimer::DeclareTimer FTM_SHADOW_AVATAR("Avatar Shadow");
 103
 104LLDrawPoolAvatar::LLDrawPoolAvatar() : 
 105	LLFacePool(POOL_AVATAR)	
 106{
 107}
 108
 109//-----------------------------------------------------------------------------
 110// instancePool()
 111//-----------------------------------------------------------------------------
 112LLDrawPool *LLDrawPoolAvatar::instancePool()
 113{
 114	return new LLDrawPoolAvatar();
 115}
 116
 117
 118S32 LLDrawPoolAvatar::getVertexShaderLevel() const
 119{
 120	return (S32) LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_AVATAR);
 121}
 122
 123void LLDrawPoolAvatar::prerender()
 124{
 125	mVertexShaderLevel = LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_AVATAR);
 126	
 127	sShaderLevel = mVertexShaderLevel;
 128	
 129	if (sShaderLevel > 0)
 130	{
 131		sBufferUsage = GL_DYNAMIC_DRAW_ARB;
 132	}
 133	else
 134	{
 135		sBufferUsage = GL_STREAM_DRAW_ARB;
 136	}
 137}
 138
 139LLMatrix4& LLDrawPoolAvatar::getModelView()
 140{
 141	static LLMatrix4 ret;
 142
 143	ret.initRows(LLVector4(gGLModelView+0),
 144				 LLVector4(gGLModelView+4),
 145				 LLVector4(gGLModelView+8),
 146				 LLVector4(gGLModelView+12));
 147
 148	return ret;
 149}
 150
 151//-----------------------------------------------------------------------------
 152// render()
 153//-----------------------------------------------------------------------------
 154
 155
 156
 157void LLDrawPoolAvatar::beginDeferredPass(S32 pass)
 158{
 159	LLFastTimer t(FTM_RENDER_CHARACTERS);
 160	
 161	sSkipTransparent = TRUE;
 162	is_deferred_render = true;
 163	
 164	if (LLPipeline::sImpostorRender)
 165	{ //impostor pass does not have rigid or impostor rendering
 166		pass += 2;
 167	}
 168
 169	switch (pass)
 170	{
 171	case 0:
 172		beginDeferredImpostor();
 173		break;
 174	case 1:
 175		beginDeferredRigid();
 176		break;
 177	case 2:
 178		beginDeferredSkinned();
 179		break;
 180	case 3:
 181		beginDeferredRiggedSimple();
 182		break;
 183	case 4:
 184		beginDeferredRiggedBump();
 185		break;
 186	}
 187}
 188
 189void LLDrawPoolAvatar::endDeferredPass(S32 pass)
 190{
 191	LLFastTimer t(FTM_RENDER_CHARACTERS);
 192
 193	sSkipTransparent = FALSE;
 194	is_deferred_render = false;
 195
 196	if (LLPipeline::sImpostorRender)
 197	{
 198		pass += 2;
 199	}
 200
 201	switch (pass)
 202	{
 203	case 0:
 204		endDeferredImpostor();
 205		break;
 206	case 1:
 207		endDeferredRigid();
 208		break;
 209	case 2:
 210		endDeferredSkinned();
 211		break;
 212	case 3:
 213		endDeferredRiggedSimple();
 214		break;
 215	case 4:
 216		endDeferredRiggedBump();
 217		break;
 218	}
 219}
 220
 221void LLDrawPoolAvatar::renderDeferred(S32 pass)
 222{
 223	render(pass);
 224}
 225
 226S32 LLDrawPoolAvatar::getNumPostDeferredPasses()
 227{
 228	return 6;
 229}
 230
 231void LLDrawPoolAvatar::beginPostDeferredPass(S32 pass)
 232{
 233	switch (pass)
 234	{
 235	case 0:
 236		beginPostDeferredAlpha();
 237		break;
 238	case 1:
 239		beginRiggedFullbright();
 240		break;
 241	case 2:
 242		beginRiggedFullbrightShiny();
 243		break;
 244	case 3:
 245		beginDeferredRiggedAlpha();
 246		break;
 247	case 4:
 248		beginRiggedFullbrightAlpha();
 249		break;
 250	case 5:
 251		beginRiggedGlow();
 252		break;
 253	}
 254}
 255
 256void LLDrawPoolAvatar::beginPostDeferredAlpha()
 257{
 258	sSkipOpaque = TRUE;
 259	sShaderLevel = mVertexShaderLevel;
 260	sVertexProgram = &gDeferredAvatarAlphaProgram;
 261	sRenderingSkinned = TRUE;
 262
 263	gPipeline.bindDeferredShader(*sVertexProgram);
 264	
 265	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 266}
 267
 268void LLDrawPoolAvatar::beginDeferredRiggedAlpha()
 269{
 270	sVertexProgram = &gDeferredSkinnedAlphaProgram;
 271	gPipeline.bindDeferredShader(*sVertexProgram);
 272	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 273	gPipeline.enableLightsDynamic();
 274}
 275
 276void LLDrawPoolAvatar::endDeferredRiggedAlpha()
 277{
 278	LLVertexBuffer::unbind();
 279	gPipeline.unbindDeferredShader(*sVertexProgram);
 280	sDiffuseChannel = 0;
 281	sVertexProgram = NULL;
 282}
 283
 284void LLDrawPoolAvatar::endPostDeferredPass(S32 pass)
 285{
 286	switch (pass)
 287	{
 288	case 0:
 289		endPostDeferredAlpha();
 290		break;
 291	case 1:
 292		endRiggedFullbright();
 293		break;
 294	case 2:
 295		endRiggedFullbrightShiny();
 296		break;
 297	case 3:
 298		endDeferredRiggedAlpha();
 299		break;
 300	case 4:
 301		endRiggedFullbrightAlpha();
 302		break;
 303	case 5:
 304		endRiggedGlow();
 305		break;
 306	}
 307}
 308
 309void LLDrawPoolAvatar::endPostDeferredAlpha()
 310{
 311	// if we're in software-blending, remember to set the fence _after_ we draw so we wait till this rendering is done
 312	sRenderingSkinned = FALSE;
 313	sSkipOpaque = FALSE;
 314		
 315	gPipeline.unbindDeferredShader(*sVertexProgram);
 316	sDiffuseChannel = 0;
 317	sShaderLevel = mVertexShaderLevel;
 318}
 319
 320void LLDrawPoolAvatar::renderPostDeferred(S32 pass)
 321{
 322	const S32 actual_pass[] =
 323	{ //map post deferred pass numbers to what render() expects
 324		2, //skinned
 325		4, // rigged fullbright
 326		6, //rigged fullbright shiny
 327		7, //rigged alpha
 328		8, //rigged fullbright alpha
 329		9, //rigged glow
 330	};
 331
 332	pass = actual_pass[pass];
 333
 334	if (LLPipeline::sImpostorRender)
 335	{ //HACK for impostors so actual pass ends up being proper pass
 336		pass -= 2;
 337	}
 338
 339	render(pass);
 340}
 341
 342
 343S32 LLDrawPoolAvatar::getNumShadowPasses()
 344{
 345	return 2;
 346}
 347
 348void LLDrawPoolAvatar::beginShadowPass(S32 pass)
 349{
 350	LLFastTimer t(FTM_SHADOW_AVATAR);
 351
 352	if (pass == 0)
 353	{
 354		sVertexProgram = &gDeferredAvatarShadowProgram;
 355		
 356		//gGL.setAlphaRejectSettings(LLRender::CF_GREATER_EQUAL, 0.2f);		
 357
 358		if ((sShaderLevel > 0))  // for hardware blending
 359		{
 360			sRenderingSkinned = TRUE;
 361			sVertexProgram->bind();
 362		}
 363
 364		gGL.diffuseColor4f(1,1,1,1);
 365	}
 366	else
 367	{
 368		sVertexProgram = &gDeferredAttachmentShadowProgram;
 369		sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 370		sVertexProgram->bind();
 371	}
 372}
 373
 374void LLDrawPoolAvatar::endShadowPass(S32 pass)
 375{
 376	LLFastTimer t(FTM_SHADOW_AVATAR);
 377	if (pass == 0)
 378	{
 379		if (sShaderLevel > 0)
 380		{
 381			sRenderingSkinned = FALSE;
 382			sVertexProgram->unbind();
 383		}
 384	}
 385	else
 386	{
 387		LLVertexBuffer::unbind();
 388		sVertexProgram->unbind();
 389		sVertexProgram = NULL;
 390	}
 391}
 392
 393void LLDrawPoolAvatar::renderShadow(S32 pass)
 394{
 395	LLFastTimer t(FTM_SHADOW_AVATAR);
 396
 397	if (mDrawFace.empty())
 398	{
 399		return;
 400	}
 401
 402	const LLFace *facep = mDrawFace[0];
 403	if (!facep->getDrawable())
 404	{
 405		return;
 406	}
 407	LLVOAvatar *avatarp = (LLVOAvatar *)facep->getDrawable()->getVObj().get();
 408
 409	if (avatarp->isDead() || avatarp->mIsDummy || avatarp->mDrawable.isNull())
 410	{
 411		return;
 412	}
 413
 414	BOOL impostor = avatarp->isImpostor();
 415	if (impostor)
 416	{
 417		return;
 418	}
 419	
 420	if (pass == 0)
 421	{
 422		avatarp->renderSkinned(AVATAR_RENDER_PASS_SINGLE);
 423	}
 424	else
 425	{
 426		renderRigged(avatarp, RIGGED_SIMPLE);
 427		renderRigged(avatarp, RIGGED_ALPHA);
 428		renderRigged(avatarp, RIGGED_FULLBRIGHT);
 429		renderRigged(avatarp, RIGGED_FULLBRIGHT_SHINY);
 430		renderRigged(avatarp, RIGGED_SHINY);
 431		renderRigged(avatarp, RIGGED_FULLBRIGHT_ALPHA);
 432	}
 433}
 434
 435S32 LLDrawPoolAvatar::getNumPasses()
 436{
 437	if (LLPipeline::sImpostorRender)
 438	{
 439		return 8;
 440	}
 441	else 
 442	{
 443		return 10;
 444	}
 445}
 446
 447
 448S32 LLDrawPoolAvatar::getNumDeferredPasses()
 449{
 450	if (LLPipeline::sImpostorRender)
 451	{
 452		return 3;
 453	}
 454	else
 455	{
 456		return 5;
 457	}
 458}
 459
 460
 461void LLDrawPoolAvatar::render(S32 pass)
 462{
 463	LLFastTimer t(FTM_RENDER_CHARACTERS);
 464	if (LLPipeline::sImpostorRender)
 465	{
 466		renderAvatars(NULL, pass+2);
 467		return;
 468	}
 469
 470	renderAvatars(NULL, pass); // render all avatars
 471}
 472
 473void LLDrawPoolAvatar::beginRenderPass(S32 pass)
 474{
 475	LLFastTimer t(FTM_RENDER_CHARACTERS);
 476	//reset vertex buffer mappings
 477	LLVertexBuffer::unbind();
 478
 479	if (LLPipeline::sImpostorRender)
 480	{ //impostor render does not have impostors or rigid rendering
 481		pass += 2;
 482	}
 483
 484	switch (pass)
 485	{
 486	case 0:
 487		beginImpostor();
 488		break;
 489	case 1:
 490		beginRigid();
 491		break;
 492	case 2:
 493		beginSkinned();
 494		break;
 495	case 3:
 496		beginRiggedSimple();
 497		break;
 498	case 4:
 499		beginRiggedFullbright();
 500		break;
 501	case 5:
 502		beginRiggedShinySimple();
 503		break;
 504	case 6:
 505		beginRiggedFullbrightShiny();
 506		break;
 507	case 7:
 508		beginRiggedAlpha();
 509		break;
 510	case 8:
 511		beginRiggedFullbrightAlpha();
 512		break;
 513	case 9:
 514		beginRiggedGlow();
 515		break;
 516	}
 517
 518	if (pass == 0)
 519	{ //make sure no stale colors are left over from a previous render
 520		gGL.diffuseColor4f(1,1,1,1);
 521	}
 522}
 523
 524void LLDrawPoolAvatar::endRenderPass(S32 pass)
 525{
 526	LLFastTimer t(FTM_RENDER_CHARACTERS);
 527
 528	if (LLPipeline::sImpostorRender)
 529	{
 530		pass += 2;		
 531	}
 532
 533	switch (pass)
 534	{
 535	case 0:
 536		endImpostor();
 537		break;
 538	case 1:
 539		endRigid();
 540		break;
 541	case 2:
 542		endSkinned();
 543		break;
 544	case 3:
 545		endRiggedSimple();
 546		break;
 547	case 4:
 548		endRiggedFullbright();
 549		break;
 550	case 5:
 551		endRiggedShinySimple();
 552		break;
 553	case 6:
 554		endRiggedFullbrightShiny();
 555		break;
 556	case 7:
 557		endRiggedAlpha();
 558		break;
 559	case 8:
 560		endRiggedFullbrightAlpha();
 561		break;
 562	case 9:
 563		endRiggedGlow();
 564		break;
 565	}
 566}
 567
 568void LLDrawPoolAvatar::beginImpostor()
 569{
 570	if (!LLPipeline::sReflectionRender)
 571	{
 572		LLVOAvatar::sRenderDistance = llclamp(LLVOAvatar::sRenderDistance, 16.f, 256.f);
 573		LLVOAvatar::sNumVisibleAvatars = 0;
 574	}
 575
 576	if (LLGLSLShader::sNoFixedFunction)
 577	{
 578		gImpostorProgram.bind();
 579		gImpostorProgram.setMinimumAlpha(0.01f);
 580	}
 581
 582	gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
 583	sDiffuseChannel = 0;
 584}
 585
 586void LLDrawPoolAvatar::endImpostor()
 587{
 588	if (LLGLSLShader::sNoFixedFunction)
 589	{
 590		gImpostorProgram.unbind();
 591	}
 592	gPipeline.enableLightsDynamic();
 593}
 594
 595void LLDrawPoolAvatar::beginRigid()
 596{
 597	if (gPipeline.canUseVertexShaders())
 598	{
 599		if (LLPipeline::sUnderWaterRender)
 600		{
 601			sVertexProgram = &gObjectAlphaMaskNoColorWaterProgram;
 602		}
 603		else
 604		{
 605			sVertexProgram = &gObjectAlphaMaskNoColorProgram;
 606		}
 607		
 608		if (sVertexProgram != NULL)
 609		{	//eyeballs render with the specular shader
 610			sVertexProgram->bind();
 611			sVertexProgram->setMinimumAlpha(0.2f);
 612		}
 613	}
 614	else
 615	{
 616		sVertexProgram = NULL;
 617	}
 618}
 619
 620void LLDrawPoolAvatar::endRigid()
 621{
 622	sShaderLevel = mVertexShaderLevel;
 623	if (sVertexProgram != NULL)
 624	{
 625		sVertexProgram->unbind();
 626	}
 627}
 628
 629void LLDrawPoolAvatar::beginDeferredImpostor()
 630{
 631	if (!LLPipeline::sReflectionRender)
 632	{
 633		LLVOAvatar::sRenderDistance = llclamp(LLVOAvatar::sRenderDistance, 16.f, 256.f);
 634		LLVOAvatar::sNumVisibleAvatars = 0;
 635	}
 636
 637	sVertexProgram = &gDeferredImpostorProgram;
 638
 639	specular_channel = sVertexProgram->enableTexture(LLViewerShaderMgr::SPECULAR_MAP);
 640	normal_channel = sVertexProgram->enableTexture(LLViewerShaderMgr::DEFERRED_NORMAL);
 641	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 642
 643	sVertexProgram->bind();
 644	sVertexProgram->setMinimumAlpha(0.01f);
 645}
 646
 647void LLDrawPoolAvatar::endDeferredImpostor()
 648{
 649	sShaderLevel = mVertexShaderLevel;
 650	sVertexProgram->disableTexture(LLViewerShaderMgr::DEFERRED_NORMAL);
 651	sVertexProgram->disableTexture(LLViewerShaderMgr::SPECULAR_MAP);
 652	sVertexProgram->disableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 653	sVertexProgram->unbind();
 654	gGL.getTexUnit(0)->activate();
 655}
 656
 657void LLDrawPoolAvatar::beginDeferredRigid()
 658{
 659	sVertexProgram = &gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram;
 660	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 661	sVertexProgram->bind();
 662	sVertexProgram->setMinimumAlpha(0.2f);
 663}
 664
 665void LLDrawPoolAvatar::endDeferredRigid()
 666{
 667	sShaderLevel = mVertexShaderLevel;
 668	sVertexProgram->disableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 669	sVertexProgram->unbind();
 670	gGL.getTexUnit(0)->activate();
 671}
 672
 673
 674void LLDrawPoolAvatar::beginSkinned()
 675{
 676	if (sShaderLevel > 0)
 677	{
 678		if (LLPipeline::sUnderWaterRender)
 679		{
 680			sVertexProgram = &gAvatarWaterProgram;
 681			sShaderLevel = llmin((U32) 1, sShaderLevel);
 682		}
 683		else
 684		{
 685			sVertexProgram = &gAvatarProgram;
 686		}
 687	}
 688	else
 689	{
 690		if (LLPipeline::sUnderWaterRender)
 691		{
 692			sVertexProgram = &gObjectAlphaMaskNoColorWaterProgram;
 693		}
 694		else
 695		{
 696			sVertexProgram = &gObjectAlphaMaskNoColorProgram;
 697		}
 698	}
 699	
 700	if (sShaderLevel > 0)  // for hardware blending
 701	{
 702		sRenderingSkinned = TRUE;
 703
 704		sVertexProgram->bind();
 705		sVertexProgram->enableTexture(LLViewerShaderMgr::BUMP_MAP);
 706		gGL.getTexUnit(0)->activate();
 707	}
 708	else
 709	{
 710		if(gPipeline.canUseVertexShaders())
 711		{
 712			// software skinning, use a basic shader for windlight.
 713			// TODO: find a better fallback method for software skinning.
 714			sVertexProgram->bind();
 715		}
 716	}
 717
 718	if (LLGLSLShader::sNoFixedFunction)
 719	{
 720		sVertexProgram->setMinimumAlpha(0.2f);
 721	}
 722}
 723
 724void LLDrawPoolAvatar::endSkinned()
 725{
 726	// if we're in software-blending, remember to set the fence _after_ we draw so we wait till this rendering is done
 727	if (sShaderLevel > 0)
 728	{
 729		sRenderingSkinned = FALSE;
 730		sVertexProgram->disableTexture(LLViewerShaderMgr::BUMP_MAP);
 731		gGL.getTexUnit(0)->activate();
 732		sVertexProgram->unbind();
 733		sShaderLevel = mVertexShaderLevel;
 734	}
 735	else
 736	{
 737		if(gPipeline.canUseVertexShaders())
 738		{
 739			// software skinning, use a basic shader for windlight.
 740			// TODO: find a better fallback method for software skinning.
 741			sVertexProgram->unbind();
 742		}
 743	}
 744
 745	gGL.getTexUnit(0)->activate();
 746}
 747
 748void LLDrawPoolAvatar::beginRiggedSimple()
 749{
 750	if (sShaderLevel > 0)
 751	{
 752		if (LLPipeline::sUnderWaterRender)
 753		{
 754			sVertexProgram = &gSkinnedObjectSimpleWaterProgram;
 755		}
 756		else
 757		{
 758			sVertexProgram = &gSkinnedObjectSimpleProgram;
 759		}
 760	}
 761	else
 762	{
 763		if (LLPipeline::sUnderWaterRender)
 764		{
 765			sVertexProgram = &gObjectSimpleNonIndexedWaterProgram;
 766		}
 767		else
 768		{
 769			sVertexProgram = &gObjectSimpleNonIndexedProgram;
 770		}
 771	}
 772
 773	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 774	{
 775		sDiffuseChannel = 0;
 776		sVertexProgram->bind();
 777	}
 778}
 779
 780void LLDrawPoolAvatar::endRiggedSimple()
 781{
 782	LLVertexBuffer::unbind();
 783	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 784	{
 785		sVertexProgram->unbind();
 786		sVertexProgram = NULL;
 787	}
 788}
 789
 790void LLDrawPoolAvatar::beginRiggedAlpha()
 791{
 792	beginRiggedSimple();
 793}
 794
 795void LLDrawPoolAvatar::endRiggedAlpha()
 796{
 797	endRiggedSimple();
 798}
 799
 800
 801void LLDrawPoolAvatar::beginRiggedFullbrightAlpha()
 802{
 803	beginRiggedFullbright();
 804}
 805
 806void LLDrawPoolAvatar::endRiggedFullbrightAlpha()
 807{
 808	endRiggedFullbright();
 809}
 810
 811void LLDrawPoolAvatar::beginRiggedGlow()
 812{
 813	if (sShaderLevel > 0)
 814	{
 815		if (LLPipeline::sUnderWaterRender)
 816		{
 817			sVertexProgram = &gSkinnedObjectEmissiveWaterProgram;
 818		}
 819		else
 820		{
 821			sVertexProgram = &gSkinnedObjectEmissiveProgram;
 822		}
 823	}
 824	else
 825	{
 826		if (LLPipeline::sUnderWaterRender)
 827		{
 828			sVertexProgram = &gObjectEmissiveNonIndexedWaterProgram;
 829		}
 830		else
 831		{
 832			sVertexProgram = &gObjectEmissiveNonIndexedProgram;
 833		}
 834	}
 835
 836	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 837	{
 838		sDiffuseChannel = 0;
 839		sVertexProgram->bind();
 840	}
 841}
 842
 843void LLDrawPoolAvatar::endRiggedGlow()
 844{
 845	endRiggedFullbright();
 846}
 847
 848void LLDrawPoolAvatar::beginRiggedFullbright()
 849{
 850	if (sShaderLevel > 0)
 851	{
 852		if (LLPipeline::sUnderWaterRender)
 853		{
 854			sVertexProgram = &gSkinnedObjectFullbrightWaterProgram;
 855		}
 856		else
 857		{
 858			sVertexProgram = &gSkinnedObjectFullbrightProgram;
 859		}
 860	}
 861	else
 862	{
 863		if (LLPipeline::sUnderWaterRender)
 864		{
 865			sVertexProgram = &gObjectFullbrightNonIndexedWaterProgram;
 866		}
 867		else
 868		{
 869			sVertexProgram = &gObjectFullbrightNonIndexedProgram;
 870		}
 871	}
 872
 873	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 874	{
 875		sDiffuseChannel = 0;
 876		sVertexProgram->bind();
 877	}
 878}
 879
 880void LLDrawPoolAvatar::endRiggedFullbright()
 881{
 882	LLVertexBuffer::unbind();
 883	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 884	{
 885		sVertexProgram->unbind();
 886		sVertexProgram = NULL;
 887	}
 888}
 889
 890void LLDrawPoolAvatar::beginRiggedShinySimple()
 891{
 892	if (sShaderLevel > 0)
 893	{
 894		if (LLPipeline::sUnderWaterRender)
 895		{
 896			sVertexProgram = &gSkinnedObjectShinySimpleWaterProgram;
 897		}
 898		else
 899		{
 900			sVertexProgram = &gSkinnedObjectShinySimpleProgram;
 901		}
 902	}
 903	else
 904	{
 905		if (LLPipeline::sUnderWaterRender)
 906		{
 907			sVertexProgram = &gObjectShinyNonIndexedWaterProgram;
 908		}
 909		else
 910		{
 911			sVertexProgram = &gObjectShinyNonIndexedProgram;
 912		}
 913	}
 914
 915	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 916	{
 917		sVertexProgram->bind();
 918		LLDrawPoolBump::bindCubeMap(sVertexProgram, 2, sDiffuseChannel, cube_channel, false);
 919	}
 920}
 921
 922void LLDrawPoolAvatar::endRiggedShinySimple()
 923{
 924	LLVertexBuffer::unbind();
 925	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 926	{
 927		LLDrawPoolBump::unbindCubeMap(sVertexProgram, 2, sDiffuseChannel, cube_channel, false);
 928		sVertexProgram->unbind();
 929		sVertexProgram = NULL;
 930	}
 931}
 932
 933void LLDrawPoolAvatar::beginRiggedFullbrightShiny()
 934{
 935	if (sShaderLevel > 0)
 936	{
 937		if (LLPipeline::sUnderWaterRender)
 938		{
 939			sVertexProgram = &gSkinnedObjectFullbrightShinyWaterProgram;
 940		}
 941		else
 942		{
 943			sVertexProgram = &gSkinnedObjectFullbrightShinyProgram;
 944		}
 945	}
 946	else
 947	{
 948		if (LLPipeline::sUnderWaterRender)
 949		{
 950			sVertexProgram = &gObjectFullbrightShinyNonIndexedWaterProgram;
 951		}
 952		else
 953		{
 954			sVertexProgram = &gObjectFullbrightShinyNonIndexedProgram;
 955		}
 956	}
 957
 958
 959	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 960	{
 961		sVertexProgram->bind();
 962		LLDrawPoolBump::bindCubeMap(sVertexProgram, 2, sDiffuseChannel, cube_channel, false);
 963	}
 964}
 965
 966void LLDrawPoolAvatar::endRiggedFullbrightShiny()
 967{
 968	LLVertexBuffer::unbind();
 969	if (sShaderLevel > 0 || gPipeline.canUseVertexShaders())
 970	{
 971		LLDrawPoolBump::unbindCubeMap(sVertexProgram, 2, sDiffuseChannel, cube_channel, false);
 972		sVertexProgram->unbind();
 973		sVertexProgram = NULL;
 974	}
 975}
 976
 977
 978void LLDrawPoolAvatar::beginDeferredRiggedSimple()
 979{
 980	sVertexProgram = &gDeferredSkinnedDiffuseProgram;
 981	sDiffuseChannel = 0;
 982	sVertexProgram->bind();
 983}
 984
 985void LLDrawPoolAvatar::endDeferredRiggedSimple()
 986{
 987	LLVertexBuffer::unbind();
 988	sVertexProgram->unbind();
 989	sVertexProgram = NULL;
 990}
 991
 992void LLDrawPoolAvatar::beginDeferredRiggedBump()
 993{
 994	sVertexProgram = &gDeferredSkinnedBumpProgram;
 995	sVertexProgram->bind();
 996	normal_channel = sVertexProgram->enableTexture(LLViewerShaderMgr::BUMP_MAP);
 997	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 998}
 999
1000void LLDrawPoolAvatar::endDeferredRiggedBump()
1001{
1002	LLVertexBuffer::unbind();
1003	sVertexProgram->disableTexture(LLViewerShaderMgr::BUMP_MAP);
1004	sVertexProgram->disableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
1005	sVertexProgram->unbind();
1006	normal_channel = -1;
1007	sDiffuseChannel = 0;
1008	sVertexProgram = NULL;
1009}
1010
1011void LLDrawPoolAvatar::beginDeferredSkinned()
1012{
1013	sShaderLevel = mVertexShaderLevel;
1014	sVertexProgram = &gDeferredAvatarProgram;
1015	sRenderingSkinned = TRUE;
1016
1017	sVertexProgram->bind();
1018	sVertexProgram->setMinimumAlpha(0.2f);
1019	
1020	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
1021	gGL.getTexUnit(0)->activate();
1022}
1023
1024void LLDrawPoolAvatar::endDeferredSkinned()
1025{
1026	// if we're in software-blending, remember to set the fence _after_ we draw so we wait till this rendering is done
1027	sRenderingSkinned = FALSE;
1028	sVertexProgram->unbind();
1029
1030	sVertexProgram->disableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
1031
1032	sShaderLevel = mVertexShaderLevel;
1033
1034	gGL.getTexUnit(0)->activate();
1035}
1036
1037
1038void LLDrawPoolAvatar::renderAvatars(LLVOAvatar* single_avatar, S32 pass)
1039{
1040	if (pass == -1)
1041	{
1042		for (S32 i = 1; i < getNumPasses(); i++)
1043		{ //skip foot shadows
1044			prerender();
1045			beginRenderPass(i);
1046			renderAvatars(single_avatar, i);
1047			endRenderPass(i);
1048		}
1049
1050		return;
1051	}
1052
1053	if (mDrawFace.empty() && !single_avatar)
1054	{
1055		return;
1056	}
1057
1058	LLVOAvatar *avatarp;
1059
1060	if (single_avatar)
1061	{
1062		avatarp = single_avatar;
1063	}
1064	else
1065	{
1066		const LLFace *facep = mDrawFace[0];
1067		if (!facep->getDrawable())
1068		{
1069			return;
1070		}
1071		avatarp = (LLVOAvatar *)facep->getDrawable()->getVObj().get();
1072	}
1073
1074    if (avatarp->isDead() || avatarp->mDrawable.isNull())
1075	{
1076		return;
1077	}
1078
1079	if (!single_avatar && !avatarp->isFullyLoaded() )
1080	{
1081		if (pass==0 && (!gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_PARTICLES) || LLViewerPartSim::getMaxPartCount() <= 0))
1082		{
1083			// debug code to draw a sphere in place of avatar
1084			gGL.getTexUnit(0)->bind(LLViewerFetchedTexture::sWhiteImagep);
1085			gGL.setColorMask(true, true);
1086			LLVector3 pos = avatarp->getPositionAgent();
1087			gGL.color4f(1.0f, 1.0f, 1.0f, 0.7f);
1088			
1089			gGL.pushMatrix();	 
1090			gGL.translatef((F32)(pos.mV[VX]),	 
1091						   (F32)(pos.mV[VY]),	 
1092							(F32)(pos.mV[VZ]));	 
1093			 gGL.scalef(0.15f, 0.15f, 0.3f);
1094
1095			 gSphere.renderGGL();
1096				 
1097			 gGL.popMatrix();
1098			 gGL.setColorMask(true, false);
1099		}
1100		// don't render please
1101		return;
1102	}
1103
1104	BOOL impostor = avatarp->isImpostor() && !single_avatar;
1105
1106	if (impostor && pass != 0)
1107	{ //don't draw anything but the impostor for impostored avatars
1108		return;
1109	}
1110	
1111	if (pass == 0 && !impostor && LLPipeline::sUnderWaterRender)
1112	{ //don't draw foot shadows under water
1113		return;
1114	}
1115
1116	if (pass == 0)
1117	{
1118		if (!LLPipeline::sReflectionRender)
1119		{
1120			LLVOAvatar::sNumVisibleAvatars++;
1121		}
1122
1123		if (impostor)
1124		{
1125			if (LLPipeline::sRenderDeferred && !LLPipeline::sReflectionRender && avatarp->mImpostor.isComplete()) 
1126			{
1127				if (normal_channel > -1)
1128				{
1129					avatarp->mImpostor.bindTexture(2, normal_channel);
1130				}
1131				if (specular_channel > -1)
1132				{
1133					avatarp->mImpostor.bindTexture(1, specular_channel);
1134				}
1135			}
1136			avatarp->renderImpostor(LLColor4U(255,255,255,255), sDiffuseChannel);
1137		}
1138		return;
1139	}
1140
1141	/*if (single_avatar && avatarp->mSpecialRenderMode >= 1) // 1=anim preview, 2=image preview,  3=morph view
1142	{
1143		gPipeline.enableLightsAvatarEdit(LLColor4(.5f, .5f, .5f, 1.f));
1144	}*/
1145	
1146	if (pass == 1)
1147	{
1148		// render rigid meshes (eyeballs) first
1149		avatarp->renderRigid();
1150		return;
1151	}
1152
1153	if (pass == 3)
1154	{
1155		if (is_deferred_render)
1156		{
1157			renderDeferredRiggedSimple(avatarp);
1158		}
1159		else
1160		{
1161			renderRiggedSimple(avatarp);
1162		}
1163		return;
1164	}
1165
1166	if (pass == 4)
1167	{
1168		if (is_deferred_render)
1169		{
1170			renderDeferredRiggedBump(avatarp);
1171		}
1172		else
1173		{
1174			renderRiggedFullbright(avatarp);
1175		}
1176
1177		return;
1178	}
1179
1180	if (pass == 5)
1181	{
1182		renderRiggedShinySimple(avatarp);
1183		return;
1184	}
1185
1186	if (pass == 6)
1187	{
1188		renderRiggedFullbrightShiny(avatarp);
1189		return;
1190	}
1191
1192	if (pass >= 7 && pass < 9)
1193	{
1194		LLGLEnable blend(GL_BLEND);
1195
1196		gGL.setColorMask(true, true);
1197		gGL.blendFunc(LLRender::BF_SOURCE_ALPHA,
1198					  LLRender::BF_ONE_MINUS_SOURCE_ALPHA,
1199					  LLRender::BF_ZERO,
1200					  LLRender::BF_ONE_MINUS_SOURCE_ALPHA);
1201
1202		
1203		if (pass == 7)
1204		{
1205			renderRiggedAlpha(avatarp);
1206			return;
1207		}
1208
1209		if (pass == 8)
1210		{
1211			renderRiggedFullbrightAlpha(avatarp);
1212			return;
1213		}
1214	}
1215
1216	if (pass == 9)
1217	{
1218		LLGLEnable blend(GL_BLEND);
1219		LLGLDisable test(GL_ALPHA_TEST);
1220		gGL.flush();
1221
1222		LLGLEnable polyOffset(GL_POLYGON_OFFSET_FILL);
1223		glPolygonOffset(-1.0f, -1.0f);
1224		gGL.setSceneBlendType(LLRender::BT_ADD);
1225
1226		LLGLDepthTest depth(GL_TRUE, GL_FALSE);
1227		gGL.setColorMask(false, true);
1228
1229		renderRiggedGlow(avatarp);
1230		gGL.setColorMask(true, false);
1231		gGL.setSceneBlendType(LLRender::BT_ALPHA);
1232		return;
1233	}
1234	
1235	if ((sShaderLevel >= SHADER_LEVEL_CLOTH))
1236	{
1237		LLMatrix4 rot_mat;
1238		LLViewerCamera::getInstance()->getMatrixToLocal(rot_mat);
1239		LLMatrix4 cfr(OGL_TO_CFR_ROTATION);
1240		rot_mat *= cfr;
1241		
1242		LLVector4 wind;
1243		wind.setVec(avatarp->mWindVec);
1244		wind.mV[VW] = 0;
1245		wind = wind * rot_mat;
1246		wind.mV[VW] = avatarp->mWindVec.mV[VW];
1247
1248		sVertexProgram->uniform4fv(LLViewerShaderMgr::AVATAR_WIND, 1, wind.mV);
1249		F32 phase = -1.f * (avatarp->mRipplePhase);
1250
1251		F32 freq = 7.f + (noise1(avatarp->mRipplePhase) * 2.f);
1252		LLVector4 sin_params(freq, freq, freq, phase);
1253		sVertexProgram->uniform4fv(LLViewerShaderMgr::AVATAR_SINWAVE, 1, sin_params.mV);
1254
1255		LLVector4 gravity(0.f, 0.f, -CLOTHING_GRAVITY_EFFECT, 0.f);
1256		gravity = gravity * rot_mat;
1257		sVertexProgram->uniform4fv(LLViewerShaderMgr::AVATAR_GRAVITY, 1, gravity.mV);
1258	}
1259
1260	if( !single_avatar || (avatarp == single_avatar) )
1261	{
1262		avatarp->renderSkinned(AVATAR_RENDER_PASS_SINGLE);
1263	}
1264}
1265
1266void LLDrawPoolAvatar::updateRiggedFaceVertexBuffer(LLVOAvatar* avatar, LLFace* face, const LLMeshSkinInfo* skin, LLVolume* volume, const LLVolumeFace& vol_face)
1267{
1268	LLVector4a* weight = vol_face.mWeights;
1269	if (!weight)
1270	{
1271		return;
1272	}
1273
1274	LLPointer<LLVertexBuffer> buffer = face->getVertexBuffer();
1275	LLDrawable* drawable = face->getDrawable();
1276
1277	U32 data_mask = face->getRiggedVertexBufferDataMask();
1278	
1279	if (buffer.isNull() || 
1280		buffer->getTypeMask() != data_mask ||
1281		buffer->getNumVerts() != vol_face.mNumVertices ||
1282		buffer->getNumIndices() != vol_face.mNumIndices ||
1283		(drawable && drawable->isState(LLDrawable::REBUILD_ALL)))
1284	{
1285		face->setGeomIndex(0);
1286		face->setIndicesIndex(0);
1287		
1288		if (buffer.isNull() || buffer->getTypeMask() != data_mask)
1289		{ //make a new buffer
1290			if (sShaderLevel > 0)
1291			{
1292				buffer = new LLVertexBuffer(data_mask, GL_DYNAMIC_DRAW_ARB);
1293			}
1294			else
1295			{
1296				buffer = new LLVertexBuffer(data_mask, GL_STREAM_DRAW_ARB);
1297			}
1298			buffer->allocateBuffer(vol_face.mNumVertices, vol_face.mNumIndices, true);
1299		}
1300		else
1301		{ //resize existing buffer
1302			buffer->resizeBuffer(vol_face.mNumVertices, vol_face.mNumIndices);
1303		}
1304
1305		face->setSize(vol_face.mNumVertices, vol_face.mNumIndices);
1306		face->setVertexBuffer(buffer);
1307
1308		U16 offset = 0;
1309		
1310		LLMatrix4 mat_vert = skin->mBindShapeMatrix;
1311		glh::matrix4f m((F32*) mat_vert.mMatrix);
1312		m = m.inverse().transpose();
1313		
1314		F32 mat3[] = 
1315		{ m.m[0], m.m[1], m.m[2],
1316		  m.m[4], m.m[5], m.m[6],
1317		  m.m[8], m.m[9], m.m[10] };
1318
1319		LLMatrix3 mat_normal(mat3);				
1320
1321		//let getGeometryVolume know if alpha should override shiny
1322		if (face->getFaceColor().mV[3] < 1.f)
1323		{
1324			face->setPoolType(LLDrawPool::POOL_ALPHA);
1325		}
1326		else
1327		{
1328			face->setPoolType(LLDrawPool::POOL_AVATAR);
1329		}
1330
1331		face->getGeometryVolume(*volume, face->getTEOffset(), mat_vert, mat_normal, offset, true);
1332
1333		buffer->flush();
1334	}
1335
1336	if (sShaderLevel <= 0 && face->mLastSkinTime < avatar->getLastSkinTime())
1337	{ //perform software vertex skinning for this face
1338		LLStrider<LLVector3> position;
1339		LLStrider<LLVector3> normal;
1340
1341		bool has_normal = buffer->hasDataType(LLVertexBuffer::TYPE_NORMAL);
1342		buffer->getVertexStrider(position);
1343
1344		if (has_normal)
1345		{
1346			buffer->getNormalStrider(normal);
1347		}
1348
1349		LLVector4a* pos = (LLVector4a*) position.get();
1350
1351		LLVector4a* norm = has_normal ? (LLVector4a*) normal.get() : NULL;
1352		
1353		//build matrix palette
1354		LLMatrix4a mp[64];
1355		LLMatrix4* mat = (LLMatrix4*) mp;
1356
1357		for (U32 j = 0; j < skin->mJointNames.size(); ++j)
1358		{
1359			LLJoint* joint = avatar->getJoint(skin->mJointNames[j]);
1360			if (joint)
1361			{
1362				mat[j] = skin->mInvBindMatrix[j];
1363				mat[j] *= joint->getWorldMatrix();
1364			}
1365		}
1366
1367		LLMatrix4a bind_shape_matrix;
1368		bind_shape_matrix.loadu(skin->mBindShapeMatrix);
1369
1370		for (U32 j = 0; j < buffer->getNumVerts(); ++j)
1371		{
1372			LLMatrix4a final_mat;
1373			final_mat.clear();
1374
1375			S32 idx[4];
1376
1377			LLVector4 wght;
1378
1379			F32 scale = 0.f;
1380			for (U32 k = 0; k < 4; k++)
1381			{
1382				F32 w = weight[j][k];
1383
1384				idx[k] = llclamp((S32) floorf(w), 0, 63);
1385				wght[k] = w - floorf(w);
1386				scale += wght[k];
1387			}
1388
1389			wght *= 1.f/scale;
1390
1391			for (U32 k = 0; k < 4; k++)
1392			{
1393				F32 w = wght[k];
1394
1395				LLMatrix4a src;
1396				src.setMul(mp[idx[k]], w);
1397
1398				final_mat.add(src);
1399			}
1400
1401			
1402			LLVector4a& v = vol_face.mPositions[j];
1403			LLVector4a t;
1404			LLVector4a dst;
1405			bind_shape_matrix.affineTransform(v, t);
1406			final_mat.affineTransform(t, dst);
1407			pos[j] = dst;
1408
1409			if (norm)
1410			{
1411				LLVector4a& n = vol_face.mNormals[j];
1412				bind_shape_matrix.rotate(n, t);
1413				final_mat.rotate(t, dst);
1414				norm[j] = dst;
1415			}
1416		}
1417	}
1418
1419	if (drawable && (face->getTEOffset() == drawable->getNumFaces()-1))
1420	{
1421		drawable->clearState(LLDrawable::REBUILD_ALL);
1422	}
1423}
1424
1425void LLDrawPoolAvatar::renderRigged(LLVOAvatar* avatar, U32 type, bool glow)
1426{
1427	if (avatar->isSelf() && !gAgent.needsRenderAvatar() || !gMeshRepo.meshRezEnabled())
1428	{
1429		return;
1430	}
1431
1432	stop_glerror();
1433
1434	for (U32 i = 0; i < mRiggedFace[type].size(); ++i)
1435	{
1436		LLFace* face = mRiggedFace[type][i];
1437		LLDrawable* drawable = face->getDrawable();
1438		if (!drawable)
1439		{
1440			continue;
1441		}
1442
1443		LLVOVolume* vobj = drawable->getVOVolume();
1444
1445		if (!vobj)
1446		{
1447			continue;
1448		}
1449
1450		LLVolume* volume = vobj->getVolume();
1451		S32 te = face->getTEOffset();
1452
1453		if (!volume || volume->getNumVolumeFaces() <= te || !volume->isMeshAssetLoaded())
1454		{
1455			continue;
1456		}
1457
1458		LLUUID mesh_id = volume->getParams().getSculptID();
1459		if (mesh_id.isNull())
1460		{
1461			continue;
1462		}
1463
1464		const LLMeshSkinInfo* skin = gMeshRepo.getSkinInfo(mesh_id, vobj);
1465		if (!skin)
1466		{
1467			continue;
1468		}
1469
1470		//stop_glerror();
1471
1472		//const LLVolumeFace& vol_face = volume->getVolumeFace(te);
1473		//updateRiggedFaceVertexBuffer(avatar, face, skin, volume, vol_face);
1474		
1475		//stop_glerror();
1476
1477		U32 data_mask = LLFace::getRiggedDataMask(type);
1478
1479		LLVertexBuffer* buff = face->getVertexBuffer();
1480
1481		if (buff)
1482		{
1483			if (sShaderLevel > 0)
1484			{ //upload matrix palette to shader
1485				LLMatrix4 mat[64];
1486
1487				for (U32 i = 0; i < skin->mJointNames.size(); ++i)
1488				{
1489					LLJoint* joint = avatar->getJoint(skin->mJointNames[i]);
1490					if (joint)
1491					{
1492						mat[i] = skin->mInvBindMatrix[i];
1493						mat[i] *= joint->getWorldMatrix();
1494					}
1495				}
1496				
1497				stop_glerror();
1498
1499				LLDrawPoolAvatar::sVertexProgram->uniformMatrix4fv("matrixPalette", 
1500					skin->mJointNames.size(),
1501					FALSE,
1502					(GLfloat*) mat[0].mMatrix);
1503				
1504				stop_glerror();
1505			}
1506			else
1507			{
1508				data_mask &= ~LLVertexBuffer::MAP_WEIGHT4;
1509			}
1510
1511			U16 start = face->getGeomStart();
1512			U16 end = start + face->getGeomCount()-1;
1513			S32 offset = face->getIndicesStart();
1514			U32 count = face->getIndicesCount();
1515
1516			/*if (glow)
1517			{
1518				gGL.diffuseColor4f(0,0,0,face->getTextureEntry()->getGlow());
1519			}*/
1520
1521			gGL.getTexUnit(sDiffuseChannel)->bind(face->getTexture());
1522			if (normal_channel > -1)
1523			{
1524				LLDrawPoolBump::bindBumpMap(face, normal_channel);
1525			}
1526
1527			if (face->mTextureMatrix)
1528			{
1529				gGL.matrixMode(LLRender::MM_TEXTURE);
1530				gGL.loadMatrix((F32*) face->mTextureMatrix->mMatrix);
1531				buff->setBuffer(data_mask);
1532				buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
1533				gGL.loadIdentity();
1534				gGL.matrixMode(LLRender::MM_MODELVIEW);
1535			}
1536			else
1537			{
1538				buff->setBuffer(data_mask);
1539				buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);		
1540			}
1541		}
1542	}
1543}
1544
1545void LLDrawPoolAvatar::renderDeferredRiggedSimple(LLVOAvatar* avatar)
1546{
1547	updateRiggedVertexBuffers(avatar);
1548	renderRigged(avatar, RIGGED_DEFERRED_SIMPLE);
1549}
1550
1551void LLDrawPoolAvatar::renderDeferredRiggedBump(LLVOAvatar* avatar)
1552{
1553	renderRigged(avatar, RIGGED_DEFERRED_BUMP);
1554}
1555
1556void LLDrawPoolAvatar::updateRiggedVertexBuffers(LLVOAvatar* avatar)
1557{
1558	//update rigged vertex buffers
1559	for (U32 type = 0; type < NUM_RIGGED_PASSES; ++type)
1560	{
1561		for (U32 i = 0; i < mRiggedFace[type].size(); ++i)
1562		{
1563			LLFace* face = mRiggedFace[type][i];
1564			LLDrawable* drawable = face->getDrawable();
1565			if (!drawable)
1566			{
1567				continue;
1568			}
1569
1570			LLVOVolume* vobj = drawable->getVOVolume();
1571
1572			if (!vobj)
1573			{
1574				continue;
1575			}
1576
1577			LLVolume* volume = vobj->getVolume();
1578			S32 te = face->getTEOffset();
1579
1580			if (!volume || volume->getNumVolumeFaces() <= te)
1581			{
1582				continue;
1583			}
1584
1585			LLUUID mesh_id = volume->getParams().getSculptID();
1586			if (mesh_id.isNull())
1587			{
1588				continue;
1589			}
1590
1591			const LLMeshSkinInfo* skin = gMeshRepo.getSkinInfo(mesh_id, vobj);
1592			if (!skin)
1593			{
1594				continue;
1595			}
1596
1597			stop_glerror();
1598
1599			const LLVolumeFace& vol_face = volume->getVolumeFace(te);
1600			updateRiggedFaceVertexBuffer(avatar, face, skin, volume, vol_face);
1601		}
1602	}
1603}
1604
1605void LLDrawPoolAvatar::renderRiggedSimple(LLVOAvatar* avatar)
1606{
1607	updateRiggedVertexBuffers(avatar);
1608	renderRigged(avatar, RIGGED_SIMPLE);
1609}
1610
1611void LLDrawPoolAvatar::renderRiggedFullbright(LLVOAvatar* avatar)
1612{
1613	renderRigged(avatar, RIGGED_FULLBRIGHT);
1614}
1615
1616	
1617void LLDrawPoolAvatar::renderRiggedShinySimple(LLVOAvatar* avatar)
1618{
1619	renderRigged(avatar, RIGGED_SHINY);
1620}
1621
1622void LLDrawPoolAvatar::renderRiggedFullbrightShiny(LLVOAvatar* avatar)
1623{
1624	renderRigged(avatar, RIGGED_FULLBRIGHT_SHINY);
1625}
1626
1627void LLDrawPoolAvatar::renderRiggedAlpha(LLVOAvatar* avatar)
1628{
1629	renderRigged(avatar, RIGGED_ALPHA);
1630}
1631
1632void LLDrawPoolAvatar::renderRiggedFullbrightAlpha(LLVOAvatar* avatar)
1633{
1634	renderRigged(avatar, RIGGED_FULLBRIGHT_ALPHA);
1635}
1636
1637void LLDrawPoolAvatar::renderRiggedGlow(LLVOAvatar* avatar)
1638{
1639	renderRigged(avatar, RIGGED_GLOW, true);
1640}
1641
1642
1643
1644//-----------------------------------------------------------------------------
1645// getDebugTexture()
1646//-----------------------------------------------------------------------------
1647LLViewerTexture *LLDrawPoolAvatar::getDebugTexture()
1648{
1649	if (mReferences.empty())
1650	{
1651		return NULL;
1652	}
1653	LLFace *face = mReferences[0];
1654	if (!face->getDrawable())
1655	{
1656		return NULL;
1657	}
1658	const LLViewerObject *objectp = face->getDrawable()->getVObj();
1659
1660	// Avatar should always have at least 1 (maybe 3?) TE's.
1661	return objectp->getTEImage(0);
1662}
1663
1664
1665LLColor3 LLDrawPoolAvatar::getDebugColor() const
1666{
1667	return LLColor3(0.f, 1.f, 0.f);
1668}
1669
1670void LLDrawPoolAvatar::addRiggedFace(LLFace* facep, U32 type)
1671{
1672	if (type >= NUM_RIGGED_PASSES)
1673	{
1674		llerrs << "Invalid rigged face type." << llendl;
1675	}
1676
1677	if (facep->getRiggedIndex(type) != -1)
1678	{
1679		llerrs << "Tried to add a rigged face that's referenced elsewhere." << llendl;
1680	}	
1681	
1682	facep->setRiggedIndex(type, mRiggedFace[type].size());
1683	facep->setPool(this);
1684	mRiggedFace[type].push_back(facep);
1685}
1686
1687void LLDrawPoolAvatar::removeRiggedFace(LLFace* facep)
1688{
1689	facep->setPool(NULL);
1690
1691	for (U32 i = 0; i < NUM_RIGGED_PASSES; ++i)
1692	{
1693		S32 index = facep->getRiggedIndex(i);
1694		
1695		if (index > -1)
1696		{
1697			if (mRiggedFace[i].size() > index && mRiggedFace[i][index] == facep)
1698			{
1699				facep->setRiggedIndex(i,-1);
1700				mRiggedFace[i].erase(mRiggedFace[i].begin()+index);
1701				for (U32 j = index; j < mRiggedFace[i].size(); ++j)
1702				{ //bump indexes down for faces referenced after erased face
1703					mRiggedFace[i][j]->setRiggedIndex(i, j);
1704				}
1705			}
1706			else
1707			{
1708				llerrs << "Face reference data corrupt for rigged type " << i << llendl;
1709			}
1710		}
1711	}
1712}
1713
1714LLVertexBufferAvatar::LLVertexBufferAvatar()
1715: LLVertexBuffer(sDataMask, 
1716	GL_STREAM_DRAW_ARB) //avatars are always stream draw due to morph targets
1717{
1718
1719}
1720
1721