Save commit for fog fix

This commit is contained in:
coolGi
2023-07-19 22:19:01 +09:30
parent 6a87a87597
commit fdb470b5b3
12 changed files with 378 additions and 24 deletions
@@ -30,6 +30,9 @@ import com.seibel.distanthorizons.core.render.glObject.GLProxy;
import com.seibel.distanthorizons.core.render.glObject.GLState;
import com.seibel.distanthorizons.core.render.glObject.buffer.GLVertexBuffer;
import com.seibel.distanthorizons.core.render.glObject.buffer.QuadElementBuffer;
import com.seibel.distanthorizons.core.render.renderer.shaders.DarkShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.FogShader;
import com.seibel.distanthorizons.core.render.renderer.shaders.SSAORenderer;
import com.seibel.distanthorizons.core.render.renderer.shaders.SSAOShader;
import com.seibel.distanthorizons.core.util.LodUtil;
import com.seibel.distanthorizons.core.util.RenderUtil;
@@ -244,7 +247,12 @@ public class LodRenderer
bufferHandler.renderOpaque(this);
if (Config.Client.Advanced.Graphics.Quality.ssao.get()) {
SSAOShader.INSTANCE.render(partialTicks);
// SSAOShader.INSTANCE.render(partialTicks);
SSAORenderer.INSTANCE.render(partialTicks);
}
{
// FogShader.INSTANCE.render(partialTicks);
// DarkShader.INSTANCE.render(partialTicks);
}
//======================//
@@ -6,6 +6,7 @@ import com.seibel.distanthorizons.core.render.glObject.GLState;
import com.seibel.distanthorizons.core.render.glObject.buffer.GLVertexBuffer;
import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram;
import com.seibel.distanthorizons.core.render.glObject.vertexAttribute.VertexAttribute;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL32;
@@ -14,6 +15,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public abstract class AbstractShaderRenderer {
protected static final IMinecraftClientWrapper MC = SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class);
protected static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
private static final float[] box_vertices = {
@@ -106,7 +108,7 @@ public abstract class AbstractShaderRenderer {
this.setApplyShaderUniforms(partialTicks);
}
GL32.glEnable(GL11.GL_BLEND);
GL32.glBlendFunc(GL32.GL_ZERO, GL32.GL_SRC_ALPHA);
GL32.glBlendFunc(GL32.GL_SRC_ALPHA, GL32.GL_ONE_MINUS_SRC_ALPHA);
GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, MC_RENDER.getTargetFrameBuffer());
GL32.glActiveTexture(GL32.GL_TEXTURE0);
GL32.glBindTexture(GL32.GL_TEXTURE_2D, shaderTexture);
@@ -149,4 +151,10 @@ public abstract class AbstractShaderRenderer {
boxBuffer.bind();
boxBuffer.uploadBuffer(buffer, box_vertices.length, EGpuUploadMethod.DATA, box_vertices.length * Float.BYTES);
}
public void free() {
this.shader.free();
if (this.applyShader != null)
this.applyShader.free();
}
}
@@ -0,0 +1,11 @@
package com.seibel.distanthorizons.core.render.renderer.shaders;
import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram;
public class DarkShader extends AbstractShaderRenderer {
public static DarkShader INSTANCE = new DarkShader();
protected DarkShader() {
super(new ShaderProgram("shaders/normal.vert", "shaders/test/dark.frag", "fragColor", new String[] { "vPosition", "color" }));
}
}
@@ -0,0 +1,92 @@
package com.seibel.distanthorizons.core.render.renderer.shaders;
import com.seibel.distanthorizons.api.enums.rendering.EFogColorMode;
import com.seibel.distanthorizons.core.config.Config;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.render.fog.LodFogConfig;
import com.seibel.distanthorizons.core.render.glObject.shader.Shader;
import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram;
import com.seibel.distanthorizons.core.util.LodUtil;
import com.seibel.distanthorizons.core.util.RenderUtil;
import com.seibel.distanthorizons.core.wrapperInterfaces.IVersionConstants;
import com.seibel.distanthorizons.coreapi.util.math.Mat4f;
import org.lwjgl.opengl.GL32;
import java.awt.*;
public class FogShader extends AbstractShaderRenderer {
public static FogShader INSTANCE = new FogShader(LodFogConfig.generateFogConfig());
private static final IVersionConstants VERSION_CONSTANTS = SingletonInjector.INSTANCE.get(IVersionConstants.class);
// Fog Uniforms
public final int fogColorUniform;
public final int fogScaleUniform;
public final int fogVerticalScaleUniform;
public final int nearFogStartUniform;
public final int nearFogLengthUniform;;
public final int fullFogModeUniform;
public FogShader(LodFogConfig fogConfig) {
// TODO & Note: This code is a bit jank, so try to update it later (preferably not using something to process the shader)
super(new ShaderProgram(
() -> Shader.loadFile("shaders/normal.vert", false, new StringBuilder()).toString(),
() -> fogConfig.loadAndProcessFragShader("shaders/fog/fog.frag", false).toString(),
"fragColor", new String[] { "vPos", "vPosition", "color" }
));
// Fog uniforms
fogColorUniform = this.shader.getUniformLocation("fogColor");
fullFogModeUniform = this.shader.getUniformLocation("fullFogMode");
fogScaleUniform = this.shader.tryGetUniformLocation("fogScale");
fogVerticalScaleUniform = this.shader.tryGetUniformLocation("fogVerticalScale");
// near
nearFogStartUniform = this.shader.tryGetUniformLocation("nearFogStart");
nearFogLengthUniform = this.shader.tryGetUniformLocation("nearFogLength");
}
@Override
void setShaderUniforms(float partialTicks) {
int lodDrawDistance = RenderUtil.getFarClipPlaneDistanceInBlocks();
int vanillaDrawDistance = MC_RENDER.getRenderDistance() * LodUtil.CHUNK_WIDTH;
// super.bind();
vanillaDrawDistance += 32; // Give it a 2 chunk boundary for near fog.
// // uniforms
// this.shader.setUniform(combinedMatUniform, combinedMatrix);
// this.shader.setUniform(mircoOffsetUniform, 0.01f); // 0.01 block offset
//
// // setUniform(skyLightUniform, skyLight);
// this.shader.setUniform(lightMapUniform, lightmapBindPoint);
//
// if (worldYOffsetUniform != -1) this.shader.setUniform(worldYOffsetUniform, (float)worldYOffset);
// Fog
this.shader.setUniform(fullFogModeUniform, MC_RENDER.isFogStateSpecial() ? 1 : 0);
this.shader.setUniform(fogColorUniform, MC_RENDER.isFogStateSpecial() ? getSpecialFogColor(partialTicks) : getFogColor(partialTicks));
float nearFogLen = vanillaDrawDistance * 0.2f / lodDrawDistance;
float nearFogStart = vanillaDrawDistance * (VERSION_CONSTANTS.isVanillaRenderedChunkSquare() ? (float)Math.sqrt(2.) : 1.f) / lodDrawDistance;
if (nearFogStartUniform != -1) this.shader.setUniform(nearFogStartUniform, nearFogStart);
if (nearFogLengthUniform != -1) this.shader.setUniform(nearFogLengthUniform, nearFogLen);
if (fogScaleUniform != -1) this.shader.setUniform(fogScaleUniform, 1.f/lodDrawDistance);
if (fogVerticalScaleUniform != -1) this.shader.setUniform(fogVerticalScaleUniform, 1.f/MC.getWrappedClientWorld().getHeight());
}
private Color getFogColor(float partialTicks)
{
Color fogColor;
if (Config.Client.Advanced.Graphics.Fog.colorMode.get() == EFogColorMode.USE_SKY_COLOR)
fogColor = MC_RENDER.getSkyColor();
else
fogColor = MC_RENDER.getFogColor(partialTicks);
return fogColor;
}
private Color getSpecialFogColor(float partialTicks)
{
return MC_RENDER.getSpecialFogColor(partialTicks);
}
}
@@ -0,0 +1,196 @@
package com.seibel.distanthorizons.core.render.renderer.shaders;
import com.seibel.distanthorizons.api.enums.config.EGpuUploadMethod;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.render.glObject.GLState;
import com.seibel.distanthorizons.core.render.glObject.buffer.GLVertexBuffer;
import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram;
import com.seibel.distanthorizons.core.render.glObject.vertexAttribute.VertexAttribute;
import com.seibel.distanthorizons.core.util.LodUtil;
import com.seibel.distanthorizons.core.util.RenderUtil;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import com.seibel.distanthorizons.coreapi.util.math.Mat4f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL32;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
// TODO: Move over to SSAOShader
// For some reason this version looks slightly different to that, even tough there isnt much visible change in the code
public class SSAORenderer {
public static SSAORenderer INSTANCE = new SSAORenderer();
public SSAORenderer() {
}
private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
private static final float[] box_vertices = {
-1, -1,
1, -1,
1, 1,
-1, -1,
1, 1,
-1, 1,
};
ShaderProgram ssaoShader;
ShaderProgram applyShader;
GLVertexBuffer boxBuffer;
VertexAttribute va;
boolean init = false;
private static final int MAX_KERNEL_SIZE = 32;
private float[] kernel = new float[MAX_KERNEL_SIZE * 3];
public void init() {
if (init) return;
init = true;
va = VertexAttribute.create();
va.bind();
// Pos
va.setVertexAttribute(0, 0, VertexAttribute.VertexPointer.addVec2Pointer(false));
va.completeAndCheck(Float.BYTES * 2);
ssaoShader = new ShaderProgram("shaders/normal.vert", "shaders/ssao/ao.frag",
"fragColor", new String[]{"vPosition"});
applyShader = new ShaderProgram("shaders/normal.vert", "shaders/ssao/apply-frag.frag",
"fragColor", new String[]{"vPosition"});
// Generate kernel
kernel = genKernel();
// Framebuffer
createBuffer();
}
private int width = -1;
private int height = -1;
private int ssaoFramebuffer = -1;
private int ssaoTexture = -1;
private void createFramebuffer(int width, int height) {
if (ssaoFramebuffer != -1) {
GL32.glDeleteFramebuffers(ssaoFramebuffer);
ssaoFramebuffer = -1;
}
if (ssaoTexture != -1) {
GL32.glDeleteTextures(ssaoTexture);
ssaoTexture = -1;
}
ssaoFramebuffer = GL32.glGenFramebuffers();
GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, ssaoFramebuffer);
ssaoTexture = GL32.glGenTextures();
GL32.glBindTexture(GL32.GL_TEXTURE_2D, ssaoTexture);
GL32.glTexImage2D(GL32.GL_TEXTURE_2D, 0, GL32.GL_RED, width, height, 0, GL32.GL_RED, GL32.GL_FLOAT, (ByteBuffer) null);
GL32.glTexParameteri(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_MIN_FILTER, GL32.GL_NEAREST);
GL32.glTexParameteri(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_MAG_FILTER, GL32.GL_NEAREST);
GL32.glFramebufferTexture2D(GL32.GL_FRAMEBUFFER, GL32.GL_COLOR_ATTACHMENT0, GL32.GL_TEXTURE_2D, ssaoTexture, 0);
}
private void createBuffer() {
ByteBuffer buffer = ByteBuffer.allocateDirect(box_vertices.length * Float.BYTES);
buffer.order(ByteOrder.nativeOrder());
buffer.asFloatBuffer().put(box_vertices);
buffer.rewind();
boxBuffer = new GLVertexBuffer(false);
boxBuffer.bind();
boxBuffer.uploadBuffer(buffer, box_vertices.length, EGpuUploadMethod.DATA, box_vertices.length * Float.BYTES);
}
private static float[] genKernel() {
float[] kernel = new float[MAX_KERNEL_SIZE * 3];
for (int i = 0; i < MAX_KERNEL_SIZE; i++) {
float sampleX = (float) (Math.random() * 2.0 - 1.0);
float sampleY = (float) (Math.random() * 2.0 - 1.0);
float sampleZ = (float) Math.random();
// Normalize
float magnitude = (float) Math.sqrt(Math.pow(sampleX, 2) + Math.pow(sampleY, 2) + Math.pow(sampleZ, 2));
sampleX /= magnitude;
sampleY /= magnitude;
sampleZ /= magnitude;
float scale = i / (float) MAX_KERNEL_SIZE;
float interpolatedScale = (float) (0.1 + (scale * scale) * (0.9));
sampleX *= interpolatedScale;
sampleY *= interpolatedScale;
sampleZ *= interpolatedScale;
kernel[i * 3] = sampleX;
kernel[i * 3 + 1] = sampleY;
kernel[i * 3 + 2] = sampleZ;
}
return kernel;
}
public void render(float partialTicks) {
GLState state = new GLState();
init();
//GL32.glDepthMask(false);
int width = MC_RENDER.getTargetFrameBufferViewportWidth();
int height = MC_RENDER.getTargetFrameBufferViewportHeight();
if (this.width != width || this.height != height) {
this.width = width;
this.height = height;
createFramebuffer(width, height);
}
GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, ssaoFramebuffer);
GL32.glViewport(0, 0, width, height);
GL32.glDisable(GL32.GL_DEPTH_TEST);
GL32.glDisable(GL32.GL_BLEND);
GL32.glDisable(GL32.GL_SCISSOR_TEST);
Mat4f perspective = Mat4f.perspective(
(float) MC_RENDER.getFov(partialTicks),
MC_RENDER.getTargetFrameBufferViewportWidth() / (float) MC_RENDER.getTargetFrameBufferViewportHeight(),
RenderUtil.getNearClipPlaneDistanceInBlocks(partialTicks),
(float) ((RenderUtil.getFarClipPlaneDistanceInBlocks() + LodUtil.REGION_WIDTH) * Math.sqrt(2)));
ssaoShader.bind();
ssaoShader.setUniform(ssaoShader.getUniformLocation("gProj"), perspective);
ssaoShader.setUniform(ssaoShader.getUniformLocation("gSampleRad"), 3.0f);
ssaoShader.setUniform(ssaoShader.getUniformLocation("gFactor"), 0.8f);
ssaoShader.setUniform(ssaoShader.getUniformLocation("gPower"), 1.0f);
va.bind();
va.bindBufferToAllBindingPoint(boxBuffer.getId());
GL32.glActiveTexture(GL32.GL_TEXTURE0);
GL32.glBindTexture(GL32.GL_TEXTURE_2D, MC_RENDER.getDepthTextureId());
GL32.glUniform3fv(ssaoShader.getUniformLocation("gKernel"), kernel);
GL32.glUniform1i(ssaoShader.getUniformLocation("gDepthMap"), 0);
GL32.glDrawArrays(GL32.GL_TRIANGLES, 0, 6);
applyShader.bind();
GL32.glEnable(GL11.GL_BLEND);
GL32.glBlendFunc(GL32.GL_SRC_ALPHA, GL32.GL_ONE_MINUS_SRC_ALPHA);
GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, MC_RENDER.getTargetFrameBuffer());
GL32.glActiveTexture(GL32.GL_TEXTURE0);
GL32.glBindTexture(GL32.GL_TEXTURE_2D, ssaoTexture);
GL32.glDrawArrays(GL32.GL_TRIANGLES, 0, 6);
//GL32.glBindFramebuffer(GL32.GL_READ_FRAMEBUFFER, ssaoFramebuffer);
//GL32.glBindFramebuffer(GL32.GL_DRAW_FRAMEBUFFER, MC_RENDER.getTargetFrameBuffer());
//GL32.glBlitFramebuffer(
// 0, 0, width, height,
// 0, 0, width, height,
// GL11.GL_COLOR_BUFFER_BIT,
// GL11.GL_NEAREST
//);
state.restore();
}
}
@@ -15,9 +15,9 @@ public class SSAOShader extends AbstractShaderRenderer {
public SSAOShader() {
super(
new ShaderProgram("shaders/ssao/ao.vert", "shaders/ssao/ao.frag",
new ShaderProgram("shaders/normal.vert", "shaders/ssao/ao.frag",
"fragColor", new String[]{"vPos"}),
new ShaderProgram("shaders/ssao/ao.vert", "shaders/ssao/apply-frag.frag",
new ShaderProgram("shaders/normal.vert", "shaders/ssao/apply-frag.frag",
"fragColor", new String[]{"vPos"})
);
+35 -12
View File
@@ -1,10 +1,7 @@
#version 150 core
in vec4 vertexColor;
in vec3 vertexWorldPos;
in float vertexYPos;
in vec4 vPos;
in vec2 TexCoord;
out vec4 fragColor;
@@ -30,6 +27,20 @@ float mixFogThickness(float near, float far, float height);
// =========================================================
// Puts steps in a float
// EG. setting stepSize to 4 then this would be the result of this function
// In: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, ..., 1.1, 1.2, 1.3
// Out: 0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, ..., 1.0, 1.0, 1.25
float quantize(float val, int stepSize) {
return floor(val*stepSize)/stepSize;
}
// The modulus function dosnt exist in GLSL so I made my own
// To speed up the mod function, this only accepts full numbers for y
float mod(float x, int y) {
return x - y * floor(x/y);
}
/**
@@ -39,19 +50,31 @@ float mixFogThickness(float near, float far, float height);
* version: 2023-6-21
*/
void main() {
float horizontalDist = length(vertexWorldPos.xz) * fogScale;
float heightDist = calculateHeightFogDepth(
if (fullFogMode != 0) {
fragColor = vec4(fogColor.r, fogColor.g, fogColor.b, 1.);
} else {
float horizontalDist = length(vertexWorldPos.xz) * fogScale;
float heightDist = calculateHeightFogDepth(
vertexWorldPos.y, vertexYPos) * fogVerticalScale;
float farDist = calculateFarFogDepth(horizontalDist,
float farDist = calculateFarFogDepth(horizontalDist,
length(vertexWorldPos.xyz) * fogScale, nearFogStart);
float nearFogThickness = getNearFogThickness(horizontalDist);
float farFogThickness = getFarFogThickness(farDist);
float heightFogThickness = getHeightFogThickness(heightDist);
float mixedFogThickness = clamp(mixFogThickness(
float nearFogThickness = getNearFogThickness(horizontalDist);
float farFogThickness = getFarFogThickness(farDist);
float heightFogThickness = getHeightFogThickness(heightDist);
float mixedFogThickness = clamp(mixFogThickness(
nearFogThickness, farFogThickness, heightFogThickness), 0.0, 1.0);
fragColor = mix(vertexColor, vec4(fogColor.rgb, 1.0), mixedFogThickness);
fragColor = vec4(fogColor.r, fogColor.g, fogColor.b, mixedFogThickness);
}
if (fragColor.r != 6969.) {
fragColor = vec4(
mod(vertexWorldPos.x, 1),
mod(vertexWorldPos.y, 1),
mod(vertexWorldPos.z, 1),
1.
);
}
}
@@ -81,11 +81,11 @@ void main() {
);
// For testing
if (vertexColor.r != 69420.) {
fragColor = vec4(
mod(fixedVPos.x, 1),
mod(fixedVPos.y, 1),
mod(fixedVPos.z, 1),
1f);
}
// if (vertexColor.r != 69420.) {
// fragColor = vec4(
// mod(fixedVPos.x, 1),
// mod(fixedVPos.y, 1),
// mod(fixedVPos.z, 1),
// 1f);
// }
}
@@ -1,5 +1,6 @@
#version 150 core
in uvec4 vPosition; // Fixme
in vec2 vPos;
out vec2 TexCoord;
@@ -10,6 +11,7 @@ out float vertexYPos;
void main()
{
vertexWorldPos = vec3(vPosition.x, vPosition.y, vPosition.z); // Fixme
gl_Position = vec4(vPos, 1.0, 1.0);
TexCoord = vPos.xy * 0.5 + 0.5;
}
@@ -30,6 +30,11 @@ vec3 calcViewPosition(vec2 coords) {
vs_pos.xyz = vs_pos.xyz / vs_pos.w;
return vs_pos.xyz;
}
// The modulus function dosnt exist in GLSL so I made my own
// To speed up the mod function, this only accepts full numbers for y
float mod(float x, int y) {
return x - y * floor(x/y);
}
void main()
{
@@ -9,5 +9,5 @@ uniform sampler2D gSSAOMap;
void main()
{
fragColor = vec4(0.0, 0.0, 0.0, texture(gSSAOMap, TexCoord).r);
fragColor = vec4(0.0, 0.0, 0.0, 1-texture(gSSAOMap, TexCoord).r);
}
@@ -0,0 +1,9 @@
#version 150 core
out vec4 fragColor;
// A test shader that makes everything darker
void main()
{
fragColor = vec4(0., 0., 1., 0.5);
}