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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Renders a cone for spotlight entities. Only renders when the parent
// entity is selected.
//
//=============================================================================//
#include "stdafx.h"
#include "Box3D.h"
#include "fgdlib/HelperInfo.h"
#include "MapDefs.h" // dvs: For COORD_NOTINIT
#include "MapEntity.h"
#include "MapFrustum.h"
#include "Render3D.h"
#include "Material.h"
#include "materialsystem/imaterialsystem.h"
#include "TextureSystem.h"
#include "hammer.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
IMPLEMENT_MAPCLASS(CMapFrustum)
//-----------------------------------------------------------------------------
// Purpose: Factory function. Used for creating a CMapFrustum helper 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 helper.
// Output : Returns a pointer to the helper, NULL if an error occurs.
//-----------------------------------------------------------------------------
CMapClass *CMapFrustum::Create(CHelperInfo *pHelperInfo, CMapEntity *pParent)
{
CMapFrustum *new1 = new CMapFrustum;
if( new1 != NULL )
{
const char *pszKeyName;
// The first parameter should be the fov key name.
pszKeyName = pHelperInfo->GetParameter(0);
if ( pszKeyName )
V_strncpy( new1->m_szFOVKeyName, pszKeyName, sizeof( new1->m_szFOVKeyName ) );
// Second parameter should be the near plane name.
pszKeyName = pHelperInfo->GetParameter(1);
if ( pszKeyName )
V_strncpy( new1->m_szNearPlaneKeyName, pszKeyName, sizeof( new1->m_szNearPlaneKeyName ) );
// Third parameter should be the far plane name.
pszKeyName = pHelperInfo->GetParameter(2);
if ( pszKeyName )
V_strncpy( new1->m_szFarPlaneKeyName, pszKeyName, sizeof( new1->m_szFarPlaneKeyName ) );
pszKeyName = pHelperInfo->GetParameter(3);
if (pszKeyName != NULL)
{
V_strncpy( new1->m_szColorKeyName, pszKeyName, sizeof( new1->m_szColorKeyName ) );
}
pszKeyName = pHelperInfo->GetParameter(4);
if (pszKeyName != NULL)
{
new1->m_flPitchScale = Q_atof( pszKeyName );
}
else
{
new1->m_flPitchScale = 1.0f;
}
}
return new1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMapFrustum::CMapFrustum(void)
{
// Set default parameter names.
V_strncpy( m_szFOVKeyName, "_fov", sizeof( m_szFOVKeyName ) );
V_strncpy( m_szNearPlaneKeyName, "_NearPlane", sizeof( m_szNearPlaneKeyName ) );
V_strncpy( m_szFarPlaneKeyName, "_FarPlane", sizeof( m_szFarPlaneKeyName ) );
V_strncpy( m_szColorKeyName, "_light", sizeof( m_szColorKeyName ) );
m_flFOV = 90;
m_flNearPlane = 10;
m_flFarPlane = 200;
m_flPitchScale = -1;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor. Deletes faces allocated by BuildCone.
//-----------------------------------------------------------------------------
CMapFrustum::~CMapFrustum(void)
{
for (int i = 0; i < m_Faces.Count(); i++)
{
CMapFace *pFace = m_Faces.Element(i);
delete pFace;
}
}
CMapFace* CMapFrustum::CreateMapFace( const Vector &v1, const Vector &v2, const Vector &v3, const Vector &v4, float flAlpha )
{
Assert( IsFinite(v1.x) && IsFinite(v1.y) && IsFinite(v1.z) );
Vector points[4] = {v1,v2,v3,v4};
CMapFace *pFace = new CMapFace;
pFace->SetRenderColor( r, g, b );
pFace->SetRenderAlpha( flAlpha );
pFace->CreateFace( points, 4 );
pFace->RenderUnlit(true);
return pFace;
}
//-----------------------------------------------------------------------------
// Purpose: Builds the light cone faces in local space. Does NOT call CalcBounds,
// because that CalcBounds updates the parent, which causes problems
// in the undo system.
//-----------------------------------------------------------------------------
void CMapFrustum::BuildFrustumFaces(void)
{
//
// Delete the current face list.
//
for (int i = 0; i < m_Faces.Count(); i++)
{
CMapFace *pFace = m_Faces.Element(i);
delete pFace;
}
m_Faces.RemoveAll();
// 6 total faces. 4 on the sides and 2 caps.
Vector vNearFace[4], vFarFace[4];
float flHalfFOV = m_flFOV / 2.0f;
flHalfFOV = clamp( flHalfFOV, 0.01f, 89.0f );
float flScaleFactor = tan( DEG2RAD( flHalfFOV ) );
float flBaseAlpha = 180;
vNearFace[0].Init( m_flNearPlane, -flScaleFactor*m_flNearPlane, -flScaleFactor*m_flNearPlane );
vNearFace[1].Init( m_flNearPlane, +flScaleFactor*m_flNearPlane, -flScaleFactor*m_flNearPlane );
vNearFace[2].Init( m_flNearPlane, +flScaleFactor*m_flNearPlane, +flScaleFactor*m_flNearPlane );
vNearFace[3].Init( m_flNearPlane, -flScaleFactor*m_flNearPlane, +flScaleFactor*m_flNearPlane );
vFarFace[0].Init( m_flFarPlane, -flScaleFactor*m_flFarPlane, -flScaleFactor*m_flFarPlane );
vFarFace[1].Init( m_flFarPlane, +flScaleFactor*m_flFarPlane, -flScaleFactor*m_flFarPlane );
vFarFace[2].Init( m_flFarPlane, +flScaleFactor*m_flFarPlane, +flScaleFactor*m_flFarPlane );
vFarFace[3].Init( m_flFarPlane, -flScaleFactor*m_flFarPlane, +flScaleFactor*m_flFarPlane );
// Build the near and far faces.
m_Faces.AddToTail( CreateMapFace( vNearFace[0], vNearFace[1], vNearFace[2], vNearFace[3], flBaseAlpha ) );
m_Faces.AddToTail( CreateMapFace( vFarFace[3], vFarFace[2], vFarFace[1], vFarFace[0], flBaseAlpha ) );
// Build the 4 cap faces.
for ( int i=0; i < 4; i++ )
{
m_Faces.AddToTail( CreateMapFace( vNearFace[i], vFarFace[i], vFarFace[(i+1)%4], vNearFace[(i+1)%4], flBaseAlpha ) );
}
// Also build some really translucent faces from the origin to the near plane.
float flOriginFacesAlpha = 40.0f;
for ( int i=0; i < 4; i++ )
{
m_Faces.AddToTail( CreateMapFace( Vector(0,0,0), vNearFace[i], vNearFace[(i+1)%4], Vector(0,0,0), flOriginFacesAlpha ) );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bFullUpdate -
//-----------------------------------------------------------------------------
void CMapFrustum::CalcBounds(BOOL bFullUpdate)
{
CMapClass::CalcBounds(bFullUpdate);
//
// HACK: Update our origin to stick to our parent.
//
if (m_pParent != NULL)
{
GetParent()->GetOrigin(m_Origin);
}
//
// Pretend to be very small for the 2D view. Won't be necessary when 2D
// rendering is done in the map classes.
//
m_Render2DBox.ResetBounds();
m_Render2DBox.UpdateBounds(m_Origin);
SetCullBoxFromFaceList( &m_Faces );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : CMapClass
//-----------------------------------------------------------------------------
CMapClass *CMapFrustum::Copy(bool bUpdateDependencies)
{
CMapFrustum *pCopy = new CMapFrustum;
if (pCopy != NULL)
{
pCopy->CopyFrom(this, bUpdateDependencies);
}
return(pCopy);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pObject -
// Output : CMapClass
//-----------------------------------------------------------------------------
CMapClass *CMapFrustum::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
{
Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapFrustum)));
CMapFrustum *pFrom = (CMapFrustum *)pObject;
CMapClass::CopyFrom(pObject, bUpdateDependencies);
m_flFOV = pFrom->m_flFOV;
m_flNearPlane = pFrom->m_flNearPlane;
m_flFarPlane = pFrom->m_flFarPlane;
m_Angles = pFrom->m_Angles;
m_flPitchScale = pFrom->m_flPitchScale;
V_strncpy( m_szFOVKeyName, pFrom->m_szFOVKeyName, sizeof( m_szFOVKeyName ) );
V_strncpy( m_szNearPlaneKeyName, pFrom->m_szNearPlaneKeyName, sizeof( m_szNearPlaneKeyName ) );
V_strncpy( m_szFarPlaneKeyName, pFrom->m_szFarPlaneKeyName, sizeof( m_szFarPlaneKeyName ) );
V_strncpy( m_szColorKeyName, pFrom->m_szColorKeyName, sizeof( m_szColorKeyName ) );
BuildFrustumFaces();
return(this);
}
//-----------------------------------------------------------------------------
// 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 CMapFrustum::OnParentKeyChanged(const char *szKey, const char *szValue)
{
bool bRebuild = true;
if (!stricmp(szKey, "angles"))
{
sscanf(szValue, "%f %f %f", &m_Angles[PITCH], &m_Angles[YAW], &m_Angles[ROLL]);
}
else if (!stricmp(szKey, m_szColorKeyName))
{
int nRed;
int nGreen;
int nBlue;
sscanf(szValue, "%d %d %d", &nRed, &nGreen, &nBlue);
r = nRed;
g = nGreen;
b = nBlue;
}
else if (!stricmp(szKey, m_szFOVKeyName))
{
m_flFOV = atof(szValue);
}
else if (!stricmp(szKey, m_szNearPlaneKeyName))
{
m_flNearPlane = atof(szValue);
}
else if (!stricmp(szKey, m_szFarPlaneKeyName))
{
m_flFarPlane = atof(szValue);
}
else
{
bRebuild = false;
}
if (bRebuild)
{
BuildFrustumFaces();
PostUpdate(Notify_Changed);
}
}
//-----------------------------------------------------------------------------
// Purpose: Called after the entire map has been loaded. This allows the object
// to perform any linking with other map objects or to do other operations
// that require all world objects to be present.
// Input : pWorld - The world that we are in.
//-----------------------------------------------------------------------------
void CMapFrustum::PostloadWorld(CMapWorld *pWorld)
{
CMapClass::PostloadWorld(pWorld);
BuildFrustumFaces();
CalcBounds();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pRender -
//-----------------------------------------------------------------------------
void CMapFrustum::Render3D(CRender3D *pRender)
{
if (m_pParent->IsSelected())
{
CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
pRenderContext->MatrixMode(MATERIAL_MODEL);
pRenderContext->PushMatrix();
pRenderContext->Translate(m_Origin[0], m_Origin[1], m_Origin[2]);
QAngle Angles;
GetAngles(Angles);
pRenderContext->Rotate(Angles[YAW], 0, 0, 1);
pRenderContext->Rotate(m_flPitchScale * Angles[PITCH], 0, -1, 0);
pRenderContext->Rotate(Angles[ROLL], 1, 0, 0);
if (
(pRender->GetCurrentRenderMode() != RENDER_MODE_LIGHT_PREVIEW2) &&
(pRender->GetCurrentRenderMode() != RENDER_MODE_LIGHT_PREVIEW_RAYTRACED) &&
(GetSelectionState() != SELECT_MODIFY )
)
{
// Render the cone faces flatshaded.
pRender->PushRenderMode( RENDER_MODE_TRANSLUCENT_FLAT );
for (int i = 0; i < m_Faces.Count(); i++)
{
CMapFace *pFace = m_Faces.Element(i);
pFace->Render3D(pRender);
}
pRender->PopRenderMode();
}
//
// Render the cone faces in yellow wireframe (on top)
//
pRender->PushRenderMode( RENDER_MODE_WIREFRAME );
for (int i = 0; i < m_Faces.Count(); i++)
{
CMapFace *pFace = m_Faces.Element(i);
pFace->Render3D(pRender);
}
//
// Restore the default rendering mode.
//
pRender->PopRenderMode();
pRenderContext->PopMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : File -
// bRMF -
// Output : int
//-----------------------------------------------------------------------------
int CMapFrustum::SerializeRMF(std::fstream &File, BOOL bRMF)
{
return(0);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : File -
// bRMF -
// Output : int
//-----------------------------------------------------------------------------
int CMapFrustum::SerializeMAP(std::fstream &File, BOOL bRMF)
{
return(0);
}
void CMapFrustum::GetAngles(QAngle& Angles)
{
Angles = m_Angles;
}
|