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

View File

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