From 42b6de79513ec63109ddd3cb9acab4811eacfc58 Mon Sep 17 00:00:00 2001 From: mmulli <6304106+mmulli@users.noreply.github.com> Date: Sat, 3 Jul 2021 17:21:21 +1000 Subject: [PATCH] Upgraded examples/webgl to use WebGL2 instead of WebGL1 --- examples/webgl/Cargo.toml | 3 +- examples/webgl/src/lib.rs | 78 ++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/examples/webgl/Cargo.toml b/examples/webgl/Cargo.toml index efd8432b6cf..6e489ce8f5b 100644 --- a/examples/webgl/Cargo.toml +++ b/examples/webgl/Cargo.toml @@ -18,7 +18,8 @@ features = [ 'Element', 'HtmlCanvasElement', 'WebGlBuffer', - 'WebGlRenderingContext', + 'WebGlVertexArrayObject', + 'WebGl2RenderingContext', 'WebGlProgram', 'WebGlShader', 'Window', diff --git a/examples/webgl/src/lib.rs b/examples/webgl/src/lib.rs index 37acafa3837..34c7a4f6a13 100644 --- a/examples/webgl/src/lib.rs +++ b/examples/webgl/src/lib.rs @@ -1,6 +1,6 @@ use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; -use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; +use web_sys::{WebGl2RenderingContext, WebGlProgram, WebGlShader}; #[wasm_bindgen(start)] pub fn start() -> Result<(), JsValue> { @@ -9,36 +9,45 @@ pub fn start() -> Result<(), JsValue> { let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; let context = canvas - .get_context("webgl")? + .get_context("webgl2")? .unwrap() - .dyn_into::()?; + .dyn_into::()?; let vert_shader = compile_shader( &context, - WebGlRenderingContext::VERTEX_SHADER, - r#" - attribute vec4 position; + WebGl2RenderingContext::VERTEX_SHADER, + r##"#version 300 es + + in vec4 position; + void main() { + gl_Position = position; } - "#, + "##, )?; + let frag_shader = compile_shader( &context, - WebGlRenderingContext::FRAGMENT_SHADER, - r#" + WebGl2RenderingContext::FRAGMENT_SHADER, + r##"#version 300 es + + precision highp float; + out vec4 outColor; + void main() { - gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + outColor = vec4(1, 1, 1, 1); } - "#, + "##, )?; let program = link_program(&context, &vert_shader, &frag_shader)?; 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 buffer = context.create_buffer().ok_or("failed to create buffer")?; - context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); + let position_attribute_location = context.get_attrib_location(&program, "position"); + let buffer = context.create_buffer().ok_or("Failed to create buffer")?; + context.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, Some(&buffer)); // Note that `Float32Array::view` is somewhat dangerous (hence the // `unsafe`!). This is creating a raw view into our module's @@ -49,31 +58,40 @@ pub fn start() -> Result<(), JsValue> { // As a result, after `Float32Array::view` we have to be very careful not to // do any memory allocations before it's dropped. unsafe { - let vert_array = js_sys::Float32Array::view(&vertices); + let positions_array_buf_view = js_sys::Float32Array::view(&vertices); context.buffer_data_with_array_buffer_view( - WebGlRenderingContext::ARRAY_BUFFER, - &vert_array, - WebGlRenderingContext::STATIC_DRAW, + WebGl2RenderingContext::ARRAY_BUFFER, + &positions_array_buf_view, + WebGl2RenderingContext::STATIC_DRAW, ); } - context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0); - context.enable_vertex_attrib_array(0); + let vao = context + .create_vertex_array() + .ok_or("Could not create vertex array object")?; + context.bind_vertex_array(Some(&vao)); - context.clear_color(0.0, 0.0, 0.0, 1.0); - context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT); + context.vertex_attrib_pointer_with_i32(0, 3, WebGl2RenderingContext::FLOAT, false, 0, 0); + context.enable_vertex_attrib_array(position_attribute_location as u32); + + context.bind_vertex_array(Some(&vao)); + + let vert_count = (vertices.len() / 3) as i32; + draw(&context, vert_count); - context.draw_arrays( - WebGlRenderingContext::TRIANGLES, - 0, - (vertices.len() / 3) as i32, - ); Ok(()) } +fn draw(context: &WebGl2RenderingContext, vert_count: i32) { + context.clear_color(0.0, 0.0, 0.0, 1.0); + context.clear(WebGl2RenderingContext::COLOR_BUFFER_BIT); + + context.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, vert_count); +} + pub fn compile_shader( - context: &WebGlRenderingContext, + context: &WebGl2RenderingContext, shader_type: u32, source: &str, ) -> Result { @@ -84,7 +102,7 @@ pub fn compile_shader( context.compile_shader(&shader); if context - .get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS) + .get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS) .as_bool() .unwrap_or(false) { @@ -97,7 +115,7 @@ pub fn compile_shader( } pub fn link_program( - context: &WebGlRenderingContext, + context: &WebGl2RenderingContext, vert_shader: &WebGlShader, frag_shader: &WebGlShader, ) -> Result { @@ -110,7 +128,7 @@ pub fn link_program( context.link_program(&program); if context - .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS) + .get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS) .as_bool() .unwrap_or(false) {