16 float aabb_t =
boxTAxisAligned(ray, aabb_center, aabb_half_size, aabb_normal);
18 if(aabb_t >= h.t)
return;
27 int nodes_visited = 0, tris_tested = 0;
30 BVHNode node = bvh_nodes[node_id];
36 vec3 tri_normal, tri_bary;
37 float t =
triangleT(ray, triangles[i].
v0.position, triangles[i].v1.position, triangles[i].v2.position, tri_normal, tri_bary);
40 int m_id = triangles[i].material_id;
42 vec2 uv = triangles[i].v0.texcoord * tri_bary.z + triangles[i].v1.texcoord * tri_bary.x + triangles[i].v2.texcoord * tri_bary.y;
44 if(
USE_TEXTURES == 1 && gpu_materials[m_id].tex_index >= 0) {
49 vec3 smooth_normal = triangles[i].v0.normal * tri_bary.z + triangles[i].v1.normal * tri_bary.x + triangles[i].v2.normal * tri_bary.y;
53 h.normal =
length(smooth_normal) >
EPS_TRI ? normalize(smooth_normal) : tri_normal;
54 h.geom_normal = tri_normal;
55 h.emission = gpu_materials[m_id].emission.rgb;
56 h.material = gpu_materials[m_id].type;
60 h.ior = gpu_materials[m_id].ior;
61 h.light_area =
triangleArea(triangles[i].
v0.position, triangles[i].v1.position, triangles[i].v2.position);
66 float side = dot(h.normal, tri_normal) < 0.0 ? -1.0 : 1.0;
72 if(
USE_TEXTURES == 1 && gpu_materials[m_id].normal_tex_index >= 0) {
73 vec4 tangent_interp = triangles[i].v0.tangent * tri_bary.z + triangles[i].v1.tangent * tri_bary.x + triangles[i].v2.tangent * tri_bary.y;
77 vec3 T_raw = tangent_interp.xyz - N * dot(tangent_interp.xyz, N);
78 float T_len =
length(T_raw);
81 vec3 T = T_raw / T_len;
83 vec3 B = cross(N, T) * (tangent_interp.w < 0.0 ? -1.0 : 1.0);
88 vec3 sampled =
texture(
tex_normal, vec3(uv,
float(gpu_materials[m_id].normal_tex_index))).rgb;
89 vec3 mapped = mat3(T, B, N) * (sampled * 2.0 - 1.0);
90 float m_len =
length(mapped);
93 if(m_len > 1e-6 && dot(mapped, side * tri_normal) > 0.0)
94 h.normal = mapped / m_len;
105 triangles[i].v0.position, triangles[i].v1.position, triangles[i].v2.position,
106 side * triangles[i].v0.normal, side * triangles[i].v1.normal, side * triangles[i].v2.normal,
107 vec3(tri_bary.z, tri_bary.x, tri_bary.y));
110 if(
USE_TEXTURES == 1 && gpu_materials[m_id].tex_index >= 0) {
111 h.albedo =
texture(
tex_albedo, vec3(uv,
float(gpu_materials[m_id].tex_index))).rgb;
114 h.albedo = gpu_materials[m_id].albedo.rgb;
132 bool hit_left = t_left < h.t;
133 bool hit_right = t_right < h.t;
138 bool hit_near = hit_left, hit_far = hit_right;
140 if(t_right < t_left) {
143 hit_near = hit_right;
147 if(hit_far) stack[stack_top++] = far_child;
148 node_id = near_child;
156 if(stack_top == 0)
break;
157 node_id = stack[--stack_top];
162 vec3 cold = vec3(0.0, 0.0, 1.0);
163 vec3 warm = vec3(0.0, 1.0, 0.0);
164 vec3 hot = vec3(1.0, 0.0, 0.0);
166 vec3 heatmap_color = heat < 0.5 ? mix(cold, warm, heat * 2.0) : mix(warm, hot, (heat - 0.5) * 2.0);
168 h.t = max(aabb_t, 0.001);
170 h.normal = vec3(0.0, 1.0, 0.0);
171 h.albedo = vec3(0.0);
172 h.emission = heatmap_color;
182 h.normal = vec3(0, 0, 1);
183 h.geom_normal = vec3(0, 0, 1);
185 h.emission = vec3(0);
200 if(ray_dist > 0.0 && ray_dist < h.t) {
202 float dist_from_center =
length(hit_plane.xy);
207 h.normal = vec3(0,0,1);
208 h.geom_normal = vec3(0,0,1);
211 h.emission = vec3(0);
bool intersects_cornell(const Ray ray, out Hit h)
Tests ray intersection with a described scene.
Definition cornell_scene.glsl:43
const vec3 WHITE
Definition cornell_scene.glsl:1
const float EPS_TRI
Definition globals.glsl:7
const float INF
Definition globals.glsl:5
uniform int GROUND_SHADOW_CATCHER
Definition globals.glsl:130
uniform float GROUND_ALBEDO
Definition globals.glsl:127
uniform float GROUND_ELEVATION
Definition globals.glsl:126
uniform int USE_GROUND_PLANE
Enable the ground.
Definition globals.glsl:125
uniform float GROUND_RADIUS
Definition globals.glsl:128
uniform int USE_BVH_HEATMAP
enables the BVH's heatmap view
Definition globals.glsl:341
uniform int BVH_HEATMAP_SCALE
Definition globals.glsl:345
uniform int FORCE_MATERIAL
Definition globals.glsl:214
const int MAT_SHADOW_CATCHER
Definition globals.glsl:210
uniform int triangle_count
Definition globals.glsl:315
uniform vec3 mesh_aabb_min
Bounding box of the loaded mesh to skip the triangle loop for rays that don't interact with the model...
Definition globals.glsl:310
uniform vec3 mesh_aabb_max
Definition globals.glsl:311
uniform sampler2DArray tex_albedo
Definition globals.glsl:262
uniform int USE_TEXTURES
Definition globals.glsl:265
uniform sampler2DArray tex_normal
Definition globals.glsl:263
GLsizei GLsizei * length
Definition imgui_impl_opengl3_loader.h:319
GLuint texture
Definition imgui_impl_opengl3_loader.h:231
GLint v0
Definition imgui_impl_opengl3_loader.h:329
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 triangleT(Ray ray, vec3 v0, vec3 v1, vec3 v2, out vec3 out_normal, out vec3 out_bary)
Definition intersection.glsl:178
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
#define MAT_DIFFUSE
Definition mesh.cpp:14
#define MAT_TINTED_GLASS
Definition mesh.cpp:17
#define MAT_GLASS
Definition mesh.cpp:16
float triangleArea(vec3 v0, vec3 v1, vec3 v2)
Definition sampling.glsl:37
bool intersects(const Ray ray, out Hit h)
Definition scene.glsl:179
void intersects_mesh(const Ray ray, inout Hit h)
Definition scene.glsl:7
uniform int SCENE_PRESET
Definition scene.glsl:5
A single node of the BVH, structured following the std430.
Definition globals.glsl:323
int right_child
Definition globals.glsl:329
vec3 aabb_max
Definition globals.glsl:326
int first_tri
Definition globals.glsl:330
vec3 aabb_min
Definition globals.glsl:324
int tri_count
Definition globals.glsl:331
int left_child
Definition globals.glsl:328
ior = index of refraction (only used with MAT_GLASS)
Definition globals.glsl:164
Definition globals.glsl:158
vec3 direction
Definition globals.glsl:160
vec3 origin
Definition globals.glsl:159