From 60a3c6518f60a9ea92d38daae1140ebec5576d9e Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 5 Jul 2017 02:20:10 +0900 Subject: [PATCH] Ladies and Gents, we have shader-powered smooth lighting! --- src/net/torvald/terrarum/StateInGameGDX.kt | 73 +++++------- src/net/torvald/terrarum/TerrarumGDX.kt | 6 +- .../terrarum/worlddrawer/LightmapRenderer.kt | 110 +++--------------- 3 files changed, 49 insertions(+), 140 deletions(-) diff --git a/src/net/torvald/terrarum/StateInGameGDX.kt b/src/net/torvald/terrarum/StateInGameGDX.kt index d432494c6..546fbe5fc 100644 --- a/src/net/torvald/terrarum/StateInGameGDX.kt +++ b/src/net/torvald/terrarum/StateInGameGDX.kt @@ -371,8 +371,6 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen { } - val testTex = Texture("assets/test_texture.tga") - private fun renderGame(batch: SpriteBatch) { Gdx.gl.glClearColor(.157f, .157f, .157f, 0f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) @@ -397,8 +395,8 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen { /////////////////// // blur lightmap // /////////////////// - val blurIterations = 16 // ideally, 4 * radius; must be even number -- odd number will flip the image - val blurRadius = 4f + val blurIterations = 5 // ideally, 4 * radius; must be even/odd number -- odd/even number will flip the image + val blurRadius = 4f // (5, 4f); using low numbers for pixel-y aesthetics @@ -418,8 +416,6 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen { var blurReadBuffer = lightmapFboB - KeyToggler.forceSet(Input.Keys.F6, false) - KeyToggler.forceSet(Input.Keys.F7, true) // initialise readBuffer with untreated lightmap @@ -433,54 +429,44 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen { blendNormal() batch.color = Color.WHITE - //LightmapRenderer.draw(batch) - - batch.draw(testTex, 0f, 0f) + LightmapRenderer.draw(batch) } } + if (TerrarumGDX.getConfigBoolean("smoothlighting")) { + for (i in 0..blurIterations - 1) { + blurWriteBuffer.inAction(camera, batch) { - for (i in 0..blurIterations - 1) { - blurWriteBuffer.inAction(camera, batch) { + batch.inUse { + val texture = blurReadBuffer.colorBufferTexture - batch.inUse { - val texture = if (i == 0) - testTex - else - blurReadBuffer.colorBufferTexture - - texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) + texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) - batch.shader = TerrarumGDX.shaderBlur - batch.shader.setUniformf("iResolution", blurWriteBuffer.width.toFloat(), blurWriteBuffer.height.toFloat()) - batch.shader.setUniformf("flip", 1f) - if (i % 2 == 0) - batch.shader.setUniformf("direction", blurRadius, 0f) - else - batch.shader.setUniformf("direction", 0f, blurRadius) + batch.shader = TerrarumGDX.shaderBlur + batch.shader.setUniformf("iResolution", blurWriteBuffer.width.toFloat(), blurWriteBuffer.height.toFloat()) + batch.shader.setUniformf("flip", 1f) + if (i % 2 == 0) + batch.shader.setUniformf("direction", blurRadius, 0f) + else + batch.shader.setUniformf("direction", 0f, blurRadius) - batch.color = Color.WHITE - batch.draw(texture, 0f, 0f) + batch.color = Color.WHITE + batch.draw(texture, 0f, 0f) - // swap - val t = blurWriteBuffer - blurWriteBuffer = blurReadBuffer - blurReadBuffer = t + // swap + val t = blurWriteBuffer + blurWriteBuffer = blurReadBuffer + blurReadBuffer = t + } } } } - - - // TEST: passthru to writeBuffer - /*blurWriteBuffer.inAction(camera, batch) { - batch.inUse { - batch.color = Color.WHITE - batch.draw(testTex, 0f, 0f) - } - }*/ + else { + blurWriteBuffer = blurReadBuffer + } @@ -536,13 +522,10 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen { setCameraPosition(0f, 0f) val lightTex = blurWriteBuffer.colorBufferTexture // TODO zoom! - if (KeyToggler.isOn(Input.Keys.F7)) blendNormal() + if (KeyToggler.isOn(KEY_LIGHTMAP_RENDER)) blendNormal() else blendMul() batch.color = Color.WHITE - //batch.draw(lightTex, 0f, 0f, lightTex.width * lightmapDownsample, lightTex.height * lightmapDownsample) - - TerrarumGDX.fontGame.draw(batch, "Thumbnail:", 100f, 80f) - batch.draw(lightTex, 100f, 100f, 100f, 100f) + batch.draw(lightTex, 0f, 0f, lightTex.width * lightmapDownsample, lightTex.height * lightmapDownsample) } diff --git a/src/net/torvald/terrarum/TerrarumGDX.kt b/src/net/torvald/terrarum/TerrarumGDX.kt index bfc4e1e55..a82087324 100644 --- a/src/net/torvald/terrarum/TerrarumGDX.kt +++ b/src/net/torvald/terrarum/TerrarumGDX.kt @@ -43,9 +43,9 @@ fun main(args: Array) { config.foregroundFPS = TerrarumGDX.RENDER_FPS config.backgroundFPS = TerrarumGDX.RENDER_FPS //config.vSyncEnabled = true - config.resizable = false - config.width = 512//1072 - config.height = 512//742 + config.resizable = true + config.width = 1072 + config.height = 742 config.backgroundFPS = 9999 config.foregroundFPS = 9999 //config.useGL30 = true diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt index ba6b75d9a..f26440f72 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt @@ -335,106 +335,32 @@ object LightmapRenderer { // loop x var x = this_x_start while (x < this_x_end) { - // smoothing enabled and zoom is 0.75 or greater - // (zoom of 0.5 should not smoothed, for performance) - if (false && //TerrarumGDX.getConfigBoolean("smoothlighting") && - TerrarumGDX.ingame!!.screenZoom >= 0.75) { + try { + val thisLightLevel = getLightForOpaque(x, y) - val thisLightLevel = getLightForOpaque(x, y) ?: 0 - - if (x < this_x_end && thisLightLevel == 0 - && getLightForOpaque(x, y - 1) == 0) { - try { - // coalesce zero intensity blocks to one - var zeroLevelCounter = 1 - while (getLightForOpaque(x + zeroLevelCounter, y) == 0) { - zeroLevelCounter += 1 - - if (x + zeroLevelCounter >= this_x_end) break - } - - batch.color = Color.BLACK - batch.fillRect( - (x.toFloat() * TILE_SIZE).round().toFloat(), - (y.toFloat() * TILE_SIZE).round().toFloat(), - (TILE_SIZE * zeroLevelCounter).toFloat(), - (TILE_SIZE).toFloat() - ) - - x += zeroLevelCounter - 1 - } - catch (e: ArrayIndexOutOfBoundsException) { - // do nothing - } + // coalesce identical intensity blocks to one + var sameLevelCounter = 1 + while (getLightForOpaque(x + sameLevelCounter, y) == thisLightLevel) { + sameLevelCounter += 1 + if (x + sameLevelCounter >= this_x_end) break } - else { - /** a - * +-+-+ - * |i|j| - * b +-+-+ c - * |k|l| - * +-+-+ - * d - */ - val a = thisLightLevel maxBlend (getLightForOpaque(x, y - 1) ?: thisLightLevel) - val d = thisLightLevel maxBlend (getLightForOpaque(x, y + 1) ?: thisLightLevel) - val b = thisLightLevel maxBlend (getLightForOpaque(x - 1, y) ?: thisLightLevel) - val c = thisLightLevel maxBlend (getLightForOpaque(x + 1, y) ?: thisLightLevel) - val colourMapItoL = IntArray(4) - val colMean = (a linMix d) linMix (b linMix c) - val colDelta = thisLightLevel colSub colMean + batch.color = (getLightForOpaque(x, y) ?: 0).normaliseToColourHDR() + batch.fillRect( + (x * DRAW_TILE_SIZE).round().toFloat(), + (y * DRAW_TILE_SIZE).round().toFloat(), + (DRAW_TILE_SIZE.ceil() * sameLevelCounter).toFloat(), + DRAW_TILE_SIZE.ceil().toFloat() + ) - colourMapItoL[0] = a linMix b colAdd colDelta - colourMapItoL[1] = a linMix c colAdd colDelta - colourMapItoL[2] = b linMix d colAdd colDelta - colourMapItoL[3] = c linMix d colAdd colDelta - - for (iy in 0..1) { - for (ix in 0..1) { - batch.color = colourMapItoL[iy * 2 + ix].normaliseToColourHDR() - batch.fillRect( - (x.toFloat() * TILE_SIZE).round() - + ix * TILE_SIZE / 2f, - (y.toFloat() * TILE_SIZE).round() - + iy * TILE_SIZE / 2f, - (TILE_SIZE / 2f).ceil().toFloat(), - (TILE_SIZE / 2f).ceil().toFloat() - ) - } - } - } + x += sameLevelCounter - 1 } - // smoothing disabled - else { - try { - val thisLightLevel = getLightForOpaque(x, y) - - // coalesce identical intensity blocks to one - var sameLevelCounter = 1 - while (getLightForOpaque(x + sameLevelCounter, y) == thisLightLevel) { - sameLevelCounter += 1 - - if (x + sameLevelCounter >= this_x_end) break - } - - batch.color = (getLightForOpaque(x, y) ?: 0).normaliseToColourHDR() - batch.fillRect( - (x * DRAW_TILE_SIZE).round().toFloat(), - (y * DRAW_TILE_SIZE).round().toFloat(), - (DRAW_TILE_SIZE.ceil() * sameLevelCounter).toFloat(), - DRAW_TILE_SIZE.ceil().toFloat() - ) - - x += sameLevelCounter - 1 - } - catch (e: ArrayIndexOutOfBoundsException) { - // do nothing - } - + catch (e: ArrayIndexOutOfBoundsException) { + // do nothing } + x++ } }