camera moving works

This commit is contained in:
minjaesong
2017-08-28 02:27:53 +09:00
parent 05560cf04c
commit 75192bef17
2 changed files with 11 additions and 9 deletions

View File

@@ -13,7 +13,7 @@ uniform sampler2D u_texture;
uniform vec2 screenDimension; uniform vec2 screenDimension;
uniform vec2 tilesInAxes; uniform vec2 tilesInAxes; // vec2(tiles_in_horizontal, tiles_in_vertical)
uniform ivec2 tilemapDimension; uniform ivec2 tilemapDimension;
uniform sampler2D tilemap; // MUST be RGBA8888 uniform sampler2D tilemap; // MUST be RGBA8888
@@ -25,7 +25,6 @@ uniform ivec2 tileInAtlas = ivec2(256, 256);
uniform ivec2 atlasTexSize = ivec2(4096, 4096); 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 vec2 cameraTranslation = vec2(0, 0); // Y-flipped
uniform int tileSizeInPx = 16; uniform int tileSizeInPx = 16;
@@ -46,15 +45,12 @@ void main() {
// Make sure you don't use gl_FragCoord unknowingly! // // 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? // 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. // // This one, however, takes exact integer by rounding down. //
vec2 overscannedScreenDimension = tilesInAxes * tileSizeInPx; // one used by the tileFromMap 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 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 mediump vec4 tileFromMap = texture2D(tilemap, flippedFragCoord / overscannedScreenDimension); // <- THE CULPRIT
int tile = getTileFromColor(tileFromMap); int tile = getTileFromColor(tileFromMap);
@@ -65,10 +61,10 @@ void main() {
vec2 coordInTile = mod(pxCoord, tileSizeInPx) / tileSizeInPx; // 0..1 regardless of tile position in atlas 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 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) highp vec2 finalUVCoordForTile = uvCoordForTile + uvCoordOffset;// where we should be actually looking for in atlas, using UV coord (0..1)

View File

@@ -108,6 +108,9 @@ object GlslTilingTest : ApplicationAdapter() {
} }
var cameraX = 0f
var cameraY = 0f
override fun render() { override fun render() {
Gdx.graphics.setTitle("GlslTilingTest — F: ${Gdx.graphics.framesPerSecond}") Gdx.graphics.setTitle("GlslTilingTest — F: ${Gdx.graphics.framesPerSecond}")
@@ -149,11 +152,14 @@ object GlslTilingTest : ApplicationAdapter() {
shader.setUniformi("tilemap", 2) shader.setUniformi("tilemap", 2)
shader.setUniformi("tilemapDimension", tilesBuffer.width, tilesBuffer.height) shader.setUniformi("tilemapDimension", tilesBuffer.width, tilesBuffer.height)
shader.setUniformf("tilesInAxes", tilesInHorizontal, tilesInVertical) shader.setUniformf("tilesInAxes", tilesInHorizontal, tilesInVertical)
shader.setUniformf("cameraTranslation", 0f, 0f) shader.setUniformf("cameraTranslation", cameraX, cameraY)
tilesQuad.render(shader, GL20.GL_TRIANGLES) tilesQuad.render(shader, GL20.GL_TRIANGLES)
shader.end() shader.end()
tilesBufferAsTex.dispose() tilesBufferAsTex.dispose()
cameraX += 160 * Gdx.graphics.deltaTime
cameraY += 160 * Gdx.graphics.deltaTime
} }
override fun dispose() { override fun dispose() {