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

@ -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