From 8798683d25c8e24dba2592e06efa5905a6d37783 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 25 Aug 2024 00:00:28 -0700 Subject: [PATCH] Ray-caster: store sphere data in arrays This is a first step toward general depth sorting. --- .../inversive-display/src/inversive.frag | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/app-proto/inversive-display/src/inversive.frag b/app-proto/inversive-display/src/inversive.frag index a4cbe8d..12b4129 100644 --- a/app-proto/inversive-display/src/inversive.frag +++ b/app-proto/inversive-display/src/inversive.frag @@ -121,29 +121,31 @@ vec2 sphere_cast(vecInv v, vec3 dir) { } void main() { + const int sphere_cnt = 2; + vec2 scr = (2.*gl_FragCoord.xy - resolution) / shortdim; vec3 dir = vec3(focal_slope * scr, -1.); // initialize two spheres - vecInv v [2]; - v[0] = sphere(vec3(0.5, 0.5, -5. + ctrl.x), radius.x); - v[1] = sphere(vec3(-0.5, -0.5, -5. + ctrl.y), radius.y); - vec3 color0 = vec3(1., 0.214, 0.); - vec3 color1 = vec3(0., 0.214, 1.); + vecInv sphere_list [sphere_cnt]; + sphere_list[0] = sphere(vec3(0.5, 0.5, -5. + ctrl.x), radius.x); + sphere_list[1] = sphere(vec3(-0.5, -0.5, -5. + ctrl.y), radius.y); + vec3 color_list [sphere_cnt]; + color_list[0] = vec3(1., 0.214, 0.); + color_list[1] = vec3(0., 0.214, 1.); // cast rays through the spheres - vec2 u0 = sphere_cast(v[0], dir); - vec2 u1 = sphere_cast(v[1], dir); + vec2 depth_pairs [sphere_cnt]; + taggedFrag frags [2*sphere_cnt]; + for (int i = 0; i < sphere_cnt; i++) { + vec2 hit_depths = sphere_cast(sphere_list[i], dir); + frags[2*i] = sphere_shading(sphere_list[i], hit_depths[0] * dir, color_list[i], i); + frags[2*i+1] = sphere_shading(sphere_list[i], hit_depths[1] * dir, color_list[i], i); + } // shade and depth-sort the impact points - taggedFrag front_hits[2] = sort( - sphere_shading(v[0], u0[0] * dir, color0, 0), - sphere_shading(v[1], u1[0] * dir, color1, 1) - ); - taggedFrag back_hits[2] = sort( - sphere_shading(v[0], u0[1] * dir, color0, 0), - sphere_shading(v[1], u1[1] * dir, color1, 1) - ); + taggedFrag front_hits[2] = sort(frags[0], frags[2]); + taggedFrag back_hits[2] = sort(frags[1], frags[3]); taggedFrag middle_frags[2] = sort(front_hits[1], back_hits[0]); // finish depth sorting @@ -170,7 +172,7 @@ void main() { // cusps float cusp_cos = abs(dot(dir, frag0.normal)); - float cusp_threshold = 2.*sqrt(ixn_threshold * v[frag0.id].lt.s); + float cusp_threshold = 2.*sqrt(ixn_threshold * sphere_list[frag0.id].lt.s); float cusp_highlight = highlight * (1. - smoothstep(2./3.*cusp_threshold, 1.5*cusp_threshold, cusp_cos)); frags_by_depth[i].color = mix(frags_by_depth[i].color, vec4(1.), cusp_highlight); }