Skip to content

Commit

Permalink
Improvement on uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
AJMC2002 committed Jul 20, 2023
1 parent cd1e540 commit b9229bf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
52 changes: 29 additions & 23 deletions src/graphics/wrapper/shader_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::{collections::HashMap, ffi::CString, fs::File, io::Read, ptr};
use egui_glfw_gl::gl;
use egui_glfw_gl::gl::types::*;

use crate::maths::Vector;
use crate::maths::{Matrix, Vector};

use super::texture::Texture2D;

pub struct ShaderProgram {
id: GLuint,
uniform_ids: HashMap<String, GLint>,
location_cache: HashMap<String, GLint>,
}

impl ShaderProgram {
Expand Down Expand Up @@ -49,7 +49,7 @@ impl ShaderProgram {

ShaderProgram {
id,
uniform_ids: HashMap::new(),
location_cache: HashMap::new(),
}
}
}
Expand All @@ -66,41 +66,47 @@ impl ShaderProgram {
}
}

pub fn uniform_4f(&mut self, name: &str, v: Vector) {
assert!(v.len() == 4);
let location = match self.uniform_ids.get(&name.to_string()) {
fn get_location(&mut self, name: &str) -> GLint {
match self.location_cache.get(&name.to_string()) {
Some(id) => *id,
None => {
let name_cstring = CString::new(name).unwrap();
let loc = unsafe { gl::GetUniformLocation(self.id, name_cstring.as_ptr()) };
if loc < 0 {
panic!("Cannot locate uniform: {}", name);
} else {
self.uniform_ids.insert(name.to_string(), loc);
self.location_cache.insert(name.to_string(), loc);
}
loc
}
};
}
}

pub fn uniform_4f(&mut self, name: &str, v1: f32, v2: f32, v3: f32, v4: f32) {
unsafe {
gl::Uniform4f(location, v[0], v[1], v[2], v[3]);
gl::Uniform4f(self.get_location(name), v1, v2, v3, v4);
}
}

pub fn uniform_2dtex(&mut self, name: &str, tex: &Texture2D) {
match self.uniform_ids.get(&name.to_string()) {
Some(id) => *id,
None => {
let name_cstring = CString::new(name).unwrap();
let loc = unsafe { gl::GetUniformLocation(self.id, name_cstring.as_ptr()) };
if loc < 0 {
panic!("Cannot locate uniform: {}", name);
} else {
self.uniform_ids.insert(name.to_string(), loc);
}
loc
}
};
pub fn uniform_4fv(&mut self, name: &str, v: Vector) {
assert_eq!(v.len(), 4);
unsafe {
gl::Uniform4fv(self.get_location(name), 1, v.as_ptr());
}
}

pub fn uniform_matrix_4fv(&mut self, name: &str, m: Matrix) {
assert_eq!(m.rows(), 4);
assert_eq!(m.cols(), 4);
unsafe {
//Matrix is a row-major matrix type, therefore we need to use
//transpose: gl::TRUE since OpenGL uses column-major matrices
gl::UniformMatrix4fv(self.get_location(name), 1, gl::TRUE, m.as_ptr());
}
}

pub fn uniform_2dtex(&mut self, name: &str, tex: &Texture2D) {
self.get_location(name);
unsafe {
tex.bind();
gl::ActiveTexture(gl::TEXTURE0);
Expand Down
26 changes: 16 additions & 10 deletions src/maths/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ impl Matrix {
assert_eq!(self.cols(), 1);
Vector::from_vec(self.data.clone())
}

pub fn as_ptr(&self) -> *const f32 {
self.data.as_ptr()
}
}

//Custom matrices
Expand All @@ -164,34 +168,36 @@ impl Matrix {
}
}

pub fn scaling(values: Vector) -> Self {
assert_eq!(values.len(), 3);
pub fn scaling(values: (f32, f32, f32)) -> Self {
Self {
data: vec![
values[0], 0., 0., 0., //row 1
0., values[1], 0., 0., //row 2
0., 0., values[2], 0., //row 3
values.0, 0., 0., 0., //row 1
0., values.1, 0., 0., //row 2
0., 0., values.2, 0., //row 3
0., 0., 0., 1., //row 4
],
rows: 4,
cols: 4,
}
}

pub fn translation(values: Vector) -> Self {
assert_eq!(values.len(), 3);
pub fn translation(values: (f32, f32, f32)) -> Self {
Self {
data: vec![
1., 0., 0., values[0], //row 1
0., 1., 0., values[1], //row 2
0., 0., 1., values[2], //row 3
1., 0., 0., values.0, //row 1
0., 1., 0., values.1, //row 2
0., 0., 1., values.2, //row 3
0., 0., 0., 1., //row 4
],
rows: 4,
cols: 4,
}
}

pub fn rotation(values: (f32, f32, f32)) -> Self {
Self::rotation_z(values.2) * Self::rotation_y(values.1) * Self::rotation_x(values.0)
}

pub fn rotation_x(angle: f32) -> Self {
Self {
data: vec![
Expand Down
4 changes: 4 additions & 0 deletions src/maths/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ impl Vector {
pub fn as_matrix(&self) -> Matrix {
Matrix::from_vector(self)
}

pub fn as_ptr(&self) -> *const f32 {
self.data.as_ptr()
}
}

// Unary Ops
Expand Down

0 comments on commit b9229bf

Please sign in to comment.