simplify LayerRenderer, move setMatrix to ElementRenderer
This commit is contained in:
@@ -20,12 +20,12 @@ public class CustomRenderLayer extends Layer {
|
|||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
currentState = someConccurentVariable;
|
currentState = someConccurentVariable;
|
||||||
|
compile();
|
||||||
}
|
}
|
||||||
Log.d(TAG, "state " + currentState);
|
Log.d(TAG, "state " + currentState);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void compile() {
|
protected void compile() {
|
||||||
setReady(true);
|
setReady(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,14 @@ package org.oscim.renderer;
|
|||||||
|
|
||||||
import org.oscim.backend.GL20;
|
import org.oscim.backend.GL20;
|
||||||
import org.oscim.core.MapPosition;
|
import org.oscim.core.MapPosition;
|
||||||
|
import org.oscim.core.Tile;
|
||||||
import org.oscim.renderer.MapRenderer.Matrices;
|
import org.oscim.renderer.MapRenderer.Matrices;
|
||||||
import org.oscim.renderer.elements.BitmapLayer;
|
import org.oscim.renderer.elements.BitmapLayer;
|
||||||
import org.oscim.renderer.elements.RenderElement;
|
|
||||||
import org.oscim.renderer.elements.ElementLayers;
|
import org.oscim.renderer.elements.ElementLayers;
|
||||||
import org.oscim.renderer.elements.LineLayer;
|
import org.oscim.renderer.elements.LineLayer;
|
||||||
import org.oscim.renderer.elements.LineTexLayer;
|
import org.oscim.renderer.elements.LineTexLayer;
|
||||||
import org.oscim.renderer.elements.PolygonLayer;
|
import org.oscim.renderer.elements.PolygonLayer;
|
||||||
|
import org.oscim.renderer.elements.RenderElement;
|
||||||
import org.oscim.renderer.elements.TextureLayer;
|
import org.oscim.renderer.elements.TextureLayer;
|
||||||
import org.oscim.utils.FastMath;
|
import org.oscim.utils.FastMath;
|
||||||
|
|
||||||
@@ -31,10 +32,18 @@ import org.oscim.utils.FastMath;
|
|||||||
*/
|
*/
|
||||||
public abstract class ElementRenderer extends LayerRenderer {
|
public abstract class ElementRenderer extends LayerRenderer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use mMapPosition.copy(position) to keep the position for which
|
||||||
|
* the Overlay is _compiled_. NOTE: required by setMatrix utility
|
||||||
|
* functions to draw this layer fixed to the map
|
||||||
|
*/
|
||||||
|
protected MapPosition mMapPosition;
|
||||||
|
|
||||||
public final ElementLayers layers;
|
public final ElementLayers layers;
|
||||||
|
|
||||||
public ElementRenderer() {
|
public ElementRenderer() {
|
||||||
layers = new ElementLayers();
|
layers = new ElementLayers();
|
||||||
|
mMapPosition = new MapPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,7 +98,11 @@ public abstract class ElementRenderer extends LayerRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
|
* Compiles all layers into one BufferObject. Sets renderer to be ready
|
||||||
|
* when successful. When no data is available (layer.getSize() == 0) then
|
||||||
|
* BufferObject will be released and layers will not be rendered.
|
||||||
|
*/
|
||||||
protected void compile() {
|
protected void compile() {
|
||||||
int newSize = layers.getSize();
|
int newSize = layers.getSize();
|
||||||
if (newSize <= 0) {
|
if (newSize <= 0) {
|
||||||
@@ -105,4 +118,50 @@ public abstract class ElementRenderer extends LayerRenderer {
|
|||||||
if (MapRenderer.uploadLayers(layers, newSize, true))
|
if (MapRenderer.uploadLayers(layers, newSize, true))
|
||||||
setReady(true);
|
setReady(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility: Set matrices.mvp matrix relative to the difference of current
|
||||||
|
* MapPosition and the last updated Overlay MapPosition.
|
||||||
|
* Use this to 'stick' your layer to the map. Note: Vertex coordinates
|
||||||
|
* are assumed to be scaled by MapRenderer.COORD_SCALE (== 8).
|
||||||
|
*
|
||||||
|
* @param position
|
||||||
|
* current MapPosition
|
||||||
|
* @param matrices
|
||||||
|
* current Matrices
|
||||||
|
* @param project
|
||||||
|
* if true apply view- and projection, or just view otherwise.
|
||||||
|
*/
|
||||||
|
protected void setMatrix(MapPosition position, Matrices matrices, boolean project) {
|
||||||
|
MapPosition oPos = mMapPosition;
|
||||||
|
|
||||||
|
double tileScale = Tile.SIZE * position.scale;
|
||||||
|
|
||||||
|
double x = oPos.x - position.x;
|
||||||
|
double y = oPos.y - position.y;
|
||||||
|
|
||||||
|
// wrap around date-line
|
||||||
|
// while (x < -1)
|
||||||
|
// x += 1.0;
|
||||||
|
// while (x > 2)
|
||||||
|
// x -= 1.0;
|
||||||
|
|
||||||
|
matrices.mvp.setTransScale((float) (x * tileScale),
|
||||||
|
(float) (y * tileScale),
|
||||||
|
(float) ((position.scale / oPos.scale) / MapRenderer.COORD_SCALE));
|
||||||
|
|
||||||
|
matrices.mvp.multiplyLhs(project ? matrices.viewproj : matrices.view);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility: Set matrices.mvp matrix relative to the difference of current
|
||||||
|
* MapPosition and the last updated Overlay MapPosition and add
|
||||||
|
* matrices.viewproj
|
||||||
|
*
|
||||||
|
* @param position ...
|
||||||
|
* @param matrices ...
|
||||||
|
*/
|
||||||
|
protected void setMatrix(MapPosition position, Matrices matrices) {
|
||||||
|
setMatrix(position, matrices, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,6 @@ public class ExtrusionRenderer extends LayerRenderer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void update(MapPosition pos, boolean changed, Matrices matrices) {
|
protected void update(MapPosition pos, boolean changed, Matrices matrices) {
|
||||||
mMapPosition.copy(pos);
|
|
||||||
|
|
||||||
if (!initialized && !initShader())
|
if (!initialized && !initShader())
|
||||||
return;
|
return;
|
||||||
@@ -157,11 +156,6 @@ public class ExtrusionRenderer extends LayerRenderer {
|
|||||||
mTileLayer.releaseTiles(mTileSet);
|
mTileLayer.releaseTiles(mTileSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void compile() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ExtrusionLayer getLayer(MapTile t) {
|
private static ExtrusionLayer getLayer(MapTile t) {
|
||||||
if (t.layers != null && t.layers.extrusionLayers != null
|
if (t.layers != null && t.layers.extrusionLayers != null
|
||||||
&& t.state(MapTile.STATE_READY))
|
&& t.state(MapTile.STATE_READY))
|
||||||
|
|||||||
@@ -16,21 +16,22 @@ package org.oscim.renderer;
|
|||||||
|
|
||||||
import org.oscim.backend.GL20;
|
import org.oscim.backend.GL20;
|
||||||
import org.oscim.core.MapPosition;
|
import org.oscim.core.MapPosition;
|
||||||
import org.oscim.core.Tile;
|
|
||||||
import org.oscim.renderer.MapRenderer.Matrices;
|
import org.oscim.renderer.MapRenderer.Matrices;
|
||||||
|
|
||||||
public abstract class LayerRenderer {
|
public abstract class LayerRenderer {
|
||||||
|
|
||||||
/**
|
protected static GL20 GL;
|
||||||
* Use mMapPosition.copy(position) to keep the position for which
|
|
||||||
* the Overlay is _compiled_. NOTE: required by setMatrix utility
|
static void init(GL20 gl) {
|
||||||
* functions to draw this layer fixed to the map
|
GL = gl;
|
||||||
*/
|
}
|
||||||
protected MapPosition mMapPosition;
|
|
||||||
|
|
||||||
/** flag to set when layer is ready for rendering */
|
/** flag to set when layer is ready for rendering */
|
||||||
boolean isReady;
|
boolean isReady;
|
||||||
|
|
||||||
|
/** set by MapRenderer */
|
||||||
|
boolean isInitialized;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set 'ready for render' state when layer data is ready for rendering.
|
* Set 'ready for render' state when layer data is ready for rendering.
|
||||||
*
|
*
|
||||||
@@ -44,23 +45,16 @@ public abstract class LayerRenderer {
|
|||||||
return isReady;
|
return isReady;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isInitialized;
|
|
||||||
|
|
||||||
public LayerRenderer() {
|
|
||||||
mMapPosition = new MapPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////// MapRender Thread ///////////////////////////
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on GL Thread before first update().
|
* 0. Called on GL Thread before first update().
|
||||||
* */
|
*/
|
||||||
protected boolean setup() {
|
protected boolean setup() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1. Called first by MapRenderer: Update the state here.
|
* 1. Called first by MapRenderer: Update the state here, compile
|
||||||
|
* vertex-data and set setReady(true).
|
||||||
*
|
*
|
||||||
* @param position current MapPosition
|
* @param position current MapPosition
|
||||||
* @param changed
|
* @param changed
|
||||||
@@ -69,17 +63,10 @@ public abstract class LayerRenderer {
|
|||||||
* and 'mvp' matrix for temporary use.
|
* and 'mvp' matrix for temporary use.
|
||||||
*/
|
*/
|
||||||
protected abstract void update(MapPosition position, boolean changed,
|
protected abstract void update(MapPosition position, boolean changed,
|
||||||
Matrices matrices);
|
Matrices matrices);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 2. Compile vertex buffers and upload textures for drawing.
|
* 2. Draw layer: called by MapRenderer when isReady == true.
|
||||||
* Not strictly required but useful when subclassing a RenderLayer.
|
|
||||||
* Should only be called from update()
|
|
||||||
*/
|
|
||||||
protected abstract void compile();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 3. Draw layer: called by MapRenderer when isReady == true.
|
|
||||||
*
|
*
|
||||||
* @param position current MapPosition
|
* @param position current MapPosition
|
||||||
* @param matrices contains the current view- and projection-matrices.
|
* @param matrices contains the current view- and projection-matrices.
|
||||||
@@ -88,53 +75,4 @@ public abstract class LayerRenderer {
|
|||||||
*/
|
*/
|
||||||
protected abstract void render(MapPosition position, Matrices matrices);
|
protected abstract void render(MapPosition position, Matrices matrices);
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility: Set matrices.mvp matrix relative to the difference of current
|
|
||||||
* MapPosition and the last updated Overlay MapPosition.
|
|
||||||
* Use this to 'stick' your layer to the map.
|
|
||||||
*
|
|
||||||
* @param position
|
|
||||||
* current MapPosition
|
|
||||||
* @param matrices
|
|
||||||
* current Matrices
|
|
||||||
* @param project
|
|
||||||
* if true apply view- and projection, or just view otherwise.
|
|
||||||
*/
|
|
||||||
protected void setMatrix(MapPosition position, Matrices matrices, boolean project) {
|
|
||||||
MapPosition oPos = mMapPosition;
|
|
||||||
|
|
||||||
double tileScale = Tile.SIZE * position.scale;
|
|
||||||
|
|
||||||
double x = oPos.x - position.x;
|
|
||||||
double y = oPos.y - position.y;
|
|
||||||
|
|
||||||
// wrap around date-line
|
|
||||||
// while (x < -1)
|
|
||||||
// x += 1.0;
|
|
||||||
// while (x > 2)
|
|
||||||
// x -= 1.0;
|
|
||||||
|
|
||||||
matrices.mvp.setTransScale((float) (x * tileScale), (float) (y * tileScale),
|
|
||||||
(float) ((position.scale / oPos.scale) / MapRenderer.COORD_SCALE));
|
|
||||||
|
|
||||||
matrices.mvp.multiplyLhs(project ? matrices.viewproj : matrices.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility: Set matrices.mvp matrix relative to the difference of current
|
|
||||||
* MapPosition and the last updated Overlay MapPosition and add
|
|
||||||
* matrices.viewproj
|
|
||||||
*
|
|
||||||
* @param position ...
|
|
||||||
* @param matrices ...
|
|
||||||
*/
|
|
||||||
protected void setMatrix(MapPosition position, Matrices matrices) {
|
|
||||||
setMatrix(position, matrices, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static GL20 GL;
|
|
||||||
|
|
||||||
static void init(GL20 gl) {
|
|
||||||
GL = gl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -316,12 +316,17 @@ public class MapRenderer {
|
|||||||
LayerRenderer[] layers = mMap.getLayers().getLayerRenderer();
|
LayerRenderer[] layers = mMap.getLayers().getLayerRenderer();
|
||||||
|
|
||||||
for (int i = 0, n = layers.length; i < n; i++) {
|
for (int i = 0, n = layers.length; i < n; i++) {
|
||||||
LayerRenderer renderLayer = layers[i];
|
LayerRenderer renderer = layers[i];
|
||||||
|
|
||||||
renderLayer.update(pos, changed, mMatrices);
|
if (!renderer.isInitialized){
|
||||||
|
renderer.setup();
|
||||||
|
renderer.isInitialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (renderLayer.isReady)
|
renderer.update(pos, changed, mMatrices);
|
||||||
renderLayer.render(mMapPosition, mMatrices);
|
|
||||||
|
if (renderer.isReady)
|
||||||
|
renderer.render(mMapPosition, mMatrices);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GLUtils.checkGlOutOfMemory("finish")) {
|
if (GLUtils.checkGlOutOfMemory("finish")) {
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import org.oscim.renderer.MapRenderer.Matrices;
|
|||||||
public class CustomRenderer extends LayerRenderer {
|
public class CustomRenderer extends LayerRenderer {
|
||||||
|
|
||||||
private final Map mMap;
|
private final Map mMap;
|
||||||
|
private final MapPosition mMapPosition;
|
||||||
|
|
||||||
private int mProgramObject;
|
private int mProgramObject;
|
||||||
private int hVertexPosition;
|
private int hVertexPosition;
|
||||||
@@ -53,6 +54,7 @@ public class CustomRenderer extends LayerRenderer {
|
|||||||
|
|
||||||
public CustomRenderer(Map map) {
|
public CustomRenderer(Map map) {
|
||||||
mMap = map;
|
mMap = map;
|
||||||
|
mMapPosition = new MapPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- everything below runs in GLRender Thread ----------
|
// ---------- everything below runs in GLRender Thread ----------
|
||||||
@@ -71,7 +73,6 @@ public class CustomRenderer extends LayerRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void compile() {
|
protected void compile() {
|
||||||
// modify mVerticesData and put in FloatBuffer
|
// modify mVerticesData and put in FloatBuffer
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ import org.oscim.backend.GL20;
|
|||||||
import org.oscim.backend.canvas.Color;
|
import org.oscim.backend.canvas.Color;
|
||||||
import org.oscim.core.MapPosition;
|
import org.oscim.core.MapPosition;
|
||||||
import org.oscim.renderer.BufferObject;
|
import org.oscim.renderer.BufferObject;
|
||||||
|
import org.oscim.renderer.ElementRenderer;
|
||||||
import org.oscim.renderer.GLState;
|
import org.oscim.renderer.GLState;
|
||||||
import org.oscim.renderer.GLUtils;
|
import org.oscim.renderer.GLUtils;
|
||||||
import org.oscim.renderer.LayerRenderer;
|
|
||||||
import org.oscim.renderer.MapRenderer;
|
import org.oscim.renderer.MapRenderer;
|
||||||
import org.oscim.renderer.MapRenderer.Matrices;
|
import org.oscim.renderer.MapRenderer.Matrices;
|
||||||
import org.oscim.utils.FastMath;
|
import org.oscim.utils.FastMath;
|
||||||
@@ -35,7 +35,7 @@ import org.oscim.utils.FastMath;
|
|||||||
* https://github.com/dalinaum/opengl-es-book-samples/tree/master/Android
|
* https://github.com/dalinaum/opengl-es-book-samples/tree/master/Android
|
||||||
* */
|
* */
|
||||||
|
|
||||||
public class CustomRenderer2 extends LayerRenderer {
|
public class CustomRenderer2 extends ElementRenderer {
|
||||||
|
|
||||||
private int mProgramObject;
|
private int mProgramObject;
|
||||||
private int hVertexPosition;
|
private int hVertexPosition;
|
||||||
|
|||||||
@@ -586,12 +586,6 @@ public class TileRenderer extends LayerRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void compile() {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void render(MapPosition position, Matrices matrices) {
|
protected void render(MapPosition position, Matrices matrices) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|||||||
Reference in New Issue
Block a user