From c12966c5b69841f49713f97f3d9fa7ba99fc42e7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 Oct 2023 20:25:42 +0200 Subject: feat(engine): add using element buffers --- engine/src/opengl/buffer.rs | 37 ++++++++++++++++++++++++++++++++----- engine/src/opengl/vertex_array.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 10 deletions(-) (limited to 'engine/src/opengl') diff --git a/engine/src/opengl/buffer.rs b/engine/src/opengl/buffer.rs index 34514fd..3a4ecf0 100644 --- a/engine/src/opengl/buffer.rs +++ b/engine/src/opengl/buffer.rs @@ -4,13 +4,14 @@ use std::mem::size_of_val; use crate::opengl::currently_bound::CurrentlyBound; #[derive(Debug)] -pub struct Buffer +pub struct Buffer { buffer: gl::types::GLuint, _item_pd: PhantomData, + _mode_pd: PhantomData, } -impl Buffer +impl Buffer { pub fn new() -> Self { @@ -23,6 +24,7 @@ impl Buffer Self { buffer, _item_pd: PhantomData, + _mode_pd: PhantomData, } } @@ -31,7 +33,7 @@ impl Buffer pub fn bind(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>)) { unsafe { - gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer); + gl::BindBuffer(ModeT::GL_ENUM, self.buffer); } // SAFETY: The buffer object is bound above @@ -46,7 +48,7 @@ impl Buffer unsafe { #[allow(clippy::cast_possible_wrap)] gl::BufferData( - gl::ARRAY_BUFFER, + ModeT::GL_ENUM, size_of_val(items) as gl::types::GLsizeiptr, items.as_ptr().cast(), usage.into_gl(), @@ -55,7 +57,7 @@ impl Buffer } } -impl Drop for Buffer +impl Drop for Buffer { fn drop(&mut self) { @@ -92,3 +94,28 @@ impl Usage } } } + +/// Buffer mode. +pub trait Mode +{ + #[doc(hidden)] + const GL_ENUM: gl::types::GLenum; +} + +/// Array buffer kind. +#[derive(Debug)] +pub struct ArrayKind; + +impl Mode for ArrayKind +{ + const GL_ENUM: gl::types::GLenum = gl::ARRAY_BUFFER; +} + +/// Element array buffer kind. +#[derive(Debug)] +pub struct ElementArrayKind; + +impl Mode for ElementArrayKind +{ + const GL_ENUM: gl::types::GLenum = gl::ELEMENT_ARRAY_BUFFER; +} diff --git a/engine/src/opengl/vertex_array.rs b/engine/src/opengl/vertex_array.rs index ba2ded2..6b6065c 100644 --- a/engine/src/opengl/vertex_array.rs +++ b/engine/src/opengl/vertex_array.rs @@ -1,6 +1,6 @@ use std::mem::size_of; -use crate::opengl::buffer::Buffer; +use crate::opengl::buffer::{ArrayKind as ArrayBufferKind, Buffer}; use crate::opengl::currently_bound::CurrentlyBound; use crate::vertex::{Attribute, AttributeComponentType, Vertex}; @@ -26,11 +26,11 @@ impl VertexArray } /// Draws the currently bound vertex array. - pub fn draw( + pub fn draw_arrays( _currently_bound: &CurrentlyBound, primitive_kind: PrimitiveKind, start_index: u32, - index_cnt: u32, + cnt: u32, ) { unsafe { @@ -38,14 +38,33 @@ impl VertexArray gl::DrawArrays( primitive_kind.into_gl(), start_index as gl::types::GLint, - index_cnt as gl::types::GLsizei, + cnt as gl::types::GLsizei, + ); + } + } + + /// Draws the currently bound vertex array. + pub fn draw_elements( + _currently_bound: &CurrentlyBound, + primitive_kind: PrimitiveKind, + offset: u32, + cnt: u32, + ) + { + unsafe { + #[allow(clippy::cast_possible_wrap)] + gl::DrawElements( + primitive_kind.into_gl(), + cnt as gl::types::GLsizei, + gl::UNSIGNED_INT, + (offset as gl::types::GLint) as *const _, ); } } pub fn configure_attrs( _currently_bound: &CurrentlyBound, - _vert_buf_curr_bound: &CurrentlyBound>, + _vert_buf_curr_bound: &CurrentlyBound>, ) { let mut offset = 0; -- cgit v1.2.3-18-g5258