summaryrefslogtreecommitdiff
path: root/engine/src/opengl
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/opengl')
-rw-r--r--engine/src/opengl/buffer.rs40
-rw-r--r--engine/src/opengl/shader.rs101
-rw-r--r--engine/src/opengl/texture.rs97
-rw-r--r--engine/src/opengl/vertex_array.rs20
4 files changed, 109 insertions, 149 deletions
diff --git a/engine/src/opengl/buffer.rs b/engine/src/opengl/buffer.rs
index 68a75fb..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,19 +36,40 @@ impl<Item> Buffer<Item>
}
}
- pub fn object(&self) -> gl::types::GLuint
+ pub fn store_mapped<Value>(
+ &mut self,
+ values: &[Value],
+ usage: Usage,
+ mut map_func: impl FnMut(&Value) -> Item,
+ )
{
- self.buf
+ 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(),
+ );
+ }
+ }
}
-}
-impl<Item> Drop for Buffer<Item>
-{
- fn drop(&mut self)
+ pub fn object(&self) -> gl::types::GLuint
{
- unsafe {
- gl::DeleteBuffers(1, &self.buf);
- }
+ self.buf
}
}
diff --git a/engine/src/opengl/shader.rs b/engine/src/opengl/shader.rs
index 36dc1a4..a626fc7 100644
--- a/engine/src/opengl/shader.rs
+++ b/engine/src/opengl/shader.rs
@@ -159,82 +159,105 @@ impl Program
}
}
- pub fn set_uniform_matrix_4fv(&mut self, name: &CStr, matrix: &Matrix<f32, 4, 4>)
+ pub fn set_uniform(&mut self, name: &CStr, var: &impl UniformVariable)
{
- let uniform_location =
- unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) };
+ let location = UniformLocation(unsafe {
+ gl::GetUniformLocation(self.program, name.as_ptr().cast())
+ });
+
+ var.set(self, location);
+ }
+
+ fn get_info_log(&self) -> String
+ {
+ let mut buf = vec![gl::types::GLchar::default(); 512];
unsafe {
- gl::ProgramUniformMatrix4fv(
+ #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
+ gl::GetProgramInfoLog(
self.program,
- uniform_location,
- 1,
- gl::FALSE,
- matrix.as_ptr(),
+ buf.len() as gl::types::GLsizei,
+ null_mut(),
+ buf.as_mut_ptr(),
);
}
+
+ let info_log = unsafe { CStr::from_ptr(buf.as_ptr()) };
+
+ unsafe { String::from_utf8_unchecked(info_log.to_bytes().to_vec()) }
}
+}
- pub fn set_uniform_vec_3fv(&mut self, name: &CStr, vec: &Vec3<f32>)
+impl Drop for Program
+{
+ fn drop(&mut self)
{
- let uniform_location =
- unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) };
-
unsafe {
- gl::ProgramUniform3fv(self.program, uniform_location, 1, vec.as_ptr());
+ gl::DeleteProgram(self.program);
}
}
+}
- pub fn set_uniform_1fv(&mut self, name: &CStr, num: f32)
- {
- let uniform_location =
- unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) };
+pub trait UniformVariable
+{
+ fn set(&self, program: &mut Program, uniform_location: UniformLocation);
+}
+impl UniformVariable for f32
+{
+ fn set(&self, program: &mut Program, uniform_location: UniformLocation)
+ {
unsafe {
- gl::ProgramUniform1fv(self.program, uniform_location, 1, &num);
+ gl::ProgramUniform1f(program.program, uniform_location.0, *self);
}
}
+}
- pub fn set_uniform_1i(&mut self, name: &CStr, num: i32)
+impl UniformVariable for i32
+{
+ fn set(&self, program: &mut Program, uniform_location: UniformLocation)
{
- let uniform_location =
- unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) };
-
unsafe {
- gl::ProgramUniform1i(self.program, uniform_location, num);
+ gl::ProgramUniform1i(program.program, uniform_location.0, *self);
}
}
+}
- fn get_info_log(&self) -> String
+impl UniformVariable for Vec3<f32>
+{
+ fn set(&self, program: &mut Program, uniform_location: UniformLocation)
{
- let mut buf = vec![gl::types::GLchar::default(); 512];
-
unsafe {
- #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
- gl::GetProgramInfoLog(
- self.program,
- buf.len() as gl::types::GLsizei,
- null_mut(),
- buf.as_mut_ptr(),
+ gl::ProgramUniform3f(
+ program.program,
+ uniform_location.0,
+ self.x,
+ self.y,
+ self.z,
);
}
-
- let info_log = unsafe { CStr::from_ptr(buf.as_ptr()) };
-
- unsafe { String::from_utf8_unchecked(info_log.to_bytes().to_vec()) }
}
}
-impl Drop for Program
+impl UniformVariable for Matrix<f32, 4, 4>
{
- fn drop(&mut self)
+ fn set(&self, program: &mut Program, uniform_location: UniformLocation)
{
unsafe {
- gl::DeleteProgram(self.program);
+ gl::ProgramUniformMatrix4fv(
+ program.program,
+ uniform_location.0,
+ 1,
+ gl::FALSE,
+ self.as_ptr(),
+ );
}
}
}
+#[derive(Debug)]
+pub struct UniformLocation(gl::types::GLint);
+
/// Shader error.
#[derive(Debug, thiserror::Error)]
pub enum Error
diff --git a/engine/src/opengl/texture.rs b/engine/src/opengl/texture.rs
index 52c8554..80a5f37 100644
--- a/engine/src/opengl/texture.rs
+++ b/engine/src/opengl/texture.rs
@@ -1,5 +1,4 @@
use crate::data_types::dimens::Dimens;
-use crate::texture::Properties;
#[derive(Debug)]
pub struct Texture
@@ -20,10 +19,10 @@ impl Texture
Self { texture }
}
- pub fn bind(&self)
+ pub fn bind_to_texture_unit(&self, texture_unit: u32)
{
unsafe {
- gl::BindTexture(gl::TEXTURE_2D, self.texture);
+ gl::BindTextureUnit(texture_unit, self.texture);
}
}
@@ -41,16 +40,9 @@ impl Texture
}
}
- pub fn apply_properties(&mut self, properties: &Properties)
- {
- self.set_wrap(properties.wrap);
- self.set_magnifying_filter(properties.magnifying_filter);
- self.set_minifying_filter(properties.minifying_filter);
- }
-
pub fn set_wrap(&mut self, wrapping: Wrapping)
{
- let wrapping_gl = wrapping.to_gl();
+ let wrapping_gl = wrapping as gl::types::GLenum;
#[allow(clippy::cast_possible_wrap)]
unsafe {
@@ -61,7 +53,7 @@ impl Texture
pub fn set_magnifying_filter(&mut self, filtering: Filtering)
{
- let filtering_gl = filtering.to_gl();
+ let filtering_gl = filtering as gl::types::GLenum;
#[allow(clippy::cast_possible_wrap)]
unsafe {
@@ -75,7 +67,7 @@ impl Texture
pub fn set_minifying_filter(&mut self, filtering: Filtering)
{
- let filtering_gl = filtering.to_gl();
+ let filtering_gl = filtering as gl::types::GLenum;
#[allow(clippy::cast_possible_wrap)]
unsafe {
@@ -132,43 +124,21 @@ impl Drop for Texture
/// Texture wrapping.
#[derive(Debug, Clone, Copy)]
+#[repr(u32)]
pub enum Wrapping
{
- Repeat,
- MirroredRepeat,
- ClampToEdge,
- ClampToBorder,
-}
-
-impl Wrapping
-{
- fn to_gl(self) -> gl::types::GLenum
- {
- match self {
- Self::Repeat => gl::REPEAT,
- Self::MirroredRepeat => gl::MIRRORED_REPEAT,
- Self::ClampToEdge => gl::CLAMP_TO_EDGE,
- Self::ClampToBorder => gl::CLAMP_TO_BORDER,
- }
- }
+ Repeat = gl::REPEAT,
+ MirroredRepeat = gl::MIRRORED_REPEAT,
+ ClampToEdge = gl::CLAMP_TO_EDGE,
+ ClampToBorder = gl::CLAMP_TO_BORDER,
}
#[derive(Debug, Clone, Copy)]
+#[repr(u32)]
pub enum Filtering
{
- Nearest,
- Linear,
-}
-
-impl Filtering
-{
- fn to_gl(self) -> gl::types::GLenum
- {
- match self {
- Self::Linear => gl::LINEAR,
- Self::Nearest => gl::NEAREST,
- }
- }
+ Nearest = gl::NEAREST,
+ Linear = gl::LINEAR,
}
/// Texture pixel data format.
@@ -197,44 +167,3 @@ impl PixelDataFormat
}
}
}
-
-pub fn set_active_texture_unit(texture_unit: TextureUnit)
-{
- unsafe {
- gl::ActiveTexture(texture_unit.into_gl());
- }
-}
-
-macro_rules! texture_unit_enum {
- (cnt=$cnt: literal) => {
- seq_macro::seq!(N in 0..$cnt {
- #[derive(Debug, Clone, Copy)]
- pub enum TextureUnit {
- #(
- No~N,
- )*
- }
-
- impl TextureUnit {
- fn into_gl(self) -> gl::types::GLenum {
- match self {
- #(
- Self::No~N => gl::TEXTURE~N,
- )*
- }
- }
-
- pub fn from_num(num: usize) -> Option<Self> {
- match num {
- #(
- N => Some(Self::No~N),
- )*
- _ => None
- }
- }
- }
- });
- };
-}
-
-texture_unit_enum!(cnt = 31);
diff --git a/engine/src/opengl/vertex_array.rs b/engine/src/opengl/vertex_array.rs
index e1e1a15..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,
);
}
}
@@ -127,16 +123,6 @@ impl VertexArray
}
}
-impl Drop for VertexArray
-{
- fn drop(&mut self)
- {
- unsafe {
- gl::DeleteVertexArrays(1, &self.array);
- }
- }
-}
-
#[derive(Debug)]
pub enum PrimitiveKind
{