1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-04-04 19:31:05 +00:00

Merge pull request from ibaryshnikov/issue-1322-simplify-webgl-example

Simplified webgl example
This commit is contained in:
Nick Fitzgerald 2019-03-11 10:46:24 -07:00 committed by GitHub
commit c857a93784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,7 +33,7 @@ pub fn start() -> Result<(), JsValue> {
} }
"#, "#,
)?; )?;
let program = link_program(&context, [vert_shader, frag_shader].iter())?; let program = link_program(&context, &vert_shader, &frag_shader)?;
context.use_program(Some(&program)); context.use_program(Some(&program));
let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0];
@ -85,20 +85,21 @@ pub fn compile_shader(
} else { } else {
Err(context Err(context
.get_shader_info_log(&shader) .get_shader_info_log(&shader)
.unwrap_or_else(|| "Unknown error creating shader".into())) .unwrap_or_else(|| String::from("Unknown error creating shader")))
} }
} }
pub fn link_program<'a, T: IntoIterator<Item = &'a WebGlShader>>( pub fn link_program(
context: &WebGlRenderingContext, context: &WebGlRenderingContext,
shaders: T, vert_shader: &WebGlShader,
frag_shader: &WebGlShader,
) -> Result<WebGlProgram, String> { ) -> Result<WebGlProgram, String> {
let program = context let program = context
.create_program() .create_program()
.ok_or_else(|| String::from("Unable to create shader object"))?; .ok_or_else(|| String::from("Unable to create shader object"))?;
for shader in shaders {
context.attach_shader(&program, shader) context.attach_shader(&program, vert_shader);
} context.attach_shader(&program, frag_shader);
context.link_program(&program); context.link_program(&program);
if context if context
@ -110,6 +111,6 @@ pub fn link_program<'a, T: IntoIterator<Item = &'a WebGlShader>>(
} else { } else {
Err(context Err(context
.get_program_info_log(&program) .get_program_info_log(&program)
.unwrap_or_else(|| "Unknown error creating program object".into())) .unwrap_or_else(|| String::from("Unknown error creating program object")))
} }
} }