diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-23 19:21:42 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-23 19:21:42 +0200 |
commit | bd427836bfa6f7228951c18e43058d3e35577702 (patch) | |
tree | 4d7276d7c7ded85362b3f199dbde2e9939481bfc /engine | |
parent | 54c0fd70f82eb1a6814872c78bc22380f438c9d1 (diff) |
refactor(engine): rename vertex buffer to buffer & make generic
Diffstat (limited to 'engine')
-rw-r--r-- | engine/src/opengl/buffer.rs (renamed from engine/src/opengl/vertex_buffer.rs) | 33 | ||||
-rw-r--r-- | engine/src/opengl/mod.rs | 2 | ||||
-rw-r--r-- | engine/src/opengl/vertex_array.rs | 4 | ||||
-rw-r--r-- | engine/src/renderer/mod.rs | 8 |
4 files changed, 24 insertions, 23 deletions
diff --git a/engine/src/opengl/vertex_buffer.rs b/engine/src/opengl/buffer.rs index 0050a9f..34514fd 100644 --- a/engine/src/opengl/vertex_buffer.rs +++ b/engine/src/opengl/buffer.rs @@ -1,15 +1,16 @@ +use std::marker::PhantomData; use std::mem::size_of_val; use crate::opengl::currently_bound::CurrentlyBound; -use crate::vertex::Vertex; #[derive(Debug)] -pub struct VertexBuffer +pub struct Buffer<Item> { buffer: gl::types::GLuint, + _item_pd: PhantomData<Item>, } -impl VertexBuffer +impl<Item> Buffer<Item> { pub fn new() -> Self { @@ -19,7 +20,10 @@ impl VertexBuffer gl::GenBuffers(1, &mut buffer); }; - Self { buffer } + Self { + buffer, + _item_pd: PhantomData, + } } #[allow(clippy::inline_always)] @@ -30,32 +34,28 @@ impl VertexBuffer gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer); } - // SAFETY: A vertex array object is currently bound + // SAFETY: The buffer object is bound above let currently_bound = unsafe { CurrentlyBound::new() }; cb(currently_bound); } - /// Stores vertices in the currently bound vertex bound. - pub fn store( - _currently_bound: &CurrentlyBound<Self>, - vertices: &[Vertex], - usage: BufferUsage, - ) + /// Stores items in the currently bound buffer. + pub fn store(_currently_bound: &CurrentlyBound<Self>, items: &[Item], usage: Usage) { unsafe { #[allow(clippy::cast_possible_wrap)] gl::BufferData( gl::ARRAY_BUFFER, - size_of_val(vertices) as gl::types::GLsizeiptr, - vertices.as_ptr().cast(), + size_of_val(items) as gl::types::GLsizeiptr, + items.as_ptr().cast(), usage.into_gl(), ); } } } -impl Drop for VertexBuffer +impl<Item> Drop for Buffer<Item> { fn drop(&mut self) { @@ -66,9 +66,10 @@ impl Drop for VertexBuffer } } +/// Buffer usage. #[derive(Debug)] #[allow(dead_code)] -pub enum BufferUsage +pub enum Usage { /// The buffer data is set only once and used by the GPU at most a few times. Stream, @@ -80,7 +81,7 @@ pub enum BufferUsage Dynamic, } -impl BufferUsage +impl Usage { fn into_gl(self) -> gl::types::GLenum { diff --git a/engine/src/opengl/mod.rs b/engine/src/opengl/mod.rs index ce375f5..6f95a1f 100644 --- a/engine/src/opengl/mod.rs +++ b/engine/src/opengl/mod.rs @@ -3,10 +3,10 @@ use glfw::WindowSize; use crate::vector::Vec2; +pub mod buffer; pub mod currently_bound; pub mod shader; pub mod vertex_array; -pub mod vertex_buffer; mod util; diff --git a/engine/src/opengl/vertex_array.rs b/engine/src/opengl/vertex_array.rs index f255b8a..ba2ded2 100644 --- a/engine/src/opengl/vertex_array.rs +++ b/engine/src/opengl/vertex_array.rs @@ -1,7 +1,7 @@ use std::mem::size_of; +use crate::opengl::buffer::Buffer; use crate::opengl::currently_bound::CurrentlyBound; -use crate::opengl::vertex_buffer::VertexBuffer; use crate::vertex::{Attribute, AttributeComponentType, Vertex}; const VERTEX_STRIDE: usize = size_of::<Vertex>(); @@ -45,7 +45,7 @@ impl VertexArray pub fn configure_attrs( _currently_bound: &CurrentlyBound<Self>, - _vert_buf_curr_bound: &CurrentlyBound<VertexBuffer>, + _vert_buf_curr_bound: &CurrentlyBound<Buffer<Vertex>>, ) { let mut offset = 0; diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 068b8f0..f31196d 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -5,11 +5,11 @@ use cstr::cstr; use glfw::WindowSize; use crate::object::Object; +use crate::opengl::buffer::{Buffer, Usage}; #[cfg(feature = "debug")] use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType}; use crate::opengl::shader::Program as ShaderProgram; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; -use crate::opengl::vertex_buffer::{BufferUsage, VertexBuffer}; use crate::opengl::{clear_buffers, BufferClearMask}; use crate::vector::Vec2; use crate::vertex::Vertex; @@ -123,7 +123,7 @@ pub struct Renderable vertex_arr: VertexArray, /// Vertex buffer has to live as long as the vertex array - _vertex_buffer: VertexBuffer, + _vertex_buffer: Buffer<Vertex>, } impl Renderable @@ -131,11 +131,11 @@ impl Renderable pub fn new(shader_program: ShaderProgram, vertices: &[Vertex]) -> Self { let vertex_arr = VertexArray::new(); - let vertex_buffer = VertexBuffer::new(); + let vertex_buffer = Buffer::new(); vertex_arr.bind(|vert_arr_curr_bound| { vertex_buffer.bind(|vert_buf_curr_bound| { - VertexBuffer::store(&vert_buf_curr_bound, vertices, BufferUsage::Static); + Buffer::store(&vert_buf_curr_bound, vertices, Usage::Static); VertexArray::configure_attrs(&vert_arr_curr_bound, &vert_buf_curr_bound); }); |