From 88e58d56770c1f1da700e1d99d98500ca344040f Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 16 Feb 2020 23:14:28 +0000 Subject: [PATCH] :recycle: extract get_uniform_locations --- src/main.rs | 16 ++-------------- src/shader.rs | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8b05d75..2ccce68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -238,26 +238,14 @@ fn main () { let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap (); - let unis: HashMap <_, _> = vec! [ + let unis = shader_program.get_uniform_locations (vec! [ "mvp", "object_space_light", "albedo", "min_albedo", "min_bright", "texture", - ].iter () - .map (|name| { - let mut s = String::from ("uni_"); - s.push_str (name); - - let c_str = CString::new (s.as_bytes ()).unwrap (); - let loc = shader_program.get_uniform_location (&c_str); - - println! ("Uni {} --> {}", name, loc); - - (String::from (*name), loc) - }) - .collect (); + ].into_iter ()); let attrs: HashMap <_, Option > = vec! [ "pos", diff --git a/src/shader.rs b/src/shader.rs index 64bf307..20132e0 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -1,5 +1,6 @@ +use std::collections::*; use std::convert::TryInto; -use std::ffi::CStr; +use std::ffi::{CStr, CString}; pub struct ShaderObject { id: u32, @@ -116,6 +117,29 @@ impl ShaderProgram { } } + pub fn get_uniform_locations <'a, I> (&self, names: I) + -> HashMap + where I: Iterator + { + unsafe { + gl::UseProgram (self.id); + } + + names + .map (|name| { + let mut s = String::from ("uni_"); + s.push_str (name); + + let c_str = CString::new (s.as_bytes ()).unwrap (); + let loc = unsafe { + gl::GetUniformLocation (self.id, c_str.as_ptr ()) + }; + + (String::from (name), loc) + }) + .collect () + } + pub fn get_attribute_location (&self, name: &CStr) -> i32 { unsafe { gl::UseProgram (self.id);