Skip to content

Commit a8245bd

Browse files
authored
Upgraded examples/webgl to use WebGL2 instead of WebGL1 (#2609)
1 parent eb855e3 commit a8245bd

File tree

2 files changed

+50
-31
lines changed

2 files changed

+50
-31
lines changed

examples/webgl/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ features = [
1818
'Element',
1919
'HtmlCanvasElement',
2020
'WebGlBuffer',
21-
'WebGlRenderingContext',
21+
'WebGlVertexArrayObject',
22+
'WebGl2RenderingContext',
2223
'WebGlProgram',
2324
'WebGlShader',
2425
'Window',

examples/webgl/src/lib.rs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use wasm_bindgen::prelude::*;
22
use wasm_bindgen::JsCast;
3-
use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader};
3+
use web_sys::{WebGl2RenderingContext, WebGlProgram, WebGlShader};
44

55
#[wasm_bindgen(start)]
66
pub fn start() -> Result<(), JsValue> {
@@ -9,36 +9,45 @@ pub fn start() -> Result<(), JsValue> {
99
let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::<web_sys::HtmlCanvasElement>()?;
1010

1111
let context = canvas
12-
.get_context("webgl")?
12+
.get_context("webgl2")?
1313
.unwrap()
14-
.dyn_into::<WebGlRenderingContext>()?;
14+
.dyn_into::<WebGl2RenderingContext>()?;
1515

1616
let vert_shader = compile_shader(
1717
&context,
18-
WebGlRenderingContext::VERTEX_SHADER,
19-
r#"
20-
attribute vec4 position;
18+
WebGl2RenderingContext::VERTEX_SHADER,
19+
r##"#version 300 es
20+
21+
in vec4 position;
22+
2123
void main() {
24+
2225
gl_Position = position;
2326
}
24-
"#,
27+
"##,
2528
)?;
29+
2630
let frag_shader = compile_shader(
2731
&context,
28-
WebGlRenderingContext::FRAGMENT_SHADER,
29-
r#"
32+
WebGl2RenderingContext::FRAGMENT_SHADER,
33+
r##"#version 300 es
34+
35+
precision highp float;
36+
out vec4 outColor;
37+
3038
void main() {
31-
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
39+
outColor = vec4(1, 1, 1, 1);
3240
}
33-
"#,
41+
"##,
3442
)?;
3543
let program = link_program(&context, &vert_shader, &frag_shader)?;
3644
context.use_program(Some(&program));
3745

3846
let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0];
3947

40-
let buffer = context.create_buffer().ok_or("failed to create buffer")?;
41-
context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer));
48+
let position_attribute_location = context.get_attrib_location(&program, "position");
49+
let buffer = context.create_buffer().ok_or("Failed to create buffer")?;
50+
context.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, Some(&buffer));
4251

4352
// Note that `Float32Array::view` is somewhat dangerous (hence the
4453
// `unsafe`!). This is creating a raw view into our module's
@@ -49,31 +58,40 @@ pub fn start() -> Result<(), JsValue> {
4958
// As a result, after `Float32Array::view` we have to be very careful not to
5059
// do any memory allocations before it's dropped.
5160
unsafe {
52-
let vert_array = js_sys::Float32Array::view(&vertices);
61+
let positions_array_buf_view = js_sys::Float32Array::view(&vertices);
5362

5463
context.buffer_data_with_array_buffer_view(
55-
WebGlRenderingContext::ARRAY_BUFFER,
56-
&vert_array,
57-
WebGlRenderingContext::STATIC_DRAW,
64+
WebGl2RenderingContext::ARRAY_BUFFER,
65+
&positions_array_buf_view,
66+
WebGl2RenderingContext::STATIC_DRAW,
5867
);
5968
}
6069

61-
context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0);
62-
context.enable_vertex_attrib_array(0);
70+
let vao = context
71+
.create_vertex_array()
72+
.ok_or("Could not create vertex array object")?;
73+
context.bind_vertex_array(Some(&vao));
6374

64-
context.clear_color(0.0, 0.0, 0.0, 1.0);
65-
context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT);
75+
context.vertex_attrib_pointer_with_i32(0, 3, WebGl2RenderingContext::FLOAT, false, 0, 0);
76+
context.enable_vertex_attrib_array(position_attribute_location as u32);
77+
78+
context.bind_vertex_array(Some(&vao));
79+
80+
let vert_count = (vertices.len() / 3) as i32;
81+
draw(&context, vert_count);
6682

67-
context.draw_arrays(
68-
WebGlRenderingContext::TRIANGLES,
69-
0,
70-
(vertices.len() / 3) as i32,
71-
);
7283
Ok(())
7384
}
7485

86+
fn draw(context: &WebGl2RenderingContext, vert_count: i32) {
87+
context.clear_color(0.0, 0.0, 0.0, 1.0);
88+
context.clear(WebGl2RenderingContext::COLOR_BUFFER_BIT);
89+
90+
context.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, vert_count);
91+
}
92+
7593
pub fn compile_shader(
76-
context: &WebGlRenderingContext,
94+
context: &WebGl2RenderingContext,
7795
shader_type: u32,
7896
source: &str,
7997
) -> Result<WebGlShader, String> {
@@ -84,7 +102,7 @@ pub fn compile_shader(
84102
context.compile_shader(&shader);
85103

86104
if context
87-
.get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS)
105+
.get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS)
88106
.as_bool()
89107
.unwrap_or(false)
90108
{
@@ -97,7 +115,7 @@ pub fn compile_shader(
97115
}
98116

99117
pub fn link_program(
100-
context: &WebGlRenderingContext,
118+
context: &WebGl2RenderingContext,
101119
vert_shader: &WebGlShader,
102120
frag_shader: &WebGlShader,
103121
) -> Result<WebGlProgram, String> {
@@ -110,7 +128,7 @@ pub fn link_program(
110128
context.link_program(&program);
111129

112130
if context
113-
.get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS)
131+
.get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS)
114132
.as_bool()
115133
.unwrap_or(false)
116134
{

0 commit comments

Comments
 (0)