diff options
Diffstat (limited to 'engine/src/opengl/vertex_array.rs')
-rw-r--r-- | engine/src/opengl/vertex_array.rs | 132 |
1 files changed, 77 insertions, 55 deletions
diff --git a/engine/src/opengl/vertex_array.rs b/engine/src/opengl/vertex_array.rs index 6b6065c..e1e1a15 100644 --- a/engine/src/opengl/vertex_array.rs +++ b/engine/src/opengl/vertex_array.rs @@ -1,10 +1,10 @@ use std::mem::size_of; -use crate::opengl::buffer::{ArrayKind as ArrayBufferKind, Buffer}; -use crate::opengl::currently_bound::CurrentlyBound; -use crate::vertex::{Attribute, AttributeComponentType, Vertex}; +use crate::opengl::buffer::Buffer; +use crate::vertex::Vertex; -const VERTEX_STRIDE: usize = size_of::<Vertex>(); +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] +const VERTEX_STRIDE: i32 = size_of::<Vertex>() as i32; #[derive(Debug)] pub struct VertexArray @@ -19,19 +19,14 @@ impl VertexArray let mut array = 0; unsafe { - gl::GenVertexArrays(1, &mut array); + gl::CreateVertexArrays(1, &mut array); } Self { array } } /// Draws the currently bound vertex array. - pub fn draw_arrays( - _currently_bound: &CurrentlyBound<Self>, - primitive_kind: PrimitiveKind, - start_index: u32, - cnt: u32, - ) + pub fn draw_arrays(primitive_kind: PrimitiveKind, start_index: u32, cnt: u32) { unsafe { #[allow(clippy::cast_possible_wrap)] @@ -44,12 +39,7 @@ impl VertexArray } /// Draws the currently bound vertex array. - pub fn draw_elements( - _currently_bound: &CurrentlyBound<Self>, - primitive_kind: PrimitiveKind, - offset: u32, - cnt: u32, - ) + pub fn draw_elements(primitive_kind: PrimitiveKind, offset: u32, cnt: u32) { unsafe { #[allow(clippy::cast_possible_wrap)] @@ -62,64 +52,78 @@ impl VertexArray } } - pub fn configure_attrs( - _currently_bound: &CurrentlyBound<Self>, - _vert_buf_curr_bound: &CurrentlyBound<Buffer<Vertex, ArrayBufferKind>>, - ) + pub fn bind_element_buffer(&mut self, element_buffer: &Buffer<u32>) { - let mut offset = 0; - - for attr in Vertex::attrs() { - Self::vertex_attrib_ptr(attr, offset); - - Self::enable_vertex_attrib_array(attr.index); - - offset += attr.component_size * attr.component_cnt as usize; + unsafe { + gl::VertexArrayElementBuffer(self.array, element_buffer.object()); } } - #[allow(clippy::inline_always)] - #[inline(always)] - pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) + pub fn bind_vertex_buffer( + &mut self, + binding_index: u32, + vertex_buffer: &Buffer<Vertex>, + offset: isize, + ) { - unsafe { gl::BindVertexArray(self.array) } - - // SAFETY: A vertex array object is currently bound - let currently_bound = unsafe { CurrentlyBound::new() }; + unsafe { + gl::VertexArrayVertexBuffer( + self.array, + binding_index, + vertex_buffer.object(), + offset, + VERTEX_STRIDE, + ); + } + } - cb(currently_bound); + pub fn enable_attrib(&mut self, attrib_index: u32) + { + unsafe { + gl::EnableVertexArrayAttrib(self.array, attrib_index as gl::types::GLuint); + } } - fn vertex_attrib_ptr(vertex_attr: &Attribute, offset: usize) + pub fn set_attrib_format( + &mut self, + attrib_index: u32, + data_type: DataType, + normalized: bool, + offset: u32, + ) { unsafe { - #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] - gl::VertexAttribPointer( - vertex_attr.index as gl::types::GLuint, - vertex_attr.component_cnt as i32, - Self::vertex_attr_type_to_gl(&vertex_attr.component_type), - gl::FALSE, - VERTEX_STRIDE as gl::types::GLsizei, - offset as *const _, + #[allow(clippy::cast_possible_wrap)] + gl::VertexArrayAttribFormat( + self.array, + attrib_index, + data_type.size() as gl::types::GLint, + data_type as u32, + if normalized { gl::TRUE } else { gl::FALSE }, + offset, ); } } - fn enable_vertex_attrib_array(index: usize) + /// Associate a vertex attribute and a vertex buffer binding. + pub fn set_attrib_vertex_buf_binding( + &mut self, + attrib_index: u32, + vertex_buf_binding_index: u32, + ) { unsafe { - #[allow(clippy::cast_possible_truncation)] - gl::EnableVertexAttribArray(index as gl::types::GLuint); + gl::VertexArrayAttribBinding( + self.array, + attrib_index, + vertex_buf_binding_index, + ); } } - fn vertex_attr_type_to_gl( - vertex_attr_type: &AttributeComponentType, - ) -> gl::types::GLenum + pub fn bind(&self) { - match vertex_attr_type { - AttributeComponentType::Float => gl::FLOAT, - } + unsafe { gl::BindVertexArray(self.array) } } } @@ -148,3 +152,21 @@ impl PrimitiveKind } } } + +#[derive(Debug, Clone, Copy)] +#[repr(u32)] +pub enum DataType +{ + Float = gl::FLOAT, +} + +impl DataType +{ + pub fn size(self) -> u32 + { + #[allow(clippy::cast_possible_truncation)] + match self { + Self::Float => size_of::<gl::types::GLfloat>() as u32, + } + } +} |