-
Notifications
You must be signed in to change notification settings - Fork 4
/
Geometry.h
85 lines (66 loc) · 2.5 KB
/
Geometry.h
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
#pragma once
#include "sf/Vector.h"
#include "sf/Matrix.h"
namespace sf {
struct Ray
{
sf::Vec3 origin;
sf::Vec3 direction;
};
struct FastRay
{
union {
sf::Ray ray;
struct {
sf::Vec3 origin;
sf::Vec3 direction;
};
};
sf::Vec3 rcpDirection;
sf_forceinline explicit FastRay(const Ray &ray)
: origin(ray.origin), direction(ray.direction), rcpDirection(sf::Vec3(1.0f) / ray.direction)
{ }
};
struct Sphere
{
sf::Vec3 origin;
float radius;
};
struct Bounds3
{
sf::Vec3 origin;
sf::Vec3 extent;
static Bounds3 minMax(const sf::Vec3 &min, const sf::Vec3 &max) {
return { (min + max) * 0.5f, (max - min) * 0.5f };
}
bool operator==(const Bounds3 &bounds) const { return origin == bounds.origin && extent == bounds.extent; }
bool operator!=(const Bounds3 &bounds) const { return origin != bounds.origin && extent != bounds.extent; }
};
struct Cube3
{
sf::Vec3 origin;
float extent;
};
Ray transformRay(const sf::Mat34 &transform, const Ray &ray);
sf::Bounds3 transformBounds(const sf::Mat34 &transform, const sf::Bounds3 &bounds);
sf::Sphere transformSphere(const sf::Mat34 &transform, const sf::Sphere &sphere);
bool intersectRay(float &outT, const Ray &ray, const Sphere &sphere, float tMin=0.0f);
bool intersectRay(float &outT, const Ray &ray, const Bounds3 &bounds, float tMin=0.0f);
bool intersectRay(float &outT, const Ray &ray, const Bounds3 &bounds, const Mat34 &transform, float tMin=0.0f);
bool intersectRayObb(float &outT, const Ray &ray, const Mat34 &obb, float tMin=0.0f);
bool intersect(const sf::Bounds3 &a, const sf::Bounds3 &b);
bool intersect(const sf::Bounds3 &a, const sf::Sphere &b);
bool intersect(const sf::Sphere &a, const sf::Sphere &b);
sf_inline bool intersect(const sf::Sphere &a, const sf::Bounds3 &b) {
return intersect(b, a);
}
Sphere sphereFromBounds3(const Bounds3 &bounds);
Sphere sphereFromBounds3(const Bounds3 &bounds, const sf::Mat34 &transform);
sf::Mat34 obbFromBounds3(const Bounds3 &bounds);
Bounds3 boundsUnion(const Bounds3 &a, const Bounds3 &b);
Sphere sphereUnion(const Sphere &a, const Sphere &b);
float intersectRayFast(const FastRay &ray, const Sphere &sphere, float tMin=0.0f, float tMax=HUGE_VALF);
float intersectRayFast(const FastRay &ray, const Bounds3 &bounds, float tMin=0.0f, float tMax=HUGE_VALF);
float intersectRayFastAabb(const FastRay &ray, const sf::Vec3 &aabbMin, const sf::Vec3 &aabbMax, float tMin=0.0f, float tMax=HUGE_VALF);
sf::Vec3 closestPointOnRayToPoint(const sf::Ray &ray, const sf::Vec3 &point, float tMin=0.0f, float tMax=HUGE_VALF);
}