mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 20:31:51 +09:00
tiling using shader, sorta works
This commit is contained in:
@@ -2,12 +2,19 @@
|
|||||||
#ifdef GL_ES
|
#ifdef GL_ES
|
||||||
precision mediump float;
|
precision mediump float;
|
||||||
#endif
|
#endif
|
||||||
|
#extension GL_EXT_gpu_shader4 : enable
|
||||||
|
|
||||||
layout(origin_upper_left) in vec4 gl_FragCoord;
|
//layout(origin_upper_left) in vec4 gl_FragCoord; // commented; requires #version 150 or later
|
||||||
|
// gl_FragCoord is origin to bottom-left
|
||||||
|
|
||||||
varying vec4 v_color;
|
varying vec4 v_color;
|
||||||
varying vec2 v_texCoords;
|
varying vec2 v_texCoords;
|
||||||
|
uniform sampler2D u_texture;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uniform vec2 tilemapSize;
|
||||||
|
uniform sampler2D tilemap; // MUST be RGBA8888
|
||||||
|
|
||||||
uniform sampler2D tilesAtlas;
|
uniform sampler2D tilesAtlas;
|
||||||
uniform sampler2D backgroundTexture;
|
uniform sampler2D backgroundTexture;
|
||||||
@@ -17,30 +24,52 @@ uniform vec2 atlasTexSize = vec2(4096, 4096);
|
|||||||
|
|
||||||
|
|
||||||
uniform vec2 tileInDim; // vec2(tiles_in_horizontal, tiles_in_vertical)
|
uniform vec2 tileInDim; // vec2(tiles_in_horizontal, tiles_in_vertical)
|
||||||
uniform vec2 cameraTranslation = vec2(0, 0);
|
uniform vec2 cameraTranslation = vec2(0, 0); // Y-flipped
|
||||||
uniform float tileSizeInPx = 16;
|
uniform float tileSizeInPx = 16;
|
||||||
|
|
||||||
|
|
||||||
uniform float tilemap[tileInDim.x * tileInDim.y]; // must be float array
|
|
||||||
|
|
||||||
|
ivec2 getTileXY(int tileNumber) {
|
||||||
|
return ivec2(tileNumber % int(tileInAtlas.x), tileNumber / int(tileInAtlas.x));
|
||||||
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|
||||||
vec2 pxCoord = gl_FragCoord.xy - cameraTranslation;
|
// READ THE FUCKING MANUAL, YOU DONKEY !! //
|
||||||
vec2 pxCoordModTilesize = mod(pxCoord, tileSizeInPx);
|
// Without further code in either GDX or this shader, //
|
||||||
vec2 tileCoord = floor(pxCoord / tileCoord);
|
// Onscreen TILE COORD WILL BE UPSIDE DOWN (bottom first). //
|
||||||
|
// This is intended behaviour. //
|
||||||
int absoluteTileCoord = int(tileCoord.x + tileCoord.y * tileInDim.x);
|
|
||||||
|
|
||||||
|
|
||||||
float tile = tilemap[absoluteTileCoord]; // sure it's integer at this point
|
vec2 pxCoord = gl_FragCoord.xy + cameraTranslation;
|
||||||
vec2 fragCoordInAtlas = vec2(
|
|
||||||
tileSizeInPx * mod(tile, tileInAtlas.x) + pxCoordModTilesize.x,
|
int tile = 0;// uses usual absolute tile ID for atlas (upper-left); sample from texture2D(tileAtlas, some more code);
|
||||||
tileSizeInPx * floor(tile / tileInAtlas.x) + pxCoordModTilesize.y
|
ivec2 tileXY = getTileXY(tile);
|
||||||
);
|
|
||||||
vec2 fragCoordUV = vec2(fragCoordInAtlas.x / atlasTexSize.x, 1 - fragCoordInAtlas.y / atlasTexSize.y);
|
vec2 coordInTile = mod(pxCoord, tileSizeInPx) / tileSizeInPx; // 0..1 regardless of tile position in atlas
|
||||||
vec4 fragInAtlas = texture2D(tilesAtlas, fragCoordUV);
|
|
||||||
|
// flip Y of coordInTile //
|
||||||
|
coordInTile = vec2(coordInTile.x, 1 - coordInTile.y);
|
||||||
|
|
||||||
|
highp vec2 singleTileSizeInUV = vec2(1) / tileInAtlas; // 0.00390625
|
||||||
|
highp vec2 uvCoordForTile = coordInTile * singleTileSizeInUV; // 0..0.00390625 regardless of tile position in atlas
|
||||||
|
|
||||||
|
highp vec2 uvCoordOffset = tileXY * singleTileSizeInUV; // where the tile starts in the atlas, using uv coord (0..1)
|
||||||
|
|
||||||
|
highp vec2 finalUVCoordForTile = uvCoordForTile + uvCoordOffset;// where we should be actually looking for in atlas, using UV coord (0..1)
|
||||||
|
|
||||||
|
|
||||||
gl_FragColor = fragInAtlas;
|
gl_FragColor = vec4(texture2D(tilesAtlas, finalUVCoordForTile));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//gl_FragColor = fragInAtlas;
|
||||||
|
//gl_FragColor = vec4((gl_FragCoord.xy / vec2(512, 512)), 0, 1.0);
|
||||||
|
|
||||||
|
//vec4 atlascol = texture2D(tilesAtlas, v_texCoords);
|
||||||
|
//vec4 tilemapcol = texture2D(tilemap, v_texCoords);
|
||||||
|
|
||||||
|
//gl_FragColor = atlascol * tilemapcol;
|
||||||
|
//gl_FragColor = vec4(v_texCoords.x, v_texCoords.y, 0, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,28 @@ import com.badlogic.gdx.Screen
|
|||||||
import com.badlogic.gdx.graphics.Color
|
import com.badlogic.gdx.graphics.Color
|
||||||
import com.badlogic.gdx.graphics.GL20
|
import com.badlogic.gdx.graphics.GL20
|
||||||
import com.badlogic.gdx.graphics.OrthographicCamera
|
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||||
import com.badlogic.gdx.graphics.Texture
|
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||||
import com.badlogic.gdx.utils.GdxRuntimeException
|
|
||||||
import net.torvald.terrarum.langpack.Lang
|
import net.torvald.terrarum.langpack.Lang
|
||||||
|
|
||||||
object ErrorDisp : Screen {
|
object ErrorDisp : Screen {
|
||||||
|
|
||||||
val logoTex = TerrarumAppLoader.logo
|
private val logoTex = TerrarumAppLoader.logo
|
||||||
val font = TerrarumAppLoader.fontGame
|
private val font = TerrarumAppLoader.fontGame
|
||||||
|
|
||||||
|
|
||||||
var title = Lang["ERROR_GENERIC_TEXT"]
|
var title = Lang["ERROR_GENERIC_TEXT"]
|
||||||
var text = arrayListOf<String>("")
|
var text: List<String> = arrayListOf<String>("")
|
||||||
|
|
||||||
lateinit var batch: SpriteBatch
|
private lateinit var batch: SpriteBatch
|
||||||
|
|
||||||
|
|
||||||
val textPosX = 45f
|
private val textPosX = 45f
|
||||||
|
|
||||||
lateinit var camera: OrthographicCamera
|
lateinit var camera: OrthographicCamera
|
||||||
|
|
||||||
|
|
||||||
val titleTextLeftMargin = 8
|
private val titleTextLeftMargin = 8
|
||||||
val titleText = "${TerrarumAppLoader.GAME_NAME} ${TerrarumAppLoader.getVERSION_STRING()}"
|
private val titleText = "${TerrarumAppLoader.GAME_NAME} ${TerrarumAppLoader.getVERSION_STRING()}"
|
||||||
|
|
||||||
|
|
||||||
override fun show() {
|
override fun show() {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ 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.ShaderProgram
|
import com.badlogic.gdx.graphics.glutils.ShaderProgram
|
||||||
import net.torvald.terrarum.gameactors.ceilInt
|
import net.torvald.terrarum.gameactors.ceilInt
|
||||||
|
import net.torvald.terrarum.worlddrawer.BlocksDrawer
|
||||||
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
|
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,13 +41,15 @@ object GlslTilingTest : ApplicationAdapter() {
|
|||||||
val TILE_SIZE = 16
|
val TILE_SIZE = 16
|
||||||
|
|
||||||
|
|
||||||
lateinit var tilesBuffer: FloatArray
|
lateinit var tilesBuffer: Pixmap
|
||||||
|
|
||||||
|
lateinit var tileAtlas: Texture
|
||||||
|
|
||||||
|
|
||||||
override fun create() {
|
override fun create() {
|
||||||
ShaderProgram.pedantic = false
|
ShaderProgram.pedantic = false
|
||||||
|
|
||||||
shader = ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/loadingCircle.frag"))
|
shader = ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/tiling.frag"))
|
||||||
|
|
||||||
|
|
||||||
font = GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", flipY = false)
|
font = GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", flipY = false)
|
||||||
@@ -54,6 +57,10 @@ object GlslTilingTest : ApplicationAdapter() {
|
|||||||
|
|
||||||
if (!shader.isCompiled) {
|
if (!shader.isCompiled) {
|
||||||
Gdx.app.log("Shader", shader.log)
|
Gdx.app.log("Shader", shader.log)
|
||||||
|
|
||||||
|
//ErrorDisp.title = "Error in shader ${shader.vertexShaderSource}"
|
||||||
|
//ErrorDisp.text = shader.log.split('\n')
|
||||||
|
//TerrarumAppLoader.getINSTANCE().setScreen(ErrorDisp)
|
||||||
System.exit(1)
|
System.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,15 +77,15 @@ object GlslTilingTest : ApplicationAdapter() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
tilesQuad.setVertices(floatArrayOf(
|
tilesQuad.setVertices(floatArrayOf(
|
||||||
0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, tilesInVertical,
|
0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, 1f,
|
||||||
Gdx.graphics.width.toFloat(), 0f, 0f, 1f, 1f, 1f, 1f, tilesInHorizontal, tilesInVertical,
|
Gdx.graphics.width.toFloat(), 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f,
|
||||||
Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, tilesInHorizontal, 0f,
|
Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 1f, 0f,
|
||||||
0f, Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 0f, 0f
|
0f, Gdx.graphics.height.toFloat(), 0f, 1f, 1f, 1f, 1f, 0f, 0f
|
||||||
))
|
))
|
||||||
tilesQuad.setIndices(shortArrayOf(0, 1, 2, 2, 3, 0))
|
tilesQuad.setIndices(shortArrayOf(0, 1, 2, 2, 3, 0))
|
||||||
|
|
||||||
|
|
||||||
tilesBuffer = FloatArray(tilesInHorizontal.ceilInt() * tilesInVertical.ceilInt())
|
tilesBuffer = Pixmap(tilesInHorizontal.ceilInt(), tilesInVertical.ceilInt(), Pixmap.Format.RGBA8888)
|
||||||
|
|
||||||
|
|
||||||
camera = OrthographicCamera(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
|
camera = OrthographicCamera(Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
|
||||||
@@ -89,39 +96,50 @@ object GlslTilingTest : ApplicationAdapter() {
|
|||||||
batch = SpriteBatch()
|
batch = SpriteBatch()
|
||||||
|
|
||||||
fucktex = Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"))
|
fucktex = Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"))
|
||||||
|
|
||||||
|
tileAtlas = Texture(Gdx.files.internal("assets/terrain.tga"))//BlocksDrawer.tilesTerrain.texture
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
println(tilesBuffer.format)
|
||||||
|
// 0brrrrrrrr_gggggggg_bbbbbbbb_aaaaaaaa
|
||||||
|
for (x in 0 until tilesBuffer.width * tilesBuffer.height) {
|
||||||
|
val color = Color(0f, 1f/16f, 0f, 1f)
|
||||||
|
tilesBuffer.drawPixel(x / tilesBuffer.width, x % tilesBuffer.width, 0x00ff00ff)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun render() {
|
override fun render() {
|
||||||
Gdx.graphics.setTitle("ShitOnGlsl — F: ${Gdx.graphics.framesPerSecond}")
|
Gdx.graphics.setTitle("GlslTilingTest — F: ${Gdx.graphics.framesPerSecond}")
|
||||||
|
|
||||||
Gdx.gl.glClearColor(.094f, .094f, .094f, 0f)
|
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight())
|
||||||
|
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
|
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)
|
||||||
|
|
||||||
|
val tilesInHorizontal = Gdx.graphics.width.toFloat() / TILE_SIZE
|
||||||
|
val tilesInVertical = Gdx.graphics.height.toFloat() / TILE_SIZE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
batch.inUse {
|
val tilesBufferAsTex = Texture(tilesBuffer)
|
||||||
|
tilesBufferAsTex.bind(2)
|
||||||
|
tileAtlas.bind(1) // for some fuck reason, it must be bound as last
|
||||||
|
|
||||||
batch.shader = shader
|
shader.begin()
|
||||||
|
shader.setUniformMatrix("u_projTrans", batch.projectionMatrix)//camera.combined)
|
||||||
shader.setUniformMatrix("u_projTrans", camera.combined)
|
shader.setUniformi("tilesAtlas", 1)
|
||||||
shader.setUniformi("u_texture", 0)
|
shader.setUniformi("tilemap", 2)
|
||||||
shader.setUniform1fv("tilesBuffer", tilesBuffer, 0, tilesBuffer.size)
|
shader.setUniformf("tilemapSize", tilesBuffer.width.toFloat(), tilesBuffer.height.toFloat())
|
||||||
//tilesQuad.render(shader, GL20.GL_TRIANGLES)
|
shader.setUniformf("tileInDim", tilesInHorizontal, tilesInVertical)
|
||||||
|
shader.setUniformf("cameraTranslation", 4f, 1f)
|
||||||
batch.draw(fucktex, 0f, 0f, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*shader.begin()
|
|
||||||
shader.setUniformMatrix("u_projTrans", batch.projectionMatrix)
|
|
||||||
shader.setUniformi("u_texture", 0)
|
|
||||||
shader.setUniformf("circleCentrePoint", Gdx.graphics.width / 2f, Gdx.graphics.height / 2f)
|
|
||||||
shader.setUniformf("colorCentrePoint", Gdx.graphics.width / 2f, Gdx.graphics.height / 2f)
|
|
||||||
shader.setUniformf("circleSize", 200f)
|
|
||||||
tilesQuad.render(shader, GL20.GL_TRIANGLES)
|
tilesQuad.render(shader, GL20.GL_TRIANGLES)
|
||||||
shader.end()*/
|
shader.end()
|
||||||
|
tilesBufferAsTex.dispose()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun dispose() {
|
override fun dispose() {
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ object Terrarum : Screen {
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
private val localeSimple = arrayOf("de", "en", "es", "it")
|
private val localeSimple = arrayOf("de", "en", "es", "it")
|
||||||
var gameLocale = "lateinit"
|
var gameLocale = "lateinit" // TODO move into AppLoader
|
||||||
set(value) {
|
set(value) {
|
||||||
if (value.isBlank() || value.isEmpty()) {
|
if (value.isBlank() || value.isEmpty()) {
|
||||||
field = sysLang
|
field = sysLang
|
||||||
|
|||||||
@@ -17,8 +17,20 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack;
|
|||||||
*/
|
*/
|
||||||
public class TerrarumAppLoader implements ApplicationListener {
|
public class TerrarumAppLoader implements ApplicationListener {
|
||||||
|
|
||||||
|
private static TerrarumAppLoader INSTANCE = null;
|
||||||
|
|
||||||
|
private TerrarumAppLoader() { }
|
||||||
|
|
||||||
|
public static TerrarumAppLoader getINSTANCE() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
INSTANCE = new TerrarumAppLoader();
|
||||||
|
}
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
public static final String GAME_NAME = "Terrarum";
|
public static final String GAME_NAME = "Terrarum";
|
||||||
public static final String COPYRIGHT_DATE_NAME = "Copyright 2013-2017 Torvald (minjaesong)";
|
public static final String COPYRIGHT_DATE_NAME = "Copyright 2013-2017 Torvald (minjaesong)";
|
||||||
|
public static final String GAME_LOCALE = System.getProperty("user.language") + System.getProperty("user.country");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 0xAA_BB_XXXX
|
* 0xAA_BB_XXXX
|
||||||
@@ -62,9 +74,6 @@ public class TerrarumAppLoader implements ApplicationListener {
|
|||||||
|
|
||||||
public Screen screen;
|
public Screen screen;
|
||||||
|
|
||||||
private TerrarumAppLoader() {
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initViewPort(int width, int height) {
|
private void initViewPort(int width, int height) {
|
||||||
// Set Y to point downwards
|
// Set Y to point downwards
|
||||||
camera.setToOrtho(true, width, height);
|
camera.setToOrtho(true, width, height);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.torvald.terrarum.langpack
|
|||||||
|
|
||||||
import net.torvald.terrarum.utils.JsonFetcher
|
import net.torvald.terrarum.utils.JsonFetcher
|
||||||
import net.torvald.terrarum.Terrarum
|
import net.torvald.terrarum.Terrarum
|
||||||
|
import net.torvald.terrarum.TerrarumAppLoader
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@@ -111,7 +112,7 @@ object Lang {
|
|||||||
fun fallback(): String = langpack["${key}_$FALLBACK_LANG_CODE"] ?: "$$key"
|
fun fallback(): String = langpack["${key}_$FALLBACK_LANG_CODE"] ?: "$$key"
|
||||||
|
|
||||||
|
|
||||||
val ret = langpack["${key}_${Terrarum.gameLocale}"]
|
val ret = langpack["${key}_${TerrarumAppLoader.GAME_LOCALE}"]
|
||||||
val ret2 = if (ret.isNullOrEmpty()) fallback() else ret!!
|
val ret2 = if (ret.isNullOrEmpty()) fallback() else ret!!
|
||||||
|
|
||||||
// special treatment
|
// special treatment
|
||||||
|
|||||||
Reference in New Issue
Block a user