5 if (abs(denom) < 1e-8)
return INF;
15 return (t >
EPS) ? t :
INF;
22 return (t >
EPS) ? t :
INF;
29 return (t >
EPS) ? t :
INF;
56 vec3 origin_to_ctr = ray.
origin - center;
59 float b = dot(origin_to_ctr, ray.
direction);
62 float c = dot(origin_to_ctr, origin_to_ctr) - radius * radius;
65 if (disc < 0.0)
return INF;
67 float sqrt_disc = sqrt(disc);
74 if (t >
EPS)
return t;
78 return (t >
EPS) ? t :
INF;
87float boxT(
Ray ray, vec3 center, vec3 half_size,
float pitch,
float yaw, out vec3 out_normal) {
90 float c_yaw = cos(radians(yaw)), s_yaw = sin(radians(yaw));
91 float c_pitch = cos(radians(pitch)), s_pitch = sin(radians(pitch));
92 vec3 axis_x = vec3(c_yaw, s_yaw, 0);
93 vec3 axis_y = vec3(-s_yaw * c_pitch, c_yaw * c_pitch, s_pitch);
94 vec3 axis_z = vec3(s_yaw * s_pitch, c_yaw * s_pitch, c_pitch);
97 vec3 local_origin = vec3(
98 dot(ray.
origin - center, axis_x),
99 dot(ray.
origin - center, axis_y),
100 dot(ray.
origin - center, axis_z)
102 vec3 local_dir = vec3(
110 vec3 inv_dir = 1.0 / local_dir;
111 vec3 t_min = (-half_size - local_origin) * inv_dir;
112 vec3 t_max = (half_size - local_origin) * inv_dir;
114 vec3 t_near = min(t_min, t_max);
115 vec3 t_far = max(t_min, t_max);
117 float t0 = max(t_near.x, max(t_near.y, t_near.z));
118 float t1 = min(t_far.x, min(t_far.y, t_far.z));
120 if (t1 < t0 || t1 <
EPS)
return INF;
121 float t = max(t0, 0.0);
124 vec3 hit_local = local_origin + t * local_dir;
125 vec3 abs_hit = abs(hit_local) / half_size;
128 if (abs_hit.x > abs_hit.y && abs_hit.x > abs_hit.z)
129 local_normal = vec3(sign(hit_local.x), 0, 0);
130 else if (abs_hit.y > abs_hit.z)
131 local_normal = vec3(0, sign(hit_local.y), 0);
133 local_normal = vec3(0, 0, sign(hit_local.z));
135 out_normal = normalize(local_normal.x * axis_x + local_normal.y * axis_y + local_normal.z * axis_z);
146 vec3 local_origin = ray.
origin - center;
149 vec3 t_min = (-half_size - local_origin) * inv_dir;
150 vec3 t_max = (half_size - local_origin) * inv_dir;
152 vec3 t_near = min(t_min, t_max);
153 vec3 t_far = max(t_min, t_max);
155 float t0 = max(t_near.x, max(t_near.y, t_near.z));
156 float t1 = min(t_far.x, min(t_far.y, t_far.z));
158 if (t1 < t0 || t1 <
EPS)
return INF;
159 float t = max(t0, 0.0);
163 vec3 hit_local = local_origin + t * ray.
direction;
164 vec3 abs_hit = abs(hit_local) / half_size;
166 if (abs_hit.x > abs_hit.y && abs_hit.x > abs_hit.z)
167 out_normal = vec3(sign(hit_local.x), 0, 0);
168 else if (abs_hit.y > abs_hit.z)
169 out_normal = vec3(0, sign(hit_local.y), 0);
171 out_normal = vec3(0, 0, sign(hit_local.z));
178float triangleT(
Ray ray, vec3
v0, vec3 v1, vec3 v2, out vec3 out_normal, out vec3 out_bary) {
179 vec3 edge1 = v1 -
v0;
180 vec3 edge2 = v2 -
v0;
181 vec3 flat_normal = cross(edge1, edge2);
183 float a = dot(edge1, h);
189 float u = f * dot(s, h);
190 if (u < 0.0 || u > 1.0)
return INF;
192 vec3 q = cross(s, edge1);
194 if (v < 0.0 || u + v > 1.0)
return INF;
196 float t = f * dot(edge2, q);
199 float tri_size =
length(flat_normal);
200 float eps = max(
EPS_TRI, t * 1e-5);
202 if (t < eps)
return INF;
204 out_normal = normalize(flat_normal);
205 if (dot(ray.
direction, out_normal) > 0.0)
206 out_normal = -out_normal;
208 out_bary = vec3(u, v, 1.0 - u - v);
214 return triangleT(ray,
v0, v1, v2, out_normal, bary);
226vec3
terminatorLift(vec3 p, vec3 a, vec3 b, vec3 c, vec3 na, vec3 nb, vec3 nc, vec3 w) {
227 return - w.x * min(0.0, dot(p - a, na)) * na
228 - w.y * min(0.0, dot(p - b, nb)) * nb
229 - w.z * min(0.0, dot(p - c, nc)) * nc;
const float EPS_TRI
Definition globals.glsl:7
const float EPS
Definition globals.glsl:6
const float INF
Definition globals.glsl:5
GLsizei GLsizei * length
Definition imgui_impl_opengl3_loader.h:319
GLint v0
Definition imgui_impl_opengl3_loader.h:329
GLintptr offset
Definition imgui_impl_opengl3_loader.h:278
float boxTAxisAligned(Ray ray, vec3 center, vec3 half_size, out vec3 out_normal)
Fast axis-aligned box intersect, same result as boxT, but skips unused math Skips OBB rotation match,...
Definition intersection.glsl:145
float planeT(Ray ray, vec3 normal, float offset)
Definition intersection.glsl:3
float triangleT(Ray ray, vec3 v0, vec3 v1, vec3 v2, out vec3 out_normal, out vec3 out_bary)
Definition intersection.glsl:178
float planeT_Y(Ray ray, float offset)
Specific Y-axis plane, with only offset.
Definition intersection.glsl:26
float sphereT(Ray ray, vec3 center, float radius)
Definition intersection.glsl:54
float planeT_Z(Ray ray, float offset)
Definition intersection.glsl:12
vec3 terminatorLift(vec3 p, vec3 a, vec3 b, vec3 c, vec3 na, vec3 nb, vec3 nc, vec3 w)
Shadow terminator lifts a hit from the flat triangle onto the smooth surface implied by the vertex no...
Definition intersection.glsl:226
float boxT(Ray ray, vec3 center, vec3 half_size, float pitch, float yaw, out vec3 out_normal)
Definition intersection.glsl:87
float planeT_X(Ray ray, float offset)
Specific X-axis plane, with only offset.
Definition intersection.glsl:19
Definition globals.glsl:158
vec3 direction
Definition globals.glsl:160
vec3 origin
Definition globals.glsl:159