-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundingbox.c
95 lines (77 loc) · 2.9 KB
/
boundingbox.c
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
#include <GL/glew.h>
#include "boundingbox.h"
#include "constants.h"
#include "shader.h"
#include "objects.h"
static const float THRESHOLD = 0.0;
int bb_intersects(struct bounding_box *a,
struct bounding_box *b)
{
return ((a->min[COORD_X] + THRESHOLD < b->max[COORD_X] && a->max[COORD_X] + THRESHOLD > b->min[COORD_X])
&& (a->min[COORD_Y] + THRESHOLD < b->max[COORD_Y] && a->max[COORD_Y] + THRESHOLD > b->min[COORD_Y])
&& (a->min[COORD_Z] + THRESHOLD < b->max[COORD_Z] && a->max[COORD_Z] + THRESHOLD > b->min[COORD_Z]));
}
int bb_intersects_top(struct bounding_box *a,
struct bounding_box *b)
{
return ((a->min[COORD_X] + THRESHOLD < b->max[COORD_X] && a->max[COORD_X] + THRESHOLD > b->min[COORD_X])
&& (a->min[COORD_Y] + THRESHOLD < b->max[COORD_Y] && a->max[COORD_Y] + THRESHOLD > b->max[COORD_Y])
&& (a->min[COORD_Z] + THRESHOLD < b->max[COORD_Z] && a->max[COORD_Z] + THRESHOLD > b->min[COORD_Z]));
}
float bb_width(struct bounding_box *b)
{
return (b->max[COORD_X] - b->min[COORD_X]);
}
float bb_horizontal_center(struct bounding_box *b)
{
return b->min[COORD_X] + (b->max[COORD_X] - b->min[COORD_X])/2;
}
void
bb_draw(struct bounding_box *b, GLuint shader_program)
{
GLfloat vertices[] = {
b->min[COORD_X], b->min[COORD_Y], b->max[COORD_Z], /* 0 */
b->min[COORD_X], b->max[COORD_Y], b->max[COORD_Z], /* 1 */
b->max[COORD_X], b->max[COORD_Y], b->max[COORD_Z], /* 2 */
b->max[COORD_X], b->min[COORD_Y], b->max[COORD_Z], /* 3 */
b->max[COORD_X], b->min[COORD_Y], b->min[COORD_Z], /* 4 */
b->max[COORD_X], b->max[COORD_Y], b->min[COORD_Z], /* 5 */
b->min[COORD_X], b->max[COORD_Y], b->min[COORD_Z], /* 6 */
b->min[COORD_X], b->min[COORD_Y], b->min[COORD_Z] /* 7 */
};
GLuint elements[] = {
0, 1, 3,
1, 2, 3,
3, 2, 4,
2, 5, 4,
4, 7, 5,
5, 7, 6,
0, 7, 1,
1, 7, 6,
1, 2, 6,
2, 6, 5,
0, 7, 3,
7, 3, 4
};
GLuint vbo;
GLuint ebo;
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof (elements), elements, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof (GLfloat), (GLvoid *) 0);
glEnableVertexAttribArray(VERTEX_VAO);
glUseProgram(shader_program);
shader_set_uniform_i(shader_program, "objectType", OBJECT_BOUNDING_BOX);
glDrawElements(GL_TRIANGLES, sizeof (elements) / sizeof (GLuint), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &ebo);
glDeleteVertexArrays(1, &vao);
}