Verified Commit 37823dc7 authored by insert's avatar insert

Port over upload code.

parent 7a5d64b4
......@@ -11,3 +11,4 @@ hashbrown = "0.7.2"
glutin = "0.24.0"
takeable-option = "0.5.0"
gl = "0.14.0"
nalgebra-glm = "0.7.0"
pub extern crate nalgebra_glm as glm;
pub mod mesh;
pub mod shader;
......@@ -4,6 +4,7 @@ use crate::util::cstring::create_whitespace_cstring_with_len;
use hashbrown::HashMap;
use std::rc::Rc;
use std::ffi::CString;
use std::sync::{Arc, Mutex};
#[derive(Clone)]
......@@ -73,6 +74,65 @@ impl Program {
gl::UseProgram(self.id);
}
}
pub fn get_uniform_location(&mut self, name: &str) -> Result<gl::types::GLint, gl::types::GLuint> {
let mut locations = self.locations.lock().unwrap();
if let Some(loc) = locations.get(name) {
return Ok(*loc);
}
let cstr = CString::new(name).unwrap();
let location = unsafe { gl::GetUniformLocation(self.id, cstr.as_ptr()) };
locations.insert(name.to_owned(), location);
if location == -1 {
let result = unsafe { gl::GetError() };
Err(result)
} else {
Ok(location)
}
}
pub fn upload_i32(&mut self, name: &str, i: i32) {
if let Ok(location) = self.get_uniform_location(&name) {
unsafe { gl::Uniform1i(location, i); }
} else {
println!("Could not find location, {}. [{}]", name, self.id);
}
}
pub fn upload_f32(&mut self, name: &str, f: f32) {
if let Ok(location) = self.get_uniform_location(&name) {
unsafe { gl::Uniform1f(location, f); }
} else {
println!("Could not find location, {}. [{}]", name, self.id);
}
}
pub fn upload_vec3(&mut self, name: &str, vec: &glm::Vec3) {
if let Ok(location) = self.get_uniform_location(&name) {
unsafe { gl::Uniform3fv(location, 1, vec.as_ptr()); }
} else {
println!("Could not find location, {}. [{}]", name, self.id);
}
}
pub fn upload_vec4(&mut self, name: &str, vec: &glm::Vec4) {
if let Ok(location) = self.get_uniform_location(&name) {
unsafe { gl::Uniform4fv(location, 1, vec.as_ptr()); }
} else {
println!("Could not find location, {}. [{}]", name, self.id);
}
}
pub fn upload_mat4(&mut self, name: &str, mat: &glm::Mat4) {
if let Ok(location) = self.get_uniform_location(&name) {
unsafe { gl::UniformMatrix4fv(location, 1, gl::FALSE, mat.as_ptr()); }
} else {
println!("Could not find location, {}. [{}]", name, self.id);
}
}
}
impl Drop for Program {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment