summaryrefslogtreecommitdiff
path: root/engine/src/opengl
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-27 20:02:08 +0100
committerHampusM <hampus@hampusmat.com>2023-11-27 20:02:08 +0100
commitc230f5aaea3df46ae9a4d7c1c9761e55ef827b82 (patch)
tree9e429a33df6e12f4b2f9adf87d08dad2a0127756 /engine/src/opengl
parent935f35455ac2e3547cdd21cd4596538958a7217e (diff)
feat(engine): add lighting maps
Diffstat (limited to 'engine/src/opengl')
-rw-r--r--engine/src/opengl/shader.rs14
-rw-r--r--engine/src/opengl/texture.rs42
2 files changed, 54 insertions, 2 deletions
diff --git a/engine/src/opengl/shader.rs b/engine/src/opengl/shader.rs
index 9bed09b..224a9bc 100644
--- a/engine/src/opengl/shader.rs
+++ b/engine/src/opengl/shader.rs
@@ -153,7 +153,7 @@ impl Program
Ok(())
}
- pub fn activate(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>))
+ pub fn activate<Ret>(&self, cb: impl FnOnce(CurrentlyBound<'_, Self>) -> Ret) -> Ret
{
unsafe {
gl::UseProgram(self.program);
@@ -162,7 +162,7 @@ impl Program
// SAFETY: The shader program object is bound above
let currently_bound = unsafe { CurrentlyBound::new() };
- cb(currently_bound);
+ cb(currently_bound)
}
pub fn set_uniform_matrix_4fv(
@@ -205,6 +205,16 @@ impl Program
}
}
+ pub fn set_uniform_1i(&self, _: &CurrentlyBound<Self>, name: &CStr, num: i32)
+ {
+ let uniform_location =
+ unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) };
+
+ unsafe {
+ gl::Uniform1i(uniform_location, num);
+ }
+ }
+
fn get_info_log(&self) -> String
{
let mut buf = vec![gl::types::GLchar::default(); 512];
diff --git a/engine/src/opengl/texture.rs b/engine/src/opengl/texture.rs
index 4186479..56eb118 100644
--- a/engine/src/opengl/texture.rs
+++ b/engine/src/opengl/texture.rs
@@ -1,6 +1,7 @@
use std::ptr::null;
use crate::opengl::currently_bound::CurrentlyBound;
+use crate::texture::Id;
use crate::vector::Vec2;
#[derive(Debug)]
@@ -234,3 +235,44 @@ 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_texture_id(texture_id: Id) -> Option<Self> {
+ match texture_id.into_inner() {
+ #(
+ N => Some(Self::No~N),
+ )*
+ _ => None
+ }
+ }
+ }
+ });
+ };
+}
+
+texture_unit_enum!(cnt = 31);