summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/common/bounds.h
blob: aea395df7806dba01757787caa55022734dce5ed (plain) (blame)
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
/*
 * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#ifndef BOUNDS_H_HEADER_GUARD
#define BOUNDS_H_HEADER_GUARD

#include <bx/math.h>

struct Aabb
{
	bx::Vec3 m_min;
	bx::Vec3 m_max;
};

struct Cylinder
{
	bx::Vec3 m_pos;
	bx::Vec3 m_end;
	float m_radius;
};

struct Capsule
{
	bx::Vec3 m_pos;
	bx::Vec3 m_end;
	float m_radius;
};

struct Cone
{
	bx::Vec3 m_pos;
	bx::Vec3 m_end;
	float m_radius;
};

struct Disk
{
	bx::Vec3 m_center;
	bx::Vec3 m_normal;
	float m_radius;
};

struct Obb
{
	float m_mtx[16];
};

struct Plane
{
	bx::Vec3 m_normal;
	float m_dist;
};

struct Ray
{
	bx::Vec3 m_pos;
	bx::Vec3 m_dir;
};

struct Sphere
{
	bx::Vec3 m_center;
	float m_radius;
};

struct Tris
{
	bx::Vec3 m_v0;
	bx::Vec3 m_v1;
	bx::Vec3 m_v2;
};

struct Hit
{
	bx::Vec3 m_pos;
	bx::Vec3 m_normal;
	float m_dist;
};

/// Convert axis aligned bounding box to oriented bounding box.
void aabbToObb(Obb& _obb, const Aabb& _aabb);

/// Convert oriented bounding box to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Obb& _obb);

/// Convert sphere to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Sphere& _sphere);

/// Convert disk to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Disk& _disk);

/// Convert cylinder to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Cylinder& _cylinder);

/// Calculate axis aligned bounding box.
void toAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);

/// Transform vertices and calculate axis aligned bounding box.
void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);

/// Expand AABB.
void aabbExpand(Aabb& _aabb, float _factor);

/// Expand AABB with xyz.
void aabbExpand(Aabb& _aabb, const float* _pos);

/// Calculate surface area of axis aligned bounding box.
float calcAreaAabb(const Aabb& _aabb);

/// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
/// test.
uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1);

/// Calculate oriented bounding box.
void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);

/// Calculate maximum bounding sphere.
void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride);

/// Calculate minimum bounding sphere.
void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f);

/// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes.
void buildFrustumPlanes(Plane* _planes, const float* _viewProj);

/// Returns point from 3 intersecting planes.
bx::Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc);

/// Make screen space ray from x, y coordinate and inverse view-projection matrix.
Ray makeRay(float _x, float _y, const float* _invVp);

/// Intersect ray / AABB.
bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL);

/// Intersect ray / OBB.
bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL);

/// Intersect ray / cylinder.
bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL);

/// Intersect ray / capsule.
bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL);

/// Intersect ray / cone.
bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL);

/// Intersect ray / disk.
bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL);

/// Intersect ray / plane.
bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit = NULL);

/// Intersect ray / sphere.
bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL);

/// Intersect ray / triangle.
bool intersect(const Ray& _ray, const Tris& _triangle, Hit* _hit = NULL);

#endif // BOUNDS_H_HEADER_GUARD