diff --git a/src/main.rs b/src/main.rs index 8c5f8f6..df624d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ const SCR_WIDTH: u32 = 1600; const SCR_HEIGHT: u32 = 900; const TITLE: &str = "GLFWtest"; -use glow; +use glow::*; use std::sync::mpsc::Receiver; use std::ptr; @@ -56,13 +56,18 @@ fn main() { let mut lastFrame: f32 = 0.0; //gl::load_with(|ptr| window.get_proc_address(ptr) as *const _); - let gl = unsafe{glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _);}; + let gl = { + unsafe{ + let glue = glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _); + glue + } + }; let (ourshader, ourModel) = unsafe { gl::Enable(gl::DEPTH_TEST); - let ourShader = shader::shader::new("model"); + let ourShader = shader::shader::new("model", gl); let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj"); diff --git a/src/shader.rs b/src/shader.rs index 8669e62..ace22d1 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -1,19 +1,19 @@ -use gl; +use glow::*; use std::fs; use std::ffi::{CString, CStr}; 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: gl::types::GLuint, + pub ID: T::Program, } - impl shader + impl shader { /// Shader Constructor, will read, Compile and Create GLSL Program for use /// @@ -21,74 +21,51 @@ 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) -> shader + pub fn new(path: &str, gl: glow::Context) -> shader { //read file contents let VERT_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.vert")).unwrap(); - let FRAG_SHADER = std::fs::read_to_string(format!("resources/shaders/{path}/shader.frag")).unwrap(); + let FRAG_SHADER = fs::read_to_string(format!("resources/shaders/{path}/shader.frag")).unwrap(); //create vertex shader - let vertex_shader = unsafe {gl::CreateShader(gl::VERTEX_SHADER) }; + let vertex_shader = unsafe {gl.create_shader(glow::VERTEX_SHADER).unwrap() }; unsafe { - gl::ShaderSource(vertex_shader, 1, &VERT_SHADER.as_bytes().as_ptr().cast(), &VERT_SHADER.len().try_into().unwrap()); - gl::CompileShader(vertex_shader); - } - //error Checking - let mut success = 0; - unsafe { - gl::GetShaderiv(vertex_shader, gl::COMPILE_STATUS, &mut success); - if success == 0 { - let mut log_len = 0_i32; + gl.shader_source(vertex_shader, &VERT_SHADER); + gl.compile_shader(vertex_shader); - let mut v: Vec = Vec::with_capacity(1024); - gl::GetShaderInfoLog(vertex_shader, 1024, &mut log_len, v.as_mut_ptr().cast()); - v.set_len(log_len.try_into().unwrap()); - panic!("Vertex Shader Compile Error: {}", String::from_utf8_lossy(&v)); + } + // create fragment shader + let fragment_shader = unsafe{gl.create_shader(glow::FRAGMENT_SHADER).unwrap()}; + unsafe { + gl.shader_source(fragment_shader, &FRAG_SHADER); + gl.compile_shader(fragment_shader); + if !gl.get_shader_compile_status(fragment_shader) { + panic!("Error, Compiling Fragment Shader: {}", gl.get_shader_info_log(fragment_shader)); } - let fragment_shader = gl::CreateShader(gl::FRAGMENT_SHADER); - gl::ShaderSource(fragment_shader, 1, &FRAG_SHADER.as_bytes().as_ptr().cast(), &FRAG_SHADER.len().try_into().unwrap()); - gl::CompileShader(fragment_shader); + let shader_program = gl.create_program().unwrap(); - let mut success = 0; - gl::GetShaderiv(fragment_shader, gl::COMPILE_STATUS, &mut success); - if success == 0 { - let mut v: Vec = Vec::with_capacity(1024); - let mut log_len = 0_i32; - gl::GetShaderInfoLog(fragment_shader, 1024, &mut log_len, v.as_mut_ptr().cast()); - v.set_len(log_len.try_into().unwrap()); - panic!("Fragment Shader Compile Error: {}", String::from_utf8_lossy(&v)); + gl.attach_shader(shader_program, vertex_shader); + gl.attach_shader(shader_program, fragment_shader); + gl.link_program(shader_program); + if !gl.get_program_link_status(shader_program) { + panic!("Error, Linking Shader Program {}", gl.get_program_info_log(shader_program)); } + gl.detach_shader(shader_program, vertex_shader); + gl.detach_shader(shader_program, fragment_shader); + gl.delete_shader(vertex_shader); + gl.delete_shader(fragment_shader); - let shader_program = gl::CreateProgram(); - - gl::AttachShader(shader_program, vertex_shader); - gl::AttachShader(shader_program, fragment_shader); - gl::LinkProgram(shader_program); - - let mut success = 0; - gl::GetProgramiv(shader_program, gl::LINK_STATUS, &mut success); - if success == 0 { - let mut v: Vec = Vec::with_capacity(1024); - let mut log_len = 0_i32; - gl::GetProgramInfoLog(shader_program, 1024, &mut log_len, v.as_mut_ptr().cast()); - v.set_len(log_len.try_into().unwrap()); - panic!("Program Link Error: {}", String::from_utf8_lossy(&v)); - } - - gl::DetachShader(shader_program, vertex_shader); - gl::DetachShader(shader_program, fragment_shader); - gl::DeleteShader(vertex_shader); - gl::DeleteShader(fragment_shader); - - - - // return program shader { - ID: shader_program, - } - } + ID: shader_program, + }; + } } + + + + // return program + /// uses Shader pub fn Use(&self) { @@ -132,11 +109,7 @@ use cgmath::prelude::*; 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;