summaryrefslogtreecommitdiff
path: root/hammer/detailobjects.cpp
blob: d1cb5b7d28b5c5ae76188dfa58c7264e83bd9ffd (plain) (blame)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Places "detail" objects which are client-only renderable things
//
// $Revision: $
// $NoKeywords: $
//=============================================================================//

#include "stdafx.h"

#include "collisionutils.h"
#include "const.h"
#include "interface.h"

#include "KeyValues.h"
#include "utlsymbol.h"
#include "utlvector.h"
#include "utilmatlib.h"
#include "mathlib/VMatrix.h"
#include "vstdlib/random.h"
#include "builddisp.h"
#include "tier1/utlbuffer.h"
#include "IEditorTexture.h"
#include "materialsystem/imaterialvar.h"
#include "materialsystem/imaterial.h"
#include "mapface.h"
#include "camera.h"
#include "options.h"

#include "hammer.h"

// Actually, this is the max per map, but for now this is better than no limit at all.
#define MAX_DETAIL_SPRITES_PER_FACE 65535

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//IMPLEMENT_MAPCLASS(DetailObjects)

bool DetailObjects::s_bBuildDetailObjects = true;

// Defaults to match the parsing defaults in ParseDetailGroup -- code path defaults may/may not execute
DetailObjects::~DetailObjects()
{ 
	m_DetailModels.PurgeAndDeleteElements();
	m_DetailSprites.PurgeAndDeleteElements(); 
}

DetailObjects::DetailModel_t::DetailModel_t() : m_ModelName()
{
	m_Amount = 0.0;
	m_MinCosAngle = -1.0;
	m_MaxCosAngle = -1.0;
	m_Flags = 0;
	m_Orientation = 0;
	m_Type = DETAIL_PROP_TYPE_SPRITE;
	m_Pos[0] = Vector2D(-10, 20);
	m_Pos[1] = Vector2D(10, 0);
	m_Tex[0] = Vector2D(0.5/512, 0.5/512);
	m_Tex[1] = Vector2D(63.5/512, 63.5/512);
	m_flRandomScaleStdDev = 0.0;
	m_ShapeSize = 0;
	m_ShapeAngle = 0;
	m_SwayAmount = 0;
}

CUtlVector<DetailObjects::DetailObject_t>	DetailObjects::s_DetailObjectDict;		// static members?

//-----------------------------------------------------------------------------
// Parses the key-value pairs in the detail.rad file
//-----------------------------------------------------------------------------
void DetailObjects::ParseDetailGroup( int detailId, KeyValues* pGroupKeyValues )
{
	// Sort the group by alpha
	float alpha = pGroupKeyValues->GetFloat( "alpha", 1.0f );
	
	int iGroup = s_DetailObjectDict[detailId].m_Groups.Count();
	while ( --iGroup >= 0 )
	{
		if (alpha > s_DetailObjectDict[detailId].m_Groups[iGroup].m_Alpha)
			break;
	}

	// Insert after the first guy who's more transparent that we are!
	iGroup = s_DetailObjectDict[detailId].m_Groups.InsertAfter( iGroup );
	DetailObjectGroup_t& group = s_DetailObjectDict[detailId].m_Groups[iGroup];

	group.m_Alpha = alpha;

	// Add in all the model groups
	KeyValues* pIter = pGroupKeyValues->GetFirstSubKey();
	float totalAmount = 0.0f;
	while( pIter )
	{
		if (pIter->GetFirstSubKey())
		{
			int i = group.m_Models.AddToTail();

			DetailModel_t &model = group.m_Models[i];

			model.m_ModelName = pIter->GetString( "model", 0 );
			if (model.m_ModelName != UTL_INVAL_SYMBOL)
			{
				model.m_Type = DETAIL_PROP_TYPE_MODEL;
			}
			else
			{
				const char *pSpriteData = pIter->GetString( "sprite", 0 );
				if (pSpriteData)
				{
					const char *pProcModelType = pIter->GetString( "sprite_shape", 0 );

					if ( pProcModelType )
					{
						if ( !Q_stricmp( pProcModelType, "cross" ) )
						{
							model.m_Type = DETAIL_PROP_TYPE_SHAPE_CROSS;
						}
						else if ( !Q_stricmp( pProcModelType, "tri" ) )
						{
							model.m_Type = DETAIL_PROP_TYPE_SHAPE_TRI;
						}
						else
							model.m_Type = DETAIL_PROP_TYPE_SPRITE;
					}					
					else
					{
						// card sprite
                        model.m_Type = DETAIL_PROP_TYPE_SPRITE;
					}

					model.m_Tex[0].Init();
					model.m_Tex[1].Init();

					float x = 0, y = 0, flWidth = 64, flHeight = 64, flTextureSize = 512;
					int nValid = sscanf( pSpriteData, "%f %f %f %f %f", &x, &y, &flWidth, &flHeight, &flTextureSize ); 
					if ( (nValid != 5) || (flTextureSize == 0) )
					{
						Error( "Invalid arguments to \"sprite\" in detail.vbsp (model %s)!\n", model.m_ModelName.String() );
					}

					model.m_Tex[0].x = ( x + 0.5f ) / flTextureSize;
					model.m_Tex[0].y = ( y + 0.5f ) / flTextureSize;
					model.m_Tex[1].x = ( x + flWidth - 0.5f ) / flTextureSize;
					model.m_Tex[1].y = ( y + flHeight - 0.5f ) / flTextureSize;

					model.m_Pos[0].Init( -10, 20 );
					model.m_Pos[1].Init( 10, 0 );

					pSpriteData = pIter->GetString( "spritesize", 0 );
					if (pSpriteData)
					{
						sscanf( pSpriteData, "%f %f %f %f", &x, &y, &flWidth, &flHeight );

						float ox = flWidth * x;
						float oy = flHeight * y;

						model.m_Pos[0].x = -ox;
						model.m_Pos[0].y = flHeight - oy;
						model.m_Pos[1].x = flWidth - ox;
						model.m_Pos[1].y = -oy;
					}

					model.m_flRandomScaleStdDev = pIter->GetFloat( "spriterandomscale", 0.0f );

					// sway is a percent of max sway, cl_detail_max_sway
					float flSway = clamp( pIter->GetFloat( "sway", 0.0f ), 0.0, 1.0 );
					model.m_SwayAmount = (unsigned char)( 255.0 * flSway );

					// shape angle
					// for the tri shape, this is the angle each side is fanned out
					model.m_ShapeAngle = pIter->GetInt( "shape_angle", 0 );

					// shape size
					// for the tri shape, this is the distance from the origin to the center of a side
					float flShapeSize = clamp( pIter->GetFloat( "shape_size", 0.0f ), 0.0, 1.0 );
					model.m_ShapeSize = (unsigned char)( 255.0 * flShapeSize );
				}
			}

			model.m_Amount = pIter->GetFloat( "amount", 1.0 ) + totalAmount;
			totalAmount = model.m_Amount;

			model.m_Flags = 0;
			if (pIter->GetInt( "upright", 0 ))
			{
				model.m_Flags |= MODELFLAG_UPRIGHT;
			}

			// These are used to prevent emission on steep surfaces
			float minAngle = pIter->GetFloat( "minAngle", 180 );
			float maxAngle = pIter->GetFloat( "maxAngle", 180 );
			model.m_MinCosAngle = cos(minAngle * M_PI / 180.f);
			model.m_MaxCosAngle = cos(maxAngle * M_PI / 180.f);
			model.m_Orientation = pIter->GetInt( "detailOrientation", 0 );

			// Make sure minAngle < maxAngle
			if ( model.m_MinCosAngle < model.m_MaxCosAngle)
			{
				model.m_MinCosAngle = model.m_MaxCosAngle;
			}
		}
		pIter = pIter->GetNextKey();
	}

	// renormalize the amount if the total > 1
	if (totalAmount > 1.0f)
	{
		for (int i = 0; i < group.m_Models.Count(); ++i)
		{
			group.m_Models[i].m_Amount /= totalAmount;
		}
	}
}


