Display: get the assembly from the app state

This commit is contained in:
Aaron Fenyes 2024-09-15 11:31:22 -07:00
parent cd18d594e0
commit f47be08d98
2 changed files with 16 additions and 21 deletions

View File

@ -1,5 +1,5 @@
use core::array; use core::array;
use nalgebra::{DMatrix, DVector, Rotation3, Vector3}; use nalgebra::{DMatrix, Rotation3, Vector3};
use sycamore::{prelude::*, motion::create_raf}; use sycamore::{prelude::*, motion::create_raf};
use web_sys::{ use web_sys::{
console, console,
@ -11,6 +11,8 @@ use web_sys::{
wasm_bindgen::{JsCast, JsValue} wasm_bindgen::{JsCast, JsValue}
}; };
use crate::AppState;
fn compile_shader( fn compile_shader(
context: &WebGl2RenderingContext, context: &WebGl2RenderingContext,
shader_type: u32, shader_type: u32,
@ -81,6 +83,8 @@ fn bind_vertex_attrib(
#[component] #[component]
pub fn Display() -> View { pub fn Display() -> View {
let state = use_context::<AppState>();
// canvas // canvas
let display = create_node_ref(); let display = create_node_ref();
@ -104,12 +108,6 @@ pub fn Display() -> View {
let mean_frame_interval = create_signal(0.0); let mean_frame_interval = create_signal(0.0);
on_mount(move || { on_mount(move || {
/* SCAFFOLDING */
/* create list of construction elements */
const SPHERE_MAX: usize = 200;
let mut sphere_vec = Vec::<DVector<f64>>::new();
let mut color_vec = Vec::<[f32; 3]>::new();
// timing // timing
let mut last_time = 0.0; let mut last_time = 0.0;
@ -183,6 +181,7 @@ pub fn Display() -> View {
); );
// find indices of vertex attributes and uniforms // find indices of vertex attributes and uniforms
const SPHERE_MAX: usize = 200;
let position_index = ctx.get_attrib_location(&program, "position") as u32; let position_index = ctx.get_attrib_location(&program, "position") as u32;
let sphere_cnt_loc = ctx.get_uniform_location(&program, "sphere_cnt"); let sphere_cnt_loc = ctx.get_uniform_location(&program, "sphere_cnt");
let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>( let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>(
@ -280,15 +279,11 @@ pub fn Display() -> View {
}; };
let construction_to_world = &location * &orientation; let construction_to_world = &location * &orientation;
// update the construction // get the construction
sphere_vec.clear(); let elements = state.assembly.elements.get_clone();
sphere_vec.push(&construction_to_world * DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25])); let element_iter = (&elements).into_iter();
sphere_vec.push(&construction_to_world * DVector::<f64>::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25])); let reps_world: Vec<_> = element_iter.clone().map(|elt| &construction_to_world * &elt.rep).collect();
sphere_vec.push(&construction_to_world * DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625])); let colors: Vec<_> = element_iter.map(|elt| elt.color).collect();
color_vec.clear();
color_vec.push([1.00_f32, 0.25_f32, 0.00_f32]);
color_vec.push([0.00_f32, 0.25_f32, 1.00_f32]);
color_vec.push([0.75_f32, 0.75_f32, 0.75_f32]);
// set the resolution // set the resolution
let width = canvas.width() as f32; let width = canvas.width() as f32;
@ -297,9 +292,9 @@ pub fn Display() -> View {
ctx.uniform1f(shortdim_loc.as_ref(), width.min(height)); ctx.uniform1f(shortdim_loc.as_ref(), width.min(height));
// pass the construction // pass the construction
ctx.uniform1i(sphere_cnt_loc.as_ref(), sphere_vec.len() as i32); ctx.uniform1i(sphere_cnt_loc.as_ref(), elements.len() as i32);
for n in 0..sphere_vec.len() { for n in 0..reps_world.len() {
let v = &sphere_vec[n]; let v = &reps_world[n];
ctx.uniform3f( ctx.uniform3f(
sphere_sp_locs[n].as_ref(), sphere_sp_locs[n].as_ref(),
v[0] as f32, v[1] as f32, v[2] as f32 v[0] as f32, v[1] as f32, v[2] as f32
@ -310,7 +305,7 @@ pub fn Display() -> View {
); );
ctx.uniform3fv_with_f32_array( ctx.uniform3fv_with_f32_array(
color_locs[n].as_ref(), color_locs[n].as_ref(),
&color_vec[n] &colors[n]
); );
} }

View File

@ -36,7 +36,7 @@ fn main() {
id: String::from("central"), id: String::from("central"),
label: String::from("Central"), label: String::from("Central"),
color: [0.75_f32, 0.75_f32, 0.75_f32], color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.25, 1.0]) rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625])
} }
]) ])
} }