diff --git a/src/iqm.rs b/src/iqm.rs index 6658acb..fcb95f7 100644 --- a/src/iqm.rs +++ b/src/iqm.rs @@ -47,7 +47,7 @@ mod types { pub const CUSTOM: u32 = 0x10; } -mod formats { +pub mod formats { pub const BYTE: u32 = 0; pub const UBYTE: u32 = 1; pub const SHORT: u32 = 2; @@ -105,10 +105,10 @@ impl Header { let mut fields = [0; 27]; fields [0] = version; - for index in 1..fields.len () { + for field in fields.iter_mut ().skip (1) { let (i, h) = le_u32 (input)?; input = i; - fields [usize::from (index)] = h; + *field = h; } Ok ((input, Header { diff --git a/src/main.rs b/src/main.rs index 50d0fe0..8cb8351 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,10 @@ use std::path::Path; use std::time::{Duration, Instant}; mod iqm; +mod shader; use iqm::Model; +use shader::{ShaderProgram, ShaderObject}; pub fn load_small_file

(name: P) -> Vec where P: AsRef @@ -26,11 +28,23 @@ where P: AsRef let mut data = vec! [0u8; len.try_into ().unwrap ()]; - f.read (&mut data [..]).unwrap (); + f.read_exact (&mut data [..]).unwrap (); data } +pub fn color_from_255 (rgb: V) -> Vec3 +where V: Into +{ + let rgb: Vec3 = rgb.into (); + + Vec3::from (( + rgb.x () / 255.0, + rgb.y () / 255.0, + rgb.z () / 255.0 + )) +} + pub fn ugly_load_texture

(name: P) -> u32 where P: AsRef { @@ -111,154 +125,21 @@ void main (void) { } "; -pub struct ShaderObject { - id: u32, -} - -impl ShaderObject { - pub fn id (&self) -> u32 { - self.id - } - - pub fn new (shader_type: u32, source: &str) -> Result - { - let id = unsafe { - gl::CreateShader (shader_type) - }; - - let sources = [ - source.as_ptr () as *const i8, - ]; - - let lengths = [ - source.len ().try_into ().unwrap (), - ]; - - let success = unsafe { - gl::ShaderSource (id, sources.len ().try_into ().unwrap (), sources.as_ptr (), lengths.as_ptr ()); - gl::CompileShader (id); - - let mut success = 0; - gl::GetShaderiv (id, gl::COMPILE_STATUS, &mut success); - success == 1 - }; - - if success { - Ok (ShaderObject { - id, - }) - } - else { - let mut info_log = vec! [0u8; 4096]; - let mut log_length = 0; - - unsafe { - gl::GetShaderInfoLog (id, (info_log.len () - 1).try_into ().unwrap (), &mut log_length, info_log.as_mut_ptr () as *mut i8); - } - - info_log.truncate (log_length.try_into ().unwrap ()); - - let info = String::from_utf8 (info_log).unwrap (); - - Err (info) - } - } -} - -impl Drop for ShaderObject { - fn drop (&mut self) { - unsafe { - gl::DeleteShader (self.id); - } - } -} - -pub struct ShaderProgram { - id: u32, -} - -impl ShaderProgram { - pub fn new (vert: &ShaderObject, frag: &ShaderObject) - -> Result - { - let id = unsafe { - gl::CreateProgram () - }; - - unsafe { - gl::AttachShader (id, vert.id ()); - gl::AttachShader (id, frag.id ()); - - gl::LinkProgram (id); - } - - let success = unsafe { - let mut success = 0; - gl::GetProgramiv (id, gl::LINK_STATUS, &mut success); - success == 1 - }; - - if success { - Ok (ShaderProgram { - id, - }) - } - else { - let mut info_log = vec! [0u8; 4096]; - let mut log_length = 0; - - unsafe { - gl::GetProgramInfoLog (id, (info_log.len () - 1).try_into ().unwrap (), &mut log_length, info_log.as_mut_ptr () as *mut i8); - } - - info_log.truncate (log_length.try_into ().unwrap ()); - - let info = String::from_utf8 (info_log).unwrap (); - - Err (info) - } - } - - pub fn get_uniform_location (&self, name: &CStr) -> i32 { - unsafe { - gl::UseProgram (self.id); - gl::GetUniformLocation (self.id, name.as_ptr ()) - } - } - - pub fn get_attribute_location (&self, name: &CStr) -> i32 { - unsafe { - gl::UseProgram (self.id); - gl::GetAttribLocation (self.id, name.as_ptr ()) - } - } -} - -impl Drop for ShaderProgram { - fn drop (&mut self) { - unsafe { - gl::DeleteProgram (self.id); - } - } -} - fn enable_vertex_attrib_array (id: Option ) { - match id { - Some (id) => unsafe { + if let Some (id) = id { + // Are safety checks really needed here? + unsafe { gl::EnableVertexAttribArray (id); - }, - _ => (), + } } } unsafe fn vertex_attrib_pointer (id: Option , num_coords: i32, slice: &[u8]) { const FALSE_U8: u8 = 0; const FLOAT_SIZE: i32 = 4; - match id { - Some (id) => { - gl::VertexAttribPointer (id, num_coords, gl::FLOAT, FALSE_U8, FLOAT_SIZE * num_coords, &slice [0] as *const u8 as *const c_void); - }, - _ => (), + + if let Some (id) = id { + gl::VertexAttribPointer (id, num_coords, gl::FLOAT, FALSE_U8, FLOAT_SIZE * num_coords, &slice [0] as *const u8 as *const c_void); } } @@ -275,7 +156,6 @@ const KEY_LEFT: usize = 0; const KEY_RIGHT: usize = KEY_LEFT + 1; const KEY_UP: usize = KEY_RIGHT + 1; const KEY_DOWN: usize = KEY_UP + 1; -const KEY_COUNT: usize = KEY_DOWN + 1; struct ControllerState { keys: Vec , @@ -390,10 +270,6 @@ impl TimeStep { } } -unsafe fn draw_world (world: &WorldState) { - -} - fn main () { let sdl_context = sdl2::init ().unwrap (); let video_subsystem = sdl_context.video ().unwrap (); @@ -491,9 +367,6 @@ fn main () { gl::Enable (gl::DEPTH_TEST); gl::Enable (gl::TEXTURE); - - let num_coords = 3; - let stride = 4 * num_coords; } let mut time_step = TimeStep::new (60, 1000); @@ -545,8 +418,8 @@ fn main () { let light = Vec3::from ((2.0, 0.0, 5.0)).normalize (); let object_space_light = model_mat.inverse () * Vec4::from ((light.x (), light.y (), light.z (), 0.0)); - let orange = Vec3::from ((255.0 / 255.0, 154.0 / 255.0, 0.0 / 255.0)); - let green = Vec3::from ((14.0 / 255.0, 127.0 / 255.0, 24.0 / 255.0)); + let orange = color_from_255 ((255.0, 154.0, 0.0)); + let green = color_from_255 ((14.0, 127.0, 24.0)); let white = Vec3::from ((1.0, 1.0, 1.0)); let black = Vec3::from ((0.0, 0.0, 0.0));