finished Porting shader Code

pull/1/head
Milk.H 2023-03-08 10:19:23 +01:00
parent adc2c93af2
commit 0df79f7509
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
2 changed files with 53 additions and 40 deletions

View File

@ -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);

View File

@ -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<T: glow::HasContext> {
pub struct shader {
pub ID: T::Program,
pub ID: NativeProgram,
gl: Rc<glow::Context>
}
impl shader<T>
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<T>
pub fn new(path: &str, gl: Rc<glow::Context>) -> 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,47 +70,64 @@ 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<f32> )
{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<f32> )
{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<f32> )
{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<f32> )
{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<f32> )
{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<f32> )
{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<f32>)
pub fn setMat2(&self, name: &CStr, value: &cgmath::Matrix2<f32>)
{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<f32>)
{
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<f32>)
{unsafe {gl::UniformMatrix3fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}}
pub fn setMat4(&self, name: &CStr, value: &cgmath::Matrix4<f32>)
{unsafe {gl::UniformMatrix4fv(gl::GetUniformLocation(self.ID, name.as_ptr()), 1, gl::FALSE, value.as_ptr());}}
pub fn setMat4(&self, name: &str, value: &cgmath::Matrix4<f32>)
{
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());
}
}
}
#[cfg(test)]