diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 52cc0d919..f8c8476ea 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -219,9 +219,9 @@ class RenderWindow( } renderStats.startFrame() + framebufferManager.clear() renderSystem.framebuffer = null renderSystem.clear(IntegratedBufferTypes.COLOR_BUFFER, IntegratedBufferTypes.DEPTH_BUFFER) - framebufferManager.clear() val currentTickTime = TimeUtil.time diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/IntegratedFramebuffer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/IntegratedFramebuffer.kt index cef9d3794..a2f0da68f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/IntegratedFramebuffer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/IntegratedFramebuffer.kt @@ -2,6 +2,7 @@ package de.bixilon.minosoft.gui.rendering.framebuffer import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.renderer.Drawable +import de.bixilon.minosoft.gui.rendering.system.base.BlendingFunctions import de.bixilon.minosoft.gui.rendering.system.base.IntegratedBufferTypes import de.bixilon.minosoft.gui.rendering.system.base.PolygonModes import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer @@ -20,18 +21,19 @@ interface IntegratedFramebuffer : Drawable { framebuffer.init() shader.load() shader.use() - shader.setInt("uTexture", 0) + shader.setInt("uColor", 0) + shader.setInt("uDepth", 1) mesh.load() } fun clear() { renderWindow.renderSystem.framebuffer = framebuffer - renderWindow.renderSystem.clear(IntegratedBufferTypes.COLOR_BUFFER, IntegratedBufferTypes.DEPTH_BUFFER, IntegratedBufferTypes.STENCIL_BUFFER) + renderWindow.renderSystem.clear(IntegratedBufferTypes.COLOR_BUFFER, IntegratedBufferTypes.DEPTH_BUFFER) } override fun draw() { renderWindow.renderSystem.framebuffer = null - renderWindow.renderSystem.reset(blending = true) + renderWindow.renderSystem.reset(blending = true, sourceRGB = BlendingFunctions.SOURCE_ALPHA, destinationRGB = BlendingFunctions.ONE_MINUS_SOURCE_ALPHA) framebuffer.bindTexture() shader.use() mesh.draw() diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/HUDRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/HUDRenderer.kt index b4a878324..37a8c25ec 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/HUDRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/HUDRenderer.kt @@ -42,7 +42,6 @@ import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent import de.bixilon.minosoft.gui.rendering.renderer.Drawable import de.bixilon.minosoft.gui.rendering.renderer.Renderer import de.bixilon.minosoft.gui.rendering.renderer.RendererBuilder -import de.bixilon.minosoft.gui.rendering.system.base.IntegratedBufferTypes import de.bixilon.minosoft.gui.rendering.system.base.PolygonModes import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer @@ -176,7 +175,6 @@ class HUDRenderer( lastTickTime = time } - renderWindow.renderSystem.clear(IntegratedBufferTypes.DEPTH_BUFFER) var z = 0 for (element in hudElements) { if (!element.enabled) { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/renderer/RendererManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/renderer/RendererManager.kt index aa88fd341..27a72c399 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/renderer/RendererManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/renderer/RendererManager.kt @@ -116,7 +116,6 @@ class RendererManager( continue } renderSystem.polygonMode = renderer.polygonMode - renderSystem.framebuffer = renderer.framebuffer renderer.drawPre() } } @@ -133,7 +132,6 @@ class RendererManager( continue } renderSystem.polygonMode = renderer.polygonMode - renderSystem.framebuffer = renderer.framebuffer renderer.drawPost() } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/OpenGLFramebuffer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/OpenGLFramebuffer.kt index 8cb16dbf3..5470d58c4 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/OpenGLFramebuffer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/OpenGLFramebuffer.kt @@ -4,8 +4,8 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.FramebufferState import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.texture.FramebufferTexture import de.bixilon.minosoft.gui.rendering.system.base.buffer.render.Renderbuffer -import de.bixilon.minosoft.gui.rendering.system.base.buffer.render.RenderbufferModes -import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.frame.texture.OpenGLFramebufferTexture +import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.frame.texture.OpenGLFramebufferColorTexture +import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.frame.texture.OpenGLFramebufferDepthTexture import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.render.OpenGLRenderbuffer import glm_.vec2.Vec2i import org.lwjgl.opengl.GL30.* @@ -16,7 +16,8 @@ class OpenGLFramebuffer(var size: Vec2i) : Framebuffer { private var id = -1 - private lateinit var texture: OpenGLFramebufferTexture + private lateinit var colorTexture: OpenGLFramebufferColorTexture + private lateinit var depthTexture: OpenGLFramebufferDepthTexture private lateinit var renderbuffer: OpenGLRenderbuffer override fun init() { @@ -26,13 +27,19 @@ class OpenGLFramebuffer(var size: Vec2i) : Framebuffer { glViewport(0, 0, size.x, size.y) - texture = OpenGLFramebufferTexture(size) - texture.init() - attach(texture) + colorTexture = OpenGLFramebufferColorTexture(size) + colorTexture.init() + attach(colorTexture) - renderbuffer = OpenGLRenderbuffer(RenderbufferModes.DEPTH_COMPONENT24, size) - renderbuffer.init() - attach(renderbuffer) + //renderbuffer = OpenGLRenderbuffer(RenderbufferModes.DEPTH_COMPONENT24, size) + //renderbuffer.init() + //attach(renderbuffer) + + depthTexture = OpenGLFramebufferDepthTexture(size) + depthTexture.init() + attach(depthTexture) + + glDrawBuffers(intArrayOf(GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT)) check(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) { "Framebuffer is incomplete!" } state = FramebufferState.COMPLETE @@ -53,8 +60,11 @@ class OpenGLFramebuffer(var size: Vec2i) : Framebuffer { } override fun attach(texture: FramebufferTexture) { - check(texture is OpenGLFramebufferTexture) { "Can not attach non OpenGL texture!" } - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.id, 0) + when (texture) { + is OpenGLFramebufferDepthTexture -> glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture.id, 0) + is OpenGLFramebufferColorTexture -> glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.id, 0) + else -> throw IllegalArgumentException("Can not attach non OpenGL texture!") + } } override fun delete() { @@ -66,16 +76,16 @@ class OpenGLFramebuffer(var size: Vec2i) : Framebuffer { override fun bindTexture() { check(state == FramebufferState.COMPLETE) { "Framebuffer is incomplete!" } - glActiveTexture(GL_TEXTURE0) - glBindTexture(GL_TEXTURE_2D, texture.id) + colorTexture.bind(0) + depthTexture.bind(1) } override fun resize(size: Vec2i) { if (size == this.size) { return } - if (this::texture.isInitialized) { - texture.unload() + if (this::colorTexture.isInitialized) { + colorTexture.unload() } if (this::renderbuffer.isInitialized) { renderbuffer.unload() diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferColorTexture.kt similarity index 69% rename from src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferTexture.kt rename to src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferColorTexture.kt index 6738634ad..c538c04d5 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferColorTexture.kt @@ -5,23 +5,15 @@ import glm_.vec2.Vec2i import org.lwjgl.opengl.GL30.* import java.nio.ByteBuffer -class OpenGLFramebufferTexture( +class OpenGLFramebufferColorTexture( override val size: Vec2i, -) : FramebufferTexture { - var id: Int = -1 - private set +) : OpenGLTexture(), FramebufferTexture { override fun init() { id = glGenTextures() glBindTexture(GL_TEXTURE_2D, id) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, null as ByteBuffer?) - //glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24, size.x, size.y, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) } - - override fun unload() { - glDeleteTextures(id) - id = -1 - } } 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 new file mode 100644 index 000000000..98d9a5378 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLFramebufferDepthTexture.kt @@ -0,0 +1,22 @@ +package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.frame.texture + +import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.texture.FramebufferTexture +import glm_.vec2.Vec2i +import org.lwjgl.opengl.GL30.* +import java.nio.ByteBuffer + +class OpenGLFramebufferDepthTexture( + override val size: Vec2i, +) : OpenGLTexture(), FramebufferTexture { + + override fun init() { + 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_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/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLTexture.kt new file mode 100644 index 000000000..2a5d0ce47 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/frame/texture/OpenGLTexture.kt @@ -0,0 +1,23 @@ +package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.frame.texture + +import org.lwjgl.opengl.GL11.* +import org.lwjgl.opengl.GL13.GL_TEXTURE0 +import org.lwjgl.opengl.GL13.glActiveTexture + +abstract class OpenGLTexture { + var id: Int = -1 + protected set + + abstract fun init() + + fun bind(target: Int) { + check(target in 0 until 12) + glActiveTexture(GL_TEXTURE0 + target) + glBindTexture(GL_TEXTURE_2D, id) + } + + fun unload() { + glDeleteTextures(id) + id = -1 + } +} diff --git a/src/main/resources/assets/minosoft/rendering/shader/framebuffer/gui/gui.fsh b/src/main/resources/assets/minosoft/rendering/shader/framebuffer/gui/gui.fsh index 7308f7bba..fd2aad0d0 100644 --- a/src/main/resources/assets/minosoft/rendering/shader/framebuffer/gui/gui.fsh +++ b/src/main/resources/assets/minosoft/rendering/shader/framebuffer/gui/gui.fsh @@ -17,11 +17,11 @@ in vec2 finUV; out vec4 foutColor; -uniform sampler2D uTexture; +uniform sampler2D uColor; void main() { - foutColor = texture(uTexture, finUV); + foutColor = texture(uColor, finUV); if (foutColor.a == 0.0f) { discard; 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 7308f7bba..7665ff662 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 @@ -17,11 +17,17 @@ in vec2 finUV; out vec4 foutColor; -uniform sampler2D uTexture; +uniform sampler2D uColor; +uniform sampler2D uDepth; void main() { - foutColor = texture(uTexture, finUV); + vec4 color = texture(uColor, finUV); + if (color.a == 0.0f) { + discard; + } + float depth = texture(uDepth, finUV).r; + foutColor = vec4(color.xyz, depth); if (foutColor.a == 0.0f) { discard; diff --git a/src/main/resources/assets/minosoft/rendering/shader/includes/postprocessing/fragment.glsl b/src/main/resources/assets/minosoft/rendering/shader/includes/postprocessing/fragment.glsl deleted file mode 100644 index af16f3d5e..000000000 --- a/src/main/resources/assets/minosoft/rendering/shader/includes/postprocessing/fragment.glsl +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -#ifdef POSTPROCESSING_FOG -in vec3 finVertexPosition; -uniform vec3 uCameraPosition; -uniform float uFogStart; -uniform float uFogEnd; -uniform vec4 uFogColor; - - -float getFogFactor(float distance) { - if (distance >= uFogEnd) { - return 0.0f; - } - if (distance <= uFogStart) { - return 1.0f; - } - - // ToDo: Exponential fog - return (uFogEnd - distance) / (uFogEnd - uFogStart); -} - - #endif - - -void main() { - work(); - - #ifdef POSTPROCESSING_FOG - float fogFactor = getFogFactor(distance(uCameraPosition, finVertexPosition)); - - if (fogFactor != 1.0f) { - foutColor = vec4(mix(uFogColor.rgb, foutColor.rgb, fogFactor), foutColor.a); - }; - #endif -} diff --git a/src/main/resources/assets/minosoft/rendering/shader/includes/postprocessing/vertex.glsl b/src/main/resources/assets/minosoft/rendering/shader/includes/postprocessing/vertex.glsl deleted file mode 100644 index 07489de4f..000000000 --- a/src/main/resources/assets/minosoft/rendering/shader/includes/postprocessing/vertex.glsl +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -out vec3 finVertexPosition; - -void main() { - work(); - finVertexPosition = vinPosition; -} diff --git a/src/main/resources/assets/minosoft/rendering/shader/world/world.fsh b/src/main/resources/assets/minosoft/rendering/shader/world/world.fsh index b4f74f919..7f2506946 100644 --- a/src/main/resources/assets/minosoft/rendering/shader/world/world.fsh +++ b/src/main/resources/assets/minosoft/rendering/shader/world/world.fsh @@ -23,11 +23,9 @@ in float finInterpolation; in vec4 finTintColor; -#define POSTPROCESSING_FOG - #include "minosoft:texture" -void work() { +void main() { vec4 firstTexelColor = getTexture(finTextureIndex1, finTextureCoordinates1); if (firstTexelColor.a == 0.0f) { discard; @@ -61,5 +59,3 @@ void work() { } #endif } - - #include "minosoft:postprocessing/fragment" diff --git a/src/main/resources/assets/minosoft/rendering/shader/world/world.vsh b/src/main/resources/assets/minosoft/rendering/shader/world/world.vsh index 32623ce4c..90120652e 100644 --- a/src/main/resources/assets/minosoft/rendering/shader/world/world.vsh +++ b/src/main/resources/assets/minosoft/rendering/shader/world/world.vsh @@ -28,13 +28,11 @@ out vec4 finTintColor; uniform mat4 uViewProjectionMatrix; -#define POSTPROCESSING_FOG - #include "minosoft:animation" #include "minosoft:color" #include "minosoft:light" -void work() { +void main() { gl_Position = uViewProjectionMatrix * vec4(vinPosition, 1.0f); finTintColor = getRGBColor(vinTintColorAndLight & 0xFFFFFFu) * getLight(vinTintColorAndLight >> 24u); @@ -60,5 +58,3 @@ void work() { finInterpolation = interpolation / 100.0f; } - - #include "minosoft:postprocessing/vertex"