From 81f9b8e04007aa2289d865b48d6bae03b19f287a Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Wed, 21 Aug 2024 22:36:56 -0700 Subject: [PATCH] Find vertex attribute indices in advance --- app-proto/inversive-display/src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app-proto/inversive-display/src/main.rs b/app-proto/inversive-display/src/main.rs index 6768864..ed8200c 100644 --- a/app-proto/inversive-display/src/main.rs +++ b/app-proto/inversive-display/src/main.rs @@ -26,8 +26,7 @@ fn compile_shader( // load the given data into the vertex input of the given name fn bind_vertex_attrib( context: &WebGl2RenderingContext, - program: &WebGlProgram, - name: &str, + index: u32, size: i32, data: &[f32] ) { @@ -48,11 +47,8 @@ fn bind_vertex_attrib( ); } - // find the target attribute in the program's attribute list - let attrib_index = context.get_attrib_location(&program, name); - // allow the target attribute to be used - context.enable_vertex_attrib_array(attrib_index as u32); + context.enable_vertex_attrib_array(index); // take whatever's bound to ARRAY_BUFFER---here, the data buffer created // above---and bind it to the target attribute @@ -60,7 +56,7 @@ fn bind_vertex_attrib( // https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer // context.vertex_attrib_pointer_with_i32( - attrib_index as u32, + index, size, WebGl2RenderingContext::FLOAT, false, // don't normalize @@ -150,6 +146,10 @@ fn main() { console::log_1(&JsValue::from(link_msg)); ctx.use_program(Some(&program)); + // find indices of vertex attributes + let position_index = ctx.get_attrib_location(&program, "position") as u32; + let color_index = ctx.get_attrib_location(&program, "color") as u32; + // create a vertex array and bind it to the graphics context let vertex_array = ctx.create_vertex_array().unwrap(); ctx.bind_vertex_array(Some(&vertex_array)); @@ -174,7 +174,7 @@ fn main() { -1.0, -1.0, -7.0, 1.0, -1.0, -7.0 ]; - bind_vertex_attrib(&ctx, &program, "position", 3, &positions); + bind_vertex_attrib(&ctx, position_index, 3, &positions); // set the vertex colors let colors: [f32; 3*VERTEX_CNT] = [ @@ -191,7 +191,7 @@ fn main() { 0.5, 0.0, 1.0, 0.5, 0.0, 1.0 ]; - bind_vertex_attrib(&ctx, &program, "color", 3, &colors); + bind_vertex_attrib(&ctx, color_index, 3, &colors); // clear the screen and draw the scene ctx.clear_color(0.0, 0.0, 0.0, 1.0);