gdritter repos chenoska / e1c6d81
Replace bind/unbind for VBOs with nicer abstraction Getty Ritter 5 years ago
2 changed file(s) with 41 addition(s) and 37 deletion(s). Collapse all Expand all
273273 len: usize,
274274 }
275275
276 pub struct VBORef<'a, Vtx : 'a> {
277 rf: &'a VertexBuffer<'a, Vtx>,
278 }
279
276280 impl<'p, Vtx> Drop for VertexBuffer<'p, Vtx> {
277281 fn drop(&mut self) {
278282 unsafe { gl::DeleteBuffers(1, &self.idx); }
325329 VertexBuffer { idx, data, phantom, len }
326330 }
327331
332 pub fn with<F, T>(&self, f: F) -> T
333 where F: for<'a> Fn(VBORef<'a, Vtx>) -> T
334 {
335 unsafe {
336 self.data.bind();
337 gl::BindBuffer(gl::ARRAY_BUFFER, self.idx);
338 }
339 let res = f(VBORef { rf: &self });
340 unsafe {
341 gl::BindBuffer(gl::ARRAY_BUFFER, 0);
342 self.data.unbind();
343 }
344 res
345 }
346 }
347
348
349 impl<'p, Vtx: vertices::Vertex> VBORef<'p, Vtx> {
350 pub fn draw(&self) {
351 unsafe {
352 gl::DrawArrays(gl::TRIANGLES, 0, self.rf.len as i32);
353 }
354 }
355
328356 pub fn bind_position(&self, name: &str) -> Result<(), Error> {
329 let attr = self.data.program.get_attrib_location(name)?;
357 let attr = self.rf.data.program.get_attrib_location(name)?;
330358 let (offset, num) = Vtx::pos_location();
331359 unsafe {
332360 let off = ptr::null::<u8>().offset(
345373 }
346374
347375 pub fn bind_uv(&self, name: &str) -> Result<(), Error> {
348 let attr = self.data.program.get_attrib_location(name)?;
376 let attr = self.rf.data.program.get_attrib_location(name)?;
349377
350378 let (offset, num) = match Vtx::uv_location() {
351379 Some(p) => p,
369397 Ok(())
370398 }
371399
372 pub fn bind(&self) {
373 unsafe {
374 self.data.bind();
375 gl::BindBuffer(gl::ARRAY_BUFFER, self.idx);
376 }
377 }
378
379 pub fn unbind(&self) {
380 unsafe {
381 gl::BindBuffer(gl::ARRAY_BUFFER, 0);
382 self.data.unbind();
383 }
384 }
385
386 pub fn draw(&self) {
387 self.bind();
388 unsafe {
389 gl::DrawArrays(gl::TRIANGLES, 0, self.len as i32);
390 }
391 self.unbind();
392 }
393 }
394
400 }
395401
396402 #[allow(dead_code)]
397403 pub struct Texture {
4747 gfx::VertexArray::new(&program),
4848 &tile_data,
4949 );
50 vbo.unbind();
5150
5251 let upper_tile_data = graphics::vertices::make_upper_grid_with(
5352 board::BOARD_WIDTH as usize,
5857 gfx::VertexArray::new(&program),
5958 &upper_tile_data,
6059 );
61 upper_vbo.unbind();
6260
6361 program.use_program();
6462 program.bind_frag_data_location(0, "out_color")?;
6563
66 vbo.bind();
67 vbo.bind_position("position")?;
68 vbo.bind_uv("uv")?;
69 vbo.unbind();
64 vbo.with (|r| {
65 r.bind_position("position")?;
66 r.bind_uv("uv")
67 })?;
7068
71 upper_vbo.bind();
72 upper_vbo.bind_position("position")?;
73 upper_vbo.bind_uv("uv")?;
74 upper_vbo.unbind();
69 upper_vbo.with( |r| {
70 r.bind_position("position")?;
71 r.bind_uv("uv")
72 })?;
7573
7674 program.set_texture("tex", &tex, 0).unwrap();
7775 program.set_texture("nrm", &nrm, 1).unwrap();
10199 }
102100
103101 gfx::clear();
104 vbo.draw();
105 upper_vbo.draw();
102 vbo.with(|r| r.draw());
103 upper_vbo.with(|r| r.draw());
106104
107105 ControlFlow::Continue
108106 })?;