wip better fog, depth texture

This commit is contained in:
Bixilon 2022-01-02 20:16:42 +01:00
parent aca57fd5f0
commit 64e8d056cc
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
14 changed files with 90 additions and 113 deletions

View File

@ -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

View File

@ -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()

View File

@ -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) {

View File

@ -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()
}
}

View File

@ -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()

View File

@ -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
}
}

View File

@ -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)
}
}

View File

@ -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
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
out vec3 finVertexPosition;
void main() {
work();
finVertexPosition = vinPosition;
}

View File

@ -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"

View File

@ -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"