Allow multiple TextureAtlas per SymbolBucket (#303)

This commit is contained in:
Longri 2017-02-24 18:47:17 +01:00 committed by Emux
parent 325f848969
commit 5480cd6df8
3 changed files with 152 additions and 3 deletions

View File

@ -0,0 +1,135 @@
/*
* Copyright 2016 devemux86
* Copyright 2017 Longri
*
* 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
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.test;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
import org.oscim.backend.canvas.Color;
import org.oscim.backend.canvas.Paint;
import org.oscim.core.GeoPoint;
import org.oscim.gdx.GdxMapApp;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.marker.ItemizedLayer;
import org.oscim.layers.marker.MarkerItem;
import org.oscim.layers.marker.MarkerSymbol;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.renderer.atlas.TextureAtlas;
import org.oscim.renderer.atlas.TextureRegion;
import org.oscim.tiling.source.bitmap.DefaultSources;
import org.oscim.utils.TextureAtlasUtils;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import static org.oscim.layers.marker.MarkerSymbol.HotspotPlace;
public class AtlasMultiTextureTest extends MarkerLayerTest {
private java.util.Map<Object, TextureRegion> regionsMap;
@Override
public void createLayers() {
BitmapTileLayer bitmapLayer = new BitmapTileLayer(mMap, DefaultSources.STAMEN_TONER.build());
bitmapLayer.tileRenderer().setBitmapAlpha(0.5f);
mMap.setBaseMap(bitmapLayer);
// Map events receiver
mMap.layers().add(new MapEventsReceiver(mMap));
mMap.setMapPosition(0, 0, 1 << 2);
Bitmap bitmapPoi = CanvasAdapter.decodeBitmap(getClass().getResourceAsStream("/res/marker_poi.png"));
Bitmap bitmapFocus = CanvasAdapter.decodeBitmap(getClass().getResourceAsStream("/res/marker_focus.png"));
// Create Atlas from Bitmaps
java.util.Map<Object, Bitmap> inputMap = new LinkedHashMap<>();
regionsMap = new LinkedHashMap<>();
List<TextureAtlas> atlasList = new ArrayList<>();
inputMap.put("poi", bitmapPoi);
inputMap.put("focus", bitmapFocus);
Canvas canvas = CanvasAdapter.newCanvas();
Paint paint = CanvasAdapter.newPaint();
paint.setTypeface(Paint.FontFamily.DEFAULT, Paint.FontStyle.NORMAL);
paint.setTextSize(12);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
List<MarkerItem> pts = new ArrayList<>();
for (double lat = -90; lat <= 90; lat += 5) {
for (double lon = -180; lon <= 180; lon += 5) {
String title = lat + "/" + lon;
pts.add(new MarkerItem(title, "", new GeoPoint(lat, lon)));
Bitmap bmp = CanvasAdapter.newBitmap(40, 40, 0);
canvas.setBitmap(bmp);
canvas.fillColor(Color.GREEN);
canvas.drawText(Double.toString(lat), 3, 17, paint);
canvas.drawText(Double.toString(lon), 3, 35, paint);
inputMap.put(title, bmp);
}
}
// Bitmaps will never used any more
// With iOS we must flip the Y-Axis
TextureAtlasUtils.createTextureRegions(inputMap, regionsMap, atlasList, true, false);
MarkerSymbol symbol;
if (BILLBOARDS)
symbol = new MarkerSymbol(regionsMap.get("poi"), HotspotPlace.BOTTOM_CENTER);
else
symbol = new MarkerSymbol(regionsMap.get("poi"), HotspotPlace.CENTER, false);
if (BILLBOARDS)
mFocusMarker = new MarkerSymbol(regionsMap.get("focus"), HotspotPlace.BOTTOM_CENTER);
else
mFocusMarker = new MarkerSymbol(regionsMap.get("focus"), HotspotPlace.CENTER, false);
mMarkerLayer = new ItemizedLayer<>(mMap, new ArrayList<MarkerItem>(), symbol, this);
mMap.layers().add(mMarkerLayer);
mMarkerLayer.addItems(pts);
mMap.layers().add(new TileGridLayer(mMap));
// set all markers
for (int i = 0; i < pts.size(); i++) {
onItemSingleTapUp(i, pts.get(i));
}
System.out.println("Atlas count: " + atlasList.size());
}
@Override
public boolean onItemSingleTapUp(int index, MarkerItem item) {
if (item.getMarker() == null) {
MarkerSymbol markerSymbol = new MarkerSymbol(regionsMap.get(item.getTitle()), HotspotPlace.BOTTOM_CENTER);
item.setMarker(markerSymbol);
} else
item.setMarker(null);
System.out.println("Marker tap " + item.getTitle());
return true;
}
public static void main(String[] args) {
GdxMapApp.init();
GdxMapApp.run(new AtlasMultiTextureTest());
}
}

17
vtm/src/org/oscim/renderer/bucket/SymbolBucket.java Normal file → Executable file
View File

@ -80,6 +80,7 @@ public final class SymbolBucket extends TextureBucket {
prevTextures = textures; prevTextures = textures;
textures = null; textures = null;
TextureItem t = null; TextureItem t = null;
TextureItem lastTexture = null;
for (SymbolItem it = mSymbols.head(); it != null; ) { for (SymbolItem it = mSymbols.head(); it != null; ) {
int width = 0, height = 0; int width = 0, height = 0;
@ -88,12 +89,24 @@ public final class SymbolBucket extends TextureBucket {
// FIXME Use simultaneously TextureAtlas and external symbols // FIXME Use simultaneously TextureAtlas and external symbols
if (it.texRegion != null) { if (it.texRegion != null) {
/* FIXME This work only with one TextureAtlas per SymbolBucket */ if (it.texRegion.texture.id == -1) {
if (textures == null) { //upload texture for give correct texID
it.texRegion.texture.upload();
}
if (textures == null || lastTexture == null || lastTexture.id != it.texRegion.texture.id) {
/* clone TextureItem to use same texID with /* clone TextureItem to use same texID with
* multiple TextureItem */ * multiple TextureItem */
int nextOffset = 0;
if (t != null) {
nextOffset = t.offset + t.indices;
}
t = TextureItem.clone(it.texRegion.texture); t = TextureItem.clone(it.texRegion.texture);
t.offset = nextOffset;
textures = Inlist.appendItem(textures, t); textures = Inlist.appendItem(textures, t);
lastTexture = t;
} }
TextureAtlas.Rect r = it.texRegion.rect; TextureAtlas.Rect r = it.texRegion.rect;

3
vtm/src/org/oscim/renderer/bucket/TextureItem.java Normal file → Executable file
View File

@ -1,6 +1,7 @@
/* /*
* Copyright 2012, 2013 Hannes Janetzek * Copyright 2012, 2013 Hannes Janetzek
* Copyright 2016 devemux86 * Copyright 2016 devemux86
* Copyright 2017 Longri
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
* *
@ -42,7 +43,7 @@ public class TextureItem extends Inlist<TextureItem> {
/** /**
* texture ID * texture ID
*/ */
private int id; int id;
/** /**
* current settings * current settings