Khaotic Engine Reborn
Loading...
Searching...
No Matches
frustum.cpp
1#include "frustum.h"
2
3void frustum::ConstructFrustum(float screenDepth, XMMATRIX projectionMatrix, XMMATRIX viewMatrix)
4{
5 XMMATRIX matrix;
6 XMVECTOR planes[6];
7
8 // Calculate the minimum Z distance in the frustum.
9 float zMinimum = -projectionMatrix.r[3].m128_f32[2] / projectionMatrix.r[2].m128_f32[2];
10 float r = screenDepth / (screenDepth - zMinimum);
11 projectionMatrix.r[2].m128_f32[2] = r;
12 projectionMatrix.r[3].m128_f32[2] = -r * zMinimum;
13
14 // Create the frustum matrix from the view matrix and updated projection matrix.
15 matrix = XMMatrixMultiply(viewMatrix, projectionMatrix);
16
17 // Calculate near plane of frustum.
18 planes[0] = XMPlaneNormalize(XMVectorSet(matrix.r[0].m128_f32[3] + matrix.r[0].m128_f32[2],
19 matrix.r[1].m128_f32[3] + matrix.r[1].m128_f32[2],
20 matrix.r[2].m128_f32[3] + matrix.r[2].m128_f32[2],
21 matrix.r[3].m128_f32[3] + matrix.r[3].m128_f32[2]));
22
23 // Calculate far plane of frustum.
24 planes[1] = XMPlaneNormalize(XMVectorSet(matrix.r[0].m128_f32[3] - matrix.r[0].m128_f32[2],
25 matrix.r[1].m128_f32[3] - matrix.r[1].m128_f32[2],
26 matrix.r[2].m128_f32[3] - matrix.r[2].m128_f32[2],
27 matrix.r[3].m128_f32[3] - matrix.r[3].m128_f32[2]));
28
29 // Calculate left plane of frustum.
30 planes[2] = XMPlaneNormalize(XMVectorSet(matrix.r[0].m128_f32[3] + matrix.r[0].m128_f32[0],
31 matrix.r[1].m128_f32[3] + matrix.r[1].m128_f32[0],
32 matrix.r[2].m128_f32[3] + matrix.r[2].m128_f32[0],
33 matrix.r[3].m128_f32[3] + matrix.r[3].m128_f32[0]));
34
35 // Calculate right plane of frustum.
36 planes[3] = XMPlaneNormalize(XMVectorSet(matrix.r[0].m128_f32[3] - matrix.r[0].m128_f32[0],
37 matrix.r[1].m128_f32[3] - matrix.r[1].m128_f32[0],
38 matrix.r[2].m128_f32[3] - matrix.r[2].m128_f32[0],
39 matrix.r[3].m128_f32[3] - matrix.r[3].m128_f32[0]));
40
41 // Calculate top plane of frustum.
42 planes[4] = XMPlaneNormalize(XMVectorSet(matrix.r[0].m128_f32[3] - matrix.r[0].m128_f32[1],
43 matrix.r[1].m128_f32[3] - matrix.r[1].m128_f32[1],
44 matrix.r[2].m128_f32[3] - matrix.r[2].m128_f32[1],
45 matrix.r[3].m128_f32[3] - matrix.r[3].m128_f32[1]));
46
47 // Calculate bottom plane of frustum.
48 planes[5] = XMPlaneNormalize(XMVectorSet(matrix.r[0].m128_f32[3] + matrix.r[0].m128_f32[1],
49 matrix.r[1].m128_f32[3] + matrix.r[1].m128_f32[1],
50 matrix.r[2].m128_f32[3] + matrix.r[2].m128_f32[1],
51 matrix.r[3].m128_f32[3] + matrix.r[3].m128_f32[1]));
52
53 for (int i = 0; i < 6; i++)
54 {
55 m_planes[i] = planes[i];
56 }
57}
58
59bool frustum::CheckCube(float xCenter, float yCenter, float zCenter, float radius, float tolerance)
60{
61 // Vérifiez chaque plan du frustum pour voir si le cube est ŕ l'intérieur
62 for (int i = 0; i < 6; i++)
63 {
64 XMVECTOR plane = m_planes[i];
65 if (XMVectorGetX(plane) * (xCenter - radius) + XMVectorGetY(plane) * (yCenter - radius) + XMVectorGetZ(plane) * (zCenter - radius) + XMVectorGetW(plane) > -tolerance)
66 continue;
67 if (XMVectorGetX(plane) * (xCenter + radius) + XMVectorGetY(plane) * (yCenter - radius) + XMVectorGetZ(plane) * (zCenter - radius) + XMVectorGetW(plane) > -tolerance)
68 continue;
69 if (XMVectorGetX(plane) * (xCenter - radius) + XMVectorGetY(plane) * (yCenter + radius) + XMVectorGetZ(plane) * (zCenter - radius) + XMVectorGetW(plane) > -tolerance)
70 continue;
71 if (XMVectorGetX(plane) * (xCenter + radius) + XMVectorGetY(plane) * (yCenter + radius) + XMVectorGetZ(plane) * (zCenter - radius) + XMVectorGetW(plane) > -tolerance)
72 continue;
73 if (XMVectorGetX(plane) * (xCenter - radius) + XMVectorGetY(plane) * (yCenter - radius) + XMVectorGetZ(plane) * (zCenter + radius) + XMVectorGetW(plane) > -tolerance)
74 continue;
75 if (XMVectorGetX(plane) * (xCenter + radius) + XMVectorGetY(plane) * (yCenter - radius) + XMVectorGetZ(plane) * (zCenter + radius) + XMVectorGetW(plane) > -tolerance)
76 continue;
77 if (XMVectorGetX(plane) * (xCenter - radius) + XMVectorGetY(plane) * (yCenter + radius) + XMVectorGetZ(plane) * (zCenter + radius) + XMVectorGetW(plane) > -tolerance)
78 continue;
79 if (XMVectorGetX(plane) * (xCenter + radius) + XMVectorGetY(plane) * (yCenter + radius) + XMVectorGetZ(plane) * (zCenter + radius) + XMVectorGetW(plane) > -tolerance)
80 continue;
81
82 // Si le cube est en dehors de l'un des plans, il n'est pas dans le frustum
83 return false;
84 }
85
86 // Si le cube est ŕ l'intérieur de tous les plans, il est dans le frustum
87 return true;
88}
void ConstructFrustum(float screenDepth, XMMATRIX projectionMatrix, XMMATRIX viewMatrix)
Definition frustum.cpp:3
bool CheckCube(float xCenter, float yCenter, float zCenter, float radius, float tolerance)
Definition frustum.cpp:59