diff --git a/app-proto/src/display.rs b/app-proto/src/display.rs index 4e0c7e4..e7478c1 100644 --- a/app-proto/src/display.rs +++ b/app-proto/src/display.rs @@ -27,6 +27,45 @@ fn compile_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( context: &WebGl2RenderingContext, program: &WebGlProgram, @@ -186,31 +225,12 @@ pub fn Display() -> View { .dyn_into::() .unwrap(); - // compile and attach the vertex and fragment shaders - let vertex_shader = compile_shader( + // create and use the rendering program + let program = create_program_with_shaders( &ctx, - WebGl2RenderingContext::VERTEX_SHADER, 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)); /* DEBUG */