Allow downscaling the resolution of the framebuffer.

fixes #99
This commit is contained in:
IntegratedQuantum 2024-06-03 21:45:18 +02:00
parent 6293db0ff8
commit 9cacd64c4d
4 changed files with 24 additions and 6 deletions

View File

@ -54,7 +54,7 @@ void main() {
fragColor = texture(color, texCoords);
fragColor += texture(bloomColor, texCoords);
float densityAdjustment = sqrt(dot(tanXY*(texCoords*2 - 1), tanXY*(texCoords*2 - 1)) + 1);
float fogDistance = calculateFogDistance(texelFetch(depthTexture, ivec2(gl_FragCoord.xy), 0).r, fog.density*densityAdjustment);
float fogDistance = calculateFogDistance(texture(depthTexture, texCoords).r, fog.density*densityAdjustment);
fragColor.rgb = applyFrontfaceFog(fogDistance, fog.color, fragColor.rgb);
float maxColor = max(1.0, max(fragColor.r, max(fragColor.g, fragColor.b)));
fragColor.rgb = fragColor.rgb/maxColor;

View File

@ -19,6 +19,8 @@ pub var window = GuiWindow {
const padding: f32 = 8;
const renderDistances = [_]u16{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
const resolutions = [_]u16{25, 50, 100};
fn renderDistanceCallback(newValue: u16) void {
settings.renderDistance = newValue + renderDistances[0];
}
@ -41,12 +43,18 @@ fn anisotropicFilteringCallback(newValue: bool) void {
// TODO: Reload the textures.
}
fn resolutionScaleCallback(newValue: u16) void {
settings.resolutionScale = @as(i32, newValue) - 2;
main.Window.GLFWCallbacks.framebufferSize(null, main.Window.width, main.Window.height);
}
pub fn onOpen() void {
const list = VerticalList.init(.{padding, 16 + padding}, 300, 16);
list.add(DiscreteSlider.init(.{0, 0}, 128, "#ffffffRender Distance: ", "{}", &renderDistances, settings.renderDistance - renderDistances[0], &renderDistanceCallback));
list.add(CheckBox.init(.{0, 0}, 128, "Bloom", settings.bloom, &bloomCallback));
list.add(CheckBox.init(.{0, 0}, 128, "Vertical Synchronization", settings.vsync, &vsyncCallback));
list.add(CheckBox.init(.{0, 0}, 128, "Anisotropic Filtering", settings.anisotropicFiltering, &anisotropicFilteringCallback));
list.add(DiscreteSlider.init(.{0, 0}, 128, "#ffffffResolution Scale: ", "{}", &resolutions, @intCast(settings.resolutionScale + 2), &resolutionScaleCallback));
list.finish(.center);
window.rootComponent = list.toComponent();
window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding));

View File

@ -113,12 +113,17 @@ var lastWidth: u31 = 0;
var lastHeight: u31 = 0;
var lastFov: f32 = 0;
pub fn updateViewport(width: u31, height: u31, fov: f32) void {
lastWidth = width;
lastHeight = height;
if(main.settings.resolutionScale < 0) {
lastWidth = width >> @intCast(-main.settings.resolutionScale);
lastHeight = height >> @intCast(-main.settings.resolutionScale);
} else {
lastWidth = width;
lastHeight = height;
}
lastFov = fov;
c.glViewport(0, 0, width, height);
game.projectionMatrix = Mat4f.perspective(std.math.degreesToRadians(fov), @as(f32, @floatFromInt(width))/@as(f32, @floatFromInt(height)), zNear, zFar);
worldFrameBuffer.updateSize(width, height, c.GL_RGB16F);
c.glViewport(0, 0, lastWidth, lastHeight);
game.projectionMatrix = Mat4f.perspective(std.math.degreesToRadians(fov), @as(f32, @floatFromInt(lastWidth))/@as(f32, @floatFromInt(lastHeight)), zNear, zFar);
worldFrameBuffer.updateSize(lastWidth, lastHeight, c.GL_RGB16F);
worldFrameBuffer.unbind();
}
@ -137,6 +142,7 @@ pub fn render(playerPosition: Vec3d) void {
const startTime = std.time.milliTimestamp();
mesh_storage.updateMeshes(startTime + maximumMeshTime);
} else {
c.glViewport(0, 0, main.Window.width, main.Window.height);
MenuBackGround.render();
}
}
@ -166,6 +172,7 @@ pub fn crosshairDirection(rotationMatrix: Mat4f, fovY: f32, width: u31, height:
pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPos: Vec3d) void {
worldFrameBuffer.bind();
c.glViewport(0, 0, lastWidth, lastHeight);
gpu_performance_measuring.startQuery(.clear);
worldFrameBuffer.clear(Vec4f{skyColor[0], skyColor[1], skyColor[2], 1});
gpu_performance_measuring.stopQuery();
@ -262,6 +269,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo
Bloom.bindReplacementImage();
}
gpu_performance_measuring.startQuery(.final_copy);
c.glViewport(0, 0, main.Window.width, main.Window.height);
worldFrameBuffer.bindTexture(c.GL_TEXTURE3);
worldFrameBuffer.bindDepthTexture(c.GL_TEXTURE4);
worldFrameBuffer.unbind();

View File

@ -24,6 +24,8 @@ pub var mouseSensitivity: f32 = 1;
pub var renderDistance: u16 = 7;
pub var resolutionScale: i32 = 0;
pub var bloom: bool = true;
pub var vsync: bool = true;