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,
}
impl VertexBuffer {
pub fn from_slice (slice: &[f32]) -> Self {
const FLOAT_SIZE: usize = 4;
let id = {
impl VertexBuffer {
// len is the number of floats
fn allocate_buffer (len: usize) -> u32 {
let mut id = 0;
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);
// 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::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
);
}
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);