Use a bind function which only binds another buffer if necessary
@@ -20,13 +20,15 @@ impl VertexBuffer
unsafe {
gl.GenBuffers(1, &mut id);
}
- Ok(VertexBuffer{gl: gl.clone(), id: id})
+ let buffer = VertexBuffer{gl: gl.clone(), id };
+ buffer.bind();
+ Ok(buffer)
}
pub fn fill_with(&self, data: &Vec<f32>)
{
+ self.bind();
unsafe {
- self.gl.BindBuffer(gl::ARRAY_BUFFER, self.id);
self.gl.BufferData(
gl::ARRAY_BUFFER, // target
(data.len() * std::mem::size_of::<f32>()) as gl::types::GLsizeiptr, // size of data in bytes
@@ -35,4 +37,16 @@ impl VertexBuffer
);
}
}
+
+ fn bind(&self)
+ {
+ unsafe {
+ static mut CURRENTLY_USED: gl::types::GLuint = 1000000 as u32;
+ if self.id != CURRENTLY_USED
+ {
+ self.gl.BindBuffer(gl::ARRAY_BUFFER, self.id);
+ CURRENTLY_USED = self.id;
+ }
+ }
+ }
}
