Ray-caster: pass colors in through uniforms

This commit is contained in:
Aaron Fenyes 2024-08-26 13:41:34 -07:00
parent b9370ceb41
commit cbec31f5df
2 changed files with 27 additions and 10 deletions

View File

@ -28,6 +28,7 @@ vecInv sphere(vec3 center, float radius) {
const int SPHERE_MAX = 12;
uniform int sphere_cnt;
uniform vecInv sphere_list[SPHERE_MAX];
uniform vec3 color_list[SPHERE_MAX];
// view
uniform vec2 resolution;
@ -135,6 +136,7 @@ void main() {
// initialize two spheres
vecInv sphere_list_internal [SPHERE_MAX];
vec3 color_list_internal [SPHERE_MAX];
if (use_test_construction) {
/* DEBUG */
// spheres 0 and 1 are identical in the test construction hard-coded
@ -144,15 +146,15 @@ void main() {
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);
color_list_internal[0] = vec3(1., 0.25, 0.);
color_list_internal[1] = vec3(0., 0.25, 1.);
color_list_internal[2] = vec3(0.25, 0., 1.0);
} else {
for (int i = 0; i < sphere_cnt; ++i) {
sphere_list_internal[i] = sphere_list[i];
color_list_internal[i] = color_list[i];
}
}
vec3 color_list [SPHERE_MAX];
color_list[0] = vec3(1., 0.25, 0.);
color_list[1] = vec3(0., 0.25, 1.);
color_list[2] = vec3(0.25, 0., 1.0);
// cast rays through the spheres
vec2 depth_pairs [SPHERE_MAX];
@ -161,11 +163,11 @@ void main() {
for (int i = 0; i < sphere_cnt; ++i) {
vec2 hit_depths = sphere_cast(sphere_list_internal[i], dir);
if (!isnan(hit_depths[0])) {
frags[frag_cnt] = sphere_shading(sphere_list_internal[i], hit_depths[0] * dir, color_list[i], i);
frags[frag_cnt] = sphere_shading(sphere_list_internal[i], hit_depths[0] * dir, color_list_internal[i], i);
++frag_cnt;
}
if (!isnan(hit_depths[1])) {
frags[frag_cnt] = sphere_shading(sphere_list_internal[i], hit_depths[1] * dir, color_list[i], i);
frags[frag_cnt] = sphere_shading(sphere_list_internal[i], hit_depths[1] * dir, color_list_internal[i], i);
++frag_cnt;
}
}

View File

@ -30,10 +30,13 @@ fn get_uniform_array_locations<const N: usize>(
context: &WebGl2RenderingContext,
program: &WebGlProgram,
var_name: &str,
member_name: &str
member_name_opt: Option<&str>
) -> [Option<WebGlUniformLocation>; N] {
array::from_fn(|n| {
let name = format!("{var_name}[{n}].{member_name}");
let name = match member_name_opt {
Some(member_name) => format!("{var_name}[{n}].{member_name}"),
None => format!("{var_name}[{n}]")
};
context.get_uniform_location(&program, name.as_str())
})
}
@ -103,6 +106,11 @@ fn main() {
// list construction elements
const SPHERE_MAX: usize = 12;
let mut sphere_vec = Vec::<DVector<f64>>::new();
let color_vec = vec![
[1.00_f32, 0.25_f32, 0.00_f32],
[0.00_f32, 0.25_f32, 1.00_f32],
[0.25_f32, 0.00_f32, 1.00_f32]
];
// get the display canvas
let canvas = display
@ -163,10 +171,13 @@ fn main() {
let position_index = ctx.get_attrib_location(&program, "position") as u32;
let sphere_cnt_loc = ctx.get_uniform_location(&program, "sphere_cnt");
let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", "sp"
&ctx, &program, "sphere_list", Some("sp")
);
let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", "lt"
&ctx, &program, "sphere_list", Some("lt")
);
let color_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "color_list", None
);
let resolution_loc = ctx.get_uniform_location(&program, "resolution");
let shortdim_loc = ctx.get_uniform_location(&program, "shortdim");
@ -221,6 +232,10 @@ fn main() {
sphere_lt_locs[n].as_ref(),
v[3] as f32, v[4] as f32
);
ctx.uniform3fv_with_f32_array(
color_locs[n].as_ref(),
&color_vec[n]
);
}
// pass the control parameters