From 420d3366d1e20c58e53c3541d48ef552cccd526a Mon Sep 17 00:00:00 2001 From: beppe Date: Tue, 18 Jun 2024 17:13:46 +0200 Subject: [PATCH] hrm --- beppe_online/src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/beppe_online/src/lib.rs b/beppe_online/src/lib.rs index 390a11d..d7ca686 100644 --- a/beppe_online/src/lib.rs +++ b/beppe_online/src/lib.rs @@ -24,4 +24,68 @@ pub fn init_webgl_context(canvas_id: &str) -> Result Result { + let shader = gl.create_shader(shader_type) + .ok_or_else(|| JsValue::from_str("Unable to create shader object"))?; + + g..shader_source(&shader, source); + gl.compile_shader(&shader); + + if gl.get_shader_parameter(&shader. WebGlRenderingContext::COMPILE_STATUS) + .as_bool().unwrap_or(false) { + Ok(shader) + } else { + Err(JsValue::from_str(&gl.get_shader_info_log(&shader).unwrap_or_else(|| "Unknown error creating shader".into()), + )) + } +} + + +pub fn setup_shaders(gl: &WebGlRenderingContext) -> Result { + let vertex_shader_source = " + attribute vec3 coordinates; + void main(void) { + gl_Position = vec4(coordinates, 1.0); + } + "; + let fragment_shader_source = " + precision mediump float; + uniform vec4 fragColor; + void main(void) { + gl_FragColor = fragColor; + } + "; + let vertex_shader = create_shader( + &gl, + WebGlRenderingContext::VERTEX_SHADER, + vertex_shader_source, + ).unwrap(); + let fragment_shader = create_shader( + &gl, + WebGlRenderingContext::FRAGMENT_SHADER, + fragment_shader_source, + ).unwrap(); + + let shader_program = gl.create_program().unwrap(); + gl.attach_shader(&shader_program, &vertex_shader); + gl.attach_shader(&shader_program, &fragment_shader); + gl.link_program(&shader_program); + + if gl.get_program_parameter(&shader_program, WebGlRenderingContext::LINK_STATUS) + .as_bool() + .unwrap_or(false) { + g.use_program(Some(&shader_program)); + Ok(shader_program); + } else { + return Err(JsValue::from_str( + &gl.get_program_info_log(&shader_program) + .unwrap_or_else(|| "Unknown error linking program".into()), + )); + } +} + +