Fix clippy lint

main
_ 2020-03-07 02:23:09 +00:00
parent 24418bfa55
commit cd46a8dfcb
1 changed files with 44 additions and 12 deletions

View File

@ -13,28 +13,60 @@ pub struct VertexBuffer {
len: usize,
}
const FLOAT_SIZE: usize = 4;
impl VertexBuffer {
pub fn from_slice (slice: &[f32]) -> Self {
const FLOAT_SIZE: usize = 4;
// len is the number of floats
let id = {
let mut id = 0;
fn allocate_buffer (len: usize) -> u32 {
let mut id = 0;
unsafe {
gl::GenBuffers (1, &mut id);
gl::BindBuffer (gl::ARRAY_BUFFER, id);
// Khronos docs say that null is not derefed here
gl::BufferData (
gl::ARRAY_BUFFER,
(len * FLOAT_SIZE).try_into ().unwrap (),
std::ptr::null (),
gl::STATIC_DRAW
);
}
assert! (id != 0);
id
}
pub fn from_slices (slices: &[&[f32]]) -> Self {
let len = slices.iter ()
.map (|slice| slice.len ())
.sum ();
let id = Self::allocate_buffer (len);
let mut offset = 0;
for slice in slices.iter () {
unsafe {
gl::GenBuffers (1, &mut id);
gl::BindBuffer (gl::ARRAY_BUFFER, id);
gl::BufferData (gl::ARRAY_BUFFER, (slice.len () * FLOAT_SIZE).try_into ().unwrap (), &slice [0] as *const f32 as *const c_void, gl::STATIC_DRAW);
gl::BufferSubData (
gl::ARRAY_BUFFER,
(offset * FLOAT_SIZE).try_into ().unwrap (),
(len * FLOAT_SIZE).try_into ().unwrap (),
&slice [0] as *const f32 as *const c_void
);
}
assert! (id != 0);
id
};
offset += slice.len ();
}
Self {
id,
len: slice.len (),
len,
}
}
pub fn from_slice (slice: &[f32]) -> Self {
Self::from_slices (&[slice])
}
pub fn bind (&self) {
unsafe {
gl::BindBuffer (gl::ARRAY_BUFFER, self.id);