Ray-caster: automate getting uniform array locations

This commit is contained in:
Aaron Fenyes 2024-08-25 22:22:14 -07:00
parent 5bf23fa789
commit c5fe725b1b

View File

@ -11,7 +11,7 @@ extern crate js_sys;
use core::array;
use nalgebra::DVector;
use sycamore::{prelude::*, rt::{JsCast, JsValue}};
use web_sys::{console, WebGl2RenderingContext, WebGlShader};
use web_sys::{console, WebGl2RenderingContext, WebGlProgram, WebGlShader, WebGlUniformLocation};
mod engine;
@ -26,6 +26,18 @@ fn compile_shader(
shader
}
fn get_uniform_array_locations<const N: usize>(
context: &WebGl2RenderingContext,
program: &WebGlProgram,
var_name: &str,
member_name: &str
) -> [Option<WebGlUniformLocation>; N] {
array::from_fn(|n| {
let name = format!("{var_name}[{n}].{member_name}");
context.get_uniform_location(&program, name.as_str())
})
}
// load the given data into the vertex input of the given name
fn bind_vertex_attrib(
context: &WebGl2RenderingContext,
@ -131,11 +143,11 @@ fn main() {
ctx.use_program(Some(&program));
// find indices of vertex attributes and uniforms
let sphere_sp_locs = array::from_fn::<_, SPHERE_MAX, _>(
|n| ctx.get_uniform_location(&program, format!("sphere_list[{}].sp", n).as_str())
let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", "sp"
);
let sphere_lt_locs = array::from_fn::<_, SPHERE_MAX, _>(
|n| ctx.get_uniform_location(&program, format!("sphere_list[{}].lt", n).as_str())
let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", "lt"
);
let position_index = ctx.get_attrib_location(&program, "position") as u32;
let resolution_loc = ctx.get_uniform_location(&program, "resolution");