mgecumshooter/esotera
implemented depth buffer shit
Commit 8aaf2182f94d · mgecumshooter · 2026-10-20 13:36 +0300
Comments
No comments yet.
Diff
diff --git a/shaders/post.glsl b/shaders/post.glsl
--- a/shaders/post.glsl
+++ b/shaders/post.glsl
@@ -5,6 +5,7 @@
in vec2 fragTexCoord;
uniform sampler2D texture0;
+uniform sampler2D depth_tex;
out vec4 finalColor;
@@ -20,6 +21,16 @@
float levels = 32.0;
vec2 gamma = vec2(0.5, 1.5);
+const float near_plane = 0.01;
+const float far_plane = 1000.0;
+const float fog_far = 150.0;
+
+float linearize_depth(float d) {
+ float z = d * 2.0 - 1.0;
+ float lin = (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane));
+ return clamp(lin / fog_far, 0.0, 1.0);
+}
+
void main() {
// pixelization
vec2 texsize = textureSize(texture0, 0);
@@ -28,9 +39,10 @@
vec2 p = pixel_uv - vec2(0.5);
float r = length(p);
- vec2 dist = p / (1.0 + 0.25 * r);
+ vec2 dist = p * (1.0 + 0.25 * r);
pixel_uv = dist * 0.5 + vec2(0.5);
+ float depth = texture(depth_tex, pixel_uv).r;
vec4 color = texture(texture0, pixel_uv);
// dither
@@ -57,6 +69,8 @@
// vignette
float diff = length(fragTexCoord - vec2(0.5));
color.rgb *= 1.0 - diff;
+ color.rgb *= pow(1.0 - linearize_depth(depth), 4.0);
+ // finalColor = color;
finalColor = color;
}
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,7 +12,7 @@
// internal modules
use chunk::*;
use player::Player;
-use util::Cube;
+use util::*;
use world::*;
@@ -38,10 +38,16 @@
// shader stuff
let mut shader = rl.load_shader(&rl_thd, Some("shaders/vert.glsl"), Some("shaders/frag.glsl")).unwrap();
let mut post_shader = rl.load_shader(&rl_thd, None, Some("shaders/post.glsl")).unwrap();
- let mut rend_tex = rl.load_render_texture
- (&rl_thd, rl.get_render_width() as u32, rl.get_render_height() as u32).unwrap();
+
+ let depth_loc = post_shader.get_shader_location("depth_tex");
+
+ assert!(depth_loc >= 0, "Uniform depth_tex not found");
+
+ // let mut rend_tex = rl.load_render_texture
+ // (&rl_thd, rl.get_render_width() as u32, rl.get_render_height() as u32).unwrap();
let mut chunk_material = rl.load_material_default(&rl_thd);
+ let mut rend_tex = unsafe { load_depth_tex(rl.get_render_width(), rl.get_render_height()) };
unsafe {
*chunk_material.shader_mut() = shader.make_weak();
}
@@ -53,7 +59,7 @@
// let test2 = [&test_chunk];
- let mut world = World::new(5);
+ let mut world = World::new(4);
let mut plr = Player::new(
Vector3::new(0f32, 10f32, 0f32),
@@ -74,8 +80,9 @@
if rend_tex.width() != rl.get_render_width() || rend_tex.height() != rl.get_render_height() {
- rend_tex = rl.load_render_texture
- (&rl_thd, rl.get_render_width() as u32, rl.get_render_height() as u32).unwrap();
+ rend_tex = unsafe { load_depth_tex(rl.get_render_width(), rl.get_render_height()) };
+ // rend_tex = rl.load_render_texture
+ // (&rl_thd, rl.get_render_width() as u32, rl.get_render_height() as u32).unwrap();
}
rl.draw(&rl_thd, |mut d| {
@@ -98,7 +105,17 @@
}
});
});
+ post_shader.set_shader_value_texture(
+ post_shader.get_shader_location("depth_tex"),
+ unsafe { Texture2D::from_raw(rend_tex.depth).make_weak() }
+ );
d.draw_shader_mode(&mut post_shader, |mut shd| {
+ unsafe {
+ rlb::ffi::rlSetUniformSampler(
+ depth_loc,
+ rend_tex.depth.id
+ );
+ }
shd.draw_texture_rec(&rend_tex, Rectangle{
x: 0f32,
y: 0f32,
diff --git a/src/util/mod.rs b/src/util/mod.rs
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -1,5 +1,6 @@
use super::rlb;
use rlb::prelude::*;
+// use rlb::core::*;
pub struct Cube {
pub width: f32,
@@ -30,3 +31,62 @@
self.min.z < other.max.z && self.max.z > other.min.z
}
}
+
+// had to do this ;(
+pub fn load_depth_tex(width: i32, height: i32) -> RenderTexture2D {
+ unsafe {
+ let fbo_id = rlb::ffi::rlLoadFramebuffer();
+ rlb::ffi::rlEnableFramebuffer(fbo_id);
+
+ let col_tex_id = rlb::ffi::rlLoadTexture(
+ std::ptr::null(),
+ width,
+ height,
+ rlb::ffi::PixelFormat::PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 as i32,
+ 1
+ );
+
+ rlb::ffi::rlFramebufferAttach(
+ fbo_id,
+ col_tex_id,
+ rlb::ffi::rlFramebufferAttachType::RL_ATTACHMENT_COLOR_CHANNEL0 as i32,
+ rlb::ffi::rlFramebufferAttachTextureType::RL_ATTACHMENT_TEXTURE2D as i32,
+ 0
+ );
+
+ let depth_tex_id = rlb::ffi::rlLoadTextureDepth(width, height, false);
+
+ rlb::ffi::rlFramebufferAttach(
+ fbo_id,
+ depth_tex_id,
+ rlb::ffi::rlFramebufferAttachType::RL_ATTACHMENT_DEPTH as i32,
+ rlb::ffi::rlFramebufferAttachTextureType::RL_ATTACHMENT_TEXTURE2D as i32,
+ 0
+ );
+
+ let fbo_complete = rlb::ffi::rlFramebufferComplete(fbo_id);
+ rlb::ffi::rlDisableFramebuffer();
+ assert!(
+ fbo_complete,
+ "Framebuffer incomplete!"
+ );
+
+ RenderTexture2D::from_raw(rlb::ffi::RenderTexture2D {
+ id: fbo_id,
+ texture: rlb::ffi::Texture2D {
+ id: col_tex_id,
+ width,
+ height,
+ mipmaps: 1,
+ format: rlb::ffi::PixelFormat::PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 as i32,
+ },
+ depth: rlb::ffi::Texture2D {
+ id: depth_tex_id,
+ width,
+ height,
+ mipmaps: 1,
+ format: 19,
+ },
+ })
+ }
+}