diff options
| author | HampusM <hampus@hampusmat.com> | 2023-10-13 23:32:00 +0200 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2023-10-13 23:32:00 +0200 | 
| commit | 12f48046b2606fc77a1312a6d5e5fc7ff9feff88 (patch) | |
| tree | 644f4abd6785a5f0c066c7fbadcfc5e820a41ebf /engine/src/renderer | |
| parent | cfa73b1ea42fa491ff9e00bb5efb5e5a5d860578 (diff) | |
refactor(engine): move uses of OpenGL to OpenGL module
Diffstat (limited to 'engine/src/renderer')
| -rw-r--r-- | engine/src/renderer/mod.rs | 35 | ||||
| -rw-r--r-- | engine/src/renderer/vertex_array.rs | 131 | ||||
| -rw-r--r-- | engine/src/renderer/vertex_buffer.rs | 93 | 
3 files changed, 12 insertions, 247 deletions
| diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index a23d5a0..4ed3c0a 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -3,14 +3,13 @@ use std::process::abort;  use glfw::WindowSize; -use crate::renderer::vertex_array::{PrimitiveKind, VertexArray}; -use crate::renderer::vertex_buffer::{BufferUsage, VertexBuffer}; +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; -mod vertex_array; -mod vertex_buffer; -  pub fn initialize(window: &glfw::Window) -> Result<(), Error>  {      gl::load_with(|symbol| { @@ -38,9 +37,7 @@ pub fn initialize(window: &glfw::Window) -> Result<(), Error>  pub fn render<'renderable>(renderables: impl IntoIterator<Item = &'renderable Renderable>)  { -    unsafe { -        gl::Clear(gl::COLOR_BUFFER_BIT); -    } +    clear_buffers(BufferClearMask::COLOR);      for renderable in renderables {          renderable.shader_program.activate(); @@ -51,10 +48,15 @@ pub fn render<'renderable>(renderables: impl IntoIterator<Item = &'renderable Re      }  } +pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize) +{ +    crate::opengl::set_viewport(position, size); +} +  #[derive(Debug)]  pub struct Renderable  { -    shader_program: crate::shader::Program, +    shader_program: ShaderProgram,      vertex_arr: VertexArray,      /// Vertex buffer has to live as long as the vertex array @@ -63,7 +65,7 @@ pub struct Renderable  impl Renderable  { -    pub fn new(shader_program: crate::shader::Program, vertices: &[Vertex]) -> Self +    pub fn new(shader_program: ShaderProgram, vertices: &[Vertex]) -> Self      {          let vertex_arr = VertexArray::new();          let vertex_buffer = VertexBuffer::new(); @@ -84,19 +86,6 @@ impl Renderable      }  } -pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize) -{ -    unsafe { -        #[allow(clippy::cast_possible_wrap)] -        gl::Viewport( -            position.x as i32, -            position.y as i32, -            size.width as i32, -            size.height as i32, -        ); -    } -} -  /// Renderer error.  #[derive(Debug, thiserror::Error)]  pub enum Error diff --git a/engine/src/renderer/vertex_array.rs b/engine/src/renderer/vertex_array.rs deleted file mode 100644 index 2bd6a5b..0000000 --- a/engine/src/renderer/vertex_array.rs +++ /dev/null @@ -1,131 +0,0 @@ -use std::mem::size_of; - -use crate::currently_bound::CurrentlyBound; -use crate::renderer::vertex_buffer::VertexBuffer; -use crate::vertex::{Attribute, AttributeComponentType, Vertex}; - -const VERTEX_STRIDE: usize = size_of::<Vertex>(); - -#[derive(Debug)] -pub struct VertexArray -{ -    array: gl::types::GLuint, -} - -impl VertexArray -{ -    pub fn new() -> Self -    { -        let mut array = 0; - -        unsafe { -            gl::GenVertexArrays(1, &mut array); -        } - -        Self { array } -    } - -    /// Draws the currently bound vertex array. -    pub fn draw( -        _currently_bound: &CurrentlyBound<Self>, -        primitive_kind: PrimitiveKind, -        start_index: u32, -        index_cnt: u32, -    ) -    { -        unsafe { -            #[allow(clippy::cast_possible_wrap)] -            gl::DrawArrays( -                primitive_kind.into_gl(), -                start_index as gl::types::GLint, -                index_cnt as gl::types::GLsizei, -            ); -        } -    } - -    pub fn configure_attrs( -        _currently_bound: &CurrentlyBound<Self>, -        _vert_buf_curr_bound: &CurrentlyBound<VertexBuffer>, -    ) -    { -        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; -        } -    } - -    #[allow(clippy::inline_always)] -    #[inline(always)] -    pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) -    { -        unsafe { gl::BindVertexArray(self.array) } - -        // SAFETY: A vertex array object is currently bound -        let currently_bound = unsafe { CurrentlyBound::new() }; - -        cb(currently_bound); -    } - -    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 -{ -    fn drop(&mut self) -    { -        unsafe { -            gl::DeleteVertexArrays(1, &self.array); -        } -    } -} - -#[derive(Debug)] -pub enum PrimitiveKind -{ -    Triangles, -} - -impl PrimitiveKind -{ -    fn into_gl(self) -> gl::types::GLenum -    { -        match self { -            Self::Triangles => gl::TRIANGLES, -        } -    } -} diff --git a/engine/src/renderer/vertex_buffer.rs b/engine/src/renderer/vertex_buffer.rs deleted file mode 100644 index 57debc3..0000000 --- a/engine/src/renderer/vertex_buffer.rs +++ /dev/null @@ -1,93 +0,0 @@ -use std::mem::size_of_val; - -use crate::currently_bound::CurrentlyBound; -use crate::vertex::Vertex; - -#[derive(Debug)] -pub struct VertexBuffer -{ -    buffer: gl::types::GLuint, -} - -impl VertexBuffer -{ -    pub fn new() -> Self -    { -        let mut buffer = gl::types::GLuint::default(); - -        unsafe { -            gl::GenBuffers(1, &mut buffer); -        }; - -        Self { buffer } -    } - -    #[allow(clippy::inline_always)] -    #[inline(always)] -    pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) -    { -        unsafe { -            gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer); -        } - -        // SAFETY: A vertex array object is currently bound -        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, -    ) -    { -        unsafe { -            #[allow(clippy::cast_possible_wrap)] -            gl::BufferData( -                gl::ARRAY_BUFFER, -                size_of_val(vertices) as gl::types::GLsizeiptr, -                vertices.as_ptr().cast(), -                usage.into_gl(), -            ); -        } -    } -} - -impl Drop for VertexBuffer -{ -    fn drop(&mut self) -    { -        #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] -        unsafe { -            gl::DeleteBuffers(1, &self.buffer); -        } -    } -} - -#[derive(Debug)] -#[allow(dead_code)] -pub enum BufferUsage -{ -    /// The buffer data is set only once and used by the GPU at most a few times. -    Stream, - -    /// The buffer data is set only once and used many times. -    Static, - -    /// The buffer data is changed a lot and used many times. -    Dynamic, -} - -impl BufferUsage -{ -    fn into_gl(self) -> gl::types::GLenum -    { -        match self { -            Self::Stream => gl::STREAM_DRAW, -            Self::Static => gl::STATIC_DRAW, -            Self::Dynamic => gl::DYNAMIC_DRAW, -        } -    } -} | 
