From 75192bef17c5b014055ccf69a413ae4e478ecb62 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 28 Aug 2017 02:27:53 +0900 Subject: [PATCH] camera moving works --- assets/tiling.frag | 12 ++++-------- src/net/torvald/terrarum/GlslTilingTest.kt | 8 +++++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/assets/tiling.frag b/assets/tiling.frag index ee35c5b3f..0f9691544 100644 --- a/assets/tiling.frag +++ b/assets/tiling.frag @@ -13,7 +13,7 @@ uniform sampler2D u_texture; uniform vec2 screenDimension; -uniform vec2 tilesInAxes; +uniform vec2 tilesInAxes; // vec2(tiles_in_horizontal, tiles_in_vertical) uniform ivec2 tilemapDimension; uniform sampler2D tilemap; // MUST be RGBA8888 @@ -25,7 +25,6 @@ uniform ivec2 tileInAtlas = ivec2(256, 256); uniform ivec2 atlasTexSize = ivec2(4096, 4096); -//uniform vec2 tileInDim; // vec2(tiles_in_horizontal, tiles_in_vertical) uniform vec2 cameraTranslation = vec2(0, 0); // Y-flipped uniform int tileSizeInPx = 16; @@ -46,15 +45,12 @@ void main() { // Make sure you don't use gl_FragCoord unknowingly! // - // FIXME: cameraTranslation not working as it should - - // default gl_FragCoord takes half-integer (represeting centre of the pixel) -- could be useful for phys solver? // This one, however, takes exact integer by rounding down. // vec2 overscannedScreenDimension = tilesInAxes * tileSizeInPx; // one used by the tileFromMap vec2 flippedFragCoord = vec2(gl_FragCoord.x, screenDimension.y - gl_FragCoord.y); // NO IVEC2!!; this flips Y - vec2 pxCoord = flippedFragCoord.xy + cameraTranslation; + vec2 pxCoord = flippedFragCoord.xy; mediump vec4 tileFromMap = texture2D(tilemap, flippedFragCoord / overscannedScreenDimension); // <- THE CULPRIT int tile = getTileFromColor(tileFromMap); @@ -65,10 +61,10 @@ void main() { vec2 coordInTile = mod(pxCoord, tileSizeInPx) / tileSizeInPx; // 0..1 regardless of tile position in atlas - highp vec2 singleTileSizeInUV = vec2(1) / tileInAtlas; // 0.00390625 + highp vec2 singleTileSizeInUV = vec2(1) / tileInAtlas; // constant 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 uvCoordOffset = (tileXY + cameraTranslation / tileSizeInPx) * 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) diff --git a/src/net/torvald/terrarum/GlslTilingTest.kt b/src/net/torvald/terrarum/GlslTilingTest.kt index afd69d094..fbc8aa9cd 100644 --- a/src/net/torvald/terrarum/GlslTilingTest.kt +++ b/src/net/torvald/terrarum/GlslTilingTest.kt @@ -108,6 +108,9 @@ object GlslTilingTest : ApplicationAdapter() { } + var cameraX = 0f + var cameraY = 0f + override fun render() { Gdx.graphics.setTitle("GlslTilingTest — F: ${Gdx.graphics.framesPerSecond}") @@ -149,11 +152,14 @@ object GlslTilingTest : ApplicationAdapter() { shader.setUniformi("tilemap", 2) shader.setUniformi("tilemapDimension", tilesBuffer.width, tilesBuffer.height) shader.setUniformf("tilesInAxes", tilesInHorizontal, tilesInVertical) - shader.setUniformf("cameraTranslation", 0f, 0f) + shader.setUniformf("cameraTranslation", cameraX, cameraY) tilesQuad.render(shader, GL20.GL_TRIANGLES) shader.end() tilesBufferAsTex.dispose() + + cameraX += 160 * Gdx.graphics.deltaTime + cameraY += 160 * Gdx.graphics.deltaTime } override fun dispose() {