hrm
This commit is contained in:
@@ -24,4 +24,68 @@ pub fn init_webgl_context(canvas_id: &str) -> Result<WebGlRenderingContext, JsVa
|
|||||||
|
|
||||||
|
|
||||||
// https://blog.logrocket.com/implement-webassembly-webgl-viewer-using-rust/#setup-environment
|
// https://blog.logrocket.com/implement-webassembly-webgl-viewer-using-rust/#setup-environment
|
||||||
pub fn create_
|
pub fn create_shader(
|
||||||
|
gl: &WebGlRenderingContext,
|
||||||
|
shader_type: u32,
|
||||||
|
source: &str,
|
||||||
|
) -> Result<WebGlShader, JsValue> {
|
||||||
|
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<WebGlProgram, JsValue> {
|
||||||
|
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()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user