Path Tracing
Loading...
Searching...
No Matches
intersection.glsl
Go to the documentation of this file.
1
3float planeT(Ray ray, vec3 normal, float offset) {
4 float denom = dot(normal, ray.direction);
5 if (abs(denom) < 1e-8) return INF;
6 float t = (offset - dot(normal, ray.origin)) / denom;
7 return (t > EPS) ? t : INF;
8}
9
12float planeT_Z(Ray ray, float offset) {
13 if (abs(ray.direction.z) < 1e-8) return INF;
14 float t = (offset - ray.origin.z) / ray.direction.z;
15 return (t > EPS) ? t : INF;
16}
17
19float planeT_X(Ray ray, float offset) {
20 if (abs(ray.direction.x) < 1e-8) return INF;
21 float t = (offset - ray.origin.x) / ray.direction.x;
22 return (t > EPS) ? t : INF;
23}
24
26float planeT_Y(Ray ray, float offset) {
27 if (abs(ray.direction.y) < 1e-8) return INF;
28 float t = (offset - ray.origin.y) / ray.direction.y;
29 return (t > EPS) ? t : INF;
30}
31
32
54float sphereT(Ray ray, vec3 center, float radius) {
55 //vetor centro da esfera até origem do raio
56 vec3 origin_to_ctr = ray.origin - center;
57
58 //projeção do vetor origin_to_ctr na direção ray.direction
59 float b = dot(origin_to_ctr, ray.direction);
60
61 //constante da equação quadrática
62 float c = dot(origin_to_ctr, origin_to_ctr) - radius * radius;
63
64 float disc = b*b - c;
65 if (disc < 0.0) return INF;
66
67 float sqrt_disc = sqrt(disc);
68
69 //soluções de interseção à entrada ou saída da esfera
70 // (t>EPS) evita auto-interseção e t negativo
71 float t;
72 //interseção próxima (entrada da esfera)
73 t = -b - sqrt_disc;
74 if (t > EPS) return t;
75
76 //interseção longe (saída da esfera)
77 t = -b + sqrt_disc;
78 return (t > EPS) ? t : INF;
79}
80
87float boxT(Ray ray, vec3 center, vec3 half_size, float pitch, float yaw, out vec3 out_normal) {
88
89 //calculate local axis from angles
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);
95
96 //ray to OBB local space
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)
101 );
102 vec3 local_dir = vec3(
103 dot(ray.direction, axis_x),
104 dot(ray.direction, axis_y),
105 dot(ray.direction, axis_z)
106 );
107
108 //AABB intersect box
109
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;
113
114 vec3 t_near = min(t_min, t_max);
115 vec3 t_far = max(t_min, t_max);
116
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));
119
120 if (t1 < t0 || t1 < EPS) return INF;
121 float t = max(t0, 0.0);
122
123 //normal in local space, then converts to world space
124 vec3 hit_local = local_origin + t * local_dir;
125 vec3 abs_hit = abs(hit_local) / half_size;
126
127 vec3 local_normal;
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);
132 else
133 local_normal = vec3(0, 0, sign(hit_local.z));
134
135 out_normal = normalize(local_normal.x * axis_x + local_normal.y * axis_y + local_normal.z * axis_z);
136
137 return t;
138}
139
145float boxTAxisAligned(Ray ray, vec3 center, vec3 half_size, out vec3 out_normal) {
146 vec3 local_origin = ray.origin - center;
147 vec3 inv_dir = 1.0 / ray.direction;
148
149 vec3 t_min = (-half_size - local_origin) * inv_dir;
150 vec3 t_max = (half_size - local_origin) * inv_dir;
151
152 vec3 t_near = min(t_min, t_max);
153 vec3 t_far = max(t_min, t_max);
154
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));
157
158 if (t1 < t0 || t1 < EPS) return INF;
159 float t = max(t0, 0.0);
160
161 //normal already in world space
162 //(one component is ±1, the others 0), so no normalize() needed either
163 vec3 hit_local = local_origin + t * ray.direction;
164 vec3 abs_hit = abs(hit_local) / half_size;
165
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);
170 else
171 out_normal = vec3(0, 0, sign(hit_local.z));
172
173 return t;
174}
175
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);
182 vec3 h = cross(ray.direction, edge2);
183 float a = dot(edge1, h);
184
185 if (abs(a) < EPS_TRI) return INF;
186
187 float f = 1.0 / a;
188 vec3 s = ray.origin - v0;
189 float u = f * dot(s, h);
190 if (u < 0.0 || u > 1.0) return INF;
191
192 vec3 q = cross(s, edge1);
193 float v = f * dot(ray.direction, q);
194 if (v < 0.0 || u + v > 1.0) return INF;
195
196 float t = f * dot(edge2, q);
197
198 //relative EPS with the triangle size
199 float tri_size = length(flat_normal);
200 float eps = max(EPS_TRI, t * 1e-5);
201
202 if (t < eps) return INF;
203
204 out_normal = normalize(flat_normal);
205 if (dot(ray.direction, out_normal) > 0.0)
206 out_normal = -out_normal;
207
208 out_bary = vec3(u, v, 1.0 - u - v);
209 return t;
210}
211
212float triangleT(Ray ray, vec3 v0, vec3 v1, vec3 v2, out vec3 out_normal) {
213 vec3 bary;
214 return triangleT(ray, v0, v1, v2, out_normal, bary);
215}
216
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;
230}
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