//-----------------------------------------------------------------------------
// Parses the key-value pairs in the detail.vbsp file
//-----------------------------------------------------------------------------
void DetailObjects::ParseDetailObjectFile( KeyValues& keyValues )
{
	// Iterate over all detail object groups...
	KeyValues* pIter;
	for( pIter = keyValues.GetFirstSubKey(); pIter; pIter = pIter->GetNextKey() )
	{
		if (!pIter->GetFirstSubKey())
			continue;

		int i = s_DetailObjectDict.AddToTail( );
		s_DetailObjectDict[i].m_Name = pIter->GetName() ;
		s_DetailObjectDict[i].m_Density = pIter->GetFloat( "density", 0.0f );

		// Iterate over all detail object groups...
		KeyValues* pIterGroups = pIter->GetFirstSubKey();
		while( pIterGroups )
		{
			if (pIterGroups->GetFirstSubKey())
			{
				ParseDetailGroup( i, pIterGroups );
			}
			pIterGroups = pIterGroups->GetNextKey();
		}
	}
}


//-----------------------------------------------------------------------------
// Finds the name of the detail.vbsp file to use
//-----------------------------------------------------------------------------
const char *DetailObjects::FindDetailVBSPName( void )
{
#if 0
	for( int i = 0; i < num_entities; i++ )
	{
		char* pEntity = ValueForKey( &entities[i], "classname" );
		if ( !strcmp( pEntity, "worldspawn" ) )
		{
			const char *pDetailVBSP = ValueForKey( &entities[i], "detailvbsp" );
			if ( !pDetailVBSP || !pDetailVBSP[0] ) 
			{
				pDetailVBSP = "detail.vbsp";
			}
			return pDetailVBSP;
		}
	}
#endif
	return "detail.vbsp";
}

#include "tier0\memdbgoff.h"

//-----------------------------------------------------------------------------
// Loads up the detail object dictionary
//-----------------------------------------------------------------------------
void DetailObjects::LoadEmitDetailObjectDictionary( const char* pGameDir )
{
	// Set the required global lights filename and try looking in qproject
	const char *pDetailVBSP = FindDetailVBSPName();
	KeyValues * values = new KeyValues( pDetailVBSP );
	if ( values->LoadFromFile( g_pFileSystem, pDetailVBSP ) )
	{
		ParseDetailObjectFile( *values );
	}
	values->deleteThis();
}


//-----------------------------------------------------------------------------
// Selects a detail group
//-----------------------------------------------------------------------------
int DetailObjects::SelectGroup( const DetailObject_t& detail, float alpha )
{
	// Find the two groups whose alpha we're between...
	int start, end;
	for ( start = 0; start < detail.m_Groups.Count() - 1; ++start )
	{
		if (alpha < detail.m_Groups[start+1].m_Alpha)
			break;
	}

	end = start + 1;
	if (end >= detail.m_Groups.Count())
		--end;

	if (start == end)
		return start;

	// Figure out how far we are between start and end...
	float dist = 0.0f;
	float dAlpha = (detail.m_Groups[end].m_Alpha - detail.m_Groups[start].m_Alpha);
	if (dAlpha != 0.0f)
	{
		dist = (alpha - detail.m_Groups[start].m_Alpha) / dAlpha;
	}

	// Pick a number, any number...
	float flR = rand() / (float)VALVE_RAND_MAX;

	// When dist == 0, we *always* want start.
	// When dist == 1, we *always* want end
	// That's why this logic looks a little reversed
	return (flR > dist) ? start : end;
}


