Ladies and Gents, we have shader-powered smooth lighting!

This commit is contained in:
minjaesong
2017-07-05 02:20:10 +09:00
parent f5d229105c
commit 4d04aadc58
3 changed files with 49 additions and 140 deletions

View File

@@ -371,8 +371,6 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen {
} }
val testTex = Texture("assets/test_texture.tga")
private fun renderGame(batch: SpriteBatch) { private fun renderGame(batch: SpriteBatch) {
Gdx.gl.glClearColor(.157f, .157f, .157f, 0f) Gdx.gl.glClearColor(.157f, .157f, .157f, 0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
@@ -397,8 +395,8 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen {
/////////////////// ///////////////////
// blur lightmap // // blur lightmap //
/////////////////// ///////////////////
val blurIterations = 16 // ideally, 4 * radius; must be even number -- odd number will flip the image val blurIterations = 5 // ideally, 4 * radius; must be even/odd number -- odd/even number will flip the image
val blurRadius = 4f 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 var blurReadBuffer = lightmapFboB
KeyToggler.forceSet(Input.Keys.F6, false)
KeyToggler.forceSet(Input.Keys.F7, true)
// initialise readBuffer with untreated lightmap // initialise readBuffer with untreated lightmap
@@ -433,21 +429,16 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen {
blendNormal() blendNormal()
batch.color = Color.WHITE batch.color = Color.WHITE
//LightmapRenderer.draw(batch) LightmapRenderer.draw(batch)
batch.draw(testTex, 0f, 0f)
} }
} }
if (TerrarumGDX.getConfigBoolean("smoothlighting")) {
for (i in 0..blurIterations - 1) { for (i in 0..blurIterations - 1) {
blurWriteBuffer.inAction(camera, batch) { blurWriteBuffer.inAction(camera, batch) {
batch.inUse { batch.inUse {
val texture = if (i == 0) val texture = blurReadBuffer.colorBufferTexture
testTex
else
blurReadBuffer.colorBufferTexture
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
@@ -472,15 +463,10 @@ class StateInGameGDX(val batch: SpriteBatch) : Screen {
} }
} }
} }
// 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) setCameraPosition(0f, 0f)
val lightTex = blurWriteBuffer.colorBufferTexture // TODO zoom! val lightTex = blurWriteBuffer.colorBufferTexture // TODO zoom!
if (KeyToggler.isOn(Input.Keys.F7)) blendNormal() if (KeyToggler.isOn(KEY_LIGHTMAP_RENDER)) blendNormal()
else blendMul() else blendMul()
batch.color = Color.WHITE batch.color = Color.WHITE
//batch.draw(lightTex, 0f, 0f, lightTex.width * lightmapDownsample, lightTex.height * lightmapDownsample) 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)
} }

View File

@@ -43,9 +43,9 @@ fun main(args: Array<String>) {
config.foregroundFPS = TerrarumGDX.RENDER_FPS config.foregroundFPS = TerrarumGDX.RENDER_FPS
config.backgroundFPS = TerrarumGDX.RENDER_FPS config.backgroundFPS = TerrarumGDX.RENDER_FPS
//config.vSyncEnabled = true //config.vSyncEnabled = true
config.resizable = false config.resizable = true
config.width = 512//1072 config.width = 1072
config.height = 512//742 config.height = 742
config.backgroundFPS = 9999 config.backgroundFPS = 9999
config.foregroundFPS = 9999 config.foregroundFPS = 9999
//config.useGL30 = true //config.useGL30 = true

View File

@@ -335,79 +335,6 @@ object LightmapRenderer {
// loop x // loop x
var x = this_x_start var x = this_x_start
while (x < this_x_end) { 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) {
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
}
}
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
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()
)
}
}
}
}
// smoothing disabled
else {
try { try {
val thisLightLevel = getLightForOpaque(x, y) val thisLightLevel = getLightForOpaque(x, y)
@@ -433,7 +360,6 @@ object LightmapRenderer {
// do nothing // do nothing
} }
}
x++ x++
} }