diff options
author | HampusM <hampus@hampusmat.com> | 2025-05-10 13:10:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-05-10 13:42:34 +0200 |
commit | 6b2feeab0b845d05cdf76328d44b7b70a5e50c18 (patch) | |
tree | c214ad5e89a0a289d32094fb934caa6325b6cb4e /engine/src/opengl | |
parent | c1db67dbeaeeb447b8b4361ae6bb750785ed04bd (diff) |
refactor(engine): make GL renderer have it's own Vertex struct
Diffstat (limited to 'engine/src/opengl')
-rw-r--r-- | engine/src/opengl/buffer.rs | 32 | ||||
-rw-r--r-- | engine/src/opengl/vertex_array.rs | 10 |
2 files changed, 35 insertions, 7 deletions
diff --git a/engine/src/opengl/buffer.rs b/engine/src/opengl/buffer.rs index 2fabe4d..eded553 100644 --- a/engine/src/opengl/buffer.rs +++ b/engine/src/opengl/buffer.rs @@ -1,5 +1,6 @@ use std::marker::PhantomData; use std::mem::size_of_val; +use std::ptr::null; #[derive(Debug)] pub struct Buffer<Item> @@ -35,6 +36,37 @@ impl<Item> Buffer<Item> } } + pub fn store_mapped<Value>( + &mut self, + values: &[Value], + usage: Usage, + mut map_func: impl FnMut(&Value) -> Item, + ) + { + unsafe { + #[allow(clippy::cast_possible_wrap)] + gl::NamedBufferData( + self.buf, + (size_of::<Item>() * values.len()) as gl::types::GLsizeiptr, + null(), + usage.into_gl(), + ); + } + + for (index, value) in values.iter().enumerate() { + let item = map_func(value); + + unsafe { + gl::NamedBufferSubData( + self.buf, + (index * size_of::<Item>()) as gl::types::GLintptr, + size_of::<Item>() as gl::types::GLsizeiptr, + (&raw const item).cast(), + ); + } + } + } + pub fn object(&self) -> gl::types::GLuint { self.buf diff --git a/engine/src/opengl/vertex_array.rs b/engine/src/opengl/vertex_array.rs index f0d04ae..1f8a870 100644 --- a/engine/src/opengl/vertex_array.rs +++ b/engine/src/opengl/vertex_array.rs @@ -1,10 +1,6 @@ use std::mem::size_of; use crate::opengl::buffer::Buffer; -use crate::vertex::Vertex; - -#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] -const VERTEX_STRIDE: i32 = size_of::<Vertex>() as i32; #[derive(Debug)] pub struct VertexArray @@ -59,10 +55,10 @@ impl VertexArray } } - pub fn bind_vertex_buffer( + pub fn bind_vertex_buffer<VertexT>( &mut self, binding_index: u32, - vertex_buffer: &Buffer<Vertex>, + vertex_buffer: &Buffer<VertexT>, offset: isize, ) { @@ -72,7 +68,7 @@ impl VertexArray binding_index, vertex_buffer.object(), offset, - VERTEX_STRIDE, + size_of::<VertexT>() as i32, ); } } |