simplify LayerRenderer, move setMatrix to ElementRenderer
This commit is contained in:
@@ -20,12 +20,12 @@ public class CustomRenderLayer extends Layer {
|
||||
|
||||
synchronized (this) {
|
||||
currentState = someConccurentVariable;
|
||||
compile();
|
||||
}
|
||||
Log.d(TAG, "state " + currentState);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void compile() {
|
||||
setReady(true);
|
||||
}
|
||||
|
||||
@@ -16,13 +16,14 @@ package org.oscim.renderer;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.renderer.MapRenderer.Matrices;
|
||||
import org.oscim.renderer.elements.BitmapLayer;
|
||||
import org.oscim.renderer.elements.RenderElement;
|
||||
import org.oscim.renderer.elements.ElementLayers;
|
||||
import org.oscim.renderer.elements.LineLayer;
|
||||
import org.oscim.renderer.elements.LineTexLayer;
|
||||
import org.oscim.renderer.elements.PolygonLayer;
|
||||
import org.oscim.renderer.elements.RenderElement;
|
||||
import org.oscim.renderer.elements.TextureLayer;
|
||||
import org.oscim.utils.FastMath;
|
||||
|
||||
@@ -31,10 +32,18 @@ import org.oscim.utils.FastMath;
|
||||
*/
|
||||
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 ElementRenderer() {
|
||||
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() {
|
||||
int newSize = layers.getSize();
|
||||
if (newSize <= 0) {
|
||||
@@ -105,4 +118,50 @@ public abstract class ElementRenderer extends LayerRenderer {
|
||||
if (MapRenderer.uploadLayers(layers, newSize, 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
|
||||
protected void update(MapPosition pos, boolean changed, Matrices matrices) {
|
||||
mMapPosition.copy(pos);
|
||||
|
||||
if (!initialized && !initShader())
|
||||
return;
|
||||
@@ -157,11 +156,6 @@ public class ExtrusionRenderer extends LayerRenderer {
|
||||
mTileLayer.releaseTiles(mTileSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void compile() {
|
||||
|
||||
}
|
||||
|
||||
private static ExtrusionLayer getLayer(MapTile t) {
|
||||
if (t.layers != null && t.layers.extrusionLayers != null
|
||||
&& t.state(MapTile.STATE_READY))
|
||||
|
||||
@@ -16,24 +16,25 @@ package org.oscim.renderer;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.renderer.MapRenderer.Matrices;
|
||||
|
||||
public abstract class 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;
|
||||
protected static GL20 GL;
|
||||
|
||||
static void init(GL20 gl) {
|
||||
GL = gl;
|
||||
}
|
||||
|
||||
/** flag to set when layer is ready for rendering */
|
||||
boolean isReady;
|
||||
|
||||
/** set by MapRenderer */
|
||||
boolean isInitialized;
|
||||
|
||||
/**
|
||||
* Set 'ready for render' state when layer data is ready for rendering.
|
||||
*
|
||||
*
|
||||
* @param true if render() should be called, false otherwise.
|
||||
*/
|
||||
protected void setReady(boolean ready) {
|
||||
@@ -44,24 +45,17 @@ public abstract class LayerRenderer {
|
||||
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() {
|
||||
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 changed
|
||||
* true when MapPosition has changed since last frame
|
||||
@@ -69,72 +63,16 @@ public abstract class LayerRenderer {
|
||||
* and 'mvp' matrix for temporary use.
|
||||
*/
|
||||
protected abstract void update(MapPosition position, boolean changed,
|
||||
Matrices matrices);
|
||||
Matrices matrices);
|
||||
|
||||
/**
|
||||
* 2. Compile vertex buffers and upload textures for drawing.
|
||||
* 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.
|
||||
*
|
||||
* 2. Draw layer: called by MapRenderer when isReady == true.
|
||||
*
|
||||
* @param position current MapPosition
|
||||
* @param matrices contains the current view- and projection-matrices.
|
||||
* 'matrices.mvp' is for temporary use to build the model-
|
||||
* view-projection to set as uniform.
|
||||
*/
|
||||
*/
|
||||
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();
|
||||
|
||||
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)
|
||||
renderLayer.render(mMapPosition, mMatrices);
|
||||
renderer.update(pos, changed, mMatrices);
|
||||
|
||||
if (renderer.isReady)
|
||||
renderer.render(mMapPosition, mMatrices);
|
||||
}
|
||||
|
||||
if (GLUtils.checkGlOutOfMemory("finish")) {
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.oscim.renderer.MapRenderer.Matrices;
|
||||
public class CustomRenderer extends LayerRenderer {
|
||||
|
||||
private final Map mMap;
|
||||
private final MapPosition mMapPosition;
|
||||
|
||||
private int mProgramObject;
|
||||
private int hVertexPosition;
|
||||
@@ -53,6 +54,7 @@ public class CustomRenderer extends LayerRenderer {
|
||||
|
||||
public CustomRenderer(Map map) {
|
||||
mMap = map;
|
||||
mMapPosition = new MapPosition();
|
||||
}
|
||||
|
||||
// ---------- everything below runs in GLRender Thread ----------
|
||||
@@ -71,7 +73,6 @@ public class CustomRenderer extends LayerRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void compile() {
|
||||
// modify mVerticesData and put in FloatBuffer
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ import org.oscim.backend.GL20;
|
||||
import org.oscim.backend.canvas.Color;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.renderer.BufferObject;
|
||||
import org.oscim.renderer.ElementRenderer;
|
||||
import org.oscim.renderer.GLState;
|
||||
import org.oscim.renderer.GLUtils;
|
||||
import org.oscim.renderer.LayerRenderer;
|
||||
import org.oscim.renderer.MapRenderer;
|
||||
import org.oscim.renderer.MapRenderer.Matrices;
|
||||
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
|
||||
* */
|
||||
|
||||
public class CustomRenderer2 extends LayerRenderer {
|
||||
public class CustomRenderer2 extends ElementRenderer {
|
||||
|
||||
private int mProgramObject;
|
||||
private int hVertexPosition;
|
||||
|
||||
@@ -586,12 +586,6 @@ public class TileRenderer extends LayerRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void compile() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void render(MapPosition position, Matrices matrices) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Reference in New Issue
Block a user