diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-12 21:29:36 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-12 21:29:36 +0200 |
commit | 341826e9a2b89713fc47ffbc914d18e23c7d9287 (patch) | |
tree | b92b85bd85690d3e6475eac7e8c254700865bded /engine/src/renderer/vertex_array.rs | |
parent | a4d66f8272c08c722a0a3fa6843ccf61e05d2928 (diff) |
feat(engine): add vertex coloring
Diffstat (limited to 'engine/src/renderer/vertex_array.rs')
-rw-r--r-- | engine/src/renderer/vertex_array.rs | 51 |
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 |