themeable area textures

This commit is contained in:
Hannes Janetzek
2013-07-15 14:15:31 +02:00
parent 23b602fcca
commit 244e52118d
6 changed files with 93 additions and 80 deletions

View File

@@ -22,7 +22,7 @@
<!-- fade out at z=7, blend over to 'blend-fill' in z=11 -->
<!-- <style-area name="forest|wood" fill="#ebefe5" fade="7" blend="11" blend-fill="#cee0bc"/>
d3dec8 -->
<style-area name="wood" fill="#d1dbc7" fade="7" blend="10" blend-fill="#9ac56e" />
<style-area name="wood" fill="#d1dbc7" fade="7" blend="10" blend-fill="#9ac56e" src="grass3.png"/>
<style-line name="wood" fix="true" cap="butt" width="1.0" stroke="#9ac56e" />
<!-- grass|meadow|garden|grassland|scrub -->
@@ -32,7 +32,7 @@
<style-area name="greens2" fill="#deecb9" fade="12" />
<!-- park|common|green|cemetery|golf_course|dog_park -->
<style-area name="park" fill="#a3ca7b" fade="11" />
<style-area name="park" fill="#a3ca7b" fade="11" src="grass2.png"/>
<style-line name="park" stroke="#a3ca7b" width="1.0" fix="true" cap="butt" fade="11" />
<!-- de:Kleingartengebiet -->
@@ -89,7 +89,7 @@
<style-line name="water" stroke="#a4bbcc" width="1.0" cap="butt" />
<!--<style-area name="water" fill="#97b7e5" /> -->
<style-area name="water" fill="#afc5e3" />
<style-area name="water" fill="#afc5e3" src="water2.png"/>
<!-- no-go area boundary -->
<style-line name="fence" stroke="#444444" width="1.2" fix="true" cap="butt" />

View File

@@ -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 */

View File

@@ -37,6 +37,7 @@ public class TextureItem extends Inlist<TextureItem> {
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<TextureItem> {
// 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<TextureItem> {
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<TextureItem> {
@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<TextureItem> {
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<TextureItem> {
*/
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<TextureItem> {
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<TextureItem> {
}
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<TextureItem> {
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) {

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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