bgfx/examples/common/bounds.h

160 lines
3.8 KiB
C
Raw Normal View History

2013-02-22 09:07:31 +04:00
/*
2018-01-01 22:16:06 +03:00
* Copyright 2011-2018 Branimir Karadzic. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-02-22 09:07:31 +04:00
*/
#ifndef BOUNDS_H_HEADER_GUARD
#define BOUNDS_H_HEADER_GUARD
2013-02-22 09:07:31 +04:00
struct Aabb
{
float m_min[3];
float m_max[3];
};
2015-11-28 10:45:42 +03:00
struct Cylinder
{
float m_pos[3];
float m_end[3];
float m_radius;
};
2017-09-26 06:18:19 +03:00
struct Capsule
{
float m_pos[3];
float m_end[3];
float m_radius;
};
2017-09-25 05:12:18 +03:00
struct Cone
{
float m_pos[3];
float m_end[3];
float m_radius;
};
2015-11-28 10:45:42 +03:00
struct Disk
{
float m_center[3];
float m_normal[3];
float m_radius;
};
2013-02-22 09:07:31 +04:00
struct Obb
{
float m_mtx[16];
};
2015-11-28 10:45:42 +03:00
struct Plane
{
float m_normal[3];
float m_dist;
};
struct Ray
{
float m_pos[3];
float m_dir[3];
};
2013-02-22 09:07:31 +04:00
struct Sphere
{
float m_center[3];
float m_radius;
};
2015-11-28 10:45:42 +03:00
struct Tris
{
float m_v0[3];
float m_v1[3];
float m_v2[3];
};
2017-09-28 07:52:25 +03:00
struct Hit
2015-11-28 10:45:42 +03:00
{
float m_pos[3];
float m_normal[3];
float m_dist;
};
2013-02-22 09:07:31 +04:00
/// 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);
2014-05-20 09:08:35 +04:00
/// 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);
2014-05-20 09:08:35 +04:00
2013-02-22 09:07:31 +04:00
/// Calculate axis aligned bounding box.
2016-12-06 06:01:11 +03:00
void toAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
2013-02-22 09:07:31 +04:00
/// Transform vertices and calculate axis aligned bounding box.
2016-12-06 06:01:11 +03:00
void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
2013-02-22 09:07:31 +04:00
2014-05-27 03:55:46 +04:00
/// Expand AABB.
void aabbExpand(Aabb& _aabb, float _factor);
/// Expand AABB with xyz.
void aabbExpand(Aabb& _aabb, const float* _pos);
2016-12-06 06:01:11 +03:00
/// Calculate surface area of axis aligned bounding box.
float calcAreaAabb(const Aabb& _aabb);
2014-05-27 03:55:46 +04:00
/// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
/// test.
2015-11-28 10:45:42 +03:00
uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1);
2014-05-27 03:55:46 +04:00
2013-02-22 09:07:31 +04:00
/// 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.
void intersectPlanes(float _result[3], const Plane& _pa, const Plane& _pb, const Plane& _pc);
2015-11-28 10:45:42 +03:00
/// 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.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / OBB.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / cylinder.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL);
2017-09-26 06:18:19 +03:00
/// Intersect ray / capsule.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
2017-09-25 05:12:18 +03:00
/// Intersect ray / cone.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL);
2017-09-25 05:12:18 +03:00
2015-11-28 10:45:42 +03:00
/// Intersect ray / disk.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / plane.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / sphere.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / triangle.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Tris& _triangle, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
#endif // BOUNDS_H_HEADER_GUARD