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

@ -8,9 +8,13 @@
//
extern crate js_sys;
use core::array;
use nalgebra::DVector;
use sycamore::{prelude::*, rt::{JsCast, JsValue}};
use web_sys::{console, WebGl2RenderingContext, WebGlShader};
mod engine;
fn compile_shader(
context: &WebGl2RenderingContext,
shader_type: u32,
@ -70,6 +74,7 @@ fn main() {
console_error_panic_hook::set_once();
sycamore::render(|| {
// controls
let ctrl_x = create_signal(0.0);
let ctrl_y = create_signal(0.0);
let radius_x = create_signal(1.0);
@ -77,9 +82,16 @@ fn main() {
let opacity = create_signal(0.5);
let highlight = create_signal(0.2);
let layer_threshold = create_signal(0.0);
let use_test_construction = create_signal(false);
// display
let display = create_node_ref();
on_mount(move || {
// list construction elements
const SPHERE_MAX: usize = 256;
let mut sphere_vec = Vec::<DVector<f64>>::new();
// get the display canvas
let canvas = display
.get::<DomNode>()
@ -119,14 +131,21 @@ 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_lt_locs = array::from_fn::<_, SPHERE_MAX, _>(
|n| ctx.get_uniform_location(&program, format!("sphere_list[{}].lt", n).as_str())
);
let position_index = ctx.get_attrib_location(&program, "position") as u32;
let resolution_loc = ctx.get_uniform_location(&program, "resolution");
let shortdim_loc = ctx.get_uniform_location(&program, "shortdim");
let ctrl_loc = ctx.get_uniform_location(&program, "ctrl");
let radius_loc = ctx.get_uniform_location(&program, "radius");
let ctrl_loc = ctx.get_uniform_location(&program, "ctrl"); /* DEBUG */
let radius_loc = ctx.get_uniform_location(&program, "radius"); /* DEBUG */
let opacity_loc = ctx.get_uniform_location(&program, "opacity");
let highlight_loc = ctx.get_uniform_location(&program, "highlight");
let layer_threshold_loc = ctx.get_uniform_location(&program, "layer_threshold");
let use_test_construction_loc = ctx.get_uniform_location(&program, "use_test_construction");
// create a vertex array and bind it to the graphics context
let vertex_array = ctx.create_vertex_array().unwrap();
@ -148,18 +167,38 @@ fn main() {
// set up a repainting routine
create_effect(move || {
// update the construction
sphere_vec.clear();
sphere_vec.push(engine::sphere(0.5, 0.5, -5.0 + ctrl_x.get(), radius_x.get()));
sphere_vec.push(engine::sphere(-0.5, -0.5, -5.0 + ctrl_y.get(), radius_y.get()));
sphere_vec.push(engine::sphere(-0.5, 0.5, -5.0, 0.75));
// set the resolution
let width = canvas.width() as f32;
let height = canvas.height() as f32;
ctx.uniform2f(resolution_loc.as_ref(), width, height);
ctx.uniform1f(shortdim_loc.as_ref(), width.min(height));
// pass the construction
for n in 0..sphere_vec.len() {
let v = &sphere_vec[n];
ctx.uniform3f(
sphere_sp_locs[n].as_ref(),
v[0] as f32, v[1] as f32, v[2] as f32
);
ctx.uniform2f(
sphere_lt_locs[n].as_ref(),
v[3] as f32, v[4] as f32
);
}
// pass the control parameters
ctx.uniform2f(ctrl_loc.as_ref(), ctrl_x.get() as f32, ctrl_y.get() as f32);
ctx.uniform2f(radius_loc.as_ref(), radius_x.get() as f32, radius_y.get() as f32);
ctx.uniform2f(ctrl_loc.as_ref(), ctrl_x.get() as f32, ctrl_y.get() as f32); /* DEBUG */
ctx.uniform2f(radius_loc.as_ref(), radius_x.get() as f32, radius_y.get() as f32); /* DEBUG */
ctx.uniform1f(opacity_loc.as_ref(), opacity.get() as f32);
ctx.uniform1f(highlight_loc.as_ref(), highlight.get() as f32);
ctx.uniform1i(layer_threshold_loc.as_ref(), layer_threshold.get() as i32);
ctx.uniform1i(use_test_construction_loc.as_ref(), use_test_construction.get() as i32);
// draw the scene
ctx.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, VERTEX_CNT as i32);
@ -215,6 +254,10 @@ fn main() {
step=1.0,
bind:valueAsNumber=layer_threshold
)
input(
type="checkbox",
bind:checked=use_test_construction
)
}
}
});