LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Ray.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <glm/glm.hpp>
8
10#include "LkEngine/Core/Math/AABB.h"
11
12namespace LkEngine {
13
26 struct FRayCast
27 {
28 LVector Pos{};
29 LVector Dir{};
30
31 FRayCast() = default;
32 FRayCast(const LVector& InPos, const LVector& InDir)
33 : Pos(InPos)
34 , Dir(InDir)
35 {
36 }
37 };
38
44 struct FRay
45 {
46 LVector Origin;
47 LVector Direction;
48
49 FRay(const LVector& InOrigin, const LVector& InDirection)
50 : Origin(InOrigin)
51 , Direction(InDirection)
52 {
53 }
54
55 FRay(const glm::vec3& InOrigin, const glm::vec3& InDirection)
56 : Origin(InOrigin)
57 , Direction(InDirection)
58 {
59 }
60
64 static FRay Zero()
65 {
66 return { LVector(0.0f, 0.0f, 0.0f), LVector(0.0f, 0.0f, 0.0f) };
67 }
68
76 FORCEINLINE bool IntersectsAABB(const FAABB& AABB, float& t) const
77 {
78 /* The direction vector is inverted to calculate the intersection points. */
79 LVector DirFraction;
80 DirFraction.X = (1.0f / Direction.X);
81 DirFraction.Y = (1.0f / Direction.Y);
82 DirFraction.Z = (1.0f / Direction.Z);
83
84 const LVector& LeftBottom = AABB.Min;
85 const LVector& RightTop = AABB.Max;
86
87 const float t1 = (LeftBottom.X - Origin.X) * DirFraction.X;
88 const float t2 = (RightTop.X - Origin.X) * DirFraction.X;
89
90 const float t3 = (LeftBottom.Y - Origin.Y) * DirFraction.Y;
91 const float t4 = (RightTop.Y - Origin.Y) * DirFraction.Y;
92
93 const float t5 = (LeftBottom.Z - Origin.Z) * DirFraction.Z;
94 const float t6 = (RightTop.Z - Origin.Z) * DirFraction.Z;
95
96 /* Point where the ray first enters the AABB. */
97 const float tmin = glm::max(
98 glm::max(glm::min(t1, t2), glm::min(t3, t4)),
99 glm::min(t5, t6)
100 );
101
102 /* Point where the ray exits the AABB. */
103 const float tmax = glm::min(
104 glm::min(glm::max(t1, t2), glm::max(t3, t4)),
105 glm::max(t5, t6)
106 );
107
108 /* Ray is intersecting with the AABB behind us. */
109 if (tmax < 0)
110 {
111 t = tmax;
112 return false;
113 }
114
115 /* Ray does not intersect the AABB. */
116 if (tmin > tmax)
117 {
118 t = tmax;
119 return false;
120 }
121
122 /* When t == 0, t is at the ray's origin. */
123
124 t = tmin;
125 return true;
126 }
127
135 FORCEINLINE bool IntersectsTriangle(const glm::vec3& A, const glm::vec3& B, const glm::vec3& C, float& t) const
136 {
137 const glm::vec3 E1 = B - A;
138 const glm::vec3 E2 = C - A;
139
140 const glm::vec3 N = cross(E1, E2);
141 const float Det = -glm::dot(glm::vec3(Direction.X, Direction.Y, Direction.Z), N);
142
143 const float InvDet = 1.0f / Det;
144
145 const glm::vec3 AO = Origin.As<glm::vec3>() - A;
146 const glm::vec3 DAO = glm::cross(AO, Direction.As<glm::vec3>());
147 const float u = glm::dot(E2, DAO) * InvDet;
148 const float v = -glm::dot(E1, DAO) * InvDet;
149
150 t = glm::dot(AO, N) * InvDet;
151
152 return ((Det >= 1e-6f)
153 && (t >= 0.0f)
154 && (u >= 0.0f)
155 && (v >= 0.0f)
156 && ((u + v) <= 1.0f));
157 }
158
166 FORCEINLINE bool IntersectsTriangle(const LVector& A, const LVector& B, const LVector& C, float& t) const
167 {
168 const LVector E1 = B - A;
169 const LVector E2 = C - A;
170
171 const LVector N = LVector::Cross(E1, E2);
172 const float Det = -LVector::Dot(Direction, N);
173
174 const float InvDet = 1.0f / Det;
175
176 const LVector AO = Origin - LVector(A);
177 const LVector DAO = LVector::Cross(AO, Direction);
178 const float u = LVector::Dot(E2, DAO) * InvDet;
179 const float v = -LVector::Dot(E1, DAO) * InvDet;
180
181 t = LVector::Dot(AO, N) * InvDet;
182
183 return ((Det >= 1e-6f)
184 && (t >= 0.0f)
185 && (u >= 0.0f)
186 && (v >= 0.0f)
187 && ((u + v) <= 1.0f));
188 }
189
190 };
191
194}
Mathematical vectors.
LVector3 LVector
Definition Vector.h:68
Definition Asset.h:11
Definition AABB.h:9
Definition Ray.h:27
Definition Ray.h:45
static FRay Zero()
Get a zero-initialized ray.
Definition Ray.h:64
FORCEINLINE bool IntersectsAABB(const FAABB &AABB, float &t) const
Checks if a ray intersects an axis-aligned bounding box (AABB) in 3D space.
Definition Ray.h:76
FORCEINLINE bool IntersectsTriangle(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C, float &t) const
Checks if a ray intersects with a triangle in 3D space.
Definition Ray.h:135
FORCEINLINE bool IntersectsTriangle(const LVector &A, const LVector &B, const LVector &C, float &t) const
Checks if a ray intersects with a triangle in 3D space.
Definition Ray.h:166
Definition Vector3.h:676
static SizeType Dot(const VectorTypeA &A, const VectorTypeB &B)
Calculate the dot product of two vectors.
Definition Vector3.h:857