PageRenderTime 38ms CodeModel.GetById 2ms app.highlight 30ms RepoModel.GetById 3ms app.codeStats 0ms

/indra/newview/llviewerjoint.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 619 lines | 280 code | 107 blank | 232 comment | 40 complexity | 255b6f6b2f1918b3075c474bdc641e07 MD5 | raw file
  1/** 
  2 * @file llviewerjoint.cpp
  3 * @brief Implementation of LLViewerJoint 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 "llviewerjoint.h"
 33
 34#include "llgl.h"
 35#include "llrender.h"
 36#include "llmath.h"
 37#include "llglheaders.h"
 38#include "llrendersphere.h"
 39#include "llvoavatar.h"
 40#include "pipeline.h"
 41
 42#define DEFAULT_LOD 0.0f
 43
 44const S32 MIN_PIXEL_AREA_3PASS_HAIR = 64*64;
 45
 46//-----------------------------------------------------------------------------
 47// Static Data
 48//-----------------------------------------------------------------------------
 49BOOL					LLViewerJoint::sDisableLOD = FALSE;
 50
 51//-----------------------------------------------------------------------------
 52// LLViewerJoint()
 53// Class Constructor
 54//-----------------------------------------------------------------------------
 55LLViewerJoint::LLViewerJoint()
 56	:       LLJoint()
 57{
 58	init();
 59}
 60
 61
 62//-----------------------------------------------------------------------------
 63// LLViewerJoint()
 64// Class Constructor
 65//-----------------------------------------------------------------------------
 66LLViewerJoint::LLViewerJoint(const std::string &name, LLJoint *parent)
 67	:	LLJoint(name, parent)
 68{
 69	init();
 70}
 71
 72
 73void LLViewerJoint::init()
 74{
 75	mValid = FALSE;
 76	mComponents = SC_JOINT | SC_BONE | SC_AXES;
 77	mMinPixelArea = DEFAULT_LOD;
 78	mPickName = PN_DEFAULT;
 79	mVisible = TRUE;
 80	mMeshID = 0;
 81}
 82
 83
 84//-----------------------------------------------------------------------------
 85// ~LLViewerJoint()
 86// Class Destructor
 87//-----------------------------------------------------------------------------
 88LLViewerJoint::~LLViewerJoint()
 89{
 90}
 91
 92
 93//--------------------------------------------------------------------
 94// setValid()
 95//--------------------------------------------------------------------
 96void LLViewerJoint::setValid( BOOL valid, BOOL recursive )
 97{
 98	//----------------------------------------------------------------
 99	// set visibility for this joint
100	//----------------------------------------------------------------
101	mValid = valid;
102	
103	//----------------------------------------------------------------
104	// set visibility for children
105	//----------------------------------------------------------------
106	if (recursive)
107	{
108		for (child_list_t::iterator iter = mChildren.begin();
109			 iter != mChildren.end(); ++iter)
110		{
111			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
112			joint->setValid(valid, TRUE);
113		}
114	}
115
116}
117
118//--------------------------------------------------------------------
119// renderSkeleton()
120// DEBUG (UNUSED)
121//--------------------------------------------------------------------
122// void LLViewerJoint::renderSkeleton(BOOL recursive)
123// {
124// 	F32 nc = 0.57735f;
125
126// 	//----------------------------------------------------------------
127// 	// push matrix stack
128// 	//----------------------------------------------------------------
129// 	gGL.pushMatrix();
130
131// 	//----------------------------------------------------------------
132// 	// render the bone to my parent
133// 	//----------------------------------------------------------------
134// 	if (mComponents & SC_BONE)
135// 	{
136// 		drawBone();
137// 	}
138
139// 	//----------------------------------------------------------------
140// 	// offset to joint position and 
141// 	// rotate to our orientation
142// 	//----------------------------------------------------------------
143// 	gGL.loadIdentity();
144// 	gGL.multMatrix( &getWorldMatrix().mMatrix[0][0] );
145
146// 	//----------------------------------------------------------------
147// 	// render joint axes
148// 	//----------------------------------------------------------------
149// 	if (mComponents & SC_AXES)
150// 	{
151// 		gGL.begin(LLRender::LINES);
152// 		gGL.color3f( 1.0f, 0.0f, 0.0f );
153// 		gGL.vertex3f( 0.0f,            0.0f, 0.0f );
154// 		gGL.vertex3f( 0.1f, 0.0f, 0.0f );
155
156// 		gGL.color3f( 0.0f, 1.0f, 0.0f );
157// 		gGL.vertex3f( 0.0f, 0.0f,            0.0f );
158// 		gGL.vertex3f( 0.0f, 0.1f, 0.0f );
159
160// 		gGL.color3f( 0.0f, 0.0f, 1.0f );
161// 		gGL.vertex3f( 0.0f, 0.0f, 0.0f );
162// 		gGL.vertex3f( 0.0f, 0.0f, 0.1f );
163// 		gGL.end();
164// 	}
165
166// 	//----------------------------------------------------------------
167// 	// render the joint graphic
168// 	//----------------------------------------------------------------
169// 	if (mComponents & SC_JOINT)
170// 	{
171// 		gGL.color3f( 1.0f, 1.0f, 0.0f );
172
173// 		gGL.begin(LLRender::TRIANGLES);
174
175// 		// joint top half
176// 		glNormal3f(nc, nc, nc);
177// 		gGL.vertex3f(0.0f,             0.0f, 0.05f);
178// 		gGL.vertex3f(0.05f,       0.0f,       0.0f);
179// 		gGL.vertex3f(0.0f,       0.05f,       0.0f);
180
181// 		glNormal3f(-nc, nc, nc);
182// 		gGL.vertex3f(0.0f,             0.0f, 0.05f);
183// 		gGL.vertex3f(0.0f,       0.05f,       0.0f);
184// 		gGL.vertex3f(-0.05f,      0.0f,       0.0f);
185		
186// 		glNormal3f(-nc, -nc, nc);
187// 		gGL.vertex3f(0.0f,             0.0f, 0.05f);
188// 		gGL.vertex3f(-0.05f,      0.0f,      0.0f);
189// 		gGL.vertex3f(0.0f,      -0.05f,      0.0f);
190
191// 		glNormal3f(nc, -nc, nc);
192// 		gGL.vertex3f(0.0f,              0.0f, 0.05f);
193// 		gGL.vertex3f(0.0f,       -0.05f,       0.0f);
194// 		gGL.vertex3f(0.05f,        0.0f,       0.0f);
195		
196// 		// joint bottom half
197// 		glNormal3f(nc, nc, -nc);
198// 		gGL.vertex3f(0.0f,             0.0f, -0.05f);
199// 		gGL.vertex3f(0.0f,       0.05f,        0.0f);
200// 		gGL.vertex3f(0.05f,       0.0f,        0.0f);
201
202// 		glNormal3f(-nc, nc, -nc);
203// 		gGL.vertex3f(0.0f,             0.0f, -0.05f);
204// 		gGL.vertex3f(-0.05f,      0.0f,        0.0f);
205// 		gGL.vertex3f(0.0f,       0.05f,        0.0f);
206		
207// 		glNormal3f(-nc, -nc, -nc);
208// 		gGL.vertex3f(0.0f,              0.0f, -0.05f);
209// 		gGL.vertex3f(0.0f,       -0.05f,        0.0f);
210// 		gGL.vertex3f(-0.05f,       0.0f,        0.0f);
211
212// 		glNormal3f(nc, -nc, -nc);
213// 		gGL.vertex3f(0.0f,             0.0f,  -0.05f);
214// 		gGL.vertex3f(0.05f,       0.0f,         0.0f);
215// 		gGL.vertex3f(0.0f,      -0.05f,         0.0f);
216		
217// 		gGL.end();
218// 	}
219
220// 	//----------------------------------------------------------------
221// 	// render children
222// 	//----------------------------------------------------------------
223// 	if (recursive)
224// 	{
225// 		for (child_list_t::iterator iter = mChildren.begin();
226// 			 iter != mChildren.end(); ++iter)
227// 		{
228// 			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
229// 			joint->renderSkeleton();
230// 		}
231// 	}
232
233// 	//----------------------------------------------------------------
234// 	// pop matrix stack
235// 	//----------------------------------------------------------------
236// 	gGL.popMatrix();
237// }
238
239
240//--------------------------------------------------------------------
241// render()
242//--------------------------------------------------------------------
243U32 LLViewerJoint::render( F32 pixelArea, BOOL first_pass, BOOL is_dummy )
244{
245	stop_glerror();
246
247	U32 triangle_count = 0;
248
249	//----------------------------------------------------------------
250	// ignore invisible objects
251	//----------------------------------------------------------------
252	if ( mValid )
253	{
254
255
256		//----------------------------------------------------------------
257		// if object is transparent, defer it, otherwise
258		// give the joint subclass a chance to draw itself
259		//----------------------------------------------------------------
260		if ( is_dummy )
261		{
262			triangle_count += drawShape( pixelArea, first_pass, is_dummy );
263		}
264		else if (LLPipeline::sShadowRender)
265		{
266			triangle_count += drawShape(pixelArea, first_pass, is_dummy );
267		}
268		else if ( isTransparent() && !LLPipeline::sReflectionRender)
269		{
270			// Hair and Skirt
271			if ((pixelArea > MIN_PIXEL_AREA_3PASS_HAIR))
272			{
273				// render all three passes
274				LLGLDisable cull(GL_CULL_FACE);
275				// first pass renders without writing to the z buffer
276				{
277					LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
278					triangle_count += drawShape( pixelArea, first_pass, is_dummy );
279				}
280				// second pass writes to z buffer only
281				gGL.setColorMask(false, false);
282				{
283					triangle_count += drawShape( pixelArea, FALSE, is_dummy  );
284				}
285				// third past respects z buffer and writes color
286				gGL.setColorMask(true, false);
287				{
288					LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
289					triangle_count += drawShape( pixelArea, FALSE, is_dummy  );
290				}
291			}
292			else
293			{
294				// Render Inside (no Z buffer write)
295				glCullFace(GL_FRONT);
296				{
297					LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
298					triangle_count += drawShape( pixelArea, first_pass, is_dummy  );
299				}
300				// Render Outside (write to the Z buffer)
301				glCullFace(GL_BACK);
302				{
303					triangle_count += drawShape( pixelArea, FALSE, is_dummy  );
304				}
305			}
306		}
307		else
308		{
309			// set up render state
310			triangle_count += drawShape( pixelArea, first_pass );
311		}
312	}
313
314	//----------------------------------------------------------------
315	// render children
316	//----------------------------------------------------------------
317	for (child_list_t::iterator iter = mChildren.begin();
318		 iter != mChildren.end(); ++iter)
319	{
320		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
321		F32 jointLOD = joint->getLOD();
322		if (pixelArea >= jointLOD || sDisableLOD)
323		{
324			triangle_count += joint->render( pixelArea, TRUE, is_dummy );
325
326			if (jointLOD != DEFAULT_LOD)
327			{
328				break;
329			}
330		}
331	}
332
333	return triangle_count;
334}
335
336
337//--------------------------------------------------------------------
338// drawBone()
339// DEBUG (UNUSED)
340//--------------------------------------------------------------------
341// void LLViewerJoint::drawBone()
342// {
343// 	if ( mParent == NULL )
344// 		return;
345
346// 	F32 boneSize = 0.02f;
347
348// 	// rotate to point to child (bone direction)
349// 	gGL.pushMatrix();
350
351// 	LLVector3 boneX = getPosition();
352// 	F32 length = boneX.normVec();
353
354// 	LLVector3 boneZ(1.0f, 0.0f, 1.0f);
355	
356// 	LLVector3 boneY = boneZ % boneX;
357// 	boneY.normVec();
358
359// 	boneZ = boneX % boneY;
360
361// 	LLMatrix4 rotateMat;
362// 	rotateMat.setFwdRow( boneX );
363// 	rotateMat.setLeftRow( boneY );
364// 	rotateMat.setUpRow( boneZ );
365// 	gGL.multMatrix( &rotateMat.mMatrix[0][0] );
366
367// 	// render the bone
368// 	gGL.color3f( 0.5f, 0.5f, 0.0f );
369
370// 	gGL.begin(LLRender::TRIANGLES);
371
372// 	gGL.vertex3f( length,     0.0f,       0.0f);
373// 	gGL.vertex3f( 0.0f,       boneSize,  0.0f);
374// 	gGL.vertex3f( 0.0f,       0.0f,       boneSize);
375
376// 	gGL.vertex3f( length,     0.0f,        0.0f);
377// 	gGL.vertex3f( 0.0f,       0.0f,        -boneSize);
378// 	gGL.vertex3f( 0.0f,       boneSize,   0.0f);
379
380// 	gGL.vertex3f( length,     0.0f,        0.0f);
381// 	gGL.vertex3f( 0.0f,       -boneSize,  0.0f);
382// 	gGL.vertex3f( 0.0f,       0.0f,        -boneSize);
383
384// 	gGL.vertex3f( length,     0.0f,        0.0f);
385// 	gGL.vertex3f( 0.0f,       0.0f,        boneSize);
386// 	gGL.vertex3f( 0.0f,       -boneSize,  0.0f);
387
388// 	gGL.end();
389
390// 	// restore matrix
391// 	gGL.popMatrix();
392// }
393
394//--------------------------------------------------------------------
395// isTransparent()
396//--------------------------------------------------------------------
397BOOL LLViewerJoint::isTransparent()
398{
399	return FALSE;
400}
401
402//--------------------------------------------------------------------
403// drawShape()
404//--------------------------------------------------------------------
405U32 LLViewerJoint::drawShape( F32 pixelArea, BOOL first_pass, BOOL is_dummy )
406{
407	return 0;
408}
409
410//--------------------------------------------------------------------
411// setSkeletonComponents()
412//--------------------------------------------------------------------
413void LLViewerJoint::setSkeletonComponents( U32 comp, BOOL recursive )
414{
415	mComponents = comp;
416	if (recursive)
417	{
418		for (child_list_t::iterator iter = mChildren.begin();
419			 iter != mChildren.end(); ++iter)
420		{
421			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
422			joint->setSkeletonComponents(comp, recursive);
423		}
424	}
425}
426
427void LLViewerJoint::updateFaceSizes(U32 &num_vertices, U32& num_indices, F32 pixel_area)
428{
429	for (child_list_t::iterator iter = mChildren.begin();
430		 iter != mChildren.end(); ++iter)
431	{
432		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
433		joint->updateFaceSizes(num_vertices, num_indices, pixel_area);
434	}
435}
436
437void LLViewerJoint::updateFaceData(LLFace *face, F32 pixel_area, BOOL damp_wind, bool terse_update)
438{
439	for (child_list_t::iterator iter = mChildren.begin();
440		 iter != mChildren.end(); ++iter)
441	{
442		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
443		joint->updateFaceData(face, pixel_area, damp_wind, terse_update);
444	}
445}
446
447void LLViewerJoint::updateJointGeometry()
448{
449	for (child_list_t::iterator iter = mChildren.begin();
450		 iter != mChildren.end(); ++iter)
451	{
452		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
453		joint->updateJointGeometry();
454	}
455}
456
457
458BOOL LLViewerJoint::updateLOD(F32 pixel_area, BOOL activate)
459{
460	BOOL lod_changed = FALSE;
461	BOOL found_lod = FALSE;
462
463	for (child_list_t::iterator iter = mChildren.begin();
464		 iter != mChildren.end(); ++iter)
465	{
466		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
467		F32 jointLOD = joint->getLOD();
468		
469		if (found_lod || jointLOD == DEFAULT_LOD)
470		{
471			// we've already found a joint to enable, so enable the rest as alternatives
472			lod_changed |= joint->updateLOD(pixel_area, TRUE);
473		}
474		else
475		{
476			if (pixel_area >= jointLOD || sDisableLOD)
477			{
478				lod_changed |= joint->updateLOD(pixel_area, TRUE);
479				found_lod = TRUE;
480			}
481			else
482			{
483				lod_changed |= joint->updateLOD(pixel_area, FALSE);
484			}
485		}
486	}
487	return lod_changed;
488}
489
490void LLViewerJoint::dump()
491{
492	for (child_list_t::iterator iter = mChildren.begin();
493		 iter != mChildren.end(); ++iter)
494	{
495		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
496		joint->dump();
497	}
498}
499
500void LLViewerJoint::setVisible(BOOL visible, BOOL recursive)
501{
502	mVisible = visible;
503
504	if (recursive)
505	{
506		for (child_list_t::iterator iter = mChildren.begin();
507			 iter != mChildren.end(); ++iter)
508		{
509			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
510			joint->setVisible(visible, recursive);
511		}
512	}
513}
514
515
516void LLViewerJoint::setMeshesToChildren()
517{
518	removeAllChildren();
519	for (std::vector<LLViewerJointMesh*>::iterator iter = mMeshParts.begin();
520		iter != mMeshParts.end(); iter++)
521	{
522		addChild((LLViewerJointMesh *) *iter);
523	}
524}
525//-----------------------------------------------------------------------------
526// LLViewerJointCollisionVolume()
527//-----------------------------------------------------------------------------
528
529LLViewerJointCollisionVolume::LLViewerJointCollisionVolume()
530{
531	mUpdateXform = FALSE;
532}
533
534LLViewerJointCollisionVolume::LLViewerJointCollisionVolume(const std::string &name, LLJoint *parent) : LLViewerJoint(name, parent)
535{
536	
537}
538
539void LLViewerJointCollisionVolume::renderCollision()
540{
541	updateWorldMatrix();
542	
543	gGL.pushMatrix();
544	gGL.multMatrix( &mXform.getWorldMatrix().mMatrix[0][0] );
545
546	gGL.diffuseColor3f( 0.f, 0.f, 1.f );
547	
548	gGL.begin(LLRender::LINES);
549	
550	LLVector3 v[] = 
551	{
552		LLVector3(1,0,0),
553		LLVector3(-1,0,0),
554		LLVector3(0,1,0),
555		LLVector3(0,-1,0),
556
557		LLVector3(0,0,-1),
558		LLVector3(0,0,1),
559	};
560
561	//sides
562	gGL.vertex3fv(v[0].mV); 
563	gGL.vertex3fv(v[2].mV);
564
565	gGL.vertex3fv(v[0].mV); 
566	gGL.vertex3fv(v[3].mV);
567
568	gGL.vertex3fv(v[1].mV); 
569	gGL.vertex3fv(v[2].mV);
570
571	gGL.vertex3fv(v[1].mV); 
572	gGL.vertex3fv(v[3].mV);
573
574
575	//top
576	gGL.vertex3fv(v[0].mV); 
577	gGL.vertex3fv(v[4].mV);
578
579	gGL.vertex3fv(v[1].mV); 
580	gGL.vertex3fv(v[4].mV);
581
582	gGL.vertex3fv(v[2].mV); 
583	gGL.vertex3fv(v[4].mV);
584
585	gGL.vertex3fv(v[3].mV); 
586	gGL.vertex3fv(v[4].mV);
587
588
589	//bottom
590	gGL.vertex3fv(v[0].mV); 
591	gGL.vertex3fv(v[5].mV);
592
593	gGL.vertex3fv(v[1].mV); 
594	gGL.vertex3fv(v[5].mV);
595
596	gGL.vertex3fv(v[2].mV); 
597	gGL.vertex3fv(v[5].mV);
598
599	gGL.vertex3fv(v[3].mV); 
600	gGL.vertex3fv(v[5].mV);
601
602	gGL.end();
603
604	gGL.popMatrix();
605}
606
607LLVector3 LLViewerJointCollisionVolume::getVolumePos(LLVector3 &offset)
608{
609	mUpdateXform = TRUE;
610	
611	LLVector3 result = offset;
612	result.scaleVec(getScale());
613	result.rotVec(getWorldRotation());
614	result += getWorldPosition();
615
616	return result;
617}
618
619// End