summaryrefslogtreecommitdiff
path: root/engine/src/renderer/vertex_buffers.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-07 20:52:09 +0200
committerHampusM <hampus@hampusmat.com>2023-10-07 20:52:09 +0200
commit715bbaf459b88575e11d90ec16bad3841bafd259 (patch)
treea253d4c40237c7f3c5decff833cf85ea0eafbe42 /engine/src/renderer/vertex_buffers.rs
parent146635292369cc8a9660139d97cd9662025bd591 (diff)
feat(engine): add ability to render triangles
Diffstat (limited to 'engine/src/renderer/vertex_buffers.rs')
-rw-r--r--engine/src/renderer/vertex_buffers.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/engine/src/renderer/vertex_buffers.rs b/engine/src/renderer/vertex_buffers.rs
new file mode 100644
index 0000000..a6d1a2c
--- /dev/null
+++ b/engine/src/renderer/vertex_buffers.rs
@@ -0,0 +1,83 @@
+use std::mem::size_of_val;
+
+pub struct VertexBuffers<const CNT: usize>
+{
+ buffers: [gl::types::GLuint; CNT],
+}
+
+impl<const CNT: usize> VertexBuffers<CNT>
+{
+ pub fn new() -> Self
+ {
+ let mut buffers = [gl::types::GLuint::default(); CNT];
+
+ unsafe {
+ #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
+ gl::GenBuffers(CNT as gl::types::GLsizei, buffers.as_mut_ptr());
+ };
+
+ Self { buffers }
+ }
+
+ pub fn store(
+ &self,
+ buffer_index: usize,
+ vertices: &[f32],
+ usage: BufferUsage,
+ ) -> Option<()>
+ {
+ let buffer = *self.buffers.get(buffer_index)?;
+
+ unsafe {
+ gl::BindBuffer(gl::ARRAY_BUFFER, buffer);
+ }
+
+ 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(),
+ );
+ }
+
+ Some(())
+ }
+}
+
+impl<const CNT: usize> Drop for VertexBuffers<CNT>
+{
+ fn drop(&mut self)
+ {
+ #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
+ unsafe {
+ gl::DeleteBuffers(CNT as gl::types::GLsizei, self.buffers.as_ptr());
+ }
+ }
+}
+
+#[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,
+ }
+ }
+}