use std::ffi::c_void;
use std::fmt;
use std::rc::Rc;
use crate::buffer::Buffer;
use crate::context::{GlContext, GlInner};
use crate::error::{checked_sizei, GlError, Result};
use crate::sys;
use crate::types::{BufferTarget, DataType};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VertexAttribDesc {
pub location: u32,
pub components: u8,
pub data_type: DataType,
pub normalized: bool,
pub stride: usize,
pub offset: usize,
}
pub struct VertexArray {
ctx: Rc<GlInner>,
id: sys::GLuint,
}
impl fmt::Debug for VertexArray {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VertexArray").field("id", &self.id).finish()
}
}
impl Drop for VertexArray {
fn drop(&mut self) {
if self.id != 0 {
unsafe { (self.ctx.fns.delete_vertex_arrays)(1, &self.id) };
}
}
}
impl VertexArray {
pub fn new(ctx: &GlContext) -> Result<Self> {
let mut id: sys::GLuint = 0;
unsafe { (ctx.inner.fns.gen_vertex_arrays)(1, &mut id) };
if id == 0 {
return Err(GlError::ObjectCreationFailed {
object: "VertexArray",
});
}
Ok(Self {
ctx: Rc::clone(&ctx.inner),
id,
})
}
#[inline]
pub const fn raw_id(&self) -> u32 {
self.id
}
#[inline]
pub fn bind(&self) {
unsafe { (self.ctx.fns.bind_vertex_array)(self.id) };
}
#[inline]
pub fn unbind(ctx: &GlContext) {
unsafe { (ctx.inner.fns.bind_vertex_array)(0) };
}
pub fn set_attrib_pointer(&self, buffer: &Buffer, desc: &VertexAttribDesc) -> Result<()> {
if !(1..=4).contains(&desc.components) {
return Err(GlError::InvalidParameter {
parameter: "components",
reason: "vertex attribute component count must be 1, 2, 3, or 4",
});
}
let stride = checked_sizei(desc.stride, "VertexArray::set_attrib_pointer stride")?;
let normalized = if desc.normalized {
sys::GL_TRUE
} else {
sys::GL_FALSE
};
unsafe {
(self.ctx.fns.bind_vertex_array)(self.id);
(self.ctx.fns.bind_buffer)(sys::GL_ARRAY_BUFFER, buffer.raw_id());
(self.ctx.fns.vertex_attrib_pointer)(
desc.location,
sys::GLint::from(desc.components),
desc.data_type as sys::GLenum,
normalized,
stride,
desc.offset as *const c_void,
);
(self.ctx.fns.enable_vertex_attrib_array)(desc.location);
}
self.ctx.check_gl_error("glVertexAttribPointer")
}
pub fn set_attrib_integer(&self, buffer: &Buffer, desc: &VertexAttribDesc) -> Result<()> {
if !(1..=4).contains(&desc.components) {
return Err(GlError::InvalidParameter {
parameter: "components",
reason: "vertex attribute component count must be 1, 2, 3, or 4",
});
}
if !desc.data_type.is_integer() {
return Err(GlError::InvalidParameter {
parameter: "data_type",
reason: "set_attrib_integer requires an integer DataType",
});
}
let stride = checked_sizei(desc.stride, "VertexArray::set_attrib_integer stride")?;
unsafe {
(self.ctx.fns.bind_vertex_array)(self.id);
(self.ctx.fns.bind_buffer)(sys::GL_ARRAY_BUFFER, buffer.raw_id());
(self.ctx.fns.vertex_attrib_ipointer)(
desc.location,
sys::GLint::from(desc.components),
desc.data_type as sys::GLenum,
stride,
desc.offset as *const c_void,
);
(self.ctx.fns.enable_vertex_attrib_array)(desc.location);
}
self.ctx.check_gl_error("glVertexAttribIPointer")
}
pub fn set_attrib_divisor(&self, location: u32, divisor: u32) -> Result<()> {
unsafe {
(self.ctx.fns.bind_vertex_array)(self.id);
(self.ctx.fns.vertex_attrib_divisor)(location, divisor);
}
self.ctx.check_gl_error("glVertexAttribDivisor")
}
pub fn set_index_buffer(&self, index_buffer: &Buffer) -> Result<()> {
if index_buffer.target() != BufferTarget::ElementArray {
return Err(GlError::InvalidParameter {
parameter: "index_buffer",
reason: "expected a buffer created with BufferTarget::ElementArray",
});
}
unsafe {
(self.ctx.fns.bind_vertex_array)(self.id);
(self.ctx.fns.bind_buffer)(sys::GL_ELEMENT_ARRAY_BUFFER, index_buffer.raw_id());
}
Ok(())
}
pub fn disable_attrib(&self, location: u32) {
unsafe {
(self.ctx.fns.bind_vertex_array)(self.id);
(self.ctx.fns.disable_vertex_attrib_array)(location);
}
}
}