Ray-caster: pass spheres in through uniforms

Keep the hard-coded spheres for comparison.
This commit is contained in:
Aaron Fenyes 2024-08-25 21:40:46 -07:00
parent 206a2df480
commit 5bf23fa789
4 changed files with 103 additions and 29 deletions

View file

@ -4,6 +4,29 @@ precision highp float;
out vec4 outColor;
// --- inversive geometry ---
struct vecInv {
vec3 sp;
vec2 lt;
};
vecInv sphere(vec3 center, float radius) {
return vecInv(
center / radius,
vec2(
0.5 / radius,
0.5 * (dot(center, center) / radius - radius)
)
);
}
// --- uniforms ---
// construction
const int SPHERE_MAX = 256;
uniform vecInv sphere_list[SPHERE_MAX];
// view
uniform vec2 resolution;
uniform float shortdim;
@ -14,6 +37,7 @@ uniform vec2 radius;
uniform float opacity;
uniform float highlight;
uniform int layer_threshold;
uniform bool use_test_construction;
// light and camera
const float focal_slope = 0.3;
@ -46,23 +70,6 @@ vec3 sRGB(vec3 color) {
return vec3(sRGB(color.r), sRGB(color.g), sRGB(color.b));
}
// --- inversive geometry ---
struct vecInv {
vec3 sp;
vec2 lt;
};
vecInv sphere(vec3 center, float radius) {
return vecInv(
center / radius,
vec2(
0.5 / radius,
0.5 * (dot(center, center) / radius - radius)
)
);
}
// --- shading ---
struct taggedFrag {
@ -127,10 +134,21 @@ void main() {
vec3 dir = vec3(focal_slope * scr, -1.);
// initialize two spheres
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);
sphere_list[2] = sphere(vec3(-0.5, 0.5, -5.), 0.75);
vecInv sphere_list_internal [sphere_cnt];
if (use_test_construction) {
/* DEBUG */
// spheres 0 and 1 are identical in the test construction hard-coded
// here and the construction passed in through uniforms. sphere 2 has
// a different radius in the construction; we can use that to show that
// the switch is working
sphere_list_internal[0] = sphere(vec3(0.5, 0.5, -5. + ctrl.x), radius.x);
sphere_list_internal[1] = sphere(vec3(-0.5, -0.5, -5. + ctrl.y), radius.y);
sphere_list_internal[2] = sphere(vec3(-0.5, 0.5, -5.), 0.5);
} else {
for (int i = 0; i < sphere_cnt; ++i) {
sphere_list_internal[i] = sphere_list[i];
}
}
vec3 color_list [sphere_cnt];
color_list[0] = vec3(1., 0.25, 0.);
color_list[1] = vec3(0., 0.25, 1.);
@ -141,13 +159,13 @@ void main() {
taggedFrag frags [2*sphere_cnt];
int frag_cnt = 0;
for (int i = 0; i < sphere_cnt; ++i) {
vec2 hit_depths = sphere_cast(sphere_list[i], dir);
vec2 hit_depths = sphere_cast(sphere_list_internal[i], dir);
if (!isnan(hit_depths[0])) {
frags[frag_cnt] = sphere_shading(sphere_list[i], hit_depths[0] * dir, color_list[i], i);
frags[frag_cnt] = sphere_shading(sphere_list_internal[i], hit_depths[0] * dir, color_list[i], i);
++frag_cnt;
}
if (!isnan(hit_depths[1])) {
frags[frag_cnt] = sphere_shading(sphere_list[i], hit_depths[1] * dir, color_list[i], i);
frags[frag_cnt] = sphere_shading(sphere_list_internal[i], hit_depths[1] * dir, color_list[i], i);
++frag_cnt;
}
}
@ -182,7 +200,7 @@ void main() {
// cusps
float cusp_cos = abs(dot(dir, frag0.normal));
float cusp_threshold = 2.*sqrt(ixn_threshold * sphere_list[frag0.id].lt.s);
float cusp_threshold = 2.*sqrt(ixn_threshold * sphere_list_internal[frag0.id].lt.s);
float cusp_highlight = highlight * (1. - smoothstep(2./3.*cusp_threshold, 1.5*cusp_threshold, cusp_cos));
frags[i].color = mix(frags[i].color, vec4(1.), cusp_highlight);
}