diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c6b2bcb..33e6519 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,4 +50,4 @@ jobs: with: components: clippy - name: Run Clippy - run: cargo clippy --workspace + run: cargo clippy --workspace -D warnings diff --git a/gloom-rs/.rustfmt.toml b/gloom-rs/.rustfmt.toml index a9ef535..8a56c54 100644 --- a/gloom-rs/.rustfmt.toml +++ b/gloom-rs/.rustfmt.toml @@ -1,12 +1,5 @@ edition = "2021" max_width = 110 -hard_tabs = false tab_spaces = 3 newline_style = "Unix" use_small_heuristics = "Max" -indent_style = "Block" -trailing_comma = "Always" -space_around_ranges = true -reorder_imports = true -max_blank_lines = 1 -spaces_around_comments = true diff --git a/gloom-rs/src/main.rs b/gloom-rs/src/main.rs index aabcb68..c7a5b7e 100644 --- a/gloom-rs/src/main.rs +++ b/gloom-rs/src/main.rs @@ -9,7 +9,11 @@ */ extern crate nalgebra_glm as glm; use std::sync::{Arc, Mutex}; -use std::{mem, os::raw::c_void, ptr}; +use std::{ + // mem, + // os::raw::c_void, + ptr, +}; mod shader; mod util; @@ -31,48 +35,48 @@ const INITIAL_SCREEN_H: u32 = 600; // Get the size of an arbitrary array of numbers measured in bytes // Example usage: byte_size_of_array(my_array) -fn byte_size_of_array(val: &[T]) -> isize { - std::mem::size_of_val(&val[..]) as isize -} +// fn byte_size_of_array(val: &[T]) -> isize { +// std::mem::size_of_val(&val[..]) as isize +// } // Get the OpenGL-compatible pointer to an arbitrary array of numbers // Example usage: pointer_to_array(my_array) -fn pointer_to_array(val: &[T]) -> *const c_void { - &val[0] as *const T as *const c_void -} +// fn pointer_to_array(val: &[T]) -> *const c_void { +// &val[0] as *const T as *const c_void +// } // Get the size of the given type in bytes // Example usage: size_of::() -fn size_of() -> i32 { - mem::size_of::() as i32 -} +// fn size_of() -> i32 { +// mem::size_of::() as i32 +// } // Get an offset in bytes for n units of type T, represented as a relative pointer // Example usage: offset::(4) -fn offset(n: u32) -> *const c_void { - (n * mem::size_of::() as u32) as *const T as *const c_void -} +// fn offset(n: u32) -> *const c_void { +// (n * mem::size_of::() as u32) as *const T as *const c_void +// } // Get a null pointer (equivalent to an offset of 0) // ptr::null() // == // Generate your VAO here -unsafe fn create_vao(vertices: &Vec, indices: &Vec) -> u32 { - // Implement me! - - // Also, feel free to delete comments :) - - // This should: - // * Generate a VAO and bind it - // * Generate a VBO and bind it - // * Fill it with data - // * Configure a VAP for the data and enable it - // * Generate a IBO and bind it - // * Fill it with data - // * Return the ID of the VAO - - 0 -} +// unsafe fn create_vao(vertices: &Vec, indices: &Vec) -> u32 { +// // Implement me! +// +// // Also, feel free to delete comments :) +// +// // This should: +// // * Generate a VAO and bind it +// // * Generate a VBO and bind it +// // * Fill it with data +// // * Configure a VAP for the data and enable it +// // * Generate a IBO and bind it +// // * Fill it with data +// // * Return the ID of the VAO +// +// 0 +// } fn main() { // Set up the necessary objects to deal with windows and event handling @@ -100,7 +104,7 @@ fn main() { // Set up shared tuple for tracking changes to the window size let arc_window_size = Arc::new(Mutex::new((INITIAL_SCREEN_W, INITIAL_SCREEN_H, false))); - let mut window_aspect_ratio = INITIAL_SCREEN_W as f32 / INITIAL_SCREEN_H as f32; + // let mut window_aspect_ratio = INITIAL_SCREEN_W as f32 / INITIAL_SCREEN_H as f32; // Set up openGL unsafe { @@ -200,7 +204,7 @@ fn main() { Event::MainEventsCleared => { // == // Set up your VAO around here - let my_vao = unsafe { 1337 }; + // let my_vao = unsafe { 1337 }; // == // Set up your shaders here @@ -224,15 +228,15 @@ fn main() { // Compute time passed since the previous frame and since the start of the program let now = std::time::Instant::now(); - let elapsed = now.duration_since(first_frame_time).as_secs_f32(); + // let elapsed = now.duration_since(first_frame_time).as_secs_f32(); let delta_time = now.duration_since(previous_frame_time).as_secs_f32(); previous_frame_time = now; if let Ok(mut new_size) = arc_window_size.lock() { if new_size.2 { context.resize(glutin::dpi::PhysicalSize::new(new_size.0, new_size.1)); - window_aspect_ratio = new_size.0 as f32 / new_size.1 as f32; - (*new_size).2 = false; + // window_aspect_ratio = new_size.0 as f32 / new_size.1 as f32; + new_size.2 = false; println!("Window was resized to {}x{}", new_size.0, new_size.1); unsafe { gl::Viewport(0, 0, new_size.0 as i32, new_size.1 as i32); diff --git a/gloom-rs/src/shader.rs b/gloom-rs/src/shader.rs index 0a96ce3..6797573 100644 --- a/gloom-rs/src/shader.rs +++ b/gloom-rs/src/shader.rs @@ -1,14 +1,13 @@ -use gl; -use std::{ffi::CString, path::Path, ptr, str}; +// use std::{ffi::CString, path::Path, ptr, str}; -pub struct Shader { - pub program_id: u32, -} +// pub struct Shader { +// pub program_id: u32, +// } -pub struct ShaderBuilder { - program_id: u32, - shaders: Vec, -} +// pub struct ShaderBuilder { +// program_id: u32, +// shaders: Vec, +// } #[allow(dead_code)] pub enum ShaderType { @@ -19,125 +18,125 @@ pub enum ShaderType { Geometry, } -impl Shader { - // Make sure the shader is active before calling this - pub unsafe fn get_uniform_location(&self, name: &str) -> i32 { - let name_cstr = CString::new(name).expect("CString::new failed"); - gl::GetUniformLocation(self.program_id, name_cstr.as_ptr()) - } - - pub unsafe fn activate(&self) { - gl::UseProgram(self.program_id); - } -} - -impl Into for ShaderType { - fn into(self) -> gl::types::GLenum { - match self { - ShaderType::Vertex => gl::VERTEX_SHADER, - ShaderType::Fragment => gl::FRAGMENT_SHADER, - ShaderType::TessellationControl => gl::TESS_CONTROL_SHADER, - ShaderType::TessellationEvaluation => gl::TESS_EVALUATION_SHADER, - ShaderType::Geometry => gl::GEOMETRY_SHADER, - } - } -} - -impl ShaderType { - fn from_ext(ext: &std::ffi::OsStr) -> Result { - match ext.to_str().expect("Failed to read extension") { - "vert" => Ok(ShaderType::Vertex), - "frag" => Ok(ShaderType::Fragment), - "tcs" => Ok(ShaderType::TessellationControl), - "tes" => Ok(ShaderType::TessellationEvaluation), - "geom" => Ok(ShaderType::Geometry), - e => Err(e.to_string()), - } - } -} - -impl ShaderBuilder { - pub unsafe fn new() -> ShaderBuilder { - ShaderBuilder { program_id: gl::CreateProgram(), shaders: vec![] } - } - - pub unsafe fn attach_file(self, shader_path: &str) -> ShaderBuilder { - let path = Path::new(shader_path); - if let Some(extension) = path.extension() { - let shader_type = ShaderType::from_ext(extension).expect("Failed to parse file extension."); - let shader_src = - std::fs::read_to_string(path).expect(&format!("Failed to read shader source. {}", shader_path)); - self.compile_shader(&shader_src, shader_type) - } else { - panic!("Failed to read extension of file with path: {}", shader_path); - } - } - - pub unsafe fn compile_shader(mut self, shader_src: &str, shader_type: ShaderType) -> ShaderBuilder { - let shader = gl::CreateShader(shader_type.into()); - let c_str_shader = CString::new(shader_src.as_bytes()).unwrap(); - gl::ShaderSource(shader, 1, &c_str_shader.as_ptr(), ptr::null()); - gl::CompileShader(shader); - - if !self.check_shader_errors(shader) { - panic!("Shader failed to compile."); - } - - self.shaders.push(shader); - - self - } - - unsafe fn check_shader_errors(&self, shader_id: u32) -> bool { - let mut success = i32::from(gl::FALSE); - let mut info_log = Vec::with_capacity(512); - info_log.set_len(512 - 1); - gl::GetShaderiv(shader_id, gl::COMPILE_STATUS, &mut success); - if success != i32::from(gl::TRUE) { - gl::GetShaderInfoLog( - shader_id, - 512, - ptr::null_mut(), - info_log.as_mut_ptr() as *mut gl::types::GLchar, - ); - println!("ERROR::Shader Compilation Failed!\n{}", String::from_utf8_lossy(&info_log)); - return false; - } - true - } - - unsafe fn check_linker_errors(&self) -> bool { - let mut success = i32::from(gl::FALSE); - let mut info_log = Vec::with_capacity(512); - info_log.set_len(512 - 1); - gl::GetProgramiv(self.program_id, gl::LINK_STATUS, &mut success); - if success != i32::from(gl::TRUE) { - gl::GetProgramInfoLog( - self.program_id, - 512, - ptr::null_mut(), - info_log.as_mut_ptr() as *mut gl::types::GLchar, - ); - println!("ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n{}", String::from_utf8_lossy(&info_log)); - return false; - } - true - } - - #[must_use = "The shader program is useless if not stored in a variable."] - pub unsafe fn link(self) -> Shader { - for &shader in &self.shaders { - gl::AttachShader(self.program_id, shader); - } - gl::LinkProgram(self.program_id); - - // todo:: use this to make safer abstraction - self.check_linker_errors(); - - for &shader in &self.shaders { - gl::DeleteShader(shader); - } - - Shader { program_id: self.program_id } - } -} +// impl Shader { +// // Make sure the shader is active before calling this +// pub unsafe fn get_uniform_location(&self, name: &str) -> i32 { +// let name_cstr = CString::new(name).expect("CString::new failed"); +// gl::GetUniformLocation(self.program_id, name_cstr.as_ptr()) +// } +// +// pub unsafe fn activate(&self) { +// gl::UseProgram(self.program_id); +// } +// } + +// impl Into for ShaderType { +// fn into(self) -> gl::types::GLenum { +// match self { +// ShaderType::Vertex => gl::VERTEX_SHADER, +// ShaderType::Fragment => gl::FRAGMENT_SHADER, +// ShaderType::TessellationControl => gl::TESS_CONTROL_SHADER, +// ShaderType::TessellationEvaluation => gl::TESS_EVALUATION_SHADER, +// ShaderType::Geometry => gl::GEOMETRY_SHADER, +// } +// } +// } + +// impl ShaderType { +// fn from_ext(ext: &std::ffi::OsStr) -> Result { +// match ext.to_str().expect("Failed to read extension") { +// "vert" => Ok(ShaderType::Vertex), +// "frag" => Ok(ShaderType::Fragment), +// "tcs" => Ok(ShaderType::TessellationControl), +// "tes" => Ok(ShaderType::TessellationEvaluation), +// "geom" => Ok(ShaderType::Geometry), +// e => Err(e.to_string()), +// } +// } +// } + +// impl ShaderBuilder { +// pub unsafe fn new() -> ShaderBuilder { +// ShaderBuilder { program_id: gl::CreateProgram(), shaders: vec![] } +// } +// +// pub unsafe fn attach_file(self, shader_path: &str) -> ShaderBuilder { +// let path = Path::new(shader_path); +// if let Some(extension) = path.extension() { +// let shader_type = ShaderType::from_ext(extension).expect("Failed to parse file extension."); +// let shader_src = +// std::fs::read_to_string(path).expect(&format!("Failed to read shader source. {}", shader_path)); +// self.compile_shader(&shader_src, shader_type) +// } else { +// panic!("Failed to read extension of file with path: {}", shader_path); +// } +// } +// +// pub unsafe fn compile_shader(mut self, shader_src: &str, shader_type: ShaderType) -> ShaderBuilder { +// let shader = gl::CreateShader(shader_type.into()); +// let c_str_shader = CString::new(shader_src.as_bytes()).unwrap(); +// gl::ShaderSource(shader, 1, &c_str_shader.as_ptr(), ptr::null()); +// gl::CompileShader(shader); +// +// if !self.check_shader_errors(shader) { +// panic!("Shader failed to compile."); +// } +// +// self.shaders.push(shader); +// +// self +// } +// +// unsafe fn check_shader_errors(&self, shader_id: u32) -> bool { +// let mut success = i32::from(gl::FALSE); +// let mut info_log = Vec::with_capacity(512); +// info_log.set_len(512 - 1); +// gl::GetShaderiv(shader_id, gl::COMPILE_STATUS, &mut success); +// if success != i32::from(gl::TRUE) { +// gl::GetShaderInfoLog( +// shader_id, +// 512, +// ptr::null_mut(), +// info_log.as_mut_ptr() as *mut gl::types::GLchar, +// ); +// println!("ERROR::Shader Compilation Failed!\n{}", String::from_utf8_lossy(&info_log)); +// return false; +// } +// true +// } +// +// unsafe fn check_linker_errors(&self) -> bool { +// let mut success = i32::from(gl::FALSE); +// let mut info_log = Vec::with_capacity(512); +// info_log.set_len(512 - 1); +// gl::GetProgramiv(self.program_id, gl::LINK_STATUS, &mut success); +// if success != i32::from(gl::TRUE) { +// gl::GetProgramInfoLog( +// self.program_id, +// 512, +// ptr::null_mut(), +// info_log.as_mut_ptr() as *mut gl::types::GLchar, +// ); +// println!("ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n{}", String::from_utf8_lossy(&info_log)); +// return false; +// } +// true +// } +// +// #[must_use = "The shader program is useless if not stored in a variable."] +// pub unsafe fn link(self) -> Shader { +// for &shader in &self.shaders { +// gl::AttachShader(self.program_id, shader); +// } +// gl::LinkProgram(self.program_id); +// +// // todo:: use this to make safer abstraction +// self.check_linker_errors(); +// +// for &shader in &self.shaders { +// gl::DeleteShader(shader); +// } +// +// Shader { program_id: self.program_id } +// } +// } diff --git a/gloom-rs/src/util.rs b/gloom-rs/src/util.rs index 68c505f..ee4ec15 100644 --- a/gloom-rs/src/util.rs +++ b/gloom-rs/src/util.rs @@ -1,4 +1,3 @@ -use libc; use std::ffi::CString; pub unsafe fn get_gl_string(name: gl::types::GLenum) -> String {