]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/frustum.c
Disable pedantic warnings
[dragonblocks_alpha.git] / src / client / frustum.c
1 #include "frustum.h"
2
3 typedef enum
4 {
5         PLANE_LEFT,
6         PLANE_RIGHT,
7         PLANE_BOTTOM,
8         PLANE_TOP,
9         PLANE_NEAR,
10         PLANE_FAR,
11         PLANE_COUNT,
12 } Plane;
13
14 static struct
15 {
16         vec3 points[8];
17         vec4 planes[PLANE_COUNT];
18         int cross_indices[PLANE_COUNT][PLANE_COUNT];
19 } frustum;
20
21 __attribute__((constructor)) static void init_frustum()
22 {
23         for (Plane a = 0; a < PLANE_COUNT; a++)
24                 for (Plane b = 0; b < PLANE_COUNT; b++)
25                         frustum.cross_indices[a][b] = a * (9 - a) / 2 + b - 1;
26 }
27
28 void frustum_update(mat4x4 view_proj)
29 {
30         mat4x4 m;
31
32         mat4x4_transpose(m, view_proj);
33
34         vec4_add(frustum.planes[PLANE_LEFT], m[3], m[0]);
35         vec4_sub(frustum.planes[PLANE_RIGHT], m[3], m[0]);
36         vec4_add(frustum.planes[PLANE_BOTTOM], m[3], m[1]);
37         vec4_sub(frustum.planes[PLANE_TOP], m[3], m[1]);
38         vec4_add(frustum.planes[PLANE_NEAR], m[3], m[2]);
39         vec4_sub(frustum.planes[PLANE_FAR], m[3], m[2]);
40
41         int i = 0;
42         vec3 crosses[PLANE_COUNT * (PLANE_COUNT - 1) / 2];
43         for (Plane a = 0; a < PLANE_COUNT; a++)
44                 for (Plane b = a + 1; b < PLANE_COUNT; b++)
45                         vec3_mul_cross(crosses[i++], frustum.planes[a], frustum.planes[b]);
46
47         int j = 0;
48         for (Plane c = PLANE_NEAR; c <= PLANE_FAR; c++) {
49                 for (Plane a = PLANE_LEFT; a <= PLANE_RIGHT; a++) {
50                         for (Plane b = PLANE_BOTTOM; b <= PLANE_TOP; b++) {
51                                 float d = -1.0f / vec3_mul_inner(frustum.planes[a], crosses[frustum.cross_indices[b][c]]);
52                                 vec3 w = {frustum.planes[a][3], frustum.planes[b][3], frustum.planes[c][3]};
53                                 float *res = frustum.points[j++];
54
55                                 vec3 res_1_cross = {-crosses[frustum.cross_indices[a][c]][0], -crosses[frustum.cross_indices[a][c]][1], -crosses[frustum.cross_indices[a][c]][2]};
56
57                                 res[0] = vec3_mul_inner(crosses[frustum.cross_indices[b][c]], w) * d;
58                                 res[1] = vec3_mul_inner(res_1_cross, w) * d;
59                                 res[2] = vec3_mul_inner(crosses[frustum.cross_indices[a][b]], w) * d;
60                         }
61                 }
62         }
63 }
64
65 static bool outside_plane(Plane i, aabb3f32 box)
66 {
67         for (int x = 0; x <= 1; x++) {
68                 for (int y = 0; y <= 1; y++) {
69                         for (int z = 0; z <= 1; z++) {
70                                 vec4 plane = {
71                                         x ? box.max.x : box.min.x,
72                                         y ? box.max.y : box.min.y,
73                                         z ? box.max.z : box.min.z,
74                                         1.0f,
75                                 };
76
77                                 if (vec4_mul_inner(frustum.planes[i], plane) > 0.0)
78                                         return false;
79                         }
80                 }
81         }
82
83         return true;
84 }
85
86 // http://iquilezles.org/www/articles/frustumcorrect/frustumcorrect.htm
87 bool frustum_is_visible(aabb3f32 box)
88 {
89         for (Plane i = 0; i < PLANE_COUNT; i++) {
90                 if (outside_plane(i, box))
91                         return false;
92         }
93
94         int box_outside[6] = {0};
95
96         for (Plane i = 0; i < PLANE_COUNT; i++) {
97                 int outside[6] = {
98                         frustum.points[i][0] > box.max.x,
99                         frustum.points[i][0] < box.min.x,
100                         frustum.points[i][1] > box.max.y,
101                         frustum.points[i][1] < box.min.y,
102                         frustum.points[i][2] > box.max.z,
103                         frustum.points[i][2] < box.min.z,
104                 };
105
106                 for (int i = 0; i < 6; i++)
107                         box_outside[i] += outside[i];
108         }
109
110         for (int i = 0; i < 6; i++) {
111                 if (box_outside[i] == 8)
112                         return false;
113         }
114
115         return true;
116 }