refactor scale/translate of screen to map coordinates for tile visibility calulation

cleanups
This commit is contained in:
Hannes Janetzek 2013-04-02 23:24:52 +02:00
parent bac46ac6e2
commit 93a723af3c
2 changed files with 35 additions and 61 deletions

View File

@ -108,7 +108,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
// drawing, proxies and text layer. needing to add placeholder only // drawing, proxies and text layer. needing to add placeholder only
// happens rarely, unless you live on Fidschi // happens rarely, unless you live on Fidschi
/* package */static int mHolderCount; /* package */static int mNumTileHolder;
/* package */static TileSet mDrawTiles; /* package */static TileSet mDrawTiles;
// scanline fill class used to check tile visibility // scanline fill class used to check tile visibility
@ -148,7 +148,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
if (xx < 0 || xx >= xmax) if (xx < 0 || xx >= xmax)
continue; continue;
for (int i = cnt; i < cnt + mHolderCount; i++) for (int i = cnt; i < cnt + mNumTileHolder; i++)
if (tiles[i].tileX == x && tiles[i].tileY == y) { if (tiles[i].tileX == x && tiles[i].tileY == y) {
found = true; found = true;
break; break;
@ -166,11 +166,11 @@ public class GLRenderer implements GLSurfaceView.Renderer {
if (tile == null) if (tile == null)
continue; continue;
holder = new MapTile(x, y, (byte)mZoom); holder = new MapTile(x, y, (byte) mZoom);
holder.isVisible = true; holder.isVisible = true;
holder.holder = tile; holder.holder = tile;
tile.isVisible = true; tile.isVisible = true;
tiles[cnt + mHolderCount++] = holder; tiles[cnt + mNumTileHolder++] = holder;
} }
} }
}; };
@ -311,36 +311,33 @@ public class GLRenderer implements GLSurfaceView.Renderer {
float[] coords = mTileCoords; float[] coords = mTileCoords;
MapPosition pos = mMapPosition; MapPosition pos = mMapPosition;
MapTile[] tiles = mDrawTiles.tiles; MapTile[] tiles = mDrawTiles.tiles;
// lock tiles while updating isVisible state // lock tiles while updating isVisible state
synchronized (GLRenderer.tilelock) { synchronized (GLRenderer.tilelock) {
for (int i = 0; i < mDrawTiles.cnt; i++) for (int i = 0; i < mDrawTiles.cnt; i++)
tiles[i].isVisible = false; tiles[i].isVisible = false;
// relative zoom-level, 'tiles' could not have been updated after // relative zoom-level, 'tiles' could not have been updated after
// zoom-level changed. // zoom-level changed.
byte z = tiles[0].zoomLevel; float div = FastMath.pow(pos.zoomLevel - tiles[0].zoomLevel);
float div = FastMath.pow(z - pos.zoomLevel);
// transform screen coordinates to tile coordinates // transform screen coordinates to tile coordinates
float scale = pos.scale / div; float tileScale = pos.scale * div * Tile.TILE_SIZE;
float px = (float) pos.x * div; double px = pos.x * pos.scale;
float py = (float) pos.y * div; double py = pos.y * pos.scale;
for (int i = 0; i < 8; i += 2) { for (int i = 0; i < 8; i += 2) {
coords[i + 0] = (px + coords[i + 0] / scale) / Tile.TILE_SIZE; coords[i + 0] = (float) (px + coords[i + 0]) / tileScale;
coords[i + 1] = (py + coords[i + 1] / scale) / Tile.TILE_SIZE; coords[i + 1] = (float) (py + coords[i + 1]) / tileScale;
} }
// count placeholder tiles // count placeholder tiles
mHolderCount = 0; mNumTileHolder = 0;
// check visibile tiles // check visibile tiles
mScanBox.scan(coords, z); mScanBox.scan(coords, tiles[0].zoomLevel);
} }
} }
private static void uploadTileData(MapTile tile) { private static void uploadTileData(MapTile tile) {
tile.state = STATE_READY; tile.state = STATE_READY;
@ -423,7 +420,6 @@ public class GLRenderer implements GLSurfaceView.Renderer {
if (mUpdateColor) { if (mUpdateColor) {
float cc[] = mClearColor; float cc[] = mClearColor;
GLES20.glClearColor(cc[0], cc[1], cc[2], cc[3]); GLES20.glClearColor(cc[0], cc[1], cc[2], cc[3]);
//GLES20.glClearColor(0.8f, 0.8f, 0.8f, 1);
mUpdateColor = false; mUpdateColor = false;
} }
@ -444,10 +440,8 @@ public class GLRenderer implements GLSurfaceView.Renderer {
// get current tiles to draw // get current tiles to draw
mDrawTiles = mMapView.getTileManager().getActiveTiles(mDrawTiles); mDrawTiles = mMapView.getTileManager().getActiveTiles(mDrawTiles);
// FIXME what if only drawing overlays? if (mDrawTiles == null || mDrawTiles.cnt == 0)
if (mDrawTiles == null || mDrawTiles.cnt == 0) {
return; return;
}
boolean tilesChanged = false; boolean tilesChanged = false;
// check if the tiles have changed... // check if the tiles have changed...
@ -471,6 +465,10 @@ public class GLRenderer implements GLSurfaceView.Renderer {
mMapViewPosition.getMatrix(mMatrices.view, null, mMatrices.viewproj); mMapViewPosition.getMatrix(mMatrices.view, null, mMatrices.viewproj);
if (debugView) {
mMatrices.mvp.setScale(0.5f, 0.5f, 1);
mMatrices.viewproj.multiplyMM(mMatrices.mvp, mMatrices.viewproj);
}
} }
int tileCnt = mDrawTiles.cnt; int tileCnt = mDrawTiles.cnt;
@ -479,8 +477,9 @@ public class GLRenderer implements GLSurfaceView.Renderer {
if (positionChanged) if (positionChanged)
updateTileVisibility(); updateTileVisibility();
tileCnt += mHolderCount; tileCnt += mNumTileHolder;
/* prepare tile for rendering */
compileTileLayers(tiles, tileCnt); compileTileLayers(tiles, tileCnt);
tilesChanged |= (uploadCnt > 0); tilesChanged |= (uploadCnt > 0);
@ -511,29 +510,6 @@ public class GLRenderer implements GLSurfaceView.Renderer {
Log.d(TAG, "draw took " + (SystemClock.uptimeMillis() - start)); Log.d(TAG, "draw took " + (SystemClock.uptimeMillis() - start));
} }
//if (debugView) {
// GLState.test(false, false);
//
// float mm = 0.5f;
// float min = -mm;
// float max = mm;
// float ymax = mm * mHeight / mWidth;
// mDebugCoords[0] = min;
// mDebugCoords[1] = ymax;
// mDebugCoords[2] = max;
// mDebugCoords[3] = ymax;
// mDebugCoords[4] = min;
// mDebugCoords[5] = -ymax;
// mDebugCoords[6] = max;
// mDebugCoords[7] = -ymax;
//
// PolygonRenderer.debugDraw(mMatrices.proj, mDebugCoords, 0);
//
// pos.zoomLevel = -1;
// mMapViewPosition.getMapViewProjection(mDebugCoords);
// PolygonRenderer.debugDraw(mMatrices.viewproj, mDebugCoords, 1);
//}
if (GlUtils.checkGlOutOfMemory("finish")) { if (GlUtils.checkGlOutOfMemory("finish")) {
checkBufferUsage(true); checkBufferUsage(true);
// TODO also throw out some textures etc // TODO also throw out some textures etc
@ -594,12 +570,12 @@ public class GLRenderer implements GLSurfaceView.Renderer {
mMapViewPosition.getMatrix(null, mMatrices.proj, null); mMapViewPosition.getMatrix(null, mMatrices.proj, null);
//if (debugView) { if (debugView) {
// // modify this to scale only the view, to see better which tiles are // modify this to scale only the view, to see better which tiles
// // rendered // are rendered
// mMatrices.mvp.setScale(0.5f, 0.5f, 1); mMatrices.mvp.setScale(0.5f, 0.5f, 1);
// mMatrices.proj.multiplyMM(mMatrices.mvp, mMatrices.proj); mMatrices.proj.multiplyMM(mMatrices.mvp, mMatrices.proj);
//} }
GLES20.glViewport(0, 0, width, height); GLES20.glViewport(0, 0, width, height);
GLES20.glScissor(0, 0, width, height); GLES20.glScissor(0, 0, width, height);
@ -682,7 +658,6 @@ public class GLRenderer implements GLSurfaceView.Renderer {
public static final boolean debugView = false; public static final boolean debugView = false;
void clearBuffer() { void clearBuffer() {
mNewSurface = true; mNewSurface = true;
} }

View File

@ -160,24 +160,23 @@ public class TileManager {
// jobs come in. // jobs come in.
mMapView.addJobs(null); mMapView.addJobs(null);
/* load some tiles more than currently visible */ // scale and translate projection to tile coordinates
float scale = mapPosition.scale * 0.7f; // load some tiles more than currently visible (* 0.75)
float px = (float) mapPosition.x; float scale = mapPosition.scale * 0.75f;
float py = (float) mapPosition.y; float tileScale = scale * Tile.TILE_SIZE;
double px = mapPosition.x * scale;
double py = mapPosition.y * scale;
float[] coords = mTileCoords; float[] coords = mTileCoords;
mMapViewPosition.getMapViewProjection(coords); mMapViewPosition.getMapViewProjection(coords);
// scale and translate projection to tile coordinates
for (int i = 0; i < 8; i += 2) { for (int i = 0; i < 8; i += 2) {
coords[i + 0] = (px + coords[i + 0] / scale) / Tile.TILE_SIZE; coords[i + 0] = (float)(px + coords[i + 0]) / tileScale;
coords[i + 1] = (py + coords[i + 1] / scale) / Tile.TILE_SIZE; coords[i + 1] = (float)(py + coords[i + 1]) / tileScale;
} }
mNewTiles.cnt = 0;
// scan visible tiles. callback function calls 'addTile' // scan visible tiles. callback function calls 'addTile'
// which sets mNewTiles // which sets mNewTiles
mNewTiles.cnt = 0;
mScanBox.scan(coords, mapPosition.zoomLevel); mScanBox.scan(coords, mapPosition.zoomLevel);
MapTile[] newTiles = mNewTiles.tiles; MapTile[] newTiles = mNewTiles.tiles;