Added optimisations to the shadow renderer and added fog

This commit is contained in:
josua 2020-07-21 09:26:20 +10:00
parent 5bd3984997
commit 3cf6f0cc1e
12 changed files with 74 additions and 58 deletions

View File

@ -37,9 +37,12 @@ public class Camera
double time = Main.world.getLayer().layergen.getSunPosition(); double time = Main.world.getLayer().layergen.getSunPosition();
projection_sun = Matrix4.identity(); projection_sun = Matrix4.identity();
projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(angle + 180, 0, 1, 0));
projection_sun = Matrix4.multiply(projection_sun, Matrix4.translate(0, 0, 8));
projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(-angle - 180, 0, 1, 0));
projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(20, 0, 0, 1)); projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(20, 0, 0, 1));
projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(-time, 1, 0, 0)); projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(-time, 1, 0, 0));
projection_sun = Matrix4.multiply(projection_sun, Matrix4.translate(0, 0, -16)); projection_sun = Matrix4.multiply(projection_sun, Matrix4.translate(0, 0, -16));
projection_sun = Matrix4.multiply(projection_sun, Matrix4.scale(new Vec3d(1/32.0, 1/32.0, -1/32.0))); projection_sun = Matrix4.multiply(projection_sun, Matrix4.scale(new Vec3d(1/20.0, 1/20.0, -1/32.0)));
} }
} }

View File

@ -24,8 +24,7 @@ public class DisplayRender
{ {
public static int shadow_fbo; public static int shadow_fbo;
public static int shadow_depth; public static int shadow_depth;
public static int shadow_size = 8192;
private static final int SHADOW_SIZE = 4096;
private static int generateDepthTexture(int width, int height) private static int generateDepthTexture(int width, int height)
{ {
@ -39,6 +38,9 @@ public class DisplayRender
GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_MIN_FILTER, GL33.GL_LINEAR); GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_MIN_FILTER, GL33.GL_LINEAR);
GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_MAG_FILTER, GL33.GL_LINEAR); GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_MAG_FILTER, GL33.GL_LINEAR);
GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_WRAP_S, GL33.GL_CLAMP_TO_BORDER);
GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_WRAP_T, GL33.GL_CLAMP_TO_BORDER);
GL33.glTexParameterfv(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_BORDER_COLOR, new float[] {0, 0, 0, 0});
GL33.glFramebufferTexture(GL33.GL_FRAMEBUFFER, GL33.GL_DEPTH_ATTACHMENT, depth, 0); GL33.glFramebufferTexture(GL33.GL_FRAMEBUFFER, GL33.GL_DEPTH_ATTACHMENT, depth, 0);
return depth; return depth;
@ -68,7 +70,7 @@ public class DisplayRender
GL33.glDrawBuffer(GL33.GL_NONE); GL33.glDrawBuffer(GL33.GL_NONE);
GL33.glReadBuffer(GL33.GL_NONE); GL33.glReadBuffer(GL33.GL_NONE);
shadow_depth = generateDepthTexture(SHADOW_SIZE, SHADOW_SIZE); shadow_depth = generateDepthTexture(shadow_size, shadow_size);
} }
public static void render(int w, int h) public static void render(int w, int h)
@ -98,6 +100,7 @@ public class DisplayRender
Matrix4 billboard = Matrix4.multiply(Matrix4.rotate(-45, 1, 0, 0), rotated); Matrix4 billboard = Matrix4.multiply(Matrix4.rotate(-45, 1, 0, 0), rotated);
Main.window.environmentRenderer.use(); Main.window.environmentRenderer.use();
GL33.glUniformMatrix4fv(Main.window.glsl_camera, true, Camera.camera.matrix.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_billboard, true, billboard.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_billboard, true, billboard.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_rotated, true, rotated.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_rotated, true, rotated.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_projection_sun, true, Camera.camera.projection_sun.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_projection_sun, true, Camera.camera.projection_sun.getArray());
@ -108,13 +111,14 @@ public class DisplayRender
// Render from the sun/moons perspective // Render from the sun/moons perspective
GL33.glBindFramebuffer(GL33.GL_FRAMEBUFFER, shadow_fbo); GL33.glBindFramebuffer(GL33.GL_FRAMEBUFFER, shadow_fbo);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, SHADOW_SIZE, SHADOW_SIZE); glViewport(0, 0, shadow_size, shadow_size);
GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, Camera.camera.projection_sun.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, Camera.camera.projection_sun.getArray());
GL33.glUniform1i(Main.window.glsl_mode, 1);
LayerGen layergen = Main.world.getLayer().layergen; LayerGen layergen = Main.world.getLayer().layergen;
double sunPosition = MathHelpers.mod(layergen.getSunPosition(), 360); double sunPosition = MathHelpers.mod(layergen.getSunPosition(), 360);
if(layergen.hasSun() && sunPosition > 0 && sunPosition < 180) { if(layergen.hasSun() && sunPosition > 0 && sunPosition < 180 && shadow_size != 1) {
Main.world.render(camera); Main.world.render(camera);
} }
@ -123,6 +127,7 @@ public class DisplayRender
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, Camera.camera.projection.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, Camera.camera.projection.getArray());
GL33.glUniform1i(Main.window.glsl_mode, 0);
Main.world.render(camera); Main.world.render(camera);
player.chunk = Main.world.getLayer().getChunk(player.getPos().xz()); player.chunk = Main.world.getLayer().getChunk(player.getPos().xz());

View File

@ -40,14 +40,17 @@ public class DisplayWindow implements IMainloopTask
public int effect_vao; public int effect_vao;
public int glsl_mist;
public int glsl_color; public int glsl_color;
public int glsl_contrast; public int glsl_contrast;
public int glsl_tex_cut; public int glsl_tex_cut;
public int glsl_model; public int glsl_model;
public int glsl_camera;
public int glsl_projection; public int glsl_projection;
public int glsl_projection_sun; public int glsl_projection_sun;
public int glsl_rotated; public int glsl_rotated;
public int glsl_billboard; public int glsl_billboard;
public int glsl_mode;
public int glsl_time; public int glsl_time;
public int glsl_day_low; public int glsl_day_low;
@ -122,7 +125,9 @@ public class DisplayWindow implements IMainloopTask
environmentRenderer = new GraphicsShader("/resources/shader/environmentRenderer"); environmentRenderer = new GraphicsShader("/resources/shader/environmentRenderer");
environmentRenderer.use(); environmentRenderer.use();
glsl_mist = GL33.glGetUniformLocation(environmentRenderer.program, "mist");
glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model"); glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model");
glsl_camera = GL33.glGetUniformLocation(environmentRenderer.program, "camera");
glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated"); glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated");
glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection"); glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection");
glsl_projection_sun = GL33.glGetUniformLocation(environmentRenderer.program, "projection_sun"); glsl_projection_sun = GL33.glGetUniformLocation(environmentRenderer.program, "projection_sun");
@ -131,6 +136,7 @@ public class DisplayWindow implements IMainloopTask
glsl_color = GL33.glGetUniformLocation(environmentRenderer.program, "color"); glsl_color = GL33.glGetUniformLocation(environmentRenderer.program, "color");
glsl_contrast = GL33.glGetUniformLocation(environmentRenderer.program, "contrast"); glsl_contrast = GL33.glGetUniformLocation(environmentRenderer.program, "contrast");
glsl_billboard = GL33.glGetUniformLocation(environmentRenderer.program, "billboard"); glsl_billboard = GL33.glGetUniformLocation(environmentRenderer.program, "billboard");
glsl_mode = GL33.glGetUniformLocation(environmentRenderer.program, "mode");
glsl_day_low = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_low"); glsl_day_low = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_low");
glsl_day_high = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_high"); glsl_day_high = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_high");
@ -191,6 +197,8 @@ public class DisplayWindow implements IMainloopTask
environmentRenderer.use(); environmentRenderer.use();
DisplayLighting.updateLighting(); DisplayLighting.updateLighting();
GL33.glUniform1f(glsl_mist, 0);
if(Main.player.getHydration() < 0.2) { if(Main.player.getHydration() < 0.2) {
GL33.glUniform1f(glsl_contrast, (float)(0.2 - Main.player.getHydration()) * 1.6f); GL33.glUniform1f(glsl_contrast, (float)(0.2 - Main.player.getHydration()) * 1.6f);
} else { } else {

View File

@ -138,10 +138,6 @@ public class EntityPlayer extends Entity implements
@Override @Override
public double getLightLevel() public double getLightLevel()
{ {
if(Main.menu.playerEmitsLight) {
return getLightWithHeight(1);
}
ItemStack item = inventory.getItem(inventory_hand); ItemStack item = inventory.getItem(inventory_hand);
if(!item.isEmpty()) { if(!item.isEmpty()) {

View File

@ -8,7 +8,6 @@ public abstract class Menu
public boolean doGameRender; public boolean doGameRender;
public boolean keepMouse = true; public boolean keepMouse = true;
public boolean showIngameGUI = true; public boolean showIngameGUI = true;
public boolean playerEmitsLight = false;
public Input input; public Input input;
public abstract void render(); public abstract void render();

View File

@ -40,7 +40,6 @@ public class MenuInventory extends Menu
doGameloop = parent.doGameloop; doGameloop = parent.doGameloop;
doGameRender = parent.doGameRender; doGameRender = parent.doGameRender;
playerEmitsLight = parent.playerEmitsLight;
showIngameGUI = parent.showIngameGUI; showIngameGUI = parent.showIngameGUI;
keepMouse = false; keepMouse = false;

View File

@ -17,7 +17,6 @@ public class MenuMain extends Menu
this.doGameRender = true; this.doGameRender = true;
this.keepMouse = false; this.keepMouse = false;
this.showIngameGUI = false; this.showIngameGUI = false;
this.playerEmitsLight = true;
this.gui = new GUI(); this.gui = new GUI();
this.input = new InputGUI(gui); this.input = new InputGUI(gui);

View File

@ -29,7 +29,6 @@ public class MenuSettings extends Menu
doGameloop = false; doGameloop = false;
doGameRender = menuOld.doGameRender; doGameRender = menuOld.doGameRender;
showIngameGUI = menuOld.showIngameGUI; showIngameGUI = menuOld.showIngameGUI;
playerEmitsLight = menuOld.playerEmitsLight;
keepMouse = false; keepMouse = false;
GUIButtonGroup group = new GUIButtonGroup(); GUIButtonGroup group = new GUIButtonGroup();

View File

@ -57,10 +57,10 @@ public class ModelGui extends Model
height = y; height = y;
return new float[] { return new float[] {
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
x, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0, x, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
x, y, 0, 1, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0, x, y, 0, 1, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
0, y, 0, 0, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0, 0, y, 0, 0, 1, 0, 0, 1, 0, 0, 0, asi, asp, o, 0, 0b10000,
}; };
} }

View File

@ -165,7 +165,7 @@ public class LayerGenEarth extends LayerGen
@Override @Override
public void spawnEntities(Layer layer, Random rand) public void spawnEntities(Layer layer, Random rand)
{ {
if(rand.nextDouble() > 0.98 && getEarthLight() < -0.3) if(rand.nextDouble() > 0.98 && getEarthLight() < 0.4)
{ {
Vec3d ppos = Main.player.getPos(); Vec3d ppos = Main.player.getPos();
@ -202,7 +202,7 @@ public class LayerGenEarth extends LayerGen
} }
private double getEarthLight() { private double getEarthLight() {
return MathHelpers.sin(((MathHelpers.TWO_PI * (GameTimer.getTime() % 720000)) / 72000.0) + MathHelpers.PI/4); return MathHelpers.sin(((MathHelpers.TWO_PI * (GameTimer.getTime() % 720000)) / 72000.0) + MathHelpers.PI/4) * 0.5 + 0.5;
} }
@Override @Override
@ -218,13 +218,13 @@ public class LayerGenEarth extends LayerGen
@Override @Override
public ColorRange getLightLevel() public ColorRange getLightLevel()
{ {
double light = getEarthLight(); double light = getEarthLight() * 2 - 1;
ColorRange daylightRange = new ColorRange( ColorRange daylightRange = new ColorRange(
new Vec3d(31/255.0, 15/255.0, 0), new Vec3d(-255/255.0, -244/255.0, -208/255.0),
new Vec3d(205/255.0, 191/255.0, 162/255.0)); new Vec3d(205/255.0, 191/255.0, 162/255.0));
Vec3d daylight = daylightRange.getColor(light); Vec3d daylight = daylightRange.getColor(MathHelpers.sin(light) * 0.5 + 0.5);
return new ColorRange(daylight.multiply(7/8.0), daylight); return new ColorRange(daylight.multiply(7/8.0), daylight);
} }
} }

View File

@ -4,6 +4,7 @@ in vec3 pPos;
in vec3 pTexture; in vec3 pTexture;
in vec3 pLightMapPos; in vec3 pLightMapPos;
in vec3 pSunDepth; in vec3 pSunDepth;
in float pCameraDepth;
flat in int pFlags; flat in int pFlags;
flat in float pFade; flat in float pFade;
@ -25,6 +26,7 @@ uniform vec2 tex_cut;
uniform vec4 color; uniform vec4 color;
uniform float contrast; uniform float contrast;
uniform int mode;
vec3 color_grass_hot_wet = vec3(0.05, 0.8, 0); vec3 color_grass_hot_wet = vec3(0.05, 0.8, 0);
vec3 color_grass_hot_dry = vec3(1, 0.6, 0); vec3 color_grass_hot_dry = vec3(1, 0.6, 0);
@ -65,39 +67,37 @@ vec3 interpolate2RGB(float x, float y, vec3 v00, vec3 v01, vec3 v10, vec3 v11) {
void main() void main()
{ {
vec4 light = texture(lightmap, vec2( vec4 textureRGB = texture(atlas, pTexture);
map(pLightMapPos.x, lightmap_offset.x, lightmap_offset.x + lightmap_size.x, 0, 1),
map(pLightMapPos.z, lightmap_offset.y, lightmap_offset.y + lightmap_size.y, 0, 1)));
vec2 sunDepthTexPos = pSunDepth.xy * 0.5 + 0.5; if(mode == 0)
float shadowAmount = 0; {
vec4 light = texture(lightmap, vec2(
//shadowAmount += pSunDepth.z < (texture(depthmap, sunDepthTexPos + vec2( 0.5, 0.5) / 2048.0).r * 2 - 1 + depth_c) ? 1 : 0; map(pLightMapPos.x, lightmap_offset.x, lightmap_offset.x + lightmap_size.x, 0, 1),
//shadowAmount += pSunDepth.z < (texture(depthmap, sunDepthTexPos + vec2( 0.5, -0.5) / 2048.0).r * 2 - 1 + depth_c) ? 1 : 0; map(pLightMapPos.z, lightmap_offset.y, lightmap_offset.y + lightmap_size.y, 0, 1)));
//shadowAmount += pSunDepth.z < (texture(depthmap, sunDepthTexPos + vec2(-0.5, 0.5) / 2048.0).r * 2 - 1 + depth_c) ? 1 : 0;
//shadowAmount += pSunDepth.z < (texture(depthmap, sunDepthTexPos + vec2(-0.5, -0.5) / 2048.0).r * 2 - 1 + depth_c) ? 1 : 0; vec2 sunDepthTexPos = pSunDepth.xy * 0.5 + 0.5;
shadowAmount += pSunDepth.z < (texture(depthmap, sunDepthTexPos).r * 2 - 1 + depth_c) ? 1 : 0;
vec3 light_day = mapVec(shadowAmount, 0, 1, lighting_day_low, lighting_day_high);
vec3 light_src = vec3(1, 1, 1) * (scaleLight(max(0, light.g)) - abs(pLightMapPos.y) * 0.1);
vec4 rgb = vec4((pRGB % 64) / 64.0, ((pRGB >> 6) % 64) / 64.0, ((pRGB >> 12) % 64) / 64.0, 1);
vec4 color_grass = vec4(interpolate2RGB(
smoothStep(light.b), smoothStep(light.a),
color_grass_cold_dry, color_grass_hot_dry,
color_grass_cold_wet, color_grass_hot_wet), 1);
float saturation = contrast * 1.6 + 1; vec3 light_day = pSunDepth.z < (texture(depthmap, sunDepthTexPos).r * 2 - 1 + depth_c
) ? lighting_day_high : lighting_day_low;
FragColor = texture(atlas, pTexture) * (mod(int(pFlags / 4), 2) == 1 ? color_grass : vec4(1,1,1,1)) vec3 light_src = vec3(1, 1, 1) * (scaleLight(max(0, light.g)) - abs(pLightMapPos.y) * 0.1);
* color * vec4(biggest(light_day, light_src), pFade) * (mod(int(pFlags / 2), 2) == 1 ? rgb : vec4(1, 1, 1, 1)) vec4 rgb = vec4((pRGB % 64) / 64.0, ((pRGB >> 6) % 64) / 64.0, ((pRGB >> 12) % 64) / 64.0, 1);
* saturation + contrast;
vec4 color_grass = vec4(interpolate2RGB(
smoothStep(light.b), smoothStep(light.a),
color_grass_cold_dry, color_grass_hot_dry,
color_grass_cold_wet, color_grass_hot_wet), 1);
float saturation = contrast * 1.6 + 1;
float fog = pCameraDepth;
FragColor.r = min(1, FragColor.r); FragColor = (fog + (1 - fog) * textureRGB * (mod(int(pFlags / 4), 2) == 1 ? color_grass : vec4(1,1,1,1)) * color)
FragColor.g = min(1, FragColor.g); * vec4(biggest(light_day, light_src), pFade) * (mod(int(pFlags / 2), 2) == 1 ? rgb : vec4(1, 1, 1, 1))
FragColor.b = min(1, FragColor.b); * saturation + contrast;
FragColor.r = min(1, FragColor.r);
FragColor.g = min(1, FragColor.g);
FragColor.b = min(1, FragColor.b);
}
discard(FragColor.a == 0 || (pPos.x > tex_cut.y && tex_cut.x > 0.5)); discard(textureRGB.a == 0 || (pPos.x > tex_cut.y && tex_cut.x > 0.5));
} }

View File

@ -7,6 +7,7 @@ layout (location = 3) in vec3 aOffset;
layout (location = 4) in vec2 aAnimate; layout (location = 4) in vec2 aAnimate;
layout (location = 5) in vec3 aFlags; layout (location = 5) in vec3 aFlags;
out float pCameraDepth;
out vec3 pSunDepth; out vec3 pSunDepth;
out vec3 pLightMapPos; out vec3 pLightMapPos;
out vec3 pTexture; out vec3 pTexture;
@ -16,12 +17,15 @@ flat out int pFlags;
flat out float pFade; flat out float pFade;
flat out int pRGB; flat out int pRGB;
uniform mat4 camera;
uniform mat4 projection_sun; uniform mat4 projection_sun;
uniform mat4 billboard; uniform mat4 billboard;
uniform mat4 projection; uniform mat4 projection;
uniform mat4 model; uniform mat4 model;
uniform mat4 rotated; uniform mat4 rotated;
uniform float mist;
uniform int time; uniform int time;
uniform int mode;
float map(float x, float in_min, float in_max, float out_min, float out_max) { float map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
@ -57,15 +61,19 @@ float getTexY()
void main() void main()
{ {
int type = int(aFlags.z); int type = int(aFlags.z);
pRGB = int(aFlags.y);
pFade = aFlags.x;
vec4 pos = vec4(aPos, 1) * (mod(type >> 3, 2) == 1 ? billboard : (mod(type, 2) == 1 ? rotated : mat4(1))) * vec4 pos = vec4(aPos, 1) * (mod(type >> 3, 2) == 1 ? billboard : (mod(type, 2) == 1 ? rotated : mat4(1))) *
translate(aOffset) * model; translate(aOffset) * model;
gl_Position = pos * projection; gl_Position = pos * projection;
pSunDepth = (pos * projection_sun).xyz;
pLightMapPos = pos.xyz; if(mode == 0) {
pCameraDepth = mod(type >> 4, 2) == 1 ? 0 : min(1, mist * (-(pos * camera).z * 0.5 + 0.5));
pSunDepth = (pos * projection_sun).xyz;
pLightMapPos = pos.xyz;
pRGB = int(aFlags.y);
pFade = aFlags.x;
}
pTexture = vec3(aTex.x, getTexY(), aTex.z); pTexture = vec3(aTex.x, getTexY(), aTex.z);
pFlags = type; pFlags = type;