summaryrefslogtreecommitdiff
path: root/engine/src/renderer/vertex_array.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/renderer/vertex_array.rs')
-rw-r--r--engine/src/renderer/vertex_array.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/engine/src/renderer/vertex_array.rs b/engine/src/renderer/vertex_array.rs
index 5862c7b..b0aac81 100644
--- a/engine/src/renderer/vertex_array.rs
+++ b/engine/src/renderer/vertex_array.rs
@@ -1,3 +1,9 @@
+use std::mem::size_of;
+
+use crate::vertex::{Attribute, AttributeComponentType, Vertex};
+
+const VERTEX_STRIDE: usize = size_of::<Vertex>();
+
#[derive(Debug)]
pub struct VertexArray
{
@@ -31,10 +37,55 @@ impl VertexArray
}
}
+ pub fn configure_attrs()
+ {
+ 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;
+ }
+ }
+
pub fn bind(&self)
{
unsafe { gl::BindVertexArray(self.array) }
}
+
+ fn vertex_attrib_ptr(vertex_attr: &Attribute, offset: usize)
+ {
+ 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 _,
+ );
+ }
+ }
+
+ fn enable_vertex_attrib_array(index: usize)
+ {
+ unsafe {
+ #[allow(clippy::cast_possible_truncation)]
+ gl::EnableVertexAttribArray(index as gl::types::GLuint);
+ }
+ }
+
+ fn vertex_attr_type_to_gl(
+ vertex_attr_type: &AttributeComponentType,
+ ) -> gl::types::GLenum
+ {
+ match vertex_attr_type {
+ AttributeComponentType::Float => gl::FLOAT,
+ }
+ }
}
impl Drop for VertexArray