improve MapRenderer
- add functions to bind shared VBOs - make MapView field non-static
This commit is contained in:
parent
5e27740761
commit
8ea6fef1ca
@ -1 +1 @@
|
||||
Subproject commit b983af2578d3b72e5cb8839d43c4c366d07d2d46
|
||||
Subproject commit 6093f28947a9188f58c6a362d196920dfd5d02c8
|
||||
@ -18,6 +18,9 @@ package org.oscim.backend;
|
||||
|
||||
public class GLAdapter {
|
||||
|
||||
public final static boolean debug = false;
|
||||
public final static boolean debugView = false;
|
||||
|
||||
/** The instance provided by backend */
|
||||
public static GL20 g;
|
||||
|
||||
@ -26,8 +29,6 @@ public class GLAdapter {
|
||||
|
||||
public static boolean NON_PREMUL_CANVAS;
|
||||
|
||||
public final static boolean debug = true;
|
||||
|
||||
public static GL20 get() {
|
||||
if (g == null)
|
||||
throw new IllegalStateException();
|
||||
|
||||
@ -35,6 +35,7 @@ public class GLState {
|
||||
|
||||
static void init(GL20 gl) {
|
||||
GL = gl;
|
||||
|
||||
vertexArray[0] = false;
|
||||
vertexArray[1] = false;
|
||||
blend = false;
|
||||
@ -44,6 +45,7 @@ public class GLState {
|
||||
|
||||
GL.glDisable(GL20.GL_STENCIL_TEST);
|
||||
GL.glDisable(GL20.GL_DEPTH_TEST);
|
||||
GL.glDisable(GL20.GL_BLEND);
|
||||
}
|
||||
|
||||
public static boolean useProgram(int shaderProgram) {
|
||||
|
||||
@ -24,6 +24,7 @@ import java.nio.ShortBuffer;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.backend.GLAdapter;
|
||||
import org.oscim.backend.canvas.Color;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.map.Map;
|
||||
import org.oscim.map.Viewport;
|
||||
@ -41,12 +42,10 @@ public class MapRenderer {
|
||||
|
||||
/** scale factor used for short vertices */
|
||||
public static final float COORD_SCALE = 8.0f;
|
||||
|
||||
private static Map mMap;
|
||||
public static int screenWidth, screenHeight;
|
||||
|
||||
private static Viewport mViewport;
|
||||
private static MapPosition mMapPosition;
|
||||
private final Map mMap;
|
||||
private final MapPosition mMapPosition;
|
||||
|
||||
public class Matrices {
|
||||
|
||||
@ -81,16 +80,15 @@ public class MapRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
private static Matrices mMatrices;
|
||||
private final Matrices mMatrices;
|
||||
|
||||
// private
|
||||
static float[] mClearColor = null;
|
||||
private static float[] mClearColor = null;
|
||||
|
||||
public static int mQuadIndicesID;
|
||||
private static int mQuadIndicesID;
|
||||
private static int mQuadVerticesID;
|
||||
public final static int maxQuads = 64;
|
||||
|
||||
private static boolean mUpdateColor = false;
|
||||
private static volatile boolean mUpdateColor = false;
|
||||
|
||||
// drawlock to synchronize Main- and GL-Thread
|
||||
// static ReentrantLock tilelock = new ReentrantLock();
|
||||
@ -144,7 +142,6 @@ public class MapRenderer {
|
||||
public MapRenderer(Map map) {
|
||||
|
||||
mMap = map;
|
||||
mViewport = map.getViewport();
|
||||
mMapPosition = new MapPosition();
|
||||
|
||||
mMatrices = new Matrices();
|
||||
@ -153,6 +150,7 @@ public class MapRenderer {
|
||||
// FIXME should be done in 'destroy' method
|
||||
// clear all previous vbo refs
|
||||
BufferObject.clear();
|
||||
setBackgroundColor(Color.DKGRAY);
|
||||
}
|
||||
|
||||
public static void setBackgroundColor(int color) {
|
||||
@ -223,7 +221,7 @@ public class MapRenderer {
|
||||
mBufferPool.releaseBuffers();
|
||||
}
|
||||
|
||||
private static void draw() {
|
||||
private void draw() {
|
||||
|
||||
if (mUpdateColor) {
|
||||
float cc[] = mClearColor;
|
||||
@ -238,34 +236,35 @@ public class MapRenderer {
|
||||
| GL20.GL_STENCIL_BUFFER_BIT);
|
||||
|
||||
GLState.blend(false);
|
||||
GL.glDisable(GL20.GL_BLEND);
|
||||
GLState.bindTex2D(-1);
|
||||
GLState.useProgram(-1);
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
MapPosition pos = mMapPosition;
|
||||
|
||||
synchronized (mViewport) {
|
||||
Viewport viewport = mMap.getViewport();
|
||||
|
||||
synchronized (viewport) {
|
||||
mMap.getAnimator().updateAnimation();
|
||||
|
||||
// get current MapPosition
|
||||
changed = mViewport.getMapPosition(pos);
|
||||
changed = viewport.getMapPosition(pos);
|
||||
|
||||
if (changed)
|
||||
mViewport.getMapViewProjection(mMatrices.mapPlane);
|
||||
viewport.getMapViewProjection(mMatrices.mapPlane);
|
||||
|
||||
mViewport.getMatrix(mMatrices.view, mMatrices.proj, mMatrices.viewproj);
|
||||
viewport.getMatrix(mMatrices.view, mMatrices.proj, mMatrices.viewproj);
|
||||
|
||||
if (debugView) {
|
||||
if (GLAdapter.debugView) {
|
||||
// modify this to scale only the view, to see
|
||||
// which tiles are rendered
|
||||
mMatrices.mvp.setScale(0.5f, 0.5f, 1);
|
||||
mMatrices.viewproj.multiplyLhs(mMatrices.mvp);
|
||||
mMatrices.proj.multiplyLhs(mMatrices.mvp);
|
||||
}
|
||||
}
|
||||
|
||||
//log.debug("begin frame");
|
||||
GLState.bindTex2D(-1);
|
||||
GLState.useProgram(-1);
|
||||
//GL.glBindTexture(GL20.GL_TEXTURE_2D, 0);
|
||||
|
||||
/* update layers */
|
||||
LayerRenderer[] layers = mMap.getLayers().getLayerRenderer();
|
||||
|
||||
@ -279,11 +278,11 @@ public class MapRenderer {
|
||||
|
||||
renderer.update(pos, changed, mMatrices);
|
||||
|
||||
if (renderer.isReady)
|
||||
renderer.render(pos, mMatrices);
|
||||
|
||||
if (GLAdapter.debug)
|
||||
GLUtils.checkGlError(renderer.getClass().getName());
|
||||
|
||||
if (renderer.isReady)
|
||||
renderer.render(mMapPosition, mMatrices);
|
||||
}
|
||||
|
||||
if (GLUtils.checkGlOutOfMemory("finish")) {
|
||||
@ -297,7 +296,7 @@ public class MapRenderer {
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(int width, int height) {
|
||||
log.debug("SurfaceChanged:" + mNewSurface + " " + width + "x" + height);
|
||||
log.debug("SurfaceChanged: " + mNewSurface + " " + width + "x" + height);
|
||||
|
||||
if (width <= 0 || height <= 0)
|
||||
return;
|
||||
@ -305,14 +304,7 @@ public class MapRenderer {
|
||||
screenWidth = width;
|
||||
screenHeight = height;
|
||||
|
||||
mViewport.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.multiplyLhs(mMatrices.mvp);
|
||||
}
|
||||
mMap.getViewport().getMatrix(null, mMatrices.proj, null);
|
||||
|
||||
GL.glViewport(0, 0, width, height);
|
||||
GL.glScissor(0, 0, width, height);
|
||||
@ -381,8 +373,6 @@ public class MapRenderer {
|
||||
|
||||
public void onSurfaceCreated() {
|
||||
GL = GLAdapter.get();
|
||||
|
||||
// log.debug("surface created");
|
||||
// log.debug(GL.glGetString(GL20.GL_EXTENSIONS));
|
||||
|
||||
GLState.init(GL);
|
||||
@ -393,7 +383,6 @@ public class MapRenderer {
|
||||
|
||||
// classes that require GL context for initialization
|
||||
ElementLayers.initRenderer(GL);
|
||||
|
||||
LayerRenderer.init(GL);
|
||||
|
||||
mNewSurface = true;
|
||||
@ -401,18 +390,26 @@ public class MapRenderer {
|
||||
|
||||
private boolean mNewSurface;
|
||||
|
||||
public static final boolean debugView = false;
|
||||
|
||||
public static int getQuadIndicesVBO() {
|
||||
return mQuadIndicesID;
|
||||
/**
|
||||
* Bind VBO for a simple quad. Handy for simple custom RenderLayers
|
||||
* Vertices: float[]{ -1, -1, -1, 1, 1, -1, 1, 1 }
|
||||
*
|
||||
* @param bind - true to activate, false to unbind
|
||||
*/
|
||||
public static void bindQuadVertexVBO(int location, boolean bind) {
|
||||
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mQuadVerticesID);
|
||||
if (location > 0)
|
||||
GL.glVertexAttribPointer(location, 2, GL20.GL_FLOAT, false, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get VBO ID for a simple quad. Handy for simple custom RenderLayers
|
||||
* Vertices: { -1, -1, -1, 1, 1, -1, 1, 1 }
|
||||
*/
|
||||
public static int getQuadVertexVBO() {
|
||||
return mQuadVerticesID;
|
||||
* Bind indices for rendering up to 64 (MapRenderer.maxQuads) in
|
||||
* one draw call. Vertex order is 0-1-2 2-1-3
|
||||
*
|
||||
* @param bind - true to activate, false to unbind (dont forget!)
|
||||
* */
|
||||
public static void bindQuadIndicesVBO(boolean bind) {
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, bind ? mQuadIndicesID : 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -207,7 +207,7 @@ public class BitmapLayer extends TextureLayer {
|
||||
|
||||
m.mvp.setAsUniform(hTextureMVMatrix);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, MapRenderer.mQuadIndicesID);
|
||||
MapRenderer.bindQuadIndicesVBO(true);
|
||||
|
||||
for (TextureItem ti = tl.textures; ti != null; ti = ti.next) {
|
||||
|
||||
@ -235,7 +235,7 @@ public class BitmapLayer extends TextureLayer {
|
||||
}
|
||||
}
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
MapRenderer.bindQuadIndicesVBO(false);
|
||||
|
||||
return renderElement.next;
|
||||
}
|
||||
|
||||
@ -348,8 +348,8 @@ public final class LineTexLayer extends RenderElement {
|
||||
m.mvp.setAsUniform(hMatrix);
|
||||
|
||||
int maxIndices = MapRenderer.maxQuads * 6;
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER,
|
||||
MapRenderer.mQuadIndicesID);
|
||||
|
||||
MapRenderer.bindQuadIndicesVBO(true);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID);
|
||||
GL.glVertexAttribPointer(hVertexFlip, 1,
|
||||
@ -446,7 +446,7 @@ public final class LineTexLayer extends RenderElement {
|
||||
//GlUtils.checkGlError(TAG);
|
||||
}
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
MapRenderer.bindQuadIndicesVBO(false);
|
||||
|
||||
GL.glDisableVertexAttribArray(hVertexPosition0);
|
||||
GL.glDisableVertexAttribArray(hVertexPosition1);
|
||||
|
||||
@ -22,6 +22,7 @@ import java.nio.FloatBuffer;
|
||||
import java.nio.ShortBuffer;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.backend.GLAdapter;
|
||||
import org.oscim.core.GeometryBuffer;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.Tile;
|
||||
@ -154,7 +155,7 @@ public final class PolygonLayer extends RenderElement {
|
||||
|
||||
// Set up the program for rendering polygons
|
||||
if (i == 0) {
|
||||
if (MapRenderer.debugView)
|
||||
if (GLAdapter.debugView)
|
||||
polygonProgram[i] = GLUtils.createProgram(polygonVertexShaderZ,
|
||||
polygonFragmentShaderZ);
|
||||
else
|
||||
|
||||
@ -133,14 +133,14 @@ public class TextureItem extends Inlist<TextureItem> {
|
||||
pool = null;
|
||||
}
|
||||
|
||||
if (num > 0) {
|
||||
int[] textureIds = GLUtils.glGenTextures(num);
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
TextureItem to = new TextureItem(textureIds[i]);
|
||||
initTexture(to);
|
||||
|
||||
pool = Inlist.push(pool, to);
|
||||
}
|
||||
}
|
||||
|
||||
fill = num;
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ public abstract class TextureLayer extends RenderElement {
|
||||
m.proj.setAsUniform(hTextureProjMatrix);
|
||||
m.mvp.setAsUniform(hTextureMVMatrix);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, MapRenderer.mQuadIndicesID);
|
||||
MapRenderer.bindQuadIndicesVBO(true);
|
||||
|
||||
for (TextureItem ti = tl.textures; ti != null; ti = ti.next) {
|
||||
|
||||
@ -176,7 +176,7 @@ public abstract class TextureLayer extends RenderElement {
|
||||
}
|
||||
}
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
MapRenderer.bindQuadIndicesVBO(false);
|
||||
|
||||
return renderElement.next;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user