I'm trying

This commit is contained in:
minjaesong
2018-06-26 09:11:01 +09:00
parent 9736d39e04
commit a1d0832b00
3 changed files with 1583 additions and 119 deletions

View File

@@ -2,7 +2,6 @@ package net.torvald.terrarum.modulebasegame
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input import com.badlogic.gdx.Input
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.* import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.FrameBuffer import com.badlogic.gdx.graphics.glutils.FrameBuffer
@@ -24,12 +23,8 @@ import net.torvald.terrarum.worlddrawer.FeaturesDrawer
import net.torvald.terrarum.worlddrawer.LightmapRenderer import net.torvald.terrarum.worlddrawer.LightmapRenderer
import net.torvald.terrarum.worlddrawer.WorldCamera import net.torvald.terrarum.worlddrawer.WorldCamera
import java.util.ArrayList
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
import javax.swing.JOptionPane
import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.OrthographicCamera
import net.torvald.random.HQRNG import net.torvald.random.HQRNG
import net.torvald.terrarum.* import net.torvald.terrarum.*
@@ -46,6 +41,8 @@ import net.torvald.terrarum.modulebasegame.ui.*
import net.torvald.terrarum.ui.* import net.torvald.terrarum.ui.*
import net.torvald.terrarum.modulebasegame.worldgenerator.RoguelikeRandomiser import net.torvald.terrarum.modulebasegame.worldgenerator.RoguelikeRandomiser
import net.torvald.terrarum.modulebasegame.worldgenerator.WorldGenerator import net.torvald.terrarum.modulebasegame.worldgenerator.WorldGenerator
import java.io.PrintStream
import java.util.*
/** /**
@@ -132,7 +129,7 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
var worldDrawFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false) var worldDrawFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false)
var worldGlowFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false) var worldGlowFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false)
var worldBlendFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false) //var worldBlendFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false)
// RGB elements of Lightmap for Color Vec4(R, G, B, 1.0) 24-bit // RGB elements of Lightmap for Color Vec4(R, G, B, 1.0) 24-bit
private lateinit var lightmapFboA: FrameBuffer private lateinit var lightmapFboA: FrameBuffer
private lateinit var lightmapFboB: FrameBuffer private lateinit var lightmapFboB: FrameBuffer
@@ -632,13 +629,6 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
worldBlendFrameBuffer.inAction(null, null) {
Gdx.gl.glClearColor(0f,0f,0f,0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glEnable(GL20.GL_TEXTURE_2D)
Gdx.gl.glEnable(GL20.GL_BLEND)
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)
}
worldDrawFrameBuffer.inAction(null, null) { worldDrawFrameBuffer.inAction(null, null) {
Gdx.gl.glClearColor(0f,0f,0f,0f) Gdx.gl.glClearColor(0f,0f,0f,0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
@@ -666,7 +656,7 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
/////////////////////////// ///////////////////////////
// draw world to the FBO // // draw world to the FBO //
/////////////////////////// ///////////////////////////
processBlur(lightmapFboA, lightmapFboB, LightmapRenderer.DRAW_FOR_RGB) prepareBlurredLightmap(lightmapFboA, lightmapFboB)
worldDrawFrameBuffer.inAction(camera, batch) { worldDrawFrameBuffer.inAction(camera, batch) {
@@ -731,33 +721,6 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
// mix lighpmap canvas to this canvas (Colors -- RGB channel)
if (!KeyToggler.isOn(Input.Keys.F6)) { // F6 to disable lightmap draw
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 = 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
if (KeyToggler.isOn(KEY_LIGHTMAP_RENDER)) blendNormal()
else blendMul()
batch.color = Color.WHITE
val xrem = -(WorldCamera.x.toFloat() fmod TILE_SIZEF)
val yrem = -(WorldCamera.y.toFloat() fmod TILE_SIZEF)
batch.draw(lightTex,
xrem,
yrem,
lightTex.width * lightmapDownsample, lightTex.height * lightmapDownsample
//lightTex.width.toFloat(), lightTex.height.toFloat() // for debugging
)
}
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // don't know why it is REALLY needed; it really depresses me Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // don't know why it is REALLY needed; it really depresses me
batch.shader = null batch.shader = null
@@ -769,14 +732,28 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
batch.projectionMatrix = camera.combined batch.projectionMatrix = camera.combined
} }
} } // let the shits flushed
worldDrawFrameBuffer.inAction(camera, batch) {
// mix lighpmap canvas to this canvas
val rgbTex = worldDrawFrameBuffer.colorBufferTexture
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
Terrarum.shaderMulRGBX.begin()
Terrarum.shaderMulRGBX.setUniformMatrix("u_projTrans", camera.combined)
Terrarum.shaderMulRGBX.setUniformi("u_texture", 0)
Terrarum.shaderMulRGBX.setUniformi("tex1", 1)
Terrarum.fullscreenQuad.render(Terrarum.shaderMulRGBX, GL20.GL_TRIANGLES)
Terrarum.shaderMulRGBX.end()
} // now worldDrawFrameBuffer has light premultiplied
////////////////////////// //////////////////////////
// draw glow to the FBO // // draw glow to the FBO //
////////////////////////// //////////////////////////
processBlur(lightmapFboA, lightmapFboB, LightmapRenderer.DRAW_FOR_ALPHA)
worldGlowFrameBuffer.inAction(camera, batch) { worldGlowFrameBuffer.inAction(camera, batch) {
batch.inUse { batch.inUse {
batch.shader = null batch.shader = null
@@ -830,44 +807,23 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
blendNormal() blendNormal()
} }
} } // let the shits flushed
worldGlowFrameBuffer.inAction(camera, batch) {
// mix lighpmap canvas to this canvas
val rgbTex = worldGlowFrameBuffer.colorBufferTexture
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
Terrarum.shaderMulAAAX.begin()
Terrarum.shaderMulAAAX.setUniformMatrix("u_projTrans", camera.combined)
Terrarum.shaderMulAAAX.setUniformi("u_texture", 0)
Terrarum.shaderMulAAAX.setUniformi("tex1", 1)
Terrarum.fullscreenQuad.render(Terrarum.shaderMulAAAX, GL20.GL_TRIANGLES)
Terrarum.shaderMulAAAX.end()
} // now worldGlowFrameBuffer has light premultiplied
worldBlendFrameBuffer.inAction(camera, batch) {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
// draw blended world
val worldTex = worldDrawFrameBuffer.colorBufferTexture // WORLD: light_color must be applied beforehand
val glowTex = worldGlowFrameBuffer.colorBufferTexture // GLOW: light_uvlight must be applied beforehand
worldTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
glowTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
worldTex.bind(0)
glowTex.bind(1)
Terrarum.shaderBlendGlow.begin()
Terrarum.shaderBlendGlow.setUniformMatrix("u_projTrans", camera.combined)
Terrarum.shaderBlendGlow.setUniformi("u_texture", 0)
Terrarum.shaderBlendGlow.setUniformi("tex1", 1)
Terrarum.fullscreenQuad.render(Terrarum.shaderBlendGlow, GL20.GL_TRIANGLES)
Terrarum.shaderBlendGlow.end()
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // don't know why it is REALLY needed; it really depresses me
batch.inUse {
batch.color = Color.WHITE
blendNormal()
batch.shader = null
}
}
///////////////////////// /////////////////////////
@@ -897,15 +853,21 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
// draw framebuffers to screen // // draw framebuffers to screen //
///////////////////////////////// /////////////////////////////////
val worldTex = worldDrawFrameBuffer.colorBufferTexture // WORLD: light_color must be applied beforehand
val glowTex = worldGlowFrameBuffer.colorBufferTexture // GLOW: light_uvlight must be applied beforehand
worldTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
glowTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
val blendedTex = worldBlendFrameBuffer.colorBufferTexture // not sure about the position test
blendedTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) setCameraPosition(0f, 0f)
batch.color = Color.WHITE
batch.shader = null batch.shader = Terrarum.shaderBlendGlow
blendNormal() Terrarum.shaderBlendGlow.setUniformi("u_texture", 0)
batch.draw(blendedTex, 0f, 0f, blendedTex.width.toFloat(), blendedTex.height.toFloat()) batch.shader.setUniformi("tex1", 1)
worldTex.bind(0)
glowTex.bind(1)
batch.draw(worldTex, 0f, 0f)
@@ -1000,7 +962,7 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
} }
} }
fun processBlur(lightmapFboA: FrameBuffer, lightmapFboB: FrameBuffer, mode: Int) { fun prepareBlurredLightmap(lightmapFboA: FrameBuffer, lightmapFboB: FrameBuffer) {
val blurIterations = 5 // ideally, 4 * radius; must be even/odd number -- odd/even 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 / lightmapDownsample // (5, 4f); using low numbers for pixel-y aesthetics val blurRadius = 4f / lightmapDownsample // (5, 4f); using low numbers for pixel-y aesthetics
@@ -1018,24 +980,12 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
} }
if (mode == LightmapRenderer.DRAW_FOR_RGB) { // initialise readBuffer with untreated lightmap
// initialise readBuffer with untreated lightmap blurReadBuffer.inAction(camera, batch) {
blurReadBuffer.inAction(camera, batch) { batch.inUse {
batch.inUse { blendNormal(batch)
blendNormal(batch) batch.color = Color.WHITE
batch.color = Color.WHITE LightmapRenderer.draw(batch, 0)
LightmapRenderer.draw(batch, LightmapRenderer.DRAW_FOR_RGB)
}
}
}
else {
// initialise readBuffer with untreated lightmap
blurReadBuffer.inAction(camera, batch) {
batch.inUse {
blendNormal(batch)
batch.color = Color.WHITE
LightmapRenderer.draw(batch, LightmapRenderer.DRAW_FOR_ALPHA)
}
} }
} }
@@ -1419,8 +1369,6 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
worldDrawFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false) worldDrawFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false)
worldGlowFrameBuffer.dispose() worldGlowFrameBuffer.dispose()
worldGlowFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false) worldGlowFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false)
worldBlendFrameBuffer.dispose()
worldBlendFrameBuffer = FrameBuffer(worldFBOformat, Terrarum.WIDTH, Terrarum.HEIGHT, false)
if (lightmapInitialised) { if (lightmapInitialised) {
lightmapFboA.dispose() lightmapFboA.dispose()
@@ -1478,7 +1426,6 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
override fun dispose() { override fun dispose() {
worldDrawFrameBuffer.dispose() worldDrawFrameBuffer.dispose()
worldGlowFrameBuffer.dispose() worldGlowFrameBuffer.dispose()
worldBlendFrameBuffer.dispose()
lightmapFboA.dispose() lightmapFboA.dispose()
lightmapFboB.dispose() lightmapFboB.dispose()

File diff suppressed because it is too large Load Diff

View File

@@ -310,7 +310,7 @@ object LightmapRenderer {
lateinit var lightBuffer: Pixmap lateinit var lightBuffer: Pixmap
fun draw(batch: SpriteBatch, drawMode: Int) { fun draw(batch: SpriteBatch, donotuse: Int) {
val this_x_start = for_x_start// + overscan_open val this_x_start = for_x_start// + overscan_open
val this_x_end = for_x_end// + overscan_open val this_x_end = for_x_end// + overscan_open
@@ -327,15 +327,7 @@ object LightmapRenderer {
for (x in this_x_start..this_x_end) { for (x in this_x_start..this_x_end) {
val color = if (drawMode == DRAW_FOR_RGB) { val color = (getLightForOpaque(x, y) ?: Color(0f,0f,0f,0f)).normaliseToRGBAHDR()
(getLightForOpaque(x, y) ?: Color(0f,0f,0f,0f)).normaliseToColourHDR()
}
else if (drawMode == DRAW_FOR_ALPHA) {
(getLightForOpaque(x, y) ?: Color(0f,0f,0f,0f)).normaliseToAlphaHDR()
}
else {
throw IllegalArgumentException()
}
lightBuffer.setColor(color) lightBuffer.setColor(color)
@@ -659,6 +651,13 @@ object LightmapRenderer {
1f 1f
) )
inline fun Color.normaliseToRGBAHDR() = Color(
hdr(this.r),
hdr(this.g),
hdr(this.b),
hdr(this.a)
)
/** /**
* color values are normalised -- 0.0 to 1.0 for 0..1023 * color values are normalised -- 0.0 to 1.0 for 0..1023
*/ */