Application prototype #14

Merged
glen merged 101 commits from app-proto into main 2024-10-21 23:38:28 +00:00
2 changed files with 27 additions and 10 deletions
Showing only changes of commit cbec31f5df - Show all commits

View File

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

View File

@ -30,10 +30,13 @@ fn get_uniform_array_locations<const N: usize>(
context: &WebGl2RenderingContext, context: &WebGl2RenderingContext,
program: &WebGlProgram, program: &WebGlProgram,
var_name: &str, var_name: &str,
member_name: &str member_name_opt: Option<&str>
) -> [Option<WebGlUniformLocation>; N] { ) -> [Option<WebGlUniformLocation>; N] {
array::from_fn(|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()) context.get_uniform_location(&program, name.as_str())
}) })
} }
@ -103,6 +106,11 @@ fn main() {
// list construction elements // list construction elements
const SPHERE_MAX: usize = 12; const SPHERE_MAX: usize = 12;
let mut sphere_vec = Vec::<DVector<f64>>::new(); 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 // get the display canvas
let canvas = display let canvas = display
@ -163,10 +171,13 @@ fn main() {
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>(
&ctx, &program, "sphere_list", "sp" &ctx, &program, "sphere_list", Some("sp")
); );
let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>( 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 resolution_loc = ctx.get_uniform_location(&program, "resolution");
let shortdim_loc = ctx.get_uniform_location(&program, "shortdim"); let shortdim_loc = ctx.get_uniform_location(&program, "shortdim");
@ -221,6 +232,10 @@ fn main() {
sphere_lt_locs[n].as_ref(), sphere_lt_locs[n].as_ref(),
v[3] as f32, v[4] as f32 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 // pass the control parameters