From 341826e9a2b89713fc47ffbc914d18e23c7d9287 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 12 Oct 2023 21:29:36 +0200 Subject: feat(engine): add vertex coloring --- engine/src/renderer/vertex_array.rs | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'engine/src/renderer/vertex_array.rs') 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::(); + #[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 -- cgit v1.2.3-18-g5258