//-----------------------------------------------------------------------------
// Selects a detail object
//-----------------------------------------------------------------------------
int DetailObjects::SelectDetail( DetailObjectGroup_t const& group )
{
	// Pick a number, any number...
	float flR = rand() / (float)VALVE_RAND_MAX;

	// Look through the list of models + pick the one associated with this number
	for ( int i = 0; i < group.m_Models.Count(); ++i )
	{
		if ( flR <= group.m_Models[i].m_Amount)
			return i;
	}

	return -1;
}

//-----------------------------------------------------------------------------
// Add a detail to the lump.
//-----------------------------------------------------------------------------

void DetailObjects::AddDetailModelToFace( const char* pModelName, const Vector& pt, const QAngle& angles, int nOrientation )
{
	StudioModel	*pStudioModel = new StudioModel();
	m_DetailModels.AddToTail( pStudioModel );
	pStudioModel->LoadModel( pModelName );
	pStudioModel->SetOrigin( pt.x, pt.y, pt.z );
	QAngle modelangle = angles;
	pStudioModel->SetAngles( modelangle );
}

//-----------------------------------------------------------------------------
// Add a detail sprite to the lump.
//-----------------------------------------------------------------------------

void DetailObjects::AddDetailSpriteToFace( const Vector &vecOrigin, const QAngle &vecAngles, DetailModel_t const& model, float flScale )
{	
	CSpriteModel	*pSpriteModel = new CSpriteModel;
	m_DetailSprites.AddToTail(pSpriteModel);

	const char	szSpriteName[_MAX_PATH] = "detail/detailsprites";

	pSpriteModel->LoadSprite( szSpriteName );

	pSpriteModel->SetRenderMode( kRenderNormal );
	pSpriteModel->SetMaterialPrimitiveType( MATERIAL_POLYGON );

	pSpriteModel->SetOrigin( vecOrigin );
	pSpriteModel->SetAngles( vecAngles );
	pSpriteModel->SetScale( flScale );
	pSpriteModel->SetInvert( true );

	pSpriteModel->SetExtent( model.m_Pos[0], model.m_Pos[1] );
	pSpriteModel->SetTextureExtent( model.m_Tex[0], model.m_Tex[1] );
}

//-----------------------------------------------------------------------------
// Got a detail! Place it on the surface...
//-----------------------------------------------------------------------------
// BUGBUG: When the global optimizer is on, "normal" gets trashed in this function
// (only when not in the debugger?)
// Printing the values of normal at the bottom of the function fixes it as does
// disabling global optimizations.
void DetailObjects::PlaceDetail( DetailModel_t const& model, const Vector& pt, const Vector& normal )
{
	// But only place it on the surface if it meets the angle constraints...
	float cosAngle = normal.z;

	// Never emit if the angle's too steep
	if (cosAngle < model.m_MaxCosAngle)
		return;

	// If it's between min + max, flip a coin...
	if (cosAngle < model.m_MinCosAngle)
	{
		float probability = (cosAngle - model.m_MaxCosAngle) / 
			(model.m_MinCosAngle - model.m_MaxCosAngle);

		float t = rand() / (float)VALVE_RAND_MAX;
		if (t > probability)
			return;
	}

	// Compute the orientation of the detail
	QAngle angles;
	if (model.m_Flags & MODELFLAG_UPRIGHT)
	{
		// If it's upright, we just select a random yaw
		angles.Init( 0, 360.0f * rand() / (float)VALVE_RAND_MAX, 0.0f );
	}
	else
	{
		// It's not upright, so it must conform to the ground. Choose
		// a random orientation based on the surface normal

		Vector zaxis;
		VectorCopy( normal, zaxis );
		VectorNormalize( zaxis );

		// Choose any two arbitrary axes which are perpendicular to the normal
		Vector xaxis( 1, 0, 0 );
		if (fabs(xaxis.Dot(zaxis)) - 1.0 > -1e-3)
			xaxis.Init( 0, 1, 0 );
		Vector yaxis;
		CrossProduct( zaxis, xaxis, yaxis );
		VectorNormalize( yaxis );
		CrossProduct( yaxis, zaxis, xaxis );
		VectorNormalize( xaxis );
		VMatrix matrix;
		matrix.SetBasisVectors( xaxis, yaxis, zaxis );
		matrix.SetTranslation( vec3_origin );

		float rotAngle = 360.0f * rand() / (float)VALVE_RAND_MAX;
		VMatrix rot = SetupMatrixAxisRot( Vector( 0, 0, 1 ), rotAngle );
		matrix = matrix * rot;

		MatrixToAngles( matrix, angles );
	}

	// FIXME: We may also want a purely random rotation too

	// Insert an element into the object dictionary if it aint there...
	switch ( model.m_Type )
	{
	case DETAIL_PROP_TYPE_MODEL:
		AddDetailModelToFace( model.m_ModelName.String(), pt, angles, model.m_Orientation );
		break;

	// Sprites and procedural models made from sprites
	case DETAIL_PROP_TYPE_SPRITE:
	default:
		{
			float flScale = 1.0f;
			if ( model.m_flRandomScaleStdDev != 0.0f ) 
			{
				flScale = fabs( RandomGaussianFloat( 1.0f, model.m_flRandomScaleStdDev ) );
			}

			AddDetailSpriteToFace( pt, angles, model, flScale );
		}
		break;
	}
}


