From 0df79f7509ccd2f8d7ee0d848e7a3dd84a562825 Mon Sep 17 00:00:00 2001 From: Melik Houij Date: Wed, 8 Mar 2023 10:19:23 +0100 Subject: [PATCH] finished Porting shader Code --- src/main.rs | 9 +----- src/shader.rs | 84 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index df624d5..492e3ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,14 +56,7 @@ fn main() { let mut lastFrame: f32 = 0.0; //gl::load_with(|ptr| window.get_proc_address(ptr) as *const _); - let gl = { - - unsafe{ - let glue = glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _); - glue - } - }; - + let gl = std::rc::Rc::new(glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _)); let (ourshader, ourModel) = unsafe { gl::Enable(gl::DEPTH_TEST); diff --git a/src/shader.rs b/src/shader.rs index ace22d1..b5e9b40 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -2,18 +2,20 @@ use glow::*; use std::fs; use std::ffi::{CString, CStr}; +use std::rc::Rc; use std::str; use cgmath::prelude::*; /// Shader Struct for creating and using shaders in the Context of engine /// - pub struct shader { + pub struct shader { - pub ID: T::Program, + pub ID: NativeProgram, + gl: Rc } - impl shader + impl shader { /// Shader Constructor, will read, Compile and Create GLSL Program for use /// @@ -21,7 +23,7 @@ use cgmath::prelude::*; /// `new(String::from("Example"))` /// if the String given was "Example" the Program expects both shaders to be in the directory "resources/shaders/Example/" /// with the vertex shader being called "shader.vert" and fragment "shader.frag" - pub fn new(path: &str, gl: glow::Context) -> shader + pub fn new(path: &str, gl: Rc) -> shader { //read file contents let VERT_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.vert")).unwrap(); @@ -58,6 +60,7 @@ use cgmath::prelude::*; shader { ID: shader_program, + gl: Rc::clone(&gl), }; } } @@ -67,49 +70,66 @@ use cgmath::prelude::*; // return program /// uses Shader - pub fn Use(&self) + pub fn Use(&self, gl: glow::Context) { - unsafe {gl::UseProgram(self.ID);} + unsafe {gl.use_program(Some(self.ID));} } // BOILERPLATE JUMPSCARE - pub fn setBool(&self, name: &CStr, value: bool ) - {unsafe {gl::Uniform1i(gl::GetUniformLocation(self.ID, name.as_ptr()), value as i32);}} + /* + pub fn setBool(&self, name: &str, value: bool ) + {unsafe {self.gl.uniform_1_bool(self.gl.get_uniform_location(self.ID, name).as_ref(), value);}} + */ + pub fn setInt(&self, name: &str, value: i32 ) + {unsafe {self.gl.uniform_1_i32(self.gl.get_uniform_location(self.ID, name).as_ref(), value);}} - pub fn setInt(&self, name: &CStr, value: u32 ) - {unsafe {gl::Uniform1i(gl::GetUniformLocation(self.ID, name.as_ptr()), value as i32);}} + pub fn setFloat(&self, name: &str, value: f32 ) + {unsafe {self.gl.uniform_1_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value);}} - pub fn setFloat(&self, name: &CStr, value: f32 ) - {unsafe {gl::Uniform1f(gl::GetUniformLocation(self.ID, name.as_ptr()), value as f32);}} + pub fn setVector2(&self, name: &str, value: cgmath::Vector2 ) + {unsafe {self.gl.uniform_2_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1]);}} - pub fn setVec2(&self, name: &CStr, value: &cgmath::Vector2 ) - {unsafe {gl::Uniform2fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, value.as_ptr());}} + pub fn setVector2d(&self, name: &str, x: f32, y: f32) + {unsafe {self.gl.uniform_2_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), x, y);}} - pub fn setVector2d(&self, name: &CStr, x: f32, y: f32) - {unsafe {gl::Uniform2f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y);}} + pub fn setVector3(&self, name: &str, value: cgmath::Vector3 ) + {unsafe {self.gl.uniform_3_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1], value[2]);}} - pub fn setVector3(&self, name: &CStr, value: cgmath::Vector3 ) - {unsafe {gl::Uniform3fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, value.as_ptr());}} + pub fn setVector3d(&self, name: &str, x: f32, y: f32, z: f32 ) + {unsafe {self.gl.uniform_3_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), x, y, z);}} - pub fn setVector3d(&self, name: &CStr, x: f32, y: f32, z: f32 ) - {unsafe {gl::Uniform3f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y, z);}} + pub fn setVector4(&self, name: &str, value: &cgmath::Vector4 ) + {unsafe {self.gl.uniform_4_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), value[0], value[1], value[2], value[3]);}} - pub fn setVector4(&self, name: &CStr, value: &cgmath::Vector4 ) - {unsafe {gl::Uniform4fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, value.as_ptr());}} + pub fn setVector4d(&self, name: &str, x: f32, y: f32, z: f32, w: f32) + {unsafe {self.gl.uniform_4_f32(self.gl.get_uniform_location(self.ID, name).as_ref(), x, y, z, w);}} - pub fn setVector4d(&self, name: &CStr, x: f32, y: f32, z: f32, w: f32) - {unsafe {gl::Uniform4f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y, z, w);}} + //pub fn setMat2(&self, name: &str, value: cgmath::Matrix2) - pub fn setMat2(&self, name: &CStr, value: &cgmath::Matrix2) - {unsafe {gl::UniformMatrix2fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}} + pub fn setMat3(&self, name: &str, value: cgmath::Matrix3) + { + let nums = vec![value.x.x, value.x.y, value.x.z, + value.y.x, value.y.y, value.y.z, + value.z.x, value.z.y, value.z.z]; + unsafe + { + self.gl.uniform_matrix_3_f32_slice(self.gl.get_uniform_location(self.ID, name).as_ref(), false, nums.as_ref()); + } + } - pub fn setMat3(&self, name: &CStr, value: &cgmath::Matrix3) - {unsafe {gl::UniformMatrix3fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}} + pub fn setMat4(&self, name: &str, value: &cgmath::Matrix4) + { + let nums = vec![value.x.x, value.x.y, value.x.z, value.x.w, + value.y.x, value.y.y, value.y.z, value.y.w, + value.z.x, value.z.y, value.z.z, value.z.w, + value.w.x, value.w.y, value.w.z, value.w.w]; + unsafe + { + self.gl.uniform_matrix_4_f32_slice(self.gl.get_uniform_location(self.ID, name).as_ref(), false, nums.as_ref()); + } + } - pub fn setMat4(&self, name: &CStr, value: &cgmath::Matrix4) - {unsafe {gl::UniformMatrix4fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}} - - } + } #[cfg(test)] mod tests { use glfw;