🚨 Fix clippy warnings

main
_ 2020-02-16 22:56:35 +00:00
parent ffcb2c114d
commit f8ceec6b9f
2 changed files with 27 additions and 154 deletions

View File

@ -47,7 +47,7 @@ mod types {
pub const CUSTOM: u32 = 0x10; pub const CUSTOM: u32 = 0x10;
} }
mod formats { pub mod formats {
pub const BYTE: u32 = 0; pub const BYTE: u32 = 0;
pub const UBYTE: u32 = 1; pub const UBYTE: u32 = 1;
pub const SHORT: u32 = 2; pub const SHORT: u32 = 2;
@ -105,10 +105,10 @@ impl Header {
let mut fields = [0; 27]; let mut fields = [0; 27];
fields [0] = version; fields [0] = version;
for index in 1..fields.len () { for field in fields.iter_mut ().skip (1) {
let (i, h) = le_u32 (input)?; let (i, h) = le_u32 (input)?;
input = i; input = i;
fields [usize::from (index)] = h; *field = h;
} }
Ok ((input, Header { Ok ((input, Header {

View File

@ -11,8 +11,10 @@ use std::path::Path;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
mod iqm; mod iqm;
mod shader;
use iqm::Model; use iqm::Model;
use shader::{ShaderProgram, ShaderObject};
pub fn load_small_file <P> (name: P) -> Vec <u8> pub fn load_small_file <P> (name: P) -> Vec <u8>
where P: AsRef <Path> where P: AsRef <Path>
@ -26,11 +28,23 @@ where P: AsRef <Path>
let mut data = vec! [0u8; len.try_into ().unwrap ()]; let mut data = vec! [0u8; len.try_into ().unwrap ()];
f.read (&mut data [..]).unwrap (); f.read_exact (&mut data [..]).unwrap ();
data data
} }
pub fn color_from_255 <V> (rgb: V) -> Vec3
where V: Into <Vec3>
{
let rgb: Vec3 = rgb.into ();
Vec3::from ((
rgb.x () / 255.0,
rgb.y () / 255.0,
rgb.z () / 255.0
))
}
pub fn ugly_load_texture <P> (name: P) -> u32 pub fn ugly_load_texture <P> (name: P) -> u32
where P: AsRef <Path> where P: AsRef <Path>
{ {
@ -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 <ShaderObject, String>
{
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 <ShaderProgram, String>
{
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 <u32>) { fn enable_vertex_attrib_array (id: Option <u32>) {
match id { if let Some (id) = id {
Some (id) => unsafe { // Are safety checks really needed here?
unsafe {
gl::EnableVertexAttribArray (id); gl::EnableVertexAttribArray (id);
}, }
_ => (),
} }
} }
unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, slice: &[u8]) { unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, slice: &[u8]) {
const FALSE_U8: u8 = 0; const FALSE_U8: u8 = 0;
const FLOAT_SIZE: i32 = 4; const FLOAT_SIZE: i32 = 4;
match id {
Some (id) => { 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); 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_RIGHT: usize = KEY_LEFT + 1;
const KEY_UP: usize = KEY_RIGHT + 1; const KEY_UP: usize = KEY_RIGHT + 1;
const KEY_DOWN: usize = KEY_UP + 1; const KEY_DOWN: usize = KEY_UP + 1;
const KEY_COUNT: usize = KEY_DOWN + 1;
struct ControllerState { struct ControllerState {
keys: Vec <bool>, keys: Vec <bool>,
@ -390,10 +270,6 @@ impl TimeStep {
} }
} }
unsafe fn draw_world (world: &WorldState) {
}
fn main () { fn main () {
let sdl_context = sdl2::init ().unwrap (); let sdl_context = sdl2::init ().unwrap ();
let video_subsystem = sdl_context.video ().unwrap (); let video_subsystem = sdl_context.video ().unwrap ();
@ -491,9 +367,6 @@ fn main () {
gl::Enable (gl::DEPTH_TEST); gl::Enable (gl::DEPTH_TEST);
gl::Enable (gl::TEXTURE); gl::Enable (gl::TEXTURE);
let num_coords = 3;
let stride = 4 * num_coords;
} }
let mut time_step = TimeStep::new (60, 1000); 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 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 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 orange = color_from_255 ((255.0, 154.0, 0.0));
let green = Vec3::from ((14.0 / 255.0, 127.0 / 255.0, 24.0 / 255.0)); let green = color_from_255 ((14.0, 127.0, 24.0));
let white = Vec3::from ((1.0, 1.0, 1.0)); let white = Vec3::from ((1.0, 1.0, 1.0));
let black = Vec3::from ((0.0, 0.0, 0.0)); let black = Vec3::from ((0.0, 0.0, 0.0));