From fba9c24c8bcb3e1556328c6dfc2d165aac99eeb0 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 3 Jan 2022 18:51:59 +0100 Subject: [PATCH] fog --- doc/Modding.md | 2 +- .../bixilon/minosoft/config/key/KeyActions.kt | 2 +- .../gui/rendering/camera/MatrixHandler.kt | 2 +- .../texture/OpenGLFramebufferDepthTexture.kt | 3 ++ .../shader/framebuffer/world/world.fsh | 28 +++++++++++++++---- .../rendering/shader/includes/texture.glsl | 4 +-- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/doc/Modding.md b/doc/Modding.md index 90a782765..b02788dba 100644 --- a/doc/Modding.md +++ b/doc/Modding.md @@ -52,7 +52,7 @@ In the root folder of your jar file (the mod) must be a file called `mod.json`. - `version_name`, `authors` and `name` is the classic implementation of metadata. Can be anything, will be displayed in the mod list. **Required** - `modding_api_version` Modding API version of minosoft. Currently `1` **Required** - `resource_namespace` is the prefix of items (for Minecraft it is `minecraft`). Aka the thing before the `:`. **Required** -- `main_class` the Main class of your mod (self explaining). The main class needs to extent the abstract class `MinosoftMod`. **Required** +- `main_class` the Main class of your mod (self explaining). The main class needs to extend the abstract class `MinosoftMod`. **Required** - `loading` Loading attributes. **Optional** - `priority` should the mod be loaded at the beginning or at the end. Possible values are `LOWEST`, `LOW`, `NORMAL`, `HIGH`, `HIGHEST` **Optional** - `dependencies` Used if you need another mod to work **Optional** diff --git a/src/main/java/de/bixilon/minosoft/config/key/KeyActions.kt b/src/main/java/de/bixilon/minosoft/config/key/KeyActions.kt index a370b3d44..bbb3e9db2 100644 --- a/src/main/java/de/bixilon/minosoft/config/key/KeyActions.kt +++ b/src/main/java/de/bixilon/minosoft/config/key/KeyActions.kt @@ -34,7 +34,7 @@ enum class KeyAction { // custom ones /** - * Key must be hold in addition to a other action + * Key must be hold in addition to another action */ MODIFIER, diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt index d3de4cd4d..2839ac189 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt @@ -75,7 +75,7 @@ class MatrixHandler( } private fun calculateProjectionMatrix(screenDimensions: Vec2 = renderWindow.window.sizef) { - projectionMatrix = glm.perspective(fov.rad.toFloat(), screenDimensions.x / screenDimensions.y, 0.01f, 10000.0f) + projectionMatrix = glm.perspective(fov.rad.toFloat(), screenDimensions.x / screenDimensions.y, 0.01f, 1000.0f) } fun init() { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferDepthTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferDepthTexture.kt index 98d9a5378..ad9753d87 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferDepthTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferDepthTexture.kt @@ -13,9 +13,12 @@ class OpenGLFramebufferDepthTexture( id = glGenTextures() glBindTexture(GL_TEXTURE_2D, id) glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null as ByteBuffer?) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) } diff --git a/src/main/resources/assets/minosoft/rendering/shader/framebuffer/world/world.fsh b/src/main/resources/assets/minosoft/rendering/shader/framebuffer/world/world.fsh index 7665ff662..02405620c 100644 --- a/src/main/resources/assets/minosoft/rendering/shader/framebuffer/world/world.fsh +++ b/src/main/resources/assets/minosoft/rendering/shader/framebuffer/world/world.fsh @@ -20,16 +20,34 @@ out vec4 foutColor; uniform sampler2D uColor; uniform sampler2D uDepth; +uniform float uFogStart = 60.0f; +uniform float uFogEnd = 75.0f; + +#define NEAR_PLANE 0.01f +#define FAR_PLANE 1000.0f + +float linearize_depth(float depth) { + return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - (depth * 2.0f - 1.0f) * (FAR_PLANE - NEAR_PLANE)); +} + +float calulate_fog_alpha(float depth) { + if (depth < uFogStart){ + return 1.0f; + } + if (depth > uFogEnd){ + discard; + } + + return pow(1.0f - (depth - uFogStart) / (uFogEnd - uFogStart), 2); +} void main() { vec4 color = texture(uColor, finUV); if (color.a == 0.0f) { discard; } - float depth = texture(uDepth, finUV).r; - foutColor = vec4(color.xyz, depth); + float depth = linearize_depth(texture(uDepth, finUV).x); + float alpha = calulate_fog_alpha(depth); - if (foutColor.a == 0.0f) { - discard; - } + foutColor = vec4(color.xyz, alpha); } diff --git a/src/main/resources/assets/minosoft/rendering/shader/includes/texture.glsl b/src/main/resources/assets/minosoft/rendering/shader/includes/texture.glsl index f4cdb0447..6e6be355d 100644 --- a/src/main/resources/assets/minosoft/rendering/shader/includes/texture.glsl +++ b/src/main/resources/assets/minosoft/rendering/shader/includes/texture.glsl @@ -14,7 +14,7 @@ uniform sampler2DArray uTextures[7]; -vec4 getTexture(uint textureId, vec3 uv) { // ToDo: This method is just stupid and workarounds a opengl crash with mesa drivers +vec4 getTexture(uint textureId, vec3 uv) { // ToDo: This method is just stupid and workarounds an opengl crash with mesa drivers #if defined __NVIDIA || defined __AMD return texture(uTextures[textureId], uv); #else @@ -30,7 +30,7 @@ vec4 getTexture(uint textureId, vec3 uv) { // ToDo: This method is just stupid a #endif } -vec4 getTexture(uint textureId, vec3 uv, float mipmapLevel) { // ToDo: This method is just stupid and workarounds a opengl crash with mesa drivers +vec4 getTexture(uint textureId, vec3 uv, float mipmapLevel) { // ToDo: This method is just stupid and workarounds an opengl crash with mesa drivers #if defined __NVIDIA || defined __AMD return textureLod(uTextures[textureId], uv, mipmapLevel); #else