diff --git a/vtm/assets/styles/default.xml b/vtm/assets/styles/default.xml index 569217ca..884327f5 100644 --- a/vtm/assets/styles/default.xml +++ b/vtm/assets/styles/default.xml @@ -22,7 +22,7 @@ - + @@ -32,7 +32,7 @@ - + @@ -89,7 +89,7 @@ - + diff --git a/vtm/src/org/oscim/renderer/sublayers/PolygonRenderer.java b/vtm/src/org/oscim/renderer/sublayers/PolygonRenderer.java index d6f8f778..1469ebe0 100644 --- a/vtm/src/org/oscim/renderer/sublayers/PolygonRenderer.java +++ b/vtm/src/org/oscim/renderer/sublayers/PolygonRenderer.java @@ -18,11 +18,9 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; -import org.oscim.backend.CanvasAdapter; import org.oscim.backend.GL20; import org.oscim.backend.GLAdapter; import org.oscim.backend.Log; -import org.oscim.backend.canvas.Bitmap; import org.oscim.core.MapPosition; import org.oscim.renderer.GLRenderer; import org.oscim.renderer.GLRenderer.Matrices; @@ -59,10 +57,10 @@ public final class PolygonRenderer { private static int[] hPolygonColor = new int[numShaders]; private static int[] hPolygonScale = new int[numShaders]; - private static boolean enableTexture = true; - private static int mTexWater; - private static int mTexWood; - private static int mTexGrass; + //private static boolean enableTexture = true; + //private static int mTexWater; + //private static int mTexWood; + //private static int mTexGrass; static boolean init() { GL = GLAdapter.get(); @@ -95,34 +93,10 @@ public final class PolygonRenderer { } mFillPolys = new PolygonLayer[STENCIL_BITS]; - if (enableTexture) { - mTexWood = loadSprite("grass3.png"); - mTexWater = loadSprite("water2.png"); - mTexGrass = loadSprite("grass2.png"); - } + return true; } - private static int loadSprite(String name) { - int[] textures; - - //Bitmap b = BitmapUtils.createBitmap(name); - Bitmap b = CanvasAdapter.g.loadBitmapAsset(name); - if (b == null) { - Log.d(TAG, "missing asset: " + name); - return 0; - } - textures = GlUtils.glGenTextures(1); - GL.glBindTexture(GL20.GL_TEXTURE_2D, textures[0]); - - GlUtils.setTextureParameter(GL20.GL_LINEAR, GL20.GL_LINEAR, - GL20.GL_REPEAT, GL20.GL_REPEAT); - - b.uploadToTexture(false); - - return textures[0]; - } - private static void fillPolygons(Matrices m, int start, int end, int zoom, float scale, float div) { @@ -135,19 +109,14 @@ public final class PolygonRenderer { for (int c = start; c < end; c++) { Area a = mFillPolys[c].area; - if (enableTexture && (a.color == 0xFFAFC5E3 - || a.color == 0xffd1dbc7 - || a.color == 0xffa3ca7b)) { + + if (a.texture != null){ shader = texShader; setShader(texShader, m); - GL.glUniform2f(hPolygonScale[1], FastMath.clamp(scale - 1, 0, 1), div); - if (a.color == 0xFFAFC5E3) - GL.glBindTexture(GL20.GL_TEXTURE_2D, mTexWater); - else if (a.color == 0xffd1dbc7) - GL.glBindTexture(GL20.GL_TEXTURE_2D, mTexWood); - else - GL.glBindTexture(GL20.GL_TEXTURE_2D, mTexGrass); + + a.texture.bind(); + } else if (a.fade >= zoom) { float f = 1.0f; /* fade in/out */ diff --git a/vtm/src/org/oscim/renderer/sublayers/TextureItem.java b/vtm/src/org/oscim/renderer/sublayers/TextureItem.java index ffb78f42..287952eb 100644 --- a/vtm/src/org/oscim/renderer/sublayers/TextureItem.java +++ b/vtm/src/org/oscim/renderer/sublayers/TextureItem.java @@ -37,6 +37,7 @@ public class TextureItem extends Inlist { public int width; public int height; + public boolean repeat; // vertex offset from which this texture is referenced public short offset; @@ -53,6 +54,9 @@ public class TextureItem extends Inlist { // released. private boolean isClone; + // texture data is ready + private boolean isReady; + private TextureItem(int id) { this.id = id; } @@ -74,6 +78,23 @@ public class TextureItem extends Inlist { this.height = bitmap.getHeight(); } + public TextureItem(Bitmap bitmap, boolean repeat) { + this.bitmap = bitmap; + this.id = -1; + this.ownBitmap = true; + this.width = bitmap.getWidth(); + this.height = bitmap.getHeight(); + this.repeat = repeat; + } + + public void bind() { + if (!isReady) { + TextureItem.uploadTexture(this); + isReady = true; + } + GL.glBindTexture(GL20.GL_TEXTURE_2D, id); + } + public synchronized static void releaseAll(TextureItem ti) { pool.releaseAll(ti); } @@ -98,14 +119,16 @@ public class TextureItem extends Inlist { @Override public void init(int num) { - int[] textureIds = GlUtils.glGenTextures(num); + //int[] textureIds = GlUtils.glGenTextures(num); + // + //for (int i = 0; i < num; i++) { + // initTexture(textureIds[i]); + // TextureItem to = new TextureItem(textureIds[i]); + // pool = Inlist.push(pool, to); + //} + //fill = num; - for (int i = 0; i < num; i++) { - initTexture(textureIds[i]); - TextureItem to = new TextureItem(textureIds[i]); - pool = Inlist.push(pool, to); - } - fill = num; + fill = 0; } public TextureItem get(int width, int height) { @@ -117,12 +140,14 @@ public class TextureItem extends Inlist { return new TextureItem(-1); } + /** called when item is added back to pool */ @Override protected boolean clearItem(TextureItem it) { - // Log.d(TAG, it.ownBitmap + " " + (it.bitmap == null)); + if (it.ownBitmap) { it.bitmap = null; it.ownBitmap = false; + releaseTexture(it); return false; } @@ -179,7 +204,7 @@ public class TextureItem extends Inlist { */ public static void uploadTexture(TextureItem to) { - // free unused textures, find a better place for this TODO + // free unused textures -> TODO find a better place for this synchronized (mTextures) { int size = mTextures.size(); if (size > 0) { @@ -198,14 +223,15 @@ public class TextureItem extends Inlist { mTexCnt++; int[] textureIds = GlUtils.glGenTextures(1); to.id = textureIds[0]; - initTexture(to.id); - // if (TextureRenderer.debug) - Log.d(TAG, "poolFill:" + pool.getFill() - + " texCnt:" + mTexCnt - + " new texture " + to.id); + initTexture(to); + if (TextureRenderer.debug) + Log.d(TAG, "poolFill:" + pool.getFill() + + " texCnt:" + mTexCnt + + " new texture " + to.id); } - uploadTexture(to, to.bitmap, mBitmapFormat, mBitmapType, + uploadTexture(to, to.bitmap, + mBitmapFormat, mBitmapType, TEXTURE_WIDTH, TEXTURE_HEIGHT); if (!to.ownBitmap) @@ -225,20 +251,13 @@ public class TextureItem extends Inlist { } GL.glBindTexture(GL20.GL_TEXTURE_2D, to.id); - //Log.d(TAG, "upload " + to.id); + if (to.ownBitmap) { bitmap.uploadToTexture(false); - // GLUtils.texImage2D(GL20.GL_TEXTURE_2D, 0, bitmap, 0); - } else if (to.width == w && to.height == h) { bitmap.uploadToTexture(true); - // GLUtils.texSubImage2D(GL20.GL_TEXTURE_2D, 0, 0, 0, bitmap, - // format, type); - } else { bitmap.uploadToTexture(false); - // GLUtils.texImage2D(GL20.GL_TEXTURE_2D, 0, format, bitmap, type, - // 0); to.width = w; to.height = h; } @@ -247,17 +266,25 @@ public class TextureItem extends Inlist { GlUtils.checkGlError(TAG); } - static void initTexture(int id) { - GL.glBindTexture(GL20.GL_TEXTURE_2D, id); + static void initTexture(TextureItem it) { + GL.glBindTexture(GL20.GL_TEXTURE_2D, it.id); GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_LINEAR); GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MAG_FILTER, GL20.GL_LINEAR); - GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_S, - GL20.GL_CLAMP_TO_EDGE); // Set U Wrapping - GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_T, - GL20.GL_CLAMP_TO_EDGE); // Set V Wrapping + + if (it.repeat) { + GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_S, + GL20.GL_REPEAT); + GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_T, + GL20.GL_REPEAT); + } else { + GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_S, + GL20.GL_CLAMP_TO_EDGE); + GL.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_T, + GL20.GL_CLAMP_TO_EDGE); + } } static void init(int num) { diff --git a/vtm/src/org/oscim/theme/RenderThemeHandler.java b/vtm/src/org/oscim/theme/RenderThemeHandler.java index 2f0add2c..f0190c9e 100644 --- a/vtm/src/org/oscim/theme/RenderThemeHandler.java +++ b/vtm/src/org/oscim/theme/RenderThemeHandler.java @@ -23,7 +23,6 @@ import java.util.Stack; import javax.xml.parsers.ParserConfigurationException; -import org.oscim.backend.BitmapUtils; import org.oscim.backend.CanvasAdapter; import org.oscim.backend.Log; import org.oscim.backend.XMLReaderAdapter; @@ -34,6 +33,7 @@ import org.oscim.backend.canvas.Paint.FontFamily; import org.oscim.backend.canvas.Paint.FontStyle; import org.oscim.renderer.atlas.TextureAtlas; import org.oscim.renderer.atlas.TextureAtlas.Rect; +import org.oscim.renderer.sublayers.TextureItem; import org.oscim.theme.renderinstruction.Area; import org.oscim.theme.renderinstruction.AreaLevel; import org.oscim.theme.renderinstruction.Circle; @@ -72,6 +72,8 @@ public class RenderThemeHandler extends DefaultHandler { private static final String ELEMENT_NAME_USE_STYLE_OUTLINE = "use-outline"; private static final String UNEXPECTED_ELEMENT = "unexpected element: "; + private static final String IMG_PATH = "styles/"; + /** * @param inputStream * an input stream containing valid render theme XML data. @@ -364,7 +366,7 @@ public class RenderThemeHandler extends DefaultHandler { "missing attribute 'img' for element: " + elementName); - Bitmap bitmap = CanvasAdapter.g.loadBitmapAsset("styles/" + img); + Bitmap bitmap = CanvasAdapter.g.loadBitmapAsset(IMG_PATH + img); mTextureAtlas = new TextureAtlas(bitmap); } @@ -691,6 +693,8 @@ public class RenderThemeHandler extends DefaultHandler { int blendFill = Color.BLACK; String style = null; + TextureItem texture = null; + for (int i = 0; i < attributes.getLength(); ++i) { String name = attributes.getLocalName(i); String value = attributes.getValue(i); @@ -715,9 +719,17 @@ public class RenderThemeHandler extends DefaultHandler { } } + validateArea(strokeWidth); - return new Area(style, src, fill, stroke, strokeWidth, fade, level, blend, - blendFill); + + if (src != null){ + Bitmap b = CanvasAdapter.g.loadBitmapAsset(src); + if (b != null) + texture = new TextureItem(b, true); + + } + return new Area(style, fill, stroke, strokeWidth, fade, level, blend, + blendFill, texture); } private static void validateArea(float strokeWidth) { diff --git a/vtm/src/org/oscim/theme/renderinstruction/Area.java b/vtm/src/org/oscim/theme/renderinstruction/Area.java index 3ccc7f31..c04269c8 100644 --- a/vtm/src/org/oscim/theme/renderinstruction/Area.java +++ b/vtm/src/org/oscim/theme/renderinstruction/Area.java @@ -14,6 +14,7 @@ */ package org.oscim.theme.renderinstruction; +import org.oscim.renderer.sublayers.TextureItem; import org.oscim.theme.IRenderCallback; /** @@ -35,11 +36,12 @@ public final class Area extends RenderInstruction { strokeWidth = 0; color = fill; + texture = null; } - public Area(String style, String src, int fill, int stroke, float strokeWidth, - int fade, int level, int blend, int blendFill) { + public Area(String style, int fill, int stroke, float strokeWidth, + int fade, int level, int blend, int blendFill, TextureItem texture) { this.style = style; @@ -59,6 +61,7 @@ public final class Area extends RenderInstruction { this.strokeWidth = strokeWidth; this.fade = fade; this.level = level; + this.texture = texture; } @Override @@ -73,4 +76,6 @@ public final class Area extends RenderInstruction { public final int fade; public final int blendColor; public final int blend; + + public final TextureItem texture; } diff --git a/vtm/src/org/oscim/theme/renderinstruction/AreaLevel.java b/vtm/src/org/oscim/theme/renderinstruction/AreaLevel.java index 0873252d..9e2856c8 100644 --- a/vtm/src/org/oscim/theme/renderinstruction/AreaLevel.java +++ b/vtm/src/org/oscim/theme/renderinstruction/AreaLevel.java @@ -1,5 +1,5 @@ /* - * Copyright 2010, 2011, 2012 mapsforge.org + * Copyright 2013 Hannes Janetzek * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software