summaryrefslogtreecommitdiff
path: root/indra/newview/llviewerjoint.cpp
blob: b1cad86a610b0db4b7f0a2922796c37742af0909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/** 
 * @file llviewerjoint.cpp
 * @brief Implementation of LLViewerJoint class
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

//-----------------------------------------------------------------------------
// Header Files
//-----------------------------------------------------------------------------
#include "llviewerprecompiledheaders.h"

#include "llviewerjoint.h"

#include "llgl.h"
#include "llglimmediate.h"
#include "llmath.h"
#include "llglheaders.h"
#include "llrendersphere.h"
#include "llvoavatar.h"
#include "pipeline.h"

#define DEFAULT_LOD 0.0f

const S32 MIN_PIXEL_AREA_3PASS_HAIR = 64*64;

//-----------------------------------------------------------------------------
// Static Data
//-----------------------------------------------------------------------------
BOOL					LLViewerJoint::sDisableLOD = FALSE;

//-----------------------------------------------------------------------------
// LLViewerJoint()
// Class Constructor
//-----------------------------------------------------------------------------
LLViewerJoint::LLViewerJoint()
{
	mUpdateXform = TRUE;
	mValid = FALSE;
	mComponents = SC_JOINT | SC_BONE | SC_AXES;
	mMinPixelArea = DEFAULT_LOD;
	mPickName = PN_DEFAULT;
	mVisible = TRUE;
}


//-----------------------------------------------------------------------------
// LLViewerJoint()
// Class Constructor
//-----------------------------------------------------------------------------
LLViewerJoint::LLViewerJoint(const std::string &name, LLJoint *parent) :
	LLJoint(name, parent)
{
	mValid = FALSE;
	mComponents = SC_JOINT | SC_BONE | SC_AXES;
	mMinPixelArea = DEFAULT_LOD;
	mPickName = PN_DEFAULT;
}


//-----------------------------------------------------------------------------
// ~LLViewerJoint()
// Class Destructor
//-----------------------------------------------------------------------------
LLViewerJoint::~LLViewerJoint()
{
}


//--------------------------------------------------------------------
// setValid()
//--------------------------------------------------------------------
void LLViewerJoint::setValid( BOOL valid, BOOL recursive )
{
	//----------------------------------------------------------------
	// set visibility for this joint
	//----------------------------------------------------------------
	mValid = valid;
	
	//----------------------------------------------------------------
	// set visibility for children
	//----------------------------------------------------------------
	if (recursive)
	{
		for (child_list_t::iterator iter = mChildren.begin();
			 iter != mChildren.end(); ++iter)
		{
			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
			joint->setValid(valid, TRUE);
		}
	}

}

//--------------------------------------------------------------------
// renderSkeleton()
//--------------------------------------------------------------------
void LLViewerJoint::renderSkeleton(BOOL recursive)
{
	F32 nc = 0.57735f;

	//----------------------------------------------------------------
	// push matrix stack
	//----------------------------------------------------------------
	glPushMatrix();

	//----------------------------------------------------------------
	// render the bone to my parent
	//----------------------------------------------------------------
	if (mComponents & SC_BONE)
	{
		drawBone();
	}

	//----------------------------------------------------------------
	// offset to joint position and 
	// rotate to our orientation
	//----------------------------------------------------------------
	glLoadIdentity();
	glMultMatrixf( &getWorldMatrix().mMatrix[0][0] );

	//----------------------------------------------------------------
	// render joint axes
	//----------------------------------------------------------------
	if (mComponents & SC_AXES)
	{
		gGL.begin(GL_LINES);
		gGL.color3f( 1.0f, 0.0f, 0.0f );
		gGL.vertex3f( 0.0f,            0.0f, 0.0f );
		gGL.vertex3f( 0.1f, 0.0f, 0.0f );

		gGL.color3f( 0.0f, 1.0f, 0.0f );
		gGL.vertex3f( 0.0f, 0.0f,            0.0f );
		gGL.vertex3f( 0.0f, 0.1f, 0.0f );

		gGL.color3f( 0.0f, 0.0f, 1.0f );
		gGL.vertex3f( 0.0f, 0.0f, 0.0f );
		gGL.vertex3f( 0.0f, 0.0f, 0.1f );
		gGL.end();
	}

	//----------------------------------------------------------------
	// render the joint graphic
	//----------------------------------------------------------------
	if (mComponents & SC_JOINT)
	{
		gGL.color3f( 1.0f, 1.0f, 0.0f );

		gGL.begin(GL_TRIANGLES);

		// joint top half
		glNormal3f(nc, nc, nc);
		gGL.vertex3f(0.0f,             0.0f, 0.05f);
		gGL.vertex3f(0.05f,       0.0f,       0.0f);
		gGL.vertex3f(0.0f,       0.05f,       0.0f);

		glNormal3f(-nc, nc, nc);
		gGL.vertex3f(0.0f,             0.0f, 0.05f);
		gGL.vertex3f(0.0f,       0.05f,       0.0f);
		gGL.vertex3f(-0.05f,      0.0f,       0.0f);
		
		glNormal3f(-nc, -nc, nc);
		gGL.vertex3f(0.0f,             0.0f, 0.05f);
		gGL.vertex3f(-0.05f,      0.0f,      0.0f);
		gGL.vertex3f(0.0f,      -0.05f,      0.0f);

		glNormal3f(nc, -nc, nc);
		gGL.vertex3f(0.0f,              0.0f, 0.05f);
		gGL.vertex3f(0.0f,       -0.05f,       0.0f);
		gGL.vertex3f(0.05f,        0.0f,       0.0f);
		
		// joint bottom half
		glNormal3f(nc, nc, -nc);
		gGL.vertex3f(0.0f,             0.0f, -0.05f);
		gGL.vertex3f(0.0f,       0.05f,        0.0f);
		gGL.vertex3f(0.05f,       0.0f,        0.0f);

		glNormal3f(-nc, nc, -nc);
		gGL.vertex3f(0.0f,             0.0f, -0.05f);
		gGL.vertex3f(-0.05f,      0.0f,        0.0f);
		gGL.vertex3f(0.0f,       0.05f,        0.0f);
		
		glNormal3f(-nc, -nc, -nc);
		gGL.vertex3f(0.0f,              0.0f, -0.05f);
		gGL.vertex3f(0.0f,       -0.05f,        0.0f);
		gGL.vertex3f(-0.05f,       0.0f,        0.0f);

		glNormal3f(nc, -nc, -nc);
		gGL.vertex3f(0.0f,             0.0f,  -0.05f);
		gGL.vertex3f(0.05f,       0.0f,         0.0f);
		gGL.vertex3f(0.0f,      -0.05f,         0.0f);
		
		gGL.end();
	}

	//----------------------------------------------------------------
	// render children
	//----------------------------------------------------------------
	if (recursive)
	{
		for (child_list_t::iterator iter = mChildren.begin();
			 iter != mChildren.end(); ++iter)
		{
			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
			joint->renderSkeleton();
		}
	}

	//----------------------------------------------------------------
	// pop matrix stack
	//----------------------------------------------------------------
	glPopMatrix();
}


//--------------------------------------------------------------------
// render()
//--------------------------------------------------------------------
U32 LLViewerJoint::render( F32 pixelArea, BOOL first_pass )
{
	U32 triangle_count = 0;

	//----------------------------------------------------------------
	// ignore invisible objects
	//----------------------------------------------------------------
	if ( mValid )
	{


		//----------------------------------------------------------------
		// if object is transparent, defer it, otherwise
		// give the joint subclass a chance to draw itself
		//----------------------------------------------------------------
		if ( gRenderForSelect )
		{
			triangle_count += drawShape( pixelArea, first_pass );
		}
		else if ( isTransparent() && !LLPipeline::sReflectionRender)
		{
			// Hair and Skirt
			if ((pixelArea > MIN_PIXEL_AREA_3PASS_HAIR))
			{
				// render all three passes
				LLGLDisable cull(GL_CULL_FACE);
				// first pass renders without writing to the z buffer
				{
					LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
					triangle_count += drawShape( pixelArea, first_pass);
				}
				// second pass writes to z buffer only
				glColorMask(FALSE, FALSE, FALSE, FALSE);
				{
					triangle_count += drawShape( pixelArea, FALSE );
				}
				// third past respects z buffer and writes color
				glColorMask(TRUE, TRUE, TRUE, FALSE);
				{
					LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
					triangle_count += drawShape( pixelArea, FALSE );
				}
			}
			else
			{
				// Render Inside (no Z buffer write)
				glCullFace(GL_FRONT);
				{
					LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
					triangle_count += drawShape( pixelArea, first_pass );
				}
				// Render Outside (write to the Z buffer)
				glCullFace(GL_BACK);
				{
					triangle_count += drawShape( pixelArea, FALSE );
				}
			}
		}
		else
		{
			// set up render state
			triangle_count += drawShape( pixelArea, first_pass );
		}
	}

	//----------------------------------------------------------------
	// render children
	//----------------------------------------------------------------
	for (child_list_t::iterator iter = mChildren.begin();
		 iter != mChildren.end(); ++iter)
	{
		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
		F32 jointLOD = joint->getLOD();
		if (pixelArea >= jointLOD || sDisableLOD)
		{
			triangle_count += joint->render( pixelArea );

			if (jointLOD != DEFAULT_LOD)
			{
				break;
			}
		}
	}

	return triangle_count;
}


//--------------------------------------------------------------------
// drawBone()
//--------------------------------------------------------------------
void LLViewerJoint::drawBone()
{
	if ( mParent == NULL )
		return;

	F32 boneSize = 0.02f;

	// rotate to point to child (bone direction)
	glPushMatrix();

	LLVector3 boneX = getPosition();
	F32 length = boneX.normVec();

	LLVector3 boneZ(1.0f, 0.0f, 1.0f);
	
	LLVector3 boneY = boneZ % boneX;
	boneY.normVec();

	boneZ = boneX % boneY;

	LLMatrix4 rotateMat;
	rotateMat.setFwdRow( boneX );
	rotateMat.setLeftRow( boneY );
	rotateMat.setUpRow( boneZ );
	glMultMatrixf( &rotateMat.mMatrix[0][0] );

	// render the bone
	gGL.color3f( 0.5f, 0.5f, 0.0f );

	gGL.begin(GL_TRIANGLES);

	gGL.vertex3f( length,     0.0f,       0.0f);
	gGL.vertex3f( 0.0f,       boneSize,  0.0f);
	gGL.vertex3f( 0.0f,       0.0f,       boneSize);

	gGL.vertex3f( length,     0.0f,        0.0f);
	gGL.vertex3f( 0.0f,       0.0f,        -boneSize);
	gGL.vertex3f( 0.0f,       boneSize,   0.0f);

	gGL.vertex3f( length,     0.0f,        0.0f);
	gGL.vertex3f( 0.0f,       -boneSize,  0.0f);
	gGL.vertex3f( 0.0f,       0.0f,        -boneSize);

	gGL.vertex3f( length,     0.0f,        0.0f);
	gGL.vertex3f( 0.0f,       0.0f,        boneSize);
	gGL.vertex3f( 0.0f,       -boneSize,  0.0f);

	gGL.end();

	// restore matrix
	glPopMatrix();
}

//--------------------------------------------------------------------
// isTransparent()
//--------------------------------------------------------------------
BOOL LLViewerJoint::isTransparent()
{
	return FALSE;
}

//--------------------------------------------------------------------
// drawShape()
//--------------------------------------------------------------------
U32 LLViewerJoint::drawShape( F32 pixelArea, BOOL first_pass )
{
	return 0;
}

//--------------------------------------------------------------------
// setSkeletonComponents()
//--------------------------------------------------------------------
void LLViewerJoint::setSkeletonComponents( U32 comp, BOOL recursive )
{
	mComponents = comp;
	if (recursive)
	{
		for (child_list_t::iterator iter = mChildren.begin();
			 iter != mChildren.end(); ++iter)
		{
			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
			joint->setSkeletonComponents(comp, recursive);
		}
	}
}

void LLViewerJoint::updateFaceSizes(U32 &num_vertices, U32& num_indices, F32 pixel_area)
{
	for (child_list_t::iterator iter = mChildren.begin();
		 iter != mChildren.end(); ++iter)
	{
		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
		joint->updateFaceSizes(num_vertices, num_indices, pixel_area);
	}
}

void LLViewerJoint::updateFaceData(LLFace *face, F32 pixel_area, BOOL damp_wind)
{
	for (child_list_t::iterator iter = mChildren.begin();
		 iter != mChildren.end(); ++iter)
	{
		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
		joint->updateFaceData(face, pixel_area, damp_wind);
	}
}

void LLViewerJoint::updateGeometry()
{
	for (child_list_t::iterator iter = mChildren.begin();
		 iter != mChildren.end(); ++iter)
	{
		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
		joint->updateGeometry();
	}
}


BOOL LLViewerJoint::updateLOD(F32 pixel_area, BOOL activate)
{
	BOOL lod_changed = FALSE;
	BOOL found_lod = FALSE;

	for (child_list_t::iterator iter = mChildren.begin();
		 iter != mChildren.end(); ++iter)
	{
		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
		F32 jointLOD = joint->getLOD();
		
		if (found_lod || jointLOD == DEFAULT_LOD)
		{
			// we've already found a joint to enable, so enable the rest as alternatives
			lod_changed |= joint->updateLOD(pixel_area, TRUE);
		}
		else
		{
			if (pixel_area >= jointLOD || sDisableLOD)
			{
				lod_changed |= joint->updateLOD(pixel_area, TRUE);
				found_lod = TRUE;
			}
			else
			{
				lod_changed |= joint->updateLOD(pixel_area, FALSE);
			}
		}
	}
	return lod_changed;
}

void LLViewerJoint::dump()
{
	for (child_list_t::iterator iter = mChildren.begin();
		 iter != mChildren.end(); ++iter)
	{
		LLViewerJoint* joint = (LLViewerJoint*)(*iter);
		joint->dump();
	}
}

void LLViewerJoint::setVisible(BOOL visible, BOOL recursive)
{
	mVisible = visible;

	if (recursive)
	{
		for (child_list_t::iterator iter = mChildren.begin();
			 iter != mChildren.end(); ++iter)
		{
			LLViewerJoint* joint = (LLViewerJoint*)(*iter);
			joint->setVisible(visible, recursive);
		}
	}
}

//-----------------------------------------------------------------------------
// LLViewerJointCollisionVolume()
//-----------------------------------------------------------------------------

LLViewerJointCollisionVolume::LLViewerJointCollisionVolume()
{
	mUpdateXform = FALSE;
}

LLViewerJointCollisionVolume::LLViewerJointCollisionVolume(const std::string &name, LLJoint *parent) : LLViewerJoint(name, parent)
{
	
}

void LLViewerJointCollisionVolume::renderCollision()
{
	updateWorldMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glMultMatrixf( &mXform.getWorldMatrix().mMatrix[0][0] );

	glColor3f( 0.f, 0.f, 1.f );
	gSphere.render();

	glPopMatrix();
}

LLVector3 LLViewerJointCollisionVolume::getVolumePos(LLVector3 &offset)
{
	mUpdateXform = TRUE;
	
	LLVector3 result = offset;
	result.scaleVec(getScale());
	result.rotVec(getWorldRotation());
	result += getWorldPosition();

	return result;
}

// End