diff --git a/src/net/torvald/terrarum/FuckingWorldRenderer.kt b/src/net/torvald/terrarum/FuckingWorldRenderer.kt index 05af18d5d..2962af989 100644 --- a/src/net/torvald/terrarum/FuckingWorldRenderer.kt +++ b/src/net/torvald/terrarum/FuckingWorldRenderer.kt @@ -29,6 +29,8 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { var camera = OrthographicCamera(Terrarum.WIDTH.toFloat(), Terrarum.HEIGHT.toFloat()) + private val processBlurBatch = SpriteBatch() + // invert Y fun initViewPort(width: Int, height: Int) { // Set Y to point downwards @@ -100,8 +102,9 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { private val gradWhiteBottom = Color(0xd8d8d8ff.toInt()) private val lightFBOformat = Pixmap.Format.RGB888 - var lightmapFboA = FrameBuffer(lightFBOformat, Terrarum.WIDTH.div(Ingame.lightmapDownsample.toInt()), Terrarum.HEIGHT.div(Ingame.lightmapDownsample.toInt()), false) - var lightmapFboB = FrameBuffer(lightFBOformat, Terrarum.WIDTH.div(Ingame.lightmapDownsample.toInt()), Terrarum.HEIGHT.div(Ingame.lightmapDownsample.toInt()), false) + lateinit var lightmapFboA: FrameBuffer + lateinit var lightmapFboB: FrameBuffer + private var lightmapInitialised = false // to avoid nullability of lightmapFBO lateinit var logo: TextureRegion @@ -110,6 +113,9 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { private lateinit var worldFBO: FrameBuffer + private val TILE_SIZE = FeaturesDrawer.TILE_SIZE + private val TILE_SIZEF = TILE_SIZE.toFloat() + private fun loadThingsWhileIntroIsVisible() { demoWorld = ReadLayerData(FileInputStream(ModMgr.getFile("basegame", "demoworld"))) @@ -172,8 +178,6 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { worldFBO = FrameBuffer(Pixmap.Format.RGBA8888, Terrarum.WIDTH, Terrarum.HEIGHT, false) } - private var blurWriteBuffer = lightmapFboA - private var blurReadBuffer = lightmapFboB private val introUncoverTime: Second = 0.3f private var introUncoverDeltaCounter = 0f @@ -216,7 +220,7 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { uiContainer.forEach { it.update(delta) } - + if (TerrarumAppLoader.GLOBAL_RENDER_TIMER % 2 == 1) { LightmapRendererNew.fireRecalculateEvent() } @@ -224,8 +228,7 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { fun renderScreen() { - // render and blur lightmap - ///////////processBlur(LightmapRendererNew.DRAW_FOR_RGB) + processBlur(LightmapRendererNew.DRAW_FOR_RGB) //camera.setToOrtho(true, Terrarum.WIDTH.toFloat(), Terrarum.HEIGHT.toFloat()) // render world @@ -291,24 +294,43 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { FeaturesDrawer.drawEnvOverlay(batch) + /////////////////// // draw lightmap // + /////////////////// + setCameraPosition(0f, 0f) + //batch.shader = Terrarum.shaderBayer //batch.shader.setUniformf("rcount", 64f) //batch.shader.setUniformf("gcount", 64f) //batch.shader.setUniformf("bcount", 64f) // de-banding - //val lightTex = blurWriteBuffer.colorBufferTexture - //lightTex.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) - //blendMul() - blendNormal() batch.shader = null + + + //processBlur(LightmapRendererNew.DRAW_FOR_RGB) + + val lightTex = lightmapFboB.colorBufferTexture // A or B? flipped in Y means you chose wrong buffer; use one that works correctly + lightTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) // blocky feeling for A E S T H E T I C S + + blendMul() + batch.color = Color.WHITE - /*batch.draw(lightTex, - 0f, 0f, - //lightTex.width * Ingame.lightmapDownsample, lightTex.height * Ingame.lightmapDownsample - lightTex.width.toFloat(), lightTex.height.toFloat() - )*/ - LightmapRendererNew.draw(batch, LightmapRendererNew.DRAW_FOR_RGB) + val xrem = -(WorldCamera.x % TILE_SIZEF) + val yrem = -(WorldCamera.y % TILE_SIZEF) + batch.draw(lightTex, + if (xrem == 0f) -TILE_SIZEF else xrem, + if (yrem == 0f) -TILE_SIZEF else yrem, + lightTex.width * Ingame.lightmapDownsample, lightTex.height * Ingame.lightmapDownsample + //lightTex.width.toFloat(), lightTex.height.toFloat() // for debugging + ) + // FIXME dae fucking jitter + + + + ////////////////////// + // Draw other shits // + ////////////////////// + batch.shader = null @@ -374,11 +396,23 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { uiMenu.setPosition(0, UITitleRemoConRoot.menubarOffY) } - lightmapFboA.dispose() - lightmapFboA = FrameBuffer(lightFBOformat, Terrarum.WIDTH.div(Ingame.lightmapDownsample.toInt()), Terrarum.HEIGHT.div(Ingame.lightmapDownsample.toInt()), false) - lightmapFboB.dispose() - lightmapFboB = FrameBuffer(lightFBOformat, Terrarum.WIDTH.div(Ingame.lightmapDownsample.toInt()), Terrarum.HEIGHT.div(Ingame.lightmapDownsample.toInt()), false) - + if (lightmapInitialised) { + lightmapFboA.dispose() + lightmapFboB.dispose() + } + lightmapFboA = FrameBuffer( + lightFBOformat, + LightmapRendererNew.lightBuffer.width * LightmapRendererNew.DRAW_TILE_SIZE.toInt(), + LightmapRendererNew.lightBuffer.height * LightmapRendererNew.DRAW_TILE_SIZE.toInt(), + false + ) + lightmapFboB = FrameBuffer( + lightFBOformat, + LightmapRendererNew.lightBuffer.width * LightmapRendererNew.DRAW_TILE_SIZE.toInt(), + LightmapRendererNew.lightBuffer.height * LightmapRendererNew.DRAW_TILE_SIZE.toInt(), + false + ) + lightmapInitialised = true // are you the first time? } override fun dispose() { @@ -400,8 +434,8 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { val blurIterations = 5 // ideally, 4 * radius; must be even/odd number -- odd/even number will flip the image val blurRadius = 4f / Ingame.lightmapDownsample // (5, 4f); using low numbers for pixel-y aesthetics - blurWriteBuffer = lightmapFboA - blurReadBuffer = lightmapFboB + var blurWriteBuffer = lightmapFboA + var blurReadBuffer = lightmapFboB lightmapFboA.inAction(null, null) { @@ -418,17 +452,7 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { // initialise readBuffer with untreated lightmap blurReadBuffer.inAction(camera, batch) { batch.inUse { - // using custom code for camera; this is obscure and tricky - camera.position.set( - (WorldCamera.gdxCamX / Ingame.lightmapDownsample).round(), - (WorldCamera.gdxCamY / Ingame.lightmapDownsample).round(), - 0f - ) // make camara work - camera.update() - batch.projectionMatrix = camera.combined - - - blendNormal() + blendNormal(batch) batch.color = Color.WHITE LightmapRendererNew.draw(batch, LightmapRendererNew.DRAW_FOR_RGB) } @@ -438,17 +462,7 @@ class FuckingWorldRenderer(val batch: SpriteBatch) : Screen { // initialise readBuffer with untreated lightmap blurReadBuffer.inAction(camera, batch) { batch.inUse { - // using custom code for camera; this is obscure and tricky - camera.position.set( - (WorldCamera.gdxCamX / Ingame.lightmapDownsample).round(), - (WorldCamera.gdxCamY / Ingame.lightmapDownsample).round(), - 0f - ) // make camara work - camera.update() - batch.projectionMatrix = camera.combined - - - blendNormal() + blendNormal(batch) batch.color = Color.WHITE LightmapRendererNew.draw(batch, LightmapRendererNew.DRAW_FOR_ALPHA) } diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index 7cdae5f8d..3fe164f73 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -773,21 +773,21 @@ infix fun Color.mul(other: Color): Color = this.cpy().mul(other) -fun blendMul() { - Terrarum.batch.enableBlending() - Terrarum.batch.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_ONE_MINUS_SRC_ALPHA) +fun blendMul(batch: SpriteBatch? = null) { + (batch ?: Terrarum.batch).enableBlending() + (batch ?: Terrarum.batch).setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_ONE_MINUS_SRC_ALPHA) Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD) // batch.flush does not touch blend equation } -fun blendNormal() { - Terrarum.batch.enableBlending() - Terrarum.batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA) +fun blendNormal(batch: SpriteBatch? = null) { + (batch ?: Terrarum.batch).enableBlending() + (batch ?: Terrarum.batch).setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA) Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD) // batch.flush does not touch blend equation } -fun blendScreen() { - Terrarum.batch.enableBlending() - Terrarum.batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_COLOR) +fun blendScreen(batch: SpriteBatch? = null) { + (batch ?: Terrarum.batch).enableBlending() + (batch ?: Terrarum.batch).setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_COLOR) Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD) // batch.flush does not touch blend equation } diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt index 0597d2d31..b293fa71d 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt @@ -56,7 +56,7 @@ object LightmapRendererNew { private val AIR = Block.AIR private const val TILE_SIZE = FeaturesDrawer.TILE_SIZE - private val DRAW_TILE_SIZE: Float = FeaturesDrawer.TILE_SIZE / Ingame.lightmapDownsample + val DRAW_TILE_SIZE: Float = FeaturesDrawer.TILE_SIZE / Ingame.lightmapDownsample // color model related constants const val MUL = 1024 // modify this to 1024 to implement 30-bit RGB @@ -366,8 +366,8 @@ object LightmapRendererNew { Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // don't know why it is needed; it really depresses me // we might not need shader here... - batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width.toFloat(), lightBufferAsTex.height.toFloat()) - //batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width * DRAW_TILE_SIZE, lightBufferAsTex.height * TILE_SIZE.toFloat()) + //batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width.toFloat(), lightBufferAsTex.height.toFloat()) + batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width * DRAW_TILE_SIZE, lightBufferAsTex.height * DRAW_TILE_SIZE) //lightBufferAsTex.dispose()