@@ -8,25 +8,20 @@ use crate::{ | |||
misc::{AsEnum, Bindable}, | |||
}; | |||
/* ArrayBuffer */ | |||
/* Buffer */ | |||
pub struct ArrayBuffer { | |||
pub struct Buffer { | |||
id: gl::GLuint, | |||
target: Target, | |||
size: usize, | |||
} | |||
impl ArrayBuffer { | |||
pub fn new(target: Target) -> Result<Self, Error> { | |||
impl Buffer { | |||
pub fn new() -> Result<Self, Error> { | |||
let mut id = 0; | |||
Error::checked(|| gl::create_buffers(1, &mut id))?; | |||
Ok(Self { | |||
id, | |||
target, | |||
size: 0, | |||
}) | |||
Ok(Self { id, size: 0 }) | |||
} | |||
pub fn id(&self) -> gl::GLuint { | |||
@@ -107,12 +102,18 @@ impl ArrayBuffer { | |||
}) | |||
} | |||
pub fn bind_buffer_base(&self, index: gl::GLuint) { | |||
gl::bind_buffer_base(self.target.as_enum(), index, self.id); | |||
pub fn bind_buffer_base(&self, target: Target, index: gl::GLuint) { | |||
gl::bind_buffer_base(target.as_enum(), index, self.id); | |||
} | |||
pub fn bind_buffer_range(&self, index: gl::GLuint, offset: gl::GLintptr, size: gl::GLsizeiptr) { | |||
gl::bind_buffer_range(self.target.as_enum(), index, self.id, offset, size); | |||
pub fn bind_buffer_range( | |||
&self, | |||
target: Target, | |||
index: gl::GLuint, | |||
offset: gl::GLintptr, | |||
size: gl::GLsizeiptr, | |||
) { | |||
gl::bind_buffer_range(target.as_enum(), index, self.id, offset, size); | |||
} | |||
fn inner_map<T>(&self, access: gl::GLenum) -> Result<&mut [T], Error> { | |||
@@ -150,26 +151,26 @@ impl ArrayBuffer { | |||
} | |||
} | |||
impl Drop for ArrayBuffer { | |||
impl Drop for Buffer { | |||
fn drop(&mut self) { | |||
gl::delete_buffers(1, &self.id); | |||
} | |||
} | |||
impl Bindable for ArrayBuffer { | |||
impl<'a> Bindable for (Target, &'a Buffer) { | |||
fn bind(&self) { | |||
gl::bind_buffer(self.target.as_enum(), self.id); | |||
gl::bind_buffer(self.0.as_enum(), self.1.id); | |||
} | |||
fn unbind(&self) { | |||
gl::bind_buffer(self.target.as_enum(), 0); | |||
gl::bind_buffer(self.0.as_enum(), 0); | |||
} | |||
} | |||
/* Map */ | |||
pub struct Map<'a, T> { | |||
buf: &'a ArrayBuffer, | |||
buf: &'a Buffer, | |||
data: &'a [T], | |||
} | |||
@@ -190,7 +191,7 @@ impl<T> Deref for Map<'_, T> { | |||
/* MapMut */ | |||
pub struct MapMut<'a, T> { | |||
buf: &'a ArrayBuffer, | |||
buf: &'a Buffer, | |||
data: &'a mut [T], | |||
} | |||
@@ -1,6 +1,6 @@ | |||
pub mod angle; | |||
pub mod animation; | |||
pub mod array_buffer; | |||
pub mod buffer; | |||
pub mod error; | |||
pub mod math; | |||
pub mod matrix; | |||
@@ -1,5 +1,5 @@ | |||
use crate::{ | |||
array_buffer::ArrayBuffer, | |||
buffer::{Buffer, Target}, | |||
error::Error, | |||
misc::{AsEnum, BindGuard, Bindable}, | |||
}; | |||
@@ -8,7 +8,7 @@ use crate::{ | |||
pub struct VertexArray { | |||
id: gl::GLuint, | |||
buffers: Vec<ArrayBuffer>, | |||
buffers: Vec<Buffer>, | |||
} | |||
impl VertexArray { | |||
@@ -16,11 +16,11 @@ impl VertexArray { | |||
Builder::default() | |||
} | |||
pub fn buffers(&self) -> &Vec<ArrayBuffer> { | |||
pub fn buffers(&self) -> &Vec<Buffer> { | |||
&self.buffers | |||
} | |||
pub fn buffers_mut(&mut self) -> &mut Vec<ArrayBuffer> { | |||
pub fn buffers_mut(&mut self) -> &mut Vec<Buffer> { | |||
&mut self.buffers | |||
} | |||
} | |||
@@ -49,7 +49,7 @@ pub struct Builder { | |||
} | |||
impl Builder { | |||
pub fn bind_buffer(mut self, buffer: ArrayBuffer) -> BindingBuilder { | |||
pub fn bind_buffer(mut self, buffer: Buffer) -> BindingBuilder { | |||
let binding = Binding { | |||
buffer, | |||
pointers: Vec::new(), | |||
@@ -74,7 +74,7 @@ impl Builder { | |||
let mut buffers = Vec::new(); | |||
for binding in self.bindings { | |||
let guard = BindGuard::new(&binding.buffer); | |||
let guard = BindGuard::new((Target::ArrayBuffer, &binding.buffer)); | |||
for pointer in binding.pointers { | |||
Error::checked(|| gl::enable_vertex_attrib_array(pointer.index))?; | |||
@@ -119,7 +119,7 @@ pub struct BindingBuilder { | |||
} | |||
impl BindingBuilder { | |||
pub fn bind_buffer(self, buffer: ArrayBuffer) -> Self { | |||
pub fn bind_buffer(self, buffer: Buffer) -> Self { | |||
self.builder.bind_buffer(buffer) | |||
} | |||
@@ -219,7 +219,7 @@ impl AsEnum for DataType { | |||
/* Binding */ | |||
struct Binding { | |||
buffer: ArrayBuffer, | |||
buffer: Buffer, | |||
pointers: Vec<Pointer>, | |||
} | |||
@@ -11,7 +11,7 @@ use std::ptr::null; | |||
use std::rc::{Rc, Weak}; | |||
use glc::{ | |||
array_buffer::{ArrayBuffer, Target, Usage}, | |||
buffer::{Buffer, Usage}, | |||
error::Error as GlcError, | |||
misc::BindGuard, | |||
shader::{Program, Type, Uniform}, | |||
@@ -592,7 +592,7 @@ impl Text { | |||
const OFFSET_TEX_MAX: gl::GLsizei = OFFSET_TEX_MIN + SIZE_VEC2; | |||
const OFFSET_COLOR: gl::GLsizei = OFFSET_TEX_MAX + SIZE_VEC2; | |||
let buffer = ArrayBuffer::new(Target::ArrayBuffer)?; | |||
let buffer = Buffer::new()?; | |||
let array = VertexArray::builder() | |||
.bind_buffer(buffer) | |||
.vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE, OFFSET_POS_MIN)? | |||
@@ -1,14 +1,14 @@ | |||
#![allow(dead_code)] | |||
use glc::{ | |||
array_buffer::{ArrayBuffer, Target, Usage}, | |||
buffer::{Buffer, Target, Usage}, | |||
error::Error, | |||
matrix::Matrix4f, | |||
vector::Vector2f, | |||
}; | |||
pub struct Camera { | |||
buffer: ArrayBuffer, | |||
buffer: Buffer, | |||
data: Data, | |||
view_invert: Matrix4f, | |||
projection_invert: Matrix4f, | |||
@@ -30,7 +30,7 @@ impl Camera { | |||
view: Matrix4f::identity(), | |||
size: Vector2f::default(), | |||
}; | |||
let mut buffer = ArrayBuffer::new(Target::UniformBuffer)?; | |||
let mut buffer = Buffer::new()?; | |||
buffer.buffer_data(Usage::StaticDraw, &[data.clone()])?; | |||
Ok(Self { | |||
@@ -100,7 +100,7 @@ impl Camera { | |||
} | |||
pub fn bind(&self, index: gl::GLuint) -> Result<(), Error> { | |||
Error::checked(|| self.buffer.bind_buffer_base(index)) | |||
Error::checked(|| self.buffer.bind_buffer_base(Target::UniformBuffer, index)) | |||
} | |||
pub fn world_to_view<T: Into<Vector2f>>(&self, pos: T) -> Vector2f { | |||
@@ -3,7 +3,7 @@ | |||
use std::mem::size_of; | |||
use glc::{ | |||
array_buffer::{ArrayBuffer, Target, Usage}, | |||
buffer::{Buffer, Usage}, | |||
misc::Bindable, | |||
shader::{Program, Type, Uniform}, | |||
vector::{Vector2f, Vector4f}, | |||
@@ -78,11 +78,11 @@ fn create_array_quad() -> Result<VertexArray, Error> { | |||
const STRIDE_POS: gl::GLsizei = size_of::<Vector2f>() as gl::GLsizei; | |||
const OFFSET_POS: gl::GLsizei = 0; | |||
let mut array_buffer = ArrayBuffer::new(Target::ArrayBuffer)?; | |||
array_buffer.buffer_data(Usage::StaticDraw, vertices)?; | |||
let mut buffer = Buffer::new()?; | |||
buffer.buffer_data(Usage::StaticDraw, vertices)?; | |||
let vertex_array = VertexArray::builder() | |||
.bind_buffer(array_buffer) | |||
.bind_buffer(buffer) | |||
.vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE_POS, OFFSET_POS)? | |||
.build()?; | |||
@@ -93,10 +93,10 @@ fn create_array_line() -> Result<VertexArray, Error> { | |||
const STRIDE_POS: gl::GLsizei = size_of::<Vector2f>() as gl::GLsizei; | |||
const OFFSET_POS: gl::GLsizei = 0; | |||
let array_buffer = ArrayBuffer::new(Target::ArrayBuffer)?; | |||
let buffer = Buffer::new()?; | |||
let vertex_array = VertexArray::builder() | |||
.bind_buffer(array_buffer) | |||
.bind_buffer(buffer) | |||
.vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE_POS, OFFSET_POS)? | |||
.build()?; | |||
@@ -1,12 +1,12 @@ | |||
use std::time::Instant; | |||
use glc::{ | |||
array_buffer::{ArrayBuffer, Target, Usage}, | |||
buffer::{Buffer, Target, Usage}, | |||
error::Error, | |||
}; | |||
pub struct Uniform { | |||
buffer: ArrayBuffer, | |||
buffer: Buffer, | |||
start_time: Instant, | |||
} | |||
@@ -17,7 +17,7 @@ struct Data { | |||
impl Uniform { | |||
pub fn new() -> Result<Self, Error> { | |||
let mut buffer = ArrayBuffer::new(Target::UniformBuffer)?; | |||
let mut buffer = Buffer::new()?; | |||
buffer.buffer_data(Usage::StaticDraw, &[Data { time: 0.0 }])?; | |||
Ok(Self { | |||
@@ -37,6 +37,6 @@ impl Uniform { | |||
} | |||
pub fn bind(&self, index: gl::GLuint) -> Result<(), Error> { | |||
Error::checked(|| self.buffer.bind_buffer_base(index)) | |||
Error::checked(|| self.buffer.bind_buffer_base(Target::UniformBuffer, index)) | |||
} | |||
} |