//-----------------------------------------------------------------------------
// Places Detail Objects on a face
//-----------------------------------------------------------------------------
void DetailObjects::EmitDetailObjectsOnFace( CMapFace *pMapFace, DetailObject_t& detail )
{
	// See how many points define this particular face
	int	nPoints = pMapFace->GetPointCount();

	// Faces with detail props need at least 3 point to form a plane 
	if (nPoints < 3)
		return;

	// Get the first point of the face
	Vector	p0;
	pMapFace->GetPoint(p0,0);

	// Get next points on the face in pairs -- ie get points necessary to tesselate the face into triangles
	for (int i = 1; i < nPoints-1; i++ )
	{
		// Get the next two points of the face
		Vector	p1, p2;
		pMapFace->GetPoint(p1,i);
		pMapFace->GetPoint(p2,i+1);

		// For the edges of the current triangle tesselating a portion of the face
		Vector	e1, e2;
		VectorSubtract( p1, p0, e1 );
		VectorSubtract( p2, p0, e2 );

		// Calculate the area of the tesselated triange using half the crossproduct of the edges
		Vector	areaVec;
		CrossProduct( e1, e2, areaVec );
		float	normalLength = areaVec.Length();
		float	area = 0.5 * normalLength;

		// Calculate the detail prop density based on the expected density and the tesselated triangle area
		int numSamples = clamp( area * detail.m_Density * 0.000001, 0, MAX_DETAIL_SPRITES_PER_FACE );
		
		// For each possible sample, attempt to randomly place a detail object there
		for (int j = 0; j < numSamples; ++j )
		{
			// Create a random sample location...
			float u = rand() / (float)VALVE_RAND_MAX;
			float v = rand() / (float)VALVE_RAND_MAX;

			// Make sure the u,v coordinate stay within the triangle boundaries (ie they NOT in the far half of the parallelogram)
			if (v > 1.0f - u)
			{
				// Triangle is out of bounds, flip the coordinates so they are in the near half of the parallelogram
				u = 1.0f - u;
				v = 1.0f - v;
				assert( u + v <= 1.0f );
			}

			// Compute alpha - assumed to be 1.0 across entire face for non-displacement map faces, since there is no alpha channel
			float alpha = 1.0f;

			// Select a group based on the alpha value
			int group = SelectGroup( detail, alpha );

			// Now that we've got a group, choose a detail
			int model = SelectDetail( detail.m_Groups[group] );
			if (model < 0)
				continue;

			// Got a detail! Place it on the surface...
			Vector pt, normal;
			VectorMA( p0, u, e1, pt );
			VectorMA( pt, v, e2, pt );
			VectorDivide( areaVec, -normalLength, normal );

			PlaceDetail( detail.m_Groups[group].m_Models[model], pt, normal );
		}
	}
}


//-----------------------------------------------------------------------------
// Places Detail Objects on a face
//-----------------------------------------------------------------------------
float DetailObjects::ComputeDisplacementFaceArea( CMapFace *pMapFace )
{
	float area = 0.0f;

	// Compute the area of the base face
	// Displacement base faces must be quads.
	Vector	edge[4];
	for( int i=0; i<4; i++ )
	{
		Vector	p0, p1;
		pMapFace->GetPoint( p0, i );
		pMapFace->GetPoint( p1, (i+1)%4 );
		VectorSubtract( p1, p0, edge[i] );
	}
	Vector	area_01, area_23;
	CrossProduct( edge[0], edge[1], area_01 );
	CrossProduct( edge[2], edge[3], area_23 );
	area = ( area_01.Length() + area_23.Length() ) * 0.5f;

	return area;
}


//-----------------------------------------------------------------------------
// Places Detail Objects on a face
//-----------------------------------------------------------------------------
void DetailObjects::EmitDetailObjectsOnDisplacementFace( CMapFace *pMapFace, 
						DetailObject_t& detail )
{
	assert(pMapFace->GetPointCount() == 4);

	// We're going to pick a bunch of random points, and then probabilistically
	// decide whether or not to plant a detail object there.

	// Compute the area of the base face
	float area = ComputeDisplacementFaceArea( pMapFace );

	// Compute the number of samples to take
	int numSamples = area * detail.m_Density * 0.000001;

	EditDispHandle_t	editdisphandle = pMapFace->GetDisp();
	CMapDisp			*pMapDisp = EditDispMgr()->GetDisp(editdisphandle);
	CCoreDispInfo		*pCoreDispInfo = pMapDisp->GetCoreDispInfo();

	// Now take a sample, and randomly place an object there
	for (int i = 0; i < numSamples; ++i )
	{
		// Create a random sample...
		float u = rand() / (float)VALVE_RAND_MAX;
		float v = rand() / (float)VALVE_RAND_MAX;

		// Compute alpha
		float alpha;
		Vector pt, normal;
		pCoreDispInfo->GetPositionOnSurface( u, v, pt, &normal, &alpha );
		alpha /= 255.0f;

		// Select a group based on the alpha value
		int group = SelectGroup( detail, alpha );

		// Now that we've got a group, choose a detail
		int model = SelectDetail( detail.m_Groups[group] );
		if (model < 0)
			continue;

		// Got a detail! Place it on the surface...
		PlaceDetail( detail.m_Groups[group].m_Models[model], pt, normal );
	}
}

//-----------------------------------------------------------------------------
// Builds Detail Objects for a particular face
//-----------------------------------------------------------------------------

bool	DetailObjects::ShouldRenderLast(void)
{
	return true;
}

//-----------------------------------------------------------------------------
// Builds Detail Objects for a particular face
//-----------------------------------------------------------------------------
void	DetailObjects::BuildAnyDetailObjects(CMapFace *pMapFace)
{
	// Ignore this call while loading the VMF or else we'll generate a lot of redundant ones.
	if ( !s_bBuildDetailObjects )
		return;
		
	if ( pMapFace->IsCordonFace() )
		return;

	// Try to get at the material
	bool found;

	IEditorTexture *pEditorTexture = pMapFace->GetTexture();
	if ( !pEditorTexture )
		return;

	IMaterial *pMaterial = pEditorTexture->GetMaterial();
	if ( !pMaterial )
		return;

	IMaterialVar *pMaterialVar = pMaterial->FindVar("%detailtype", &found, false );
	if ( !found || !pMaterialVar )
		return;

	const char* pDetailType = pMaterialVar->GetStringValue();
	if ( !pDetailType )
		return;

	// Get the detail type...
	DetailObject_t search;
	search.m_Name = pDetailType;

	DetailObjects	*pDetails = pMapFace->m_pDetailObjects;
	if ( pMapFace->m_pDetailObjects )
	{
		pDetails->m_DetailModels.PurgeAndDeleteElements();
		pDetails->m_DetailSprites.PurgeAndDeleteElements(); 
	}
	else
	{
		pMapFace->m_pDetailObjects = pDetails = new DetailObjects;
	}

	if ( pDetails )
	{
		// Set the center the "detailobjects" to be the average of the face points
		int	nPoints = pMapFace->GetPointCount();
		Vector	faceCenter, faceCorner;
		faceCenter.Init();
		for ( int point=0; point < nPoints; point++ )
		{
			pMapFace->GetPoint(faceCorner,point);
			faceCenter += faceCorner;
		}
		faceCenter /= nPoints;

		pDetails->SetOrigin( faceCenter );

		int objectType = s_DetailObjectDict.Find(search);
		if (objectType < 0)
		{
			char	szTextureName[MAX_PATH];
			pMapFace->GetTextureName(szTextureName);
			Warning("Material %s uses unknown detail object type %s!\n", szTextureName, pDetailType);
			return;
		}

		// Emit objects on a particular face
		DetailObject_t& detail = s_DetailObjectDict[objectType];

		// Initialize the Random Number generators for detail prop placement based on the origFace num.
		int	detailpropseed = pMapFace->GetFaceID();
#ifdef WARNSEEDNUMBER
		Warning("[%d]\n",detailpropseed);
#endif
		srand( detailpropseed );
		RandomSeed( detailpropseed );

		if ( pMapFace->HasDisp() )
		{
			pDetails->EmitDetailObjectsOnDisplacementFace( pMapFace, detail );
		}
		else
		{
			pDetails->EmitDetailObjectsOnFace( pMapFace, detail );
		}
	}
	else
	{
		Warning("Could not allocate DetailObject for CMapFace!\n");
	}
}

void DetailObjects::EnableBuildDetailObjects( bool bEnable )
{
	s_bBuildDetailObjects = bEnable;
}

void	DetailObjects::Render3D(CRender3D *pRender)
{
	Vector Mins, Maxs;
	float fDetailDistance = Options.view3d.nDetailDistance;
	Vector viewPoint; pRender->GetCamera()->GetViewPoint( viewPoint );

	int models = m_DetailModels.Count();
	if ( models )
	{
		pRender->PushRenderMode( RENDER_MODE_DEFAULT );
		for ( int i = 0; i < models; i++ )
		{
			StudioModel	*pModel = m_DetailModels[i];
			pModel->GetOrigin(Mins);
			pModel->GetOrigin(Maxs);
			for( int j=0; j<3; j++ )
			{
				Mins[j] -= fDetailDistance;
				Maxs[j] += fDetailDistance;
			}
			if ( IsPointInBox( viewPoint, Mins, Maxs ) )
				pModel->DrawModel3D( pRender, 1, false  );
		}
		pRender->PopRenderMode();

	}

	int sprites = m_DetailSprites.Count();
	if ( sprites )
	{
		unsigned char	color[3] = { 255, 255, 255 };
		pRender->PushRenderMode( RENDER_MODE_DEFAULT );
		for ( int i = 0; i < sprites; i++ )
		{
			CSpriteModel	*pSprite = m_DetailSprites[i];
			pSprite->GetOrigin(Mins);
			pSprite->GetOrigin(Maxs);
			for( int j=0; j<3; j++ )
			{
				Mins[j] -= fDetailDistance;
				Maxs[j] += fDetailDistance;
			}
			if ( IsPointInBox( viewPoint, Mins, Maxs ) )
				pSprite->DrawSprite3D( pRender, color  );
		}
		pRender->PopRenderMode();
	}
}

// EOF