shader seems working!; it's messed up by all the test codes but I commit anyway

This commit is contained in:
minjaesong
2017-07-05 02:05:03 +09:00
parent cf2932a519
commit 3af196004e
8 changed files with 284 additions and 132 deletions

View File

@@ -2,40 +2,50 @@ package net.torvald.terrarum
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
import com.badlogic.gdx.graphics.OrthographicCamera
/**
* Created by minjaesong on 2017-06-11.
*/
class TestTestTest : ApplicationAdapter() {
class TestTestTest(val batch: SpriteBatch) : Screen {
lateinit var batch: SpriteBatch
lateinit var img: Texture
lateinit var gameFont: BitmapFont
lateinit var blurShader: ShaderProgram
lateinit var blurFboA: FrameBuffer
lateinit var blurFboB: FrameBuffer
lateinit var cam: OrthographicCamera
lateinit var worldFbo: FrameBuffer
lateinit var camera: OrthographicCamera
// invert Y
fun initViewPort(width: Int, height: Int) {
// Set Y to point downwards
camera.setToOrtho(true, width.toFloat(), height.toFloat())
// Update camera matrix
camera.update()
// Set viewport to restrict drawing
Gdx.gl20.glViewport(0, 0, width, height)
}
fun enter() {
// init view port
camera = OrthographicCamera(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
override fun create() {
batch = SpriteBatch()
img = Texture("assets/test_texture.tga")
gameFont = GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap")
@@ -45,20 +55,21 @@ class TestTestTest : ApplicationAdapter() {
blurFboA = FrameBuffer(Pixmap.Format.RGBA8888, img.width, img.height, false)
blurFboB = FrameBuffer(Pixmap.Format.RGBA8888, img.width, img.height, false)
ShaderProgram.pedantic = false
blurShader = ShaderProgram(Gdx.files.internal("assets/blur.vert"), Gdx.files.internal("assets/blur.frag"))
worldFbo = FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.width, Gdx.graphics.height, false)
blurShader.begin()
blurShader.setUniformf("iResolution", img.width.toFloat(), img.height.toFloat(), 0f)
blurShader.end()
//blurShader.begin()
//blurShader.setUniformf("iResolution", img.width.toFloat(), img.height.toFloat(), 0f)
//blurShader.end()
cam = OrthographicCamera(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
cam.setToOrtho(false)
initViewPort(Gdx.graphics.width, Gdx.graphics.height)
}
override fun render() {
val iterations = 16
override fun render(delta: Float) {
Gdx.graphics.setTitle("TestTestTest — F: ${Gdx.graphics.framesPerSecond}")
val iterations = 16 // ideally, 4 * radius; must be even number -- odd number will flip the image
val radius = 4f
@@ -66,12 +77,12 @@ class TestTestTest : ApplicationAdapter() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
blurFboA.inAction {
blurFboA.inAction(null, null) {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
}
blurFboB.inAction {
blurFboB.inAction(null, null) {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
}
@@ -82,12 +93,9 @@ class TestTestTest : ApplicationAdapter() {
for (i in 0..iterations - 1) {
writeBuffer.inAction {
writeBuffer.inAction(camera, batch) {
batch.inUse {
cam.setToOrtho(false, writeBuffer.width.toFloat(), writeBuffer.height.toFloat())
batch.projectionMatrix = cam.combined
val texture = if (i == 0)
img
else
@@ -95,7 +103,9 @@ class TestTestTest : ApplicationAdapter() {
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
batch.shader = blurShader
batch.shader = TestTestMain.blurShader
batch.shader.setUniformf("iResolution", writeBuffer.width.toFloat(), writeBuffer.height.toFloat())
batch.shader.setUniformf("flip", 1f)
if (i % 2 == 0)
batch.shader.setUniformf("direction", radius, 0f)
@@ -103,7 +113,7 @@ class TestTestTest : ApplicationAdapter() {
batch.shader.setUniformf("direction", 0f, radius)
batch.color = Color.WHITE
batch.draw(texture, 0f, 0f)
@@ -115,16 +125,57 @@ class TestTestTest : ApplicationAdapter() {
}
}
// draw last FBO to screen
batch.inUse {
cam.setToOrtho(false, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
batch.projectionMatrix = cam.combined
worldFbo.inAction(camera, batch) {
Gdx.gl.glClearColor(0f,0f,0f,0f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
batch.inUse {
batch.shader = null
camera.position.set(Gdx.graphics.width / 2f - 50f, Gdx.graphics.height / 2f - 50f, 0f)
camera.update()
batch.projectionMatrix = camera.combined
batch.shader.setUniformf("direction", 0f, 0f)
batch.shader.setUniformf("flip", if (iterations % 2 != 0) 1f else 0f)
batch.draw(writeBuffer.colorBufferTexture, 0f, 0f)
batch.color = Color.WHITE
batch.draw(writeBuffer.colorBufferTexture, 0f, 0f)
}
}
camera.setToOrtho(true, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
batch.projectionMatrix = camera.combined
batch.inUse {
camera.position.set(Gdx.graphics.width / 2f, Gdx.graphics.height / 2f, 0f)
camera.update()
batch.projectionMatrix = camera.combined
batch.color = Color.WHITE
batch.draw(worldFbo.colorBufferTexture, 0f, 0f)
batch.draw(img, 0f, 0f, 100f, 100f)
}
}
override fun hide() {
}
override fun show() {
initViewPort(Gdx.graphics.width, Gdx.graphics.height)
}
override fun pause() {
}
override fun resume() {
}
override fun resize(width: Int, height: Int) {
}
override fun dispose() {
@@ -133,18 +184,42 @@ class TestTestTest : ApplicationAdapter() {
}
private inline fun SpriteBatch.inUse(action: () -> Unit) {
this.begin()
action()
this.end()
}
object TestTestMain : ApplicationAdapter() {
lateinit var blurShader: ShaderProgram
lateinit var batch: SpriteBatch
lateinit var currentScreen: TestTestTest
override fun create() {
ShaderProgram.pedantic = false
blurShader = ShaderProgram(Gdx.files.internal("assets/blur.vert"), Gdx.files.internal("assets/blur.frag"))
// culprit #4
blurShader.begin()
blurShader.setUniformf("dir", 0f, 0f); //direction of blur; nil for now
blurShader.setUniformf("resolution", maxOf(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())) //size of FBO texture
blurShader.setUniformf("radius", 9f) //radius of blur
blurShader.end()
Gdx.graphics.isContinuousRendering = true // culprit #3
batch = SpriteBatch()
currentScreen = TestTestTest(batch)
currentScreen.enter()
}
inline fun FrameBuffer.inAction(action: (FrameBuffer) -> Unit) {
this.begin()
action(this)
this.end()
override fun render() {
currentScreen.render(Gdx.graphics.deltaTime)
}
override fun dispose() {
currentScreen.dispose()
}
}
fun main(args: Array<String>) { // LWJGL 3 won't work? java.lang.VerifyError
@@ -155,5 +230,5 @@ fun main(args: Array<String>) { // LWJGL 3 won't work? java.lang.VerifyError
config.width = 1072
config.height = 742
config.foregroundFPS = 9999
LwjglApplication(TestTestTest(), config)
LwjglApplication(TestTestMain, config)
}