Encapsulate rendering program setup

This commit is contained in:
Aaron Fenyes 2025-04-21 15:56:32 -07:00
parent 360ce12d8b
commit 8a82fe457f

View file

@ -27,6 +27,45 @@ fn compile_shader(
shader shader
} }
fn create_program_with_shaders(
context: &WebGl2RenderingContext,
vertex_shader_source: &str,
fragment_shader_source: &str
) -> WebGlProgram {
// compile the shaders
let vertex_shader = compile_shader(
&context,
WebGl2RenderingContext::VERTEX_SHADER,
vertex_shader_source,
);
let fragment_shader = compile_shader(
&context,
WebGl2RenderingContext::FRAGMENT_SHADER,
fragment_shader_source,
);
// create the program and attach the shaders
let program = context.create_program().unwrap();
context.attach_shader(&program, &vertex_shader);
context.attach_shader(&program, &fragment_shader);
context.link_program(&program);
/* DEBUG */
// report whether linking succeeded
let link_status = context
.get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS)
.as_bool()
.unwrap();
let link_msg = if link_status {
"Linked successfully"
} else {
"Linking failed"
};
console::log_1(&JsValue::from(link_msg));
program
}
fn get_uniform_array_locations<const N: usize>( fn get_uniform_array_locations<const N: usize>(
context: &WebGl2RenderingContext, context: &WebGl2RenderingContext,
program: &WebGlProgram, program: &WebGlProgram,
@ -186,31 +225,12 @@ pub fn Display() -> View {
.dyn_into::<WebGl2RenderingContext>() .dyn_into::<WebGl2RenderingContext>()
.unwrap(); .unwrap();
// compile and attach the vertex and fragment shaders // create and use the rendering program
let vertex_shader = compile_shader( let program = create_program_with_shaders(
&ctx, &ctx,
WebGl2RenderingContext::VERTEX_SHADER,
include_str!("identity.vert"), include_str!("identity.vert"),
include_str!("inversive.frag")
); );
let fragment_shader = compile_shader(
&ctx,
WebGl2RenderingContext::FRAGMENT_SHADER,
include_str!("inversive.frag"),
);
let program = ctx.create_program().unwrap();
ctx.attach_shader(&program, &vertex_shader);
ctx.attach_shader(&program, &fragment_shader);
ctx.link_program(&program);
let link_status = ctx
.get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS)
.as_bool()
.unwrap();
let link_msg = if link_status {
"Linked successfully"
} else {
"Linking failed"
};
console::log_1(&JsValue::from(link_msg));
ctx.use_program(Some(&program)); ctx.use_program(Some(&program));
/* DEBUG */ /* DEBUG */