File: Frustum.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (412 lines) | stat: -rw-r--r-- 13,513 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#include "hpl1/engine/math/Frustum.h"

#include "hpl1/engine/graphics/LowLevelGraphics.h"
#include "hpl1/engine/math/Math.h"
#include "hpl1/engine/system/low_level_system.h"

namespace hpl {

//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cFrustum::cFrustum() {
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

void cFrustum::SetViewProjMatrix(const cMatrixf &a_mtxProj, const cMatrixf &a_mtxView,
								 float afFarPlane, float afNearPlane, float afFOV, float afAspect,
								 const cVector3f &avOrigin, bool abInfFarPlane) {
	m_mtxViewProj = cMath::MatrixMul(a_mtxProj, a_mtxView);
	m_mtxModelView = a_mtxView;

	mfFarPlane = afFarPlane;
	mfNearPlane = afNearPlane;
	mfFOV = afFOV;
	mfAspect = afAspect;

	mvOrigin = avOrigin;

	mbInfFarPlane = abInfFarPlane;

	// This could be made more accurate.
	mOriginBV.SetSize(afNearPlane * 2);
	mOriginBV.SetPosition(mvOrigin);

	UpdatePlanes();
	UpdateSphere();
	UpdateEndPoints();
	UpdateBV();
}

//-----------------------------------------------------------------------

cPlanef cFrustum::GetPlane(eFrustumPlane aType) {
	return mPlane[aType];
}

//-----------------------------------------------------------------------

eFrustumCollision cFrustum::CollideBoundingVolume(cBoundingVolume *aBV) {
	// Check if the BV is in the Frustum sphere.
	if (CollideFustrumSphere(aBV) == eFrustumCollision_Outside) {
		return eFrustumCollision_Outside;
	}

	// Do a simple sphere collide test
	eFrustumCollision ret = CollideBVSphere(aBV);

	// If there was an intersection, collide with the AABB
	if (ret == eFrustumCollision_Intersect) {
		return CollideBVAABB(aBV);
	}

	return ret;
}

//-----------------------------------------------------------------------

bool cFrustum::CheckLineIntersection(const cVector3f &avPoint1, const cVector3f &avPoint2) {
	return cMath::CheckFrustumLineIntersection(&mPlane[0], avPoint1, avPoint2, 3);
}

//-----------------------------------------------------------------------

bool cFrustum::CheckQuadMeshIntersection(tVector3fVec *apPoints) {
	return cMath::CheckFrustumQuadMeshIntersection(&mPlane[0], apPoints, 3);
}

//-----------------------------------------------------------------------

bool cFrustum::CheckVolumeIntersection(cShadowVolumeBV *apVolume) {
	if (CheckQuadMeshIntersection(&apVolume->mvPoints)) {
		return true;
	}

	// The first shadow plane is the near plane which we can skip.
	int lPairNum = (apVolume->mlPlaneCount - 1) / 2;

	// Check with volume planes.
	for (int i = 0; i < 4; ++i) {
		if (cMath::CheckFrustumLineIntersection(&apVolume->mvPlanes[1], mvOrigin, mvEndPoints[i],
												lPairNum)) {
			return true;
		}
	}

	return false;
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

eFrustumCollision cFrustum::CollideFustrumSphere(cBoundingVolume *aBV) {
	float fRadiusSum = mBoundingSphere.r + aBV->GetRadius();
	cVector3f vSepAxis = mBoundingSphere.center - aBV->GetWorldCenter();
	if (vSepAxis.SqrLength() > (fRadiusSum * fRadiusSum)) {
		return eFrustumCollision_Outside;
	}

	return eFrustumCollision_Intersect;
}

//-----------------------------------------------------------------------

eFrustumCollision cFrustum::CollideBVSphere(cBoundingVolume *aBV) {
	int lPlanes = 6;
	if (mbInfFarPlane)
		lPlanes = 5;

	for (int i = 0; i < lPlanes; i++) {
		float fDist = cMath::PlaneToPointDist(mPlane[i], aBV->GetWorldCenter());

		if (fDist < -aBV->GetRadius()) {
			return eFrustumCollision_Outside;
		}

		if (ABS(fDist) < aBV->GetRadius()) {
			return eFrustumCollision_Intersect;
		}
	}

	return eFrustumCollision_Inside;
}
//-----------------------------------------------------------------------

eFrustumCollision cFrustum::CollideBVAABB(cBoundingVolume *aBV) {
	cVector3f vMax = aBV->GetMax();
	cVector3f vMin = aBV->GetMin();

	// Get the corners from the AAB
	cVector3f vCorners[9] = {
		cVector3f(vMax.x, vMax.y, vMax.z),
		cVector3f(vMax.x, vMax.y, vMin.z),
		cVector3f(vMax.x, vMin.y, vMax.z),
		cVector3f(vMax.x, vMin.y, vMin.z),

		cVector3f(vMin.x, vMax.y, vMax.z),
		cVector3f(vMin.x, vMax.y, vMin.z),
		cVector3f(vMin.x, vMin.y, vMax.z),
		cVector3f(vMin.x, vMin.y, vMin.z),

		// The "fuling", add center as well...
		aBV->GetPosition()};

	int lTotalIn = 0;

	int lPlanes = 6;
	if (mbInfFarPlane)
		lPlanes = 5;

	// Go through all the planes
	for (int i = 0; i < lPlanes; i++) {
		int lInCount = 9;
		bool bIsIn = true;

		for (int j = 0; j < 9; j++) {
			float fDist = cMath::PlaneToPointDist(mPlane[i], vCorners[j]);
			if (fDist < 0) {
				lInCount--;
				bIsIn = false;
			}
		}

		if (lInCount == 0)
			return eFrustumCollision_Outside;
		if (bIsIn)
			lTotalIn++;
	}

	if (lTotalIn == lPlanes)
		return eFrustumCollision_Inside;

	return eFrustumCollision_Intersect;
}

//-----------------------------------------------------------------------

void cFrustum::UpdateSphere() {
	// calculate the radius of the frustum sphere
	float fViewLen = mfFarPlane - mfNearPlane;

	float fHeight = fViewLen * tan(mfFOV * 0.5f);
	float fWidth = fHeight * mfAspect;

	// halfway point between near/far planes starting at the origin and extending along the z axis
	cVector3f P(0.0f, 0.0f, mfNearPlane + fViewLen * 0.5f);

	// the calculate far corner of the frustum
	cVector3f Q(fWidth, fHeight, fViewLen);

	// the vector between P and Q
	cVector3f vDiff = P - Q;

	// the radius becomes the length of this vector
	float fRadius = vDiff.Length();

	// get the look vector of the camera from the view matrix
	cVector3f vLookVector = m_mtxModelView.GetForward() * -1;

	// calculate the center of the sphere
	cVector3f vCenter = (mvOrigin) + (vLookVector * (fViewLen * 0.5f + mfNearPlane));

	mBoundingSphere = cSpheref(vCenter, fRadius);
}

//-----------------------------------------------------------------------

void cFrustum::UpdatePlanes() {
	// Left
	mPlane[eFrustumPlane_Left] = cPlanef(m_mtxViewProj.m[3][0] + m_mtxViewProj.m[0][0],
										 m_mtxViewProj.m[3][1] + m_mtxViewProj.m[0][1],
										 m_mtxViewProj.m[3][2] + m_mtxViewProj.m[0][2],
										 m_mtxViewProj.m[3][3] + m_mtxViewProj.m[0][3]);

	// Right
	mPlane[eFrustumPlane_Right] = cPlanef(m_mtxViewProj.m[3][0] - m_mtxViewProj.m[0][0],
										  m_mtxViewProj.m[3][1] - m_mtxViewProj.m[0][1],
										  m_mtxViewProj.m[3][2] - m_mtxViewProj.m[0][2],
										  m_mtxViewProj.m[3][3] - m_mtxViewProj.m[0][3]);

	// Bottom
	mPlane[eFrustumPlane_Bottom] = cPlanef(m_mtxViewProj.m[3][0] + m_mtxViewProj.m[1][0],
										   m_mtxViewProj.m[3][1] + m_mtxViewProj.m[1][1],
										   m_mtxViewProj.m[3][2] + m_mtxViewProj.m[1][2],
										   m_mtxViewProj.m[3][3] + m_mtxViewProj.m[1][3]);

	// Top
	mPlane[eFrustumPlane_Top] = cPlanef(m_mtxViewProj.m[3][0] - m_mtxViewProj.m[1][0],
										m_mtxViewProj.m[3][1] - m_mtxViewProj.m[1][1],
										m_mtxViewProj.m[3][2] - m_mtxViewProj.m[1][2],
										m_mtxViewProj.m[3][3] - m_mtxViewProj.m[1][3]);

	// Near
	mPlane[eFrustumPlane_Near] = cPlanef(m_mtxViewProj.m[3][0] + m_mtxViewProj.m[2][0],
										 m_mtxViewProj.m[3][1] + m_mtxViewProj.m[2][1],
										 m_mtxViewProj.m[3][2] + m_mtxViewProj.m[2][2],
										 m_mtxViewProj.m[3][3] + m_mtxViewProj.m[2][3]);
	// Far
	mPlane[eFrustumPlane_Far] = cPlanef(m_mtxViewProj.m[3][0] - m_mtxViewProj.m[2][0],
										m_mtxViewProj.m[3][1] - m_mtxViewProj.m[2][1],
										m_mtxViewProj.m[3][2] - m_mtxViewProj.m[2][2],
										m_mtxViewProj.m[3][3] - m_mtxViewProj.m[2][3]);

	for (int i = 0; i < 6; i++) {
		mPlane[i].Normalise();
		mPlane[i].CalcNormal();
	}
}

//-----------------------------------------------------------------------

void cFrustum::UpdateEndPoints() {
	float fXAngle = mfFOV * 0.5f;
	float fYAngle = mfFOV * 0.5f * mfAspect;

	cVector3f vForward = m_mtxModelView.GetForward();
	// cVector3f vUp = m_mtxModelView.GetUp();
	cVector3f vRight = m_mtxModelView.GetRight();

	// Point the forward vec in different dirs.
	cVector3f vUpDir = cMath::MatrixMul(cMath::MatrixQuaternion(cQuaternion(fXAngle, vRight)), vForward);
	cVector3f vDownDir = cMath::MatrixMul(cMath::MatrixQuaternion(cQuaternion(-fXAngle, vRight)), vForward);

	// cVector3f vRightDir = cMath::MatrixMul(cMath::MatrixQuaternion(cQuaternion(fYAngle,vUp)), vForward);
	// cVector3f vLeftDir = cMath::MatrixMul(cMath::MatrixQuaternion(cQuaternion(-fYAngle,vUp)), vForward);

	vForward = vForward * -1;

	float fRightAdd = sin(fYAngle);
	// float fUpAdd = sin(fXAngle);

	cVector3f vVec0 = vUpDir + vRight * fRightAdd;
	cVector3f vVec2 = vDownDir + vRight * fRightAdd;
	cVector3f vVec3 = vDownDir + vRight * -fRightAdd;
	cVector3f vVec1 = vUpDir + vRight * -fRightAdd;

	/*cVector3f vVec0 = vUpDir;//cMath::Vector3Normalize(vUpDir+vRightDir);
	cVector3f vVec1 = vRightDir;//cMath::Vector3Normalize(vUpDir+vLeftDir);
	cVector3f vVec2 = vDownDir;//cMath::Vector3Normalize(vDownDir+vLeftDir);
	cVector3f vVec3 = vLeftDir;//cMath::Vector3Normalize(vDownDir+vRightDir);*/

	// angles between forward and the vectors
	float fAngle0 = cMath::Vector3Angle(vVec0, vForward);
	float fAngle1 = cMath::Vector3Angle(vVec1, vForward);
	float fAngle2 = cMath::Vector3Angle(vVec2, vForward);
	float fAngle3 = cMath::Vector3Angle(vVec3, vForward);

	// create end points.
	mvEndPoints[0] = mvOrigin + vVec0 * (mfFarPlane / cos(fAngle0));
	mvEndPoints[1] = mvOrigin + vVec1 * (mfFarPlane / cos(fAngle1));
	mvEndPoints[2] = mvOrigin + vVec2 * (mfFarPlane / cos(fAngle2));
	mvEndPoints[3] = mvOrigin + vVec3 * (mfFarPlane / cos(fAngle3));

	/*mvEndPoints[0] = mvOrigin + (vUpDir+vRightDir)	 * mfFarPlane *-1;
	mvEndPoints[1] = mvOrigin + (vUpDir+vLeftDir)	 * mfFarPlane*-1;
	mvEndPoints[2] = mvOrigin + (vDownDir+vRightDir)* mfFarPlane*-1;
	mvEndPoints[3] = mvOrigin + (vDownDir+vLeftDir) * mfFarPlane*-1;*/
}

//-----------------------------------------------------------------------

void cFrustum::UpdateBV() {
	cVector3f vMin = mvOrigin;
	cVector3f vMax = mvOrigin;

	for (int i = 0; i < 4; i++) {
		if (vMax.x < mvEndPoints[i].x)
			vMax.x = mvEndPoints[i].x;
		else if (vMin.x > mvEndPoints[i].x)
			vMin.x = mvEndPoints[i].x;

		if (vMax.y < mvEndPoints[i].y)
			vMax.y = mvEndPoints[i].y;
		else if (vMin.y > mvEndPoints[i].y)
			vMin.y = mvEndPoints[i].y;

		if (vMax.z < mvEndPoints[i].z)
			vMax.z = mvEndPoints[i].z;
		else if (vMin.z > mvEndPoints[i].z)
			vMin.z = mvEndPoints[i].z;
	}

	mBoundingVolume.SetLocalMinMax(vMin, vMax);
}

//-----------------------------------------------------------------------

const cVector3f &cFrustum::GetOrigin() {
	return mvOrigin;
}

//-----------------------------------------------------------------------

cBoundingVolume *cFrustum::GetOriginBV() {
	return &mOriginBV;
}

//-----------------------------------------------------------------------

cVector3f cFrustum::GetForward() {
	return m_mtxModelView.GetForward();
}
//-----------------------------------------------------------------------

void cFrustum::Draw(iLowLevelGraphics *apLowLevelGraphics) {
	apLowLevelGraphics->DrawLine(mvOrigin, mvEndPoints[0], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvOrigin, mvEndPoints[1], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvOrigin, mvEndPoints[2], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvOrigin, mvEndPoints[3], cColor(1, 1, 1, 1));

	apLowLevelGraphics->DrawLine(mvEndPoints[0], mvEndPoints[1], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvEndPoints[1], mvEndPoints[2], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvEndPoints[2], mvEndPoints[3], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvEndPoints[3], mvEndPoints[0], cColor(1, 1, 1, 1));

	apLowLevelGraphics->DrawLine(mvEndPoints[0], mvEndPoints[2], cColor(1, 1, 1, 1));
	apLowLevelGraphics->DrawLine(mvEndPoints[1], mvEndPoints[3], cColor(1, 1, 1, 1));
}

//-----------------------------------------------------------------------
} // namespace hpl