Fix clippy lint
parent
24418bfa55
commit
cd46a8dfcb
|
@ -13,28 +13,60 @@ pub struct VertexBuffer {
|
||||||
len: usize,
|
len: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const FLOAT_SIZE: usize = 4;
|
||||||
|
|
||||||
impl VertexBuffer {
|
impl VertexBuffer {
|
||||||
pub fn from_slice (slice: &[f32]) -> Self {
|
// len is the number of floats
|
||||||
const FLOAT_SIZE: usize = 4;
|
|
||||||
|
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 = {
|
let id = Self::allocate_buffer (len);
|
||||||
let mut id = 0;
|
|
||||||
|
let mut offset = 0;
|
||||||
|
for slice in slices.iter () {
|
||||||
unsafe {
|
unsafe {
|
||||||
gl::GenBuffers (1, &mut id);
|
gl::BufferSubData (
|
||||||
gl::BindBuffer (gl::ARRAY_BUFFER, id);
|
gl::ARRAY_BUFFER,
|
||||||
|
(offset * FLOAT_SIZE).try_into ().unwrap (),
|
||||||
gl::BufferData (gl::ARRAY_BUFFER, (slice.len () * FLOAT_SIZE).try_into ().unwrap (), &slice [0] as *const f32 as *const c_void, gl::STATIC_DRAW);
|
(len * FLOAT_SIZE).try_into ().unwrap (),
|
||||||
|
&slice [0] as *const f32 as *const c_void
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert! (id != 0);
|
|
||||||
id
|
offset += slice.len ();
|
||||||
};
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
len: slice.len (),
|
len,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_slice (slice: &[f32]) -> Self {
|
||||||
|
Self::from_slices (&[slice])
|
||||||
|
}
|
||||||
|
|
||||||
pub fn bind (&self) {
|
pub fn bind (&self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
gl::BindBuffer (gl::ARRAY_BUFFER, self.id);
|
gl::BindBuffer (gl::ARRAY_BUFFER, self.id);
|
||||||
|
|
Loading…
Reference in New Issue