14void camera_axes(out vec3 cam_x, out vec3 cam_y, out vec3 cam_z) {
16 cam_x = normalize(cross(
camera_up, cam_z));
17 cam_y = cross(cam_z, cam_x);
27vec3
cameraRay(vec2 uv, vec3 cam_x, vec3 cam_y, vec3 cam_z,
float fov_scale) {
29 vec2 p = (2.0 * uv - 1.0) * vec2(aspect, 1.0);
37 float half_fov = 0.5 *
CAM_FOV * fov_scale;
40 float f_len = 1.0 / tan(half_fov);
41 vec3 ray_cam = vec3(p.x, p.y, -f_len);
42 return normalize(cam_x * ray_cam.x + cam_y * ray_cam.y + cam_z * ray_cam.z);
47 if(r <
EPS)
return -cam_z;
54 theta = 2.0 * atan(r * tan(half_fov * 0.5));
57 theta = 2.0 * asin(clamp(r * sin(half_fov * 0.5), -1.0, 1.0));
61 vec3 local_dir = vec3(dir2d * sin(theta), -cos(theta));
63 return normalize(cam_x * local_dir.x + cam_y * local_dir.y + cam_z * local_dir.z);
71 if(blades < 3)
return 1.0;
72 float corner =
TWO_PI / float(blades);
73 float a = mod(angle, corner) - corner * 0.5;
74 return cos(corner * 0.5) / cos(a);
85vec3
sampleAperture(vec3 cam_x, vec3 cam_y, vec2 uv,
float angle,
float radius_rand) {
89 vec2 disk = vec2(cos(angle), sin(angle)) * radius;
95 vec2 frame_offset = (uv - 0.5) * 2.0;
99 return cam_x * disk.x + cam_y * disk.y;
106vec3
focalPlanePoint(vec3 cam_x, vec3 cam_y, vec3 cam_z, vec3 ray_dir,
float focal_distance) {
114 float denom = dot(ray_dir, plane_normal);
127 vec3 cam_x, cam_y, cam_z;
133 vec3 rj =
rand3(-2, spp_index, px);
137 vec3 base_dir =
cameraRay(uv + jitter, cam_x, cam_y, cam_z, fov_scale);
138 vec3 focal_point =
focalPlanePoint(cam_x, cam_y, cam_z, base_dir, focal_distance);
141 float angle = rj.x *
TWO_PI;
142 vec3 rDOF =
rand3(-3, spp_index, px);
143 vec3 lens_offset =
sampleAperture(cam_x, cam_y, uv, angle, rDOF.x);
146 return Ray(origin, normalize(focal_point - origin));
165 float phi = atan(dir.y, dir.x);
166 float theta = acos(clamp(dir.z, -1.0, 1.0));
167 vec2 uv = vec2(phi /
TWO_PI + 0.5, theta /
PI);
199 vec3 rnd =
rand3(bounce + 200, spp_index, px);
206 float attenuation = 1.0;
209 switch (light.
type) {
213 float r = light.
radius * pow(rnd.y, 1.0 / 3.0);
214 float phi =
TWO_PI * rnd.z;
215 float cos_t = 2.0 * rnd.x - 1.0;
216 float sin_t = sqrt(1.0 - cos_t * cos_t);
217 offset = r * vec3(sin_t * cos(phi), sin_t * sin(phi), cos_t);
221 attenuation = 1.0 / (dist * dist);
240 attenuation = 1.0 / (dist * dist);
241 vec3 dir_to_dir = -normalize(to_light);
242 float cos_angle = dot(dir_to_dir, light.
direction.xyz);
245 if (attenuation <= 0)
return vec3(0);
252 vec3 dir_light = normalize(to_light);
253 float cos_surface = dot(h.
normal, dir_light);
254 if (cos_surface <= 0.0)
return vec3(0);
266 if (occluded)
return vec3(0);
269 float pdf_light = pdf_select * pdf_dir;
270 if (pdf_light <= 0.0)
return vec3(0);
282 return h.
albedo * light.
emission.rgb * cos_surface * weight / (
PI * pdf_light);
288 vec3 rnd =
rand3(bounce + 100, spp_index, px);
289 int light_idx = light_indices[int(rnd.x *
float(
light_count))];
293 vec3 emission = gpu_materials[m_id].emission.rgb;
295 if (dot(emission, emission) == 0.0)
return vec3(0);
301 vec3 to_light = light_pos - h.
pos;
302 float dist =
length(to_light);
303 vec3 dir_light = to_light / dist;
306 float cos_surface = dot(h.
normal, dir_light);
307 if (cos_surface <= 0.0)
return vec3(0);
311 float cos_light = dot(-dir_light, light_normal);
312 if(cos_light <= 0.0)
return vec3(0);
326 if (occluded)
return vec3(0);
331 if(pdf_light <= 0.0)
return vec3(0);
338 return h.
albedo * emission * cos_surface * weight / (
PI * pdf_light);
348 vec3 r =
rand3(bounce + 300, spp_index, px);
351 float cosT = sqrt(r.x);
352 float sinT = sqrt(1.0 - r.x);
354 vec3 local_dir = vec3(sinT * cos(phi), sinT * sin(phi), cosT);
355 vec3 world_dir =
onb(h.
normal) * local_dir;
366 if (occluded)
return vec3(0);
372 return h.
albedo * env_color * 0.5;
386 if(!
intersects(ray, h))
return vec4(0.0, 0.0, 0.0, 1.0);
389 return vec4(n * 0.5 + 0.5, 1.0);
403 vec3 color = vec3(0);
404 vec3 throughput = vec3(1);
407 int diffuse_bounces = 0;
411 float prev_bsdf_pdf = -1.0;
414 for (
int b = 0; b <
DEPTH; b++) {
421 if(cos_angle > cos(light.
radius)) {
422 float mis_weight = 1.0;
423 if (light.
radius > 0.0 && prev_bsdf_pdf >= 0.0) {
427 color += throughput * light.
emission.rgb * mis_weight;
433 float mis_weight = (prev_bsdf_pdf >= 0.0 && b < 3) ? 0.5 : 1.0;
442 color += throughput * vec3(0.9);
445 float t = clamp(ray.
direction.z * 0.5 + 0.5, 0.0, 1.0);
446 color += throughput * mix(vec3(0), vec3(1), t);
457 color += vec3(0.0, 1.0, 0.0) * 0.5;
464 float mis_weight = 1.0;
465 if(prev_bsdf_pdf >= 0.0) {
468 mis_weight = (pdf_light > 0.0) ?
powerHeuristic(prev_bsdf_pdf, pdf_light) : 1.0;
470 color += throughput * h.
emission * mis_weight;
491 vec3 r =
rand3(b, spp_index, px);
496 bool end_path =
false;
502 if(diffuse_bounces++ >= 2 &&
USE_NEE == 1) {
507 float cosT = sqrt(r.x);
508 float sinT = sqrt(1.0 - r.x);
520 prev_bsdf_pdf = -1.0;
533 float eta = h_entering ? (1.0 / ior) : ior;
535 float cos_theta = abs(dot(-ray.
direction, normal));
536 float r0 = (1.0 - eta) / (1.0 + eta);
538 float fresnel = r0 + (1.0 - r0) * pow(1.0 - cos_theta, 5.0);
540 float sin_theta_sq = eta * eta * (1.0 - cos_theta * cos_theta);
541 bool total_reflect = sin_theta_sq > 1.0;
543 if (total_reflect || r.z < fresnel) {
552 prev_bsdf_pdf = -1.0;
561 prev_bsdf_pdf = -1.0;
566 bool in_shadow =
false;
590 return vec4(color,
alpha);
598 float survival = max(throughput.r, max(throughput.g, throughput.b));
603 if (
rand3(b +
DEPTH, spp_index, px).x > survival)
break;
605 throughput /= survival;
610 if(max(throughput.r, max(throughput.g, throughput.b)) < 0.001)
break;
615 float lum = dot(color, vec3(0.2126, 0.7152, 0.0722));
620 return vec4(color,
alpha);
634 float final_alpha = 0.0;
635 for(
int c = 0; c < 3; c++) {
645 vec3 rc = traces[0], gc = traces[1], bc = traces[2];
647 final_color.r = 0.70 * rc.r + 0.25 * gc.r + 0.05 * bc.r;
648 final_color.g = 0.15 * rc.g + 0.70 * gc.g + 0.16 * bc.g;
649 final_color.b = 0.05 * rc.b + 0.25 * gc.b + 0.70 * bc.b;
651 return vec4(final_color, final_alpha / 3.0);
const float EPS_SHADOW
Definition globals.glsl:8
const float TWO_PI
Definition globals.glsl:3
const float EPS
Definition globals.glsl:6
const float PI
Definition globals.glsl:2
uniform int BACKGROUND
Color result if the ray doesn't reach the light.
Definition globals.glsl:118
uniform int USE_ENV_MAP
Definition globals.glsl:121
const int BG_GRADIENT
Definition globals.glsl:115
uniform sampler2D env_map
Definition globals.glsl:120
uniform float GROUND_SHADOW_OPACITY
Definition globals.glsl:131
const int BG_BLACK
Definition globals.glsl:113
const int BG_WHITE
Definition globals.glsl:114
uniform int CAM_APERTURE_BLADES
Definition globals.glsl:69
const float DISPERSION_COEFF[3]
Definition globals.glsl:59
uniform float CAM_APERTURE
Definition globals.glsl:15
uniform vec3 camera_position
Camera position in world space.
Definition globals.glsl:29
uniform float CAM_CATEYE_STRENGTH
Definition globals.glsl:84
const int PROJ_FISHEYE_EQUIDISTANT
Definition globals.glsl:94
uniform float CAM_TILT
Definition globals.glsl:104
uniform float CAM_DISTORTION_K2
Definition globals.glsl:90
uniform vec3 camera_lookat
Camera direction.
Definition globals.glsl:32
uniform float FOCAL_BAND_DEBUG
Thickness of the focal plane line.
Definition globals.glsl:24
const int PROJ_FISHEYE_STEREOGRAPHIC
Definition globals.glsl:95
const float DISPERSION_JITTER_WIDTH
Definition globals.glsl:65
uniform float CAM_FOCAL_DISTANCE
Worldspace distance between camera and focal plane.
Definition globals.glsl:18
uniform float CAM_BLADE_ROTATION
Rotates the polygon aperture shape (in radians).
Definition globals.glsl:72
uniform float CAM_AXIAL_CA
Definition globals.glsl:51
uniform float CAM_ANAMORPHIC_SQUEEZE
Definition globals.glsl:78
uniform bool FOCAL_DEBUG
Enables debug focal plane viewer.
Definition globals.glsl:21
uniform float CAM_DISTORTION_K1
Definition globals.glsl:89
uniform vec3 camera_up
Camera's up vector.
Definition globals.glsl:35
uniform float CAM_LATERAL_CA
Definition globals.glsl:43
const int PROJ_RECTILINEAR
Definition globals.glsl:93
uniform int CAM_PROJECTION_MODE
Camera projection model, see PROJ_* constants.
Definition globals.glsl:99
uniform float CAM_FOV
Definition globals.glsl:26
uniform int DEBUG_VIEW
Definition globals.glsl:355
const int DEBUG_VIEW_OFF
Definition globals.glsl:356
const int DEBUG_VIEW_GEOM_NORMALS
flat face normal (h.geom_normal)
Definition globals.glsl:362
const int MAT_SHADOW_CATCHER
Definition globals.glsl:210
uniform float GLASS_DISPERSION
Glass dispersion strength (prism effect).
Definition globals.glsl:219
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 int analytic_light_count
Definition globals.glsl:298
uniform int light_count
Definition globals.glsl:293
uniform float FIREFLY_CLAMP
Definition globals.glsl:199
uniform int DEPTH
Definition globals.glsl:174
uniform float RR_MAX_SURVIVAL
Definition globals.glsl:191
uniform int USE_NEE
Enables NEE.
Definition globals.glsl:194
uniform int RR_MIN_BOUNCES
Russian Roulette, minimum bounces before enabling. Changing to more or less gives minimal performance...
Definition globals.glsl:185
GLsizei GLsizei * length
Definition imgui_impl_opengl3_loader.h:319
GLfloat GLfloat GLfloat alpha
Definition imgui_impl_opengl3_loader.h:195
GLuint texture
Definition imgui_impl_opengl3_loader.h:231
GLuint GLuint64EXT * result
Definition imgui_impl_opengl3_loader.h:457
GLintptr offset
Definition imgui_impl_opengl3_loader.h:278
vector< GPULight > analytic_lights
Definition main.cpp:96
#define MAT_DIFFUSE
Definition mesh.cpp:14
#define MAT_MIRROR
Definition mesh.cpp:15
#define MAT_TINTED_GLASS
Definition mesh.cpp:17
#define MAT_GLASS
Definition mesh.cpp:16
#define LIGHT_POINT
Definition mesh.hpp:122
#define LIGHT_DIRECTIONAL
Definition mesh.hpp:123
#define LIGHT_SPOT
Definition mesh.hpp:124
uniform vec2 resolution
Definition path_trace.frag:14
float rayEps()
Scene-relative epsilon for ray origins, avoids self-intersection.
Definition path_trace_core.glsl:178
void camera_axes(out vec3 cam_x, out vec3 cam_y, out vec3 cam_z)
Calculates the base of the camera in world-space.
Definition path_trace_core.glsl:14
vec3 cameraRay(vec2 uv, vec3 cam_x, vec3 cam_y, vec3 cam_z, float fov_scale)
Definition path_trace_core.glsl:27
vec3 sampleAperture(vec3 cam_x, vec3 cam_y, vec2 uv, float angle, float radius_rand)
Samples a point on the camera's aperture (lens disk) Shaped by CAM_APERTURE_BLADES/CAM_BLADE_ROTATION...
Definition path_trace_core.glsl:85
vec3 sampleEmissiveTriangles(Hit h, int bounce, int spp_index, uvec2 px)
Definition path_trace_core.glsl:285
vec3 sampleEnvMap(vec3 dir)
Definition path_trace_core.glsl:164
vec4 pathTraceFromRay(Ray ray, int spp_index, uvec2 px, float disp_coeff)
Traces a single already-generated path through the bounce loop and returns the radiance....
Definition path_trace_core.glsl:402
vec3 sampleAnalyticLight(Hit h, int bounce, int spp_index, uvec2 px)
Definition path_trace_core.glsl:195
Ray cameraRayDOF(vec2 uv, int spp_index, uvec2 px)
Creates a ray with depth of field.
Definition path_trace_core.glsl:157
vec4 pathTrace(vec2 uv, int spp_index, uvec2 px)
Definition path_trace_core.glsl:623
Ray cameraRayDOFChannel(vec2 uv, int spp_index, uvec2 px, float disp_coeff)
Creates a ray with depth of field, using one channel's lens parameters.
Definition path_trace_core.glsl:126
vec4 pathTraceDebug(vec2 uv, int spp_index, uvec2 px)
Definition path_trace_core.glsl:383
float polygonAperture(float angle, int blades)
Max radius of a regular N-gon at a given angle.
Definition path_trace_core.glsl:70
vec3 focalPlanePoint(vec3 cam_x, vec3 cam_y, vec3 cam_z, vec3 ray_dir, float focal_distance)
Point on the (maybe tilted) focal plane that a given ray focuses on.
Definition path_trace_core.glsl:106
vec3 surfaceOrigin(Hit h)
Origin for any ray leaving an opaque surface on the side it was hit from (diffuse/mirror bounces and ...
Definition path_trace_core.glsl:188
vec3 sampleEnvLight(Hit h, int bounce, int spp_index, uvec2 px)
Definition path_trace_core.glsl:345
vec3 rand3(int bounce, int path_id, uvec2 px)
Definition sampling.glsl:13
vec3 sampleCone(vec3 axis, float theta_max, vec2 rnd)
Definition sampling.glsl:46
float conePDF(float theta_max)
Definition sampling.glsl:55
float triangleArea(vec3 v0, vec3 v1, vec3 v2)
Definition sampling.glsl:37
float bsdfPDF_diffuse(vec3 normal, vec3 dir)
Definition sampling.glsl:71
mat3 onb(vec3 n)
Definition sampling.glsl:23
vec3 sampleTriangle(vec3 v0, vec3 v1, vec3 v2, vec2 rnd)
Samples a random point in the triangle surface uniformly distributed, based on R. Osada,...
Definition sampling.glsl:30
float trianglelightPDF(float dist, float cos_light, float area)
Definition sampling.glsl:77
float powerHeuristic(float pdf_a, float pdf_b)
Definition sampling.glsl:62
bool intersects(const Ray ray, out Hit h)
Definition scene.glsl:179
Definition globals.glsl:267
float radius
Definition globals.glsl:272
int type
Definition globals.glsl:271
vec4 direction
Definition globals.glsl:270
vec4 emission
Definition globals.glsl:269
vec4 position
Definition globals.glsl:268
float spot_outer
Definition globals.glsl:274
float spot_inner
Definition globals.glsl:273
Definition globals.glsl:245
GPUVertex v2
Definition globals.glsl:246
int material_id
Definition globals.glsl:247
GPUVertex v1
Definition globals.glsl:246
GPUVertex v0
Definition globals.glsl:246
vec3 position
Definition globals.glsl:236
ior = index of refraction (only used with MAT_GLASS)
Definition globals.glsl:164
vec3 normal
Definition globals.glsl:166
vec3 lift
Offset from the flat triangle to the smooth surface, for rays leaving it.
Definition globals.glsl:171
float t
Definition globals.glsl:165
vec3 geom_normal
Definition globals.glsl:166
int material
Definition globals.glsl:168
float ior
Definition globals.glsl:169
vec3 albedo
Definition globals.glsl:167
vec3 pos
Definition globals.glsl:166
float light_area
Used for MIS.
Definition globals.glsl:170
vec3 emission
Definition globals.glsl:167
Definition globals.glsl:158
vec3 direction
Definition globals.glsl:160
vec3 origin
Definition globals.glsl:159