more work on BitmapLayer: allow to set target width and height

This commit is contained in:
Hannes Janetzek 2013-05-01 03:27:03 +02:00
parent a1bd0c9eaa
commit 45fee11336
2 changed files with 90 additions and 61 deletions

View File

@ -16,70 +16,95 @@ package org.oscim.renderer.layer;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
import org.oscim.core.Tile;
import org.oscim.renderer.GLRenderer; import org.oscim.renderer.GLRenderer;
import android.graphics.Bitmap; import android.graphics.Bitmap;
/**
* Renderer for a single bitmap, width and height must be power of 2.
*/
public class BitmapLayer extends TextureLayer { public class BitmapLayer extends TextureLayer {
// private final static String TAG = BitmapLayer.class.getName(); // private final static String TAG = BitmapLayer.class.getName();
private Bitmap mBitmap; private Bitmap mBitmap;
private final boolean mReuseBitmap;
public BitmapLayer() { private final short[] mVertices;
/**
* @param reuseBitmap false if the Bitmap should be recycled after
* it is compiled to texture.
* */
public BitmapLayer(boolean reuseBitmap) {
type = Layer.BITMAP; type = Layer.BITMAP;
mReuseBitmap = reuseBitmap;
mVertices = new short[24];
textures = new TextureItem(-1);
// used for size calculation of Layers buffer.
verticesCnt = 4;
} }
public void setBitmap(Bitmap bitmap) { public void setBitmap(Bitmap bitmap, int w, int h) {
mWidth = w;
mHeight = h;
mBitmap = bitmap; mBitmap = bitmap;
vertexItems = VertexItem.pool.get(); TextureItem ti = this.textures;
short[] buf = vertexItems.vertices;
short size = (short) (Tile.SIZE * GLRenderer.COORD_SCALE);
short center = (short) (size >> 1);
short m = (short) (-(size >> 1));
short p = center;
short t = (8 * 256);
int pos = 0;
// top-left
buf[pos++] = 0;
buf[pos++] = 0;
buf[pos++] = m;
buf[pos++] = m;
buf[pos++] = 0;
buf[pos++] = 0;
// bot-left
buf[pos++] = 0;
buf[pos++] = size;
buf[pos++] = m;
buf[pos++] = p;
buf[pos++] = 0;
buf[pos++] = t;
// top-right
buf[pos++] = size;
buf[pos++] = 0;
buf[pos++] = p;
buf[pos++] = m;
buf[pos++] = t;
buf[pos++] = 0;
// bot-right
buf[pos++] = size;
buf[pos++] = size;
buf[pos++] = p;
buf[pos++] = p;
buf[pos++] = t;
buf[pos++] = t;
vertexItems.used = 24;
TextureItem ti = this.textures = new TextureItem(-1);
ti.ownBitmap = true; ti.ownBitmap = true;
ti.width = mBitmap.getWidth(); ti.width = mBitmap.getWidth();
ti.height = mBitmap.getHeight(); ti.height = mBitmap.getHeight();
ti.bitmap = mBitmap; ti.bitmap = mBitmap;
ti.vertices = TextureRenderer.INDICES_PER_SPRITE; ti.vertices = TextureRenderer.INDICES_PER_SPRITE;
}
verticesCnt = 4; private int mWidth, mHeight;
/**
* Set target dimension to renderthe bitmap */
public void setSize(int w, int h){
mWidth = w;
mHeight = h;
}
private void setVertices(ShortBuffer sbuf){
short[] buf = mVertices;
short w = (short) (mWidth * GLRenderer.COORD_SCALE);
short h = (short) (mHeight* GLRenderer.COORD_SCALE);
short t = 1;
int pos = 0;
// top-left
buf[pos++] = 0;
buf[pos++] = 0;
buf[pos++] = -1;
buf[pos++] = -1;
buf[pos++] = 0;
buf[pos++] = 0;
// bot-left
buf[pos++] = 0;
buf[pos++] = h;
buf[pos++] = -1;
buf[pos++] = -1;
buf[pos++] = 0;
buf[pos++] = t;
// top-right
buf[pos++] = w;
buf[pos++] = 0;
buf[pos++] = -1;
buf[pos++] = -1;
buf[pos++] = t;
buf[pos++] = 0;
// bot-right
buf[pos++] = w;
buf[pos++] = h;
buf[pos++] = -1;
buf[pos++] = -1;
buf[pos++] = t;
buf[pos++] = t;
this.offset = sbuf.position() * 2; // bytes
sbuf.put(buf);
} }
@Override @Override
@ -89,21 +114,30 @@ public class BitmapLayer extends TextureLayer {
@Override @Override
protected void compile(ShortBuffer sbuf) { protected void compile(ShortBuffer sbuf) {
if (mBitmap == null) if (mBitmap == null)
return; return;
super.compile(sbuf); setVertices(sbuf);
mBitmap.recycle(); //for (TextureItem to = textures; to != null; to = to.next)
mBitmap = null; TextureItem.uploadTexture(textures);
textures.bitmap = null;
if (!mReuseBitmap) {
mBitmap.recycle();
mBitmap = null;
textures.bitmap = null;
}
} }
@Override @Override
protected void clear() { protected void clear() {
if (mBitmap != null) { if (mBitmap != null) {
mBitmap.recycle(); if (!mReuseBitmap)
mBitmap.recycle();
mBitmap = null; mBitmap = null;
textures.bitmap = null; textures.bitmap = null;
} }

View File

@ -15,9 +15,6 @@
package org.oscim.renderer.layer; package org.oscim.renderer.layer;
import static org.oscim.renderer.GLRenderer.COORD_SCALE;
import static org.oscim.renderer.layer.TextureItem.TEXTURE_HEIGHT;
import org.oscim.renderer.GLRenderer; import org.oscim.renderer.GLRenderer;
import org.oscim.renderer.GLRenderer.Matrices; import org.oscim.renderer.GLRenderer.Matrices;
import org.oscim.renderer.GLState; import org.oscim.renderer.GLState;
@ -110,10 +107,8 @@ public final class BitmapRenderer {
return layer.next; return layer.next;
} }
//private final static double TEX_COORD_DIV_X = 1.0 / (TEXTURE_WIDTH * COORD_SCALE); //private final static double TEX_COORD_DIV = 1.0 / (1024 * COORD_SCALE);
private final static double TEX_COORD_DIV_X = 1.0 / (TEXTURE_HEIGHT* COORD_SCALE); //private final static double COORD_DIV = 1.0 / GLRenderer.COORD_SCALE;
private final static double TEX_COORD_DIV_Y = 1.0 / (TEXTURE_HEIGHT * COORD_SCALE);
private final static double COORD_DIV = 1.0 / GLRenderer.COORD_SCALE;
private final static String textVertexShader = "" private final static String textVertexShader = ""
+ "precision mediump float; " + "precision mediump float; "
@ -124,11 +119,11 @@ public final class BitmapRenderer {
+ "uniform float u_scale;" + "uniform float u_scale;"
+ "uniform float u_swidth;" + "uniform float u_swidth;"
+ "varying vec2 tex_c;" + "varying vec2 tex_c;"
+ "const vec2 div = vec2(" + TEX_COORD_DIV_X + "," + TEX_COORD_DIV_Y + ");" //+ "const vec2 div = vec2(" + TEX_COORD_DIV + "," + TEX_COORD_DIV + ");"
+ "const float coord_scale = " + COORD_DIV + ";" //+ "const float coord_scale = " + COORD_DIV + ";"
+ "void main() {" + "void main() {"
+ " gl_Position = u_mv * vec4(vertex.xy, 0.0, 1.0);" + " gl_Position = u_mv * vec4(vertex.xy, 0.0, 1.0);"
+ " tex_c = tex_coord * div;" + " tex_c = tex_coord;" // * div;"
+ "}"; + "}";
private final static String textFragmentShader = "" private final static String textFragmentShader = ""