forked from Vogtinator/crafti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aabb.cpp
179 lines (151 loc) · 4.31 KB
/
aabb.cpp
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
#include "aabb.h"
#include <algorithm>
AABB::AABB(VERTEX *v1, VERTEX *v2)
{
set(v1, v2);
}
AABB::AABB(GLFix low_x, GLFix low_y, GLFix low_z, GLFix high_x, GLFix high_y, GLFix high_z)
: low_x(low_x), low_y(low_y), low_z(low_z), high_x(high_x), high_y(high_y), high_z(high_z)
{}
AABB::AABB(VERTEX *list, unsigned int size)
{
set(list, size);
}
void AABB::set(VERTEX *v1, VERTEX *v2)
{
low_x = std::min(v1->x, v1->y);
high_x = std::max(v1->x, v2->x);
low_y = std::min(v1->y, v2->y);
high_y = std::max(v1->y, v2->y);
low_z = std::min(v1->z, v2->z);
high_z = std::max(v1->z, v2->z);
}
void AABB::set(GLFix low_x, GLFix low_y, GLFix low_z, GLFix high_x, GLFix high_y, GLFix high_z)
{
this->low_x = low_x;
this->low_y = low_y;
this->low_z = low_z;
this->high_x = high_x;
this->high_y = high_y;
this->high_z = high_z;
}
void AABB::set(VERTEX *list, unsigned int size)
{
low_x = high_x = list->x;
low_y = high_y = list->y;
low_z = high_z = list->z;
list++;
for(unsigned int i = 1; i < size; i++)
{
VERTEX *v = list++;
if(v->x < low_x)
low_x = v->x;
if(v->x > high_x)
high_x = v->x;
if(v->y < low_y)
low_y = v->y;
if(v->y > high_y)
high_y = v->y;
if(v->z < low_z)
low_z = v->z;
if(v->z > high_z)
high_z = v->z;
}
}
bool AABB::intersects(AABB &other)
{
return high_x >= other.low_x && high_y >= other.low_y && high_z >= other.low_z
&& low_x <= other.high_x && low_y <= other.high_y && low_z <= other.high_z;
}
AABB::SIDE AABB::intersectsRay(GLFix x, GLFix y, GLFix z, GLFix dx, GLFix dy, GLFix dz, GLFix &dist)
{
GLFix t_min_x = 0, t_max_x = GLFix::maxValue(),
t_min_y = 0, t_max_y = GLFix::maxValue(),
t_min_z = 0, t_max_z = GLFix::maxValue();
if(dx == GLFix(0))
{
if(x < low_x || x > high_x)
return NONE;
}
else
{
t_min_x = (low_x - x) / dx;
t_max_x = (high_x - x) / dx;
}
if(dy == GLFix(0))
{
if(y < low_y || y > high_y)
return NONE;
}
else
{
t_min_y = (low_y - y) / dy;
t_max_y = (high_y - y) / dy;
}
if(dz == GLFix(0))
{
if(z < low_z || z > high_z)
return NONE;
}
else
{
t_min_z = (low_z - z) / dz;
t_max_z = (high_z - z) / dz;
}
GLFix min = std::max(std::max(std::min(t_min_x, t_max_x), std::min(t_min_y, t_max_y)), std::min(t_min_z, t_max_z));
GLFix max = std::min(std::min(std::max(t_min_x, t_max_x), std::max(t_min_y, t_max_y)), std::max(t_min_z, t_max_z));
if(max < GLFix(0) || min > max)
return NONE;
dist = min;
if(min == t_min_x || min == t_max_x)
return dx < GLFix(0) ? RIGHT : LEFT;
if(min == t_min_y || min == t_max_y)
return dy < GLFix(0) ? TOP : BOTTOM;
//if(min == t_min_z || min == t_max_z)
return dz < GLFix(0) ? BACK : FRONT;
}
void AABB::render()
{
VERTEX v1{low_x, low_y, low_z, 0, 0, 0},
v2{high_x, low_y, low_z, 0, 0, 0},
v3{low_x, high_y, low_z, 0, 0, 0},
v4{high_x, high_y, low_z, 0, 0, 0},
v5{low_x, low_y, high_z, 0, 0, 0},
v6{high_x, low_y, high_z, 0, 0, 0},
v7{low_x, high_y, high_z, 0, 0, 0},
v8{high_x, high_y, high_z, 0, 0, 0};
glBegin(GL_LINE_STRIP);
nglAddVertex(&v1);
nglAddVertex(&v2);
nglAddVertex(&v4);
nglAddVertex(&v3);
nglAddVertex(&v1);
glEnd();
glBegin(GL_LINE_STRIP);
nglAddVertex(&v5);
nglAddVertex(&v6);
nglAddVertex(&v8);
nglAddVertex(&v7);
nglAddVertex(&v5);
glEnd();
glBegin(GL_LINE_STRIP);
nglAddVertex(&v1);
nglAddVertex(&v5);
glEnd();
glBegin(GL_LINE_STRIP);
nglAddVertex(&v2);
nglAddVertex(&v6);
glEnd();
glBegin(GL_LINE_STRIP);
nglAddVertex(&v3);
nglAddVertex(&v7);
glEnd();
glBegin(GL_LINE_STRIP);
nglAddVertex(&v4);
nglAddVertex(&v8);
glEnd();
}
void AABB::print()
{
printf("X: %d-%d Y: %d-%d Z: %d-%d", low_x.toInteger<int>(), high_x.toInteger<int>(), low_y.toInteger<int>(), high_y.toInteger<int>(), low_z.toInteger<int>(), high_z.toInteger<int>());
}