Path Tracing
Loading...
Searching...
No Matches
scene.glsl
Go to the documentation of this file.
1#include "cornell_scene.glsl"
2
5uniform int SCENE_PRESET;
6
7void intersects_mesh(const Ray ray, inout Hit h) {
8 if(triangle_count == 0) return;
9
10 //AABB Early Rejection
11 //Converts uniform min/max corners to center/half_size for boxT
12 //Avoids O(triangle_count) tests for most rays
13 vec3 aabb_center = (mesh_aabb_min + mesh_aabb_max) * 0.5;
14 vec3 aabb_half_size = (mesh_aabb_max - mesh_aabb_min) * 0.5;
15 vec3 aabb_normal;
16 float aabb_t = boxTAxisAligned(ray, aabb_center, aabb_half_size, aabb_normal);
17
18 if(aabb_t >= h.t) return;
19
23 int stack[32];
24 int stack_top = 0;
25 int node_id = 0;
26
27 int nodes_visited = 0, tris_tested = 0;
28
29 while(true) {
30 BVHNode node = bvh_nodes[node_id];
31 nodes_visited++;
32
33 if (node.tri_count > 0) {
34 tris_tested += node.tri_count;
35 for (int i = node.first_tri; i < node.first_tri + node.tri_count; i++) {
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);
38
39 if (t < h.t) {
40 int m_id = triangles[i].material_id;
41
42 vec2 uv = triangles[i].v0.texcoord * tri_bary.z + triangles[i].v1.texcoord * tri_bary.x + triangles[i].v2.texcoord * tri_bary.y;
43
44 if(USE_TEXTURES == 1 && gpu_materials[m_id].tex_index >= 0) {
45 if (texture(tex_albedo, vec3(uv, float(gpu_materials[m_id].tex_index))).a < 0.5)
46 continue;
47 }
48
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;
50
51 h.t = t;
52 h.pos = ray.origin + t * ray.direction;
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;
57 if(FORCE_MATERIAL >= 0) {
58 h.material = FORCE_MATERIAL;
59 }
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);
62
63 //tri_normal already faces the incoming ray (triangleT flips it), the vertex normals
64 //may not: back faces of open meshes, inverted parts.
65 //side = +1 when they agree, -1 when the ray hit the surface from behind
66 float side = dot(h.normal, tri_normal) < 0.0 ? -1.0 : 1.0;
67
68 //NORMAL MAPPING (perturbs h.normal for shading)
69 //Done in the asset's own (outward) frame, before the side flip below.
70 //h.geom_normal stays the pure flat face normal,
71 //being used for shadow-rays, so don't change that one
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;
74 vec3 N = h.normal;
75
76 //T collapses when interpolating between nearly opposite tangents
77 vec3 T_raw = tangent_interp.xyz - N * dot(tangent_interp.xyz, N);
78 float T_len = length(T_raw);
79
80 if(T_len > 1e-6) {
81 vec3 T = T_raw / T_len;
82 //w is +-1 per vertex, but interpolated across a mirror seam it can approach 0
83 vec3 B = cross(N, T) * (tangent_interp.w < 0.0 ? -1.0 : 1.0);
84
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);
91
92 //A mapped normal below the flat triangle would send most bounces into the surface
93 if(m_len > 1e-6 && dot(mapped, side * tri_normal) > 0.0)
94 h.normal = mapped / m_len;
95 }
96 }
97
98 //Opaque surfaces are shaded on the side the ray came from.
99 //Glass keeps the outward normal, it's how it knows if it's entering or leaving
100 if(h.material != MAT_GLASS && h.material != MAT_TINTED_GLASS)
101 h.normal *= side;
102
103 //Shadow terminator: rays leaving the surface start on the smooth surface, not the flat triangle
104 h.lift = terminatorLift(h.pos,
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)); //same weights as smooth_normal
108
109 //Texture sample
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;
112 }
113 else {
114 h.albedo = gpu_materials[m_id].albedo.rgb;
115 }
116 }
117 }
118 }
119 else { //BVH magic
120 BVHNode left_node = bvh_nodes[node.left_child];
121 BVHNode right_node = bvh_nodes[node.right_child];
122
123 vec3 lc = (left_node.aabb_min + left_node.aabb_max) * 0.5;
124 vec3 lh = (left_node.aabb_max - left_node.aabb_min) * 0.5;
125 vec3 rc = (right_node.aabb_min + right_node.aabb_max) * 0.5;
126 vec3 rh = (right_node.aabb_max - right_node.aabb_min) * 0.5;
127 vec3 nn;
128
129 float t_left = boxTAxisAligned(ray, lc, lh, nn);
130 float t_right = boxTAxisAligned(ray, rc, rh, nn);
131
132 bool hit_left = t_left < h.t;
133 bool hit_right = t_right < h.t;
134
137 int near_child = node.left_child, far_child = node.right_child;
138 bool hit_near = hit_left, hit_far = hit_right;
139
140 if(t_right < t_left) {
141 near_child = node.right_child;
142 far_child = node.left_child;
143 hit_near = hit_right;
144 hit_far = hit_left;
145 }
146 if (hit_near) {
147 if(hit_far) stack[stack_top++] = far_child;
148 node_id = near_child;
149 continue;
150 }
151 if (hit_far) {
152 node_id = far_child;
153 continue;
154 }
155 }
156 if(stack_top == 0) break;
157 node_id = stack[--stack_top];
158 }
159
160 if (USE_BVH_HEATMAP == 1) {
161 float heat = clamp(float(nodes_visited) / float(BVH_HEATMAP_SCALE), 0.0, 1.0);
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);
165
166 vec3 heatmap_color = heat < 0.5 ? mix(cold, warm, heat * 2.0) : mix(warm, hot, (heat - 0.5) * 2.0);
167
168 h.t = max(aabb_t, 0.001);
169 h.pos = ray.origin + h.t * ray.direction;
170 h.normal = vec3(0.0, 1.0, 0.0);
171 h.albedo = vec3(0.0);
172 h.emission = heatmap_color;
173 h.material = MAT_DIFFUSE;
174 h.ior = 1.0;
175 h.lift = vec3(0);
176 }
177}
178
179bool intersects(const Ray ray, out Hit h) {
180 h.t = INF;
181 h.pos = vec3(0);
182 h.normal = vec3(0, 0, 1);
183 h.geom_normal = vec3(0, 0, 1);
184 h.albedo = WHITE;
185 h.emission = vec3(0);
186 h.material = MAT_DIFFUSE;
187 h.ior = 0.0;
188 h.light_area = 0.0;
189 h.lift = vec3(0);
190
191 if(SCENE_PRESET == 1 || SCENE_PRESET == 2) {
192 intersects_cornell(ray, h);
193 h.lift = vec3(0); //"out Hit" rewrites the whole struct; analytic planes have nothing to lift
194 }
195 if(SCENE_PRESET == 0 || SCENE_PRESET == 1 || SCENE_PRESET == 3)
196 intersects_mesh(ray, h);
197
198 if(USE_GROUND_PLANE == 1) {
199 float ray_dist = planeT_Z(ray, GROUND_ELEVATION);
200 if(ray_dist > 0.0 && ray_dist < h.t) {
201 vec3 hit_plane = ray.origin + ray_dist * ray.direction;
202 float dist_from_center = length(hit_plane.xy);
203
204 if(dist_from_center < GROUND_RADIUS) {
205 h.t = ray_dist;
206 h.pos = hit_plane;
207 h.normal = vec3(0,0,1);
208 h.geom_normal = vec3(0,0,1);
209 h.lift = vec3(0);
210 h.albedo = vec3(GROUND_ALBEDO);
211 h.emission = vec3(0);
213 h.ior = 0.0;
214 }
215 }
216 }
217
218 return h.t < INF;
219}
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