summaryrefslogtreecommitdiff
path: root/hammer/mapstudiomodel.cpp
blob: 9d164fcf18d08585de1fed53b82a84b255597234 (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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "stdafx.h"
#include "Box3D.h"
#include "GlobalFunctions.h"
#include "MapDefs.h"		// dvs: For COORD_NOTINIT
#include "MapDoc.h"
#include "MapEntity.h"
#include "MapStudioModel.h"
#include "Render2D.h"
#include "Render3D.h"
#include "ViewerSettings.h"
#include "hammer.h"
#include "materialsystem/imesh.h"
#include "TextureSystem.h"
#include "Material.h"
#include "Options.h"
#include "camera.h"

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


#define STUDIO_RENDER_DISTANCE		400


IMPLEMENT_MAPCLASS(CMapStudioModel)


float CMapStudioModel::m_fRenderDistance = STUDIO_RENDER_DISTANCE;
BOOL CMapStudioModel::m_bAnimateModels = TRUE;


//-----------------------------------------------------------------------------
// Purpose: Factory function. Used for creating a CMapStudioModel from a set
//			of string parameters from the FGD file.
// Input  : pInfo - Pointer to helper info class which gives us information
//				about how to create the class.
// Output : Returns a pointer to the class, NULL if an error occurs.
//-----------------------------------------------------------------------------
CMapClass *CMapStudioModel::CreateMapStudioModel(CHelperInfo *pHelperInfo, CMapEntity *pParent)
{
	const char *pszModel = pHelperInfo->GetParameter(0);

	//
	// If we weren't passed a model name as an argument, get it from our parent
	// entity's "model" key.
	//
	if (pszModel == NULL)
	{
		pszModel = pParent->GetKeyValue("model");
	}

	//
	// If we have a model name, create a studio model object.
	//
	if (pszModel != NULL)
	{
		bool bLightProp = !stricmp(pHelperInfo->GetName(), "lightprop");
		bool bOrientedBounds = (bLightProp | !stricmp(pHelperInfo->GetName(), "studioprop"));
		return CreateMapStudioModel(pszModel, bOrientedBounds, bLightProp);
	}

	return(NULL);
}


//-----------------------------------------------------------------------------
// Purpose: Factory function. Creates a CMapStudioModel object from a relative
//			path to an MDL file.
// Input  : pszModelPath - Relative path to the .MDL file. The path is appended
//				to each path in the	application search path until the model is found.
//			bOrientedBounds - Whether the bounding box should consider the orientation of the model.
// Output : Returns a pointer to the newly created CMapStudioModel object.
//-----------------------------------------------------------------------------
CMapStudioModel *CMapStudioModel::CreateMapStudioModel(const char *pszModelPath, bool bOrientedBounds, bool bReversePitch)
{
	CMapStudioModel *pModel = new CMapStudioModel;
	pModel->m_pStudioModel = CStudioModelCache::CreateModel(pszModelPath);
	if ( pModel->m_pStudioModel )
	{
		pModel->SetOrientedBounds(bOrientedBounds);
		pModel->ReversePitch(bReversePitch);

		pModel->CalcBounds();
	}
	else
	{
		delete pModel;
		pModel = NULL;
	}
	return(pModel);
}


//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CMapStudioModel::CMapStudioModel(void)
{
	Initialize();
	InitViewerSettings();
}


//-----------------------------------------------------------------------------
// Purpose: Destructor. Releases the studio model cache reference.
//-----------------------------------------------------------------------------
CMapStudioModel::~CMapStudioModel(void)
{
	if (m_pStudioModel != NULL)
	{
		CStudioModelCache::Release(m_pStudioModel);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called by the renderer before every frame to animate the models.
//-----------------------------------------------------------------------------
void CMapStudioModel::AdvanceAnimation(float flInterval)
{
	if (m_bAnimateModels)
	{
		CStudioModelCache::AdvanceAnimation(flInterval);
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : bFullUpdate - 
//-----------------------------------------------------------------------------
void CMapStudioModel::CalcBounds(BOOL bFullUpdate)
{
	CMapClass::CalcBounds(bFullUpdate);

	Vector Mins(0, 0, 0);
	Vector Maxs(0, 0, 0);

	if (m_pStudioModel != NULL)
	{
		//
		// The 3D bounds are the bounds of the oriented model's first sequence, so that
		// frustum culling works properly in the 3D view.
		//
		QAngle angles;
		GetRenderAngles(angles);

		m_pStudioModel->SetAngles(angles);
		m_pStudioModel->ExtractBbox(m_CullBox.bmins, m_CullBox.bmaxs);

		if (m_bOrientedBounds)
		{
			//
			// Oriented bounds - the 2D bounds are the same as the 3D bounds.
			//
			Mins = m_CullBox.bmins;
			Maxs = m_CullBox.bmaxs;
		}
		else
		{
			//
			// The 2D bounds are the movement bounding box of the model, which is not affected
			// by the entity's orientation. This is used for character models for which we want
			// to render a meaningful collision box in the editor.
			//
			m_pStudioModel->ExtractMovementBbox(Mins, Maxs);
		}

		Mins += m_Origin;
		Maxs += m_Origin;

		m_CullBox.bmins += m_Origin;
		m_CullBox.bmaxs += m_Origin;
	}

	//
	// If we do not yet have a valid bounding box, use a default box.
	//
	if ((Maxs - Mins) == Vector(0, 0, 0))
	{
		Mins = m_CullBox.bmins = m_Origin - Vector(10, 10, 10);
		Maxs = m_CullBox.bmaxs = m_Origin + Vector(10, 10, 10);
	}

	m_BoundingBox = m_CullBox;
	m_Render2DBox.UpdateBounds(Mins, Maxs);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Output : CMapClass
//-----------------------------------------------------------------------------
CMapClass *CMapStudioModel::Copy(bool bUpdateDependencies)
{
	CMapStudioModel *pCopy = new CMapStudioModel;

	if (pCopy != NULL)
	{
		pCopy->CopyFrom(this, bUpdateDependencies);
	}

	return(pCopy);
}


//-----------------------------------------------------------------------------
// Purpose: Makes this an exact duplicate of pObject.
// Input  : pObject - Object to copy.
// Output : Returns this.
//-----------------------------------------------------------------------------
CMapClass *CMapStudioModel::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
{
	Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapStudioModel)));
	CMapStudioModel *pFrom = (CMapStudioModel *)pObject;

	CMapClass::CopyFrom(pObject, bUpdateDependencies);

	m_pStudioModel = pFrom->m_pStudioModel;
	if (m_pStudioModel != NULL)
	{
		CStudioModelCache::AddRef(m_pStudioModel);
	}

	m_Angles = pFrom->m_Angles;
	m_Skin = pFrom->m_Skin;
	m_bOrientedBounds = pFrom->m_bOrientedBounds;
	m_bReversePitch = pFrom->m_bReversePitch;
	m_bPitchSet = pFrom->m_bPitchSet;
	m_flPitch = pFrom->m_flPitch;

	m_bScreenSpaceFade = pFrom->m_bScreenSpaceFade;
	m_flFadeScale = pFrom->m_flFadeScale;
	m_flFadeMinDist = pFrom->m_flFadeMinDist;
	m_flFadeMaxDist = pFrom->m_flFadeMaxDist;
	m_iSolid = pFrom->m_iSolid;

	return(this);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : bEnable - 
//-----------------------------------------------------------------------------
void CMapStudioModel::EnableAnimation(BOOL bEnable)
{
	m_bAnimateModels = bEnable;
}


//-----------------------------------------------------------------------------
// Purpose: Returns this object's pitch, yaw, and roll.
//-----------------------------------------------------------------------------
void CMapStudioModel::GetAngles(QAngle &Angles)
{
	Angles = m_Angles;

	if (m_bPitchSet)
	{
		Angles[PITCH] = m_flPitch;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns this object's pitch, yaw, and roll for rendering.
//-----------------------------------------------------------------------------
void CMapStudioModel::GetRenderAngles(QAngle &Angles)
{
	GetAngles(Angles);

	if (m_bReversePitch)
	{
		Angles[PITCH] *= -1;
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMapStudioModel::Initialize(void)
{
	m_Angles.Init();
	m_bPitchSet = false;
	m_flPitch = 0;
	m_bReversePitch = false;
	m_pStudioModel = NULL;
	m_Skin = 0;

	m_bScreenSpaceFade = false;
	m_flFadeScale = 1.0f;
	m_flFadeMinDist = 0.0f;
	m_flFadeMaxDist = 0.0f;
	m_iSolid = -1;
}


//-----------------------------------------------------------------------------
// Purpose: Notifies that this object's parent entity has had a key value change.
// Input  : szKey - The key that changed.
//			szValue - The new value of the key.
//-----------------------------------------------------------------------------
void CMapStudioModel::OnParentKeyChanged(const char* szKey, const char* szValue)
{
	if (!stricmp(szKey, "angles"))
	{
		sscanf(szValue, "%f %f %f", &m_Angles[PITCH], &m_Angles[YAW], &m_Angles[ROLL]);
		PostUpdate(Notify_Changed);
	}
	else if (!stricmp(szKey, "pitch"))
	{
		m_flPitch = atof(szValue);
		m_bPitchSet = true;

		PostUpdate(Notify_Changed);
	}
	else if (!stricmp(szKey, "skin"))
	{
		m_Skin = atoi(szValue);
		PostUpdate(Notify_Changed);
	}
	else if (!stricmp(szKey, "fademindist"))
	{
		m_flFadeMinDist = atoi(szValue);
	}
	else if (!stricmp(szKey, "fademaxdist"))
	{
		m_flFadeMaxDist = atoi(szValue);
	}
	else if (!stricmp(szKey, "screenspacefade"))
	{
		m_bScreenSpaceFade = (atoi(szValue) != 0);
	}
	else if (!stricmp(szKey, "fadescale"))
	{
		m_flFadeScale = atof(szValue);
	}
	else if ( !stricmp( szKey, "solid") )
	{
		m_iSolid = atof( szValue );
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pRender - 
//-----------------------------------------------------------------------------
bool CMapStudioModel::RenderPreload(CRender3D *pRender, bool bNewContext)
{
	return(m_pStudioModel != NULL);
}


//-----------------------------------------------------------------------------
// Draws basis vectors
//-----------------------------------------------------------------------------
static void DrawBasisVectors( CRender3D* pRender, const Vector &origin, const QAngle &angles)
{
	matrix3x4_t fCurrentMatrix;
	AngleMatrix(angles, fCurrentMatrix);

	pRender->PushRenderMode( RENDER_MODE_WIREFRAME );

	CMeshBuilder meshBuilder;
	CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
	IMesh* pMesh = pRenderContext->GetDynamicMesh();
	meshBuilder.Begin( pMesh, MATERIAL_LINES, 3 );

	meshBuilder.Color3ub(255, 0, 0);
	meshBuilder.Position3f(origin[0], origin[1], origin[2]);
	meshBuilder.AdvanceVertex();

	meshBuilder.Color3ub(255, 0, 0);
	meshBuilder.Position3f(origin[0] + (100 * fCurrentMatrix[0][0]), 
		origin[1] + (100 * fCurrentMatrix[1][0]), origin[2] + (100 * fCurrentMatrix[2][0]));
	meshBuilder.AdvanceVertex();

	meshBuilder.Color3ub(0, 255, 0);
	meshBuilder.Position3f(origin[0], origin[1], origin[2]);
	meshBuilder.AdvanceVertex();

	meshBuilder.Color3ub(0, 255, 0);
	meshBuilder.Position3f(origin[0] + (100 * fCurrentMatrix[0][1]), 
		origin[1] + (100 * fCurrentMatrix[1][1]), origin[2] + (100 * fCurrentMatrix[2][1]));
	meshBuilder.AdvanceVertex();

	meshBuilder.Color3ub(0, 0, 255);
	meshBuilder.Position3f(origin[0], origin[1], origin[2]);
	meshBuilder.AdvanceVertex();

	meshBuilder.Color3ub(0, 0, 255);
	meshBuilder.Position3f(origin[0] + (100 * fCurrentMatrix[0][2]), 
		origin[1] + (100 * fCurrentMatrix[1][2]), origin[2] + (100 * fCurrentMatrix[2][2]));
	meshBuilder.AdvanceVertex();

	meshBuilder.End();
	pMesh->Draw();

	pRender->PopRenderMode();
}


//-----------------------------------------------------------------------------
// It should render last if any of its materials are translucent, or if
// we are previewing model fades.
//-----------------------------------------------------------------------------
bool CMapStudioModel::ShouldRenderLast()
{
	return m_pStudioModel->IsTranslucent() || Options.view3d.bPreviewModelFade;
}


//-----------------------------------------------------------------------------
// Purpose: Renders the studio model in the 2D views.
// Input  : pRender - Interface to the 2D renderer.
//-----------------------------------------------------------------------------
void CMapStudioModel::Render2D(CRender2D *pRender)
{
	Vector vecMins;
	Vector vecMaxs;
	GetRender2DBox(vecMins, vecMaxs);

	Vector2D pt,pt2;
	pRender->TransformPoint(pt, vecMins);
	pRender->TransformPoint(pt2, vecMaxs);

	color32 rgbColor = GetRenderColor();
	bool	bIsEditable = IsEditable();

	if (GetSelectionState() != SELECT_NONE)
	{
		pRender->SetDrawColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
		pRender->SetHandleColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
	}
	else
	{
		pRender->SetDrawColor( rgbColor.r, rgbColor.g, rgbColor.b );
		pRender->SetHandleColor( rgbColor.r, rgbColor.g, rgbColor.b );
	}

	int sizeX = abs(pt2.x-pt.x);
	int sizeY = abs(pt2.y-pt.y);

	//
	// Don't draw the center handle if the model is smaller than the handle cross 	
	//
	if ( bIsEditable && sizeX >= 8 && sizeY >= 8 && pRender->IsActiveView() )
	{
		pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_CROSS );

		pRender->DrawHandle( (vecMins+vecMaxs)/2 );
	}
	
	QAngle vecAngles;
	GetRenderAngles(vecAngles);

	bool bDrawAsModel = (Options.view2d.bDrawModels && ((sizeX+sizeY) > 50)) ||	
						IsSelected() ||	( pRender->IsInLocalTransformMode() && !pRender->GetInstanceRendering() );
						
	if ( !bDrawAsModel || IsSelected() )
	{
		// Draw the bounding box.
		pRender->DrawBox( vecMins, vecMaxs );
	}

	if ( bDrawAsModel )
	{
		//
		// Draw the model as wireframe.
		//

		m_pStudioModel->SetAngles(vecAngles);
		m_pStudioModel->SetOrigin(m_Origin[0], m_Origin[1], m_Origin[2]);
		m_pStudioModel->SetSkin(m_Skin);

		if ( GetSelectionState() == SELECT_NORMAL || ( pRender->IsInLocalTransformMode() && pRender->GetInstanceRendering() == false ) )
		{
 			// draw textured model half translucent
			m_pStudioModel->DrawModel2D(pRender, 0.6 , false );
		}
		else
		{
			// just draw the wireframe 
			m_pStudioModel->DrawModel2D(pRender, 1.0 , true );
		}
	}

	if ( IsSelected() )
	{
		//
		// Render the forward vector if the object is selected.
		//
		
		Vector Forward;
		AngleVectors(vecAngles, &Forward, NULL, NULL);

		pRender->SetDrawColor( 255, 255, 0 );
		pRender->DrawLine(m_Origin, m_Origin + Forward * 24);
	}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline float CMapStudioModel::ComputeDistanceFade( CRender3D *pRender ) const
{
	Vector vecViewPos;
	pRender->GetCamera()->GetViewPoint( vecViewPos );

	Vector vecDelta;		
	vecDelta = m_Origin - vecViewPos;

	float flMin = min(m_flFadeMinDist, m_flFadeMaxDist);
	float flMax = max(m_flFadeMinDist, m_flFadeMaxDist);

	if (flMin < 0)
	{
		flMin = 0;
	}

	float alpha = 1.0f;
	if (flMax > 0)
	{
		float flDist = vecDelta.Length();
		if ( flDist > flMax )
		{
			alpha = 0.0f;
		}
		else if ( flDist > flMin )
		{
			alpha = RemapValClamped( flDist, flMin, flMax, 1.0f, 0 );
		}
	}
		
	return alpha;
}


//-----------------------------------------------------------------------------
// Computes fade alpha based on distance fade + screen fade
//-----------------------------------------------------------------------------
inline float CMapStudioModel::ComputeScreenFade( CRender3D *pRender ) const
{
	return 1.0;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline float CMapStudioModel::ComputeFade( CRender3D *pRender ) const
{
	if ( m_bScreenSpaceFade )
	{
		return ComputeScreenFade( pRender );
	}
	else
	{
		return ComputeDistanceFade( pRender );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Renders the studio model in the 3D views.
// Input  : pRender - Interface to the 3D renderer.
//-----------------------------------------------------------------------------
void CMapStudioModel::Render3D(CRender3D *pRender)
{
	Color CurrentColor;
	CurrentColor.SetColor( r, g, b );

	//
	// Set to the default rendering mode, unless we're in lightmap mode
	//
	if (pRender->GetCurrentRenderMode() == RENDER_MODE_LIGHTMAP_GRID)
		pRender->PushRenderMode(RENDER_MODE_TEXTURED);
	else
		pRender->PushRenderMode(RENDER_MODE_CURRENT);

	//
	// Set up our angles for rendering.
	//
	QAngle vecAngles;
	GetRenderAngles(vecAngles);

	//
	// If we have a model, render it if it is close enough to the camera.
	//
	if (m_pStudioModel != NULL)
	{
		Vector ViewPoint;
		pRender->GetCamera()->GetViewPoint(ViewPoint);

		Vector	Origin( m_Origin );
		if ( pRender->GetInstanceRendering() )
		{
			pRender->TransformInstanceVector( m_Origin, Origin );
		}

		if ((fabs(ViewPoint[0] - Origin[0]) < m_fRenderDistance) &&
			(fabs(ViewPoint[1] - Origin[1]) < m_fRenderDistance) &&
			(fabs(ViewPoint[2] - Origin[2]) < m_fRenderDistance))
		{
			color32 rgbColor = GetRenderColor();

			if (GetSelectionState() != SELECT_NONE)
			{
				pRender->SetDrawColor( GetRValue(Options.colors.clrSelection), GetGValue(Options.colors.clrSelection), GetBValue(Options.colors.clrSelection) );
			}
			else
			{
				// If the user disabled collisions on this instance of the model, color the wireframe differently
				if ( m_iSolid != -1 )
				{
					if ( m_iSolid == 0 )
					{
						rgbColor.r = GetRValue( Options.colors.clrModelCollisionWireframeDisabled );
						rgbColor.g = GetGValue( Options.colors.clrModelCollisionWireframeDisabled );
						rgbColor.b = GetBValue( Options.colors.clrModelCollisionWireframeDisabled );
						rgbColor.a = 255;
					}
					else
					{
						rgbColor.r = GetRValue( Options.colors.clrModelCollisionWireframe );
						rgbColor.g = GetGValue( Options.colors.clrModelCollisionWireframe );
						rgbColor.b = GetBValue( Options.colors.clrModelCollisionWireframe );
						rgbColor.a = 255;
					}
				}
				pRender->SetDrawColor( rgbColor.r, rgbColor.g, rgbColor.b );
			}

			//
			// Move the model to the proper place and orient it.
			//
			m_pStudioModel->SetAngles(vecAngles);
			m_pStudioModel->SetOrigin(m_Origin[0], m_Origin[1], m_Origin[2]);
			m_pStudioModel->SetSkin(m_Skin);

			float flAlpha = 1.0;
			if ( Options.view3d.bPreviewModelFade )
			{
				flAlpha = ComputeFade( pRender );
			}

			bool bWireframe = pRender->GetCurrentRenderMode() == RENDER_MODE_WIREFRAME;
 
			if ( GetSelectionState() == SELECT_MODIFY )
				bWireframe = true;

			pRender->BeginRenderHitTarget(this);
			m_pStudioModel->DrawModel3D(pRender, flAlpha, bWireframe );
			pRender->EndRenderHitTarget();

			if (IsSelected())
			{
				pRender->RenderWireframeBox(m_Render2DBox.bmins, m_Render2DBox.bmaxs, 255, 255, 0);
			}
		}
		else
		{
			pRender->BeginRenderHitTarget(this);
			pRender->RenderBox(m_Render2DBox.bmins, m_Render2DBox.bmaxs, CurrentColor.r(), CurrentColor.g(), CurrentColor.b(), GetSelectionState());
			pRender->EndRenderHitTarget();
		}
	}
	//
	// Else no model, render as a bounding box.
	//
	else
	{
		pRender->BeginRenderHitTarget(this);
		pRender->RenderBox(m_Render2DBox.bmins, m_Render2DBox.bmaxs, CurrentColor.r(), CurrentColor.g(), CurrentColor.b(), GetSelectionState());
		pRender->EndRenderHitTarget();
	}

	//
	// Draw our basis vectors.
	//
	if (IsSelected())
	{
		DrawBasisVectors( pRender, m_Origin, vecAngles );
	}

	pRender->PopRenderMode();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &File - 
//			bRMF - 
// Output : int
//-----------------------------------------------------------------------------
int CMapStudioModel::SerializeRMF(std::fstream &File, BOOL bRMF)
{
	return(0);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &File - 
//			bRMF - 
// Output : int
//-----------------------------------------------------------------------------
int CMapStudioModel::SerializeMAP(std::fstream &File, BOOL bRMF)
{
	return(0);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : Angles - 
//-----------------------------------------------------------------------------
void CMapStudioModel::SetAngles(QAngle &Angles)
{
	m_Angles = Angles;

	//
	// Round very small angles to zero.
	//
	for (int nDim = 0; nDim < 3; nDim++)
	{
		if (fabs(m_Angles[nDim]) < 0.001)
		{
			m_Angles[nDim] = 0;
		}
	}

	while (m_Angles[YAW] < 0)
	{
		m_Angles[YAW] += 360;
	}

	if (m_bPitchSet)
	{
		m_flPitch = m_Angles[PITCH];
	}

	//
	// Update the angles of our parent entity.
	//
	CMapEntity *pEntity = dynamic_cast<CMapEntity *>(m_pParent);
	if (pEntity != NULL)
	{
		char szValue[80];
		sprintf(szValue, "%g %g %g", m_Angles[0], m_Angles[1], m_Angles[2]);
		pEntity->NotifyChildKeyChanged(this, "angles", szValue);

		if (m_bPitchSet)
		{
			sprintf(szValue, "%g", m_flPitch);
			pEntity->NotifyChildKeyChanged(this, "pitch", szValue);
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Sets the distance at which studio models become rendered as bounding
//			boxes. If this is set to zero, studio models are never rendered.
// Input  : fRenderDistance - Distance in world units.
//-----------------------------------------------------------------------------
void CMapStudioModel::SetRenderDistance(float fRenderDistance)
{
	m_fRenderDistance = fRenderDistance;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pTransBox - 
//-----------------------------------------------------------------------------
void CMapStudioModel::DoTransform(const VMatrix &matrix)
{
	BaseClass::DoTransform(matrix);

	// rotate model angles

	matrix3x4_t fRotateMatrix, fCurrentMatrix, fMatrixNew;
	fRotateMatrix = matrix.As3x4();

	// Light entities negate pitch again!
	if ( m_bReversePitch )
	{
		QAngle rotAngles;
		MatrixAngles(fRotateMatrix, rotAngles);
		rotAngles[PITCH] *= -1;
		rotAngles[ROLL] *= -1;
		AngleMatrix(rotAngles, fRotateMatrix);
	}

	QAngle angles;
	GetAngles( angles );
	
	AngleMatrix( angles, fCurrentMatrix);
	ConcatTransforms(fRotateMatrix, fCurrentMatrix, fMatrixNew);
	MatrixAngles( fMatrixNew, angles );

	SetAngles( angles );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : int
//-----------------------------------------------------------------------------
int CMapStudioModel::GetFrame(void)
{
	// TODO:
	return 0;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nFrame - 
//-----------------------------------------------------------------------------
void CMapStudioModel::SetFrame(int nFrame)
{
	// TODO:
}


//-----------------------------------------------------------------------------
// Purpose: Returns the current sequence being used for rendering.
//-----------------------------------------------------------------------------
int CMapStudioModel::GetSequence(void)
{
	if (!m_pStudioModel)
	{
		return 0;
	}
	return m_pStudioModel->GetSequence();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Output : int
//-----------------------------------------------------------------------------
int CMapStudioModel::GetSequenceCount(void)
{
	if (!m_pStudioModel)
	{
		return 0;
	}
	return m_pStudioModel->GetSequenceCount();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nIndex - 
//			szName - 
//-----------------------------------------------------------------------------
void CMapStudioModel::GetSequenceName(int nIndex, char *szName)
{
	if (m_pStudioModel)
	{
		m_pStudioModel->GetSequenceName(nIndex, szName);
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nIndex - 
//-----------------------------------------------------------------------------
void CMapStudioModel::SetSequence(int nIndex)
{
	if (m_pStudioModel)
	{
		m_pStudioModel->SetSequence(nIndex);
	}
}


int CMapStudioModel::GetSequenceIndex( const char *pSequenceName ) const
{
	if ( m_pStudioModel )
	{
		int cnt = m_pStudioModel->GetSequenceCount();
		for ( int i=0; i < cnt; i++ )
		{
			char name[2048];
			m_pStudioModel->GetSequenceName( i, name );
			if ( Q_stricmp( pSequenceName, name ) == 0 )
				return i;
		}
	}

	return -1;
}