273 | 273 |
len: usize,
|
274 | 274 |
}
|
275 | 275 |
|
| 276 |
pub struct VBORef<'a, Vtx : 'a> {
|
| 277 |
rf: &'a VertexBuffer<'a, Vtx>,
|
| 278 |
}
|
| 279 |
|
276 | 280 |
impl<'p, Vtx> Drop for VertexBuffer<'p, Vtx> {
|
277 | 281 |
fn drop(&mut self) {
|
278 | 282 |
unsafe { gl::DeleteBuffers(1, &self.idx); }
|
|
325 | 329 |
VertexBuffer { idx, data, phantom, len }
|
326 | 330 |
}
|
327 | 331 |
|
| 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 |
|
328 | 356 |
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)?;
|
330 | 358 |
let (offset, num) = Vtx::pos_location();
|
331 | 359 |
unsafe {
|
332 | 360 |
let off = ptr::null::<u8>().offset(
|
|
345 | 373 |
}
|
346 | 374 |
|
347 | 375 |
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)?;
|
349 | 377 |
|
350 | 378 |
let (offset, num) = match Vtx::uv_location() {
|
351 | 379 |
Some(p) => p,
|
|
369 | 397 |
Ok(())
|
370 | 398 |
}
|
371 | 399 |
|
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 |
}
|
395 | 401 |
|
396 | 402 |
#[allow(dead_code)]
|
397 | 403 |
pub struct Texture {
|