Improve code / xml formatting, closes #54
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package org.oscim.test.renderer;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.oscim.backend.canvas.Color;
|
||||
import org.oscim.backend.canvas.Paint.Cap;
|
||||
import org.oscim.gdx.GdxMap;
|
||||
@@ -20,123 +18,125 @@ import org.oscim.theme.styles.TextStyle;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class AtlasTest extends GdxMap {
|
||||
|
||||
@Override
|
||||
protected void createLayers() {
|
||||
mMap.setMapPosition(0, 0, 1 << 4);
|
||||
mMap.layers().add(new GenericLayer(mMap, new AtlasRenderLayer()));
|
||||
}
|
||||
@Override
|
||||
protected void createLayers() {
|
||||
mMap.setMapPosition(0, 0, 1 << 4);
|
||||
mMap.layers().add(new GenericLayer(mMap, new AtlasRenderLayer()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new AtlasTest(), null, 400);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new AtlasTest(), null, 400);
|
||||
}
|
||||
|
||||
static class AtlasRenderLayer extends BucketRenderer {
|
||||
static class AtlasRenderLayer extends BucketRenderer {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(AtlasRenderLayer.class);
|
||||
Logger log = LoggerFactory.getLogger(AtlasRenderLayer.class);
|
||||
|
||||
public AtlasRenderLayer() {
|
||||
public AtlasRenderLayer() {
|
||||
|
||||
TextureAtlas mAtlas = TextureAtlas.create(2048, 2048, 1);
|
||||
TextureAtlas mAtlas = TextureAtlas.create(2048, 2048, 1);
|
||||
|
||||
TextBucket tl = new TextBucket();
|
||||
TextStyle t = TextStyle.builder().fontSize(20).color(Color.BLACK).build();
|
||||
buckets.set(tl);
|
||||
TextBucket tl = new TextBucket();
|
||||
TextStyle t = TextStyle.builder().fontSize(20).color(Color.BLACK).build();
|
||||
buckets.set(tl);
|
||||
|
||||
LineBucket ll = buckets.getLineBucket(0);
|
||||
ll.line = new LineStyle(Color.BLUE, 3, Cap.BUTT);
|
||||
ll.scale = 1f;
|
||||
LineBucket ll = buckets.getLineBucket(0);
|
||||
ll.line = new LineStyle(Color.BLUE, 3, Cap.BUTT);
|
||||
ll.scale = 1f;
|
||||
|
||||
LineBucket ll2 = buckets.getLineBucket(1);
|
||||
ll2.line = new LineStyle(Color.RED, 3, Cap.BUTT);
|
||||
ll2.scale = 1f;
|
||||
LineBucket ll2 = buckets.getLineBucket(1);
|
||||
ll2.line = new LineStyle(Color.RED, 3, Cap.BUTT);
|
||||
ll2.scale = 1f;
|
||||
|
||||
LineBucket ll3 = buckets.getLineBucket(2);
|
||||
ll3.line = new LineStyle(Color.GREEN, 3, Cap.BUTT);
|
||||
ll3.scale = 1f;
|
||||
LineBucket ll3 = buckets.getLineBucket(2);
|
||||
ll3.line = new LineStyle(Color.GREEN, 3, Cap.BUTT);
|
||||
ll3.scale = 1f;
|
||||
|
||||
float[] points = new float[10];
|
||||
float[] points = new float[10];
|
||||
|
||||
for (int i = 0; i < 400; i++) {
|
||||
int w = (int) (20 + Math.random() * 256);
|
||||
int h = (int) (20 + Math.random() * 56);
|
||||
Rect r = mAtlas.getRegion(w, h);
|
||||
if (r == null) {
|
||||
log.debug("no space left");
|
||||
continue;
|
||||
}
|
||||
r.x += 1;
|
||||
r.y += 1;
|
||||
for (int i = 0; i < 400; i++) {
|
||||
int w = (int) (20 + Math.random() * 256);
|
||||
int h = (int) (20 + Math.random() * 56);
|
||||
Rect r = mAtlas.getRegion(w, h);
|
||||
if (r == null) {
|
||||
log.debug("no space left");
|
||||
continue;
|
||||
}
|
||||
r.x += 1;
|
||||
r.y += 1;
|
||||
|
||||
points[0] = r.x;
|
||||
points[1] = r.y;
|
||||
points[2] = r.x + (r.w - 2);
|
||||
points[3] = r.y;
|
||||
points[4] = r.x + (r.w - 2);
|
||||
points[5] = r.y + (r.h - 2);
|
||||
points[6] = r.x;
|
||||
points[7] = r.y + (r.h - 2);
|
||||
points[8] = r.x;
|
||||
points[9] = r.y;
|
||||
ll.addLine(points, 10, false);
|
||||
points[0] = r.x;
|
||||
points[1] = r.y;
|
||||
points[2] = r.x + (r.w - 2);
|
||||
points[3] = r.y;
|
||||
points[4] = r.x + (r.w - 2);
|
||||
points[5] = r.y + (r.h - 2);
|
||||
points[6] = r.x;
|
||||
points[7] = r.y + (r.h - 2);
|
||||
points[8] = r.x;
|
||||
points[9] = r.y;
|
||||
ll.addLine(points, 10, false);
|
||||
|
||||
r.x += 1;
|
||||
r.y += 1;
|
||||
points[0] = r.x;
|
||||
points[1] = r.y;
|
||||
points[2] = r.x + (w - 4);
|
||||
points[3] = r.y;
|
||||
points[4] = r.x + (w - 4);
|
||||
points[5] = r.y + (h - 4);
|
||||
points[6] = r.x;
|
||||
points[7] = r.y + (h - 4);
|
||||
points[8] = r.x;
|
||||
points[9] = r.y;
|
||||
r.x += 1;
|
||||
r.y += 1;
|
||||
points[0] = r.x;
|
||||
points[1] = r.y;
|
||||
points[2] = r.x + (w - 4);
|
||||
points[3] = r.y;
|
||||
points[4] = r.x + (w - 4);
|
||||
points[5] = r.y + (h - 4);
|
||||
points[6] = r.x;
|
||||
points[7] = r.y + (h - 4);
|
||||
points[8] = r.x;
|
||||
points[9] = r.y;
|
||||
|
||||
log.debug("add region: " + Arrays.toString(points));
|
||||
ll2.addLine(points, 10, false);
|
||||
log.debug("add region: " + Arrays.toString(points));
|
||||
ll2.addLine(points, 10, false);
|
||||
|
||||
TextItem ti = TextItem.pool.get();
|
||||
ti.set(r.x + r.w / 2, r.y + r.h / 2, "" + i, t);
|
||||
ti.x1 = 0;
|
||||
ti.y1 = 1; // (short) (size / 2);
|
||||
ti.x2 = 1; // (short) size;
|
||||
ti.y2 = 1;
|
||||
tl.addText(ti);
|
||||
}
|
||||
TextItem ti = TextItem.pool.get();
|
||||
ti.set(r.x + r.w / 2, r.y + r.h / 2, "" + i, t);
|
||||
ti.x1 = 0;
|
||||
ti.y1 = 1; // (short) (size / 2);
|
||||
ti.x2 = 1; // (short) size;
|
||||
ti.y2 = 1;
|
||||
tl.addText(ti);
|
||||
}
|
||||
|
||||
for (Slot s = mAtlas.mSlots; s != null; s = s.next) {
|
||||
points[0] = s.x;
|
||||
points[1] = s.y;
|
||||
points[2] = s.x + s.w;
|
||||
points[3] = s.y;
|
||||
points[4] = s.x + s.w;
|
||||
points[5] = 2048;
|
||||
points[6] = s.x;
|
||||
points[7] = 2048;
|
||||
points[8] = s.x;
|
||||
points[9] = s.y;
|
||||
for (Slot s = mAtlas.mSlots; s != null; s = s.next) {
|
||||
points[0] = s.x;
|
||||
points[1] = s.y;
|
||||
points[2] = s.x + s.w;
|
||||
points[3] = s.y;
|
||||
points[4] = s.x + s.w;
|
||||
points[5] = 2048;
|
||||
points[6] = s.x;
|
||||
points[7] = 2048;
|
||||
points[8] = s.x;
|
||||
points[9] = s.y;
|
||||
|
||||
ll3.addLine(points, 10, false);
|
||||
}
|
||||
ll3.addLine(points, 10, false);
|
||||
}
|
||||
|
||||
tl.prepare();
|
||||
//tl.labels = TextItem.pool.releaseAll(tl.labels);
|
||||
}
|
||||
tl.prepare();
|
||||
//tl.labels = TextItem.pool.releaseAll(tl.labels);
|
||||
}
|
||||
|
||||
boolean initial = true;
|
||||
boolean initial = true;
|
||||
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
|
||||
if (initial) {
|
||||
mMapPosition.copy(v.pos);
|
||||
initial = false;
|
||||
if (initial) {
|
||||
mMapPosition.copy(v.pos);
|
||||
initial = false;
|
||||
|
||||
compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.oscim.test.renderer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.oscim.backend.canvas.Color;
|
||||
import org.oscim.core.GeometryBuffer;
|
||||
import org.oscim.core.Point;
|
||||
@@ -14,75 +12,77 @@ import org.oscim.renderer.bucket.LineBucket;
|
||||
import org.oscim.theme.styles.LineStyle;
|
||||
import org.oscim.utils.geom.BezierPath;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BezierTest extends GdxMap {
|
||||
|
||||
@Override
|
||||
protected void createLayers() {
|
||||
mMap.layers().add(new GenericLayer(mMap, new BezierPathLayer()));
|
||||
}
|
||||
@Override
|
||||
protected void createLayers() {
|
||||
mMap.layers().add(new GenericLayer(mMap, new BezierPathLayer()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new BezierTest(), null, 400);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new BezierTest(), null, 400);
|
||||
}
|
||||
|
||||
static class BezierPathLayer extends BucketRenderer {
|
||||
static class BezierPathLayer extends BucketRenderer {
|
||||
|
||||
public BezierPathLayer() {
|
||||
mMapPosition.scale = 0;
|
||||
public BezierPathLayer() {
|
||||
mMapPosition.scale = 0;
|
||||
|
||||
GeometryBuffer g = new GeometryBuffer(100, 1);
|
||||
g.startLine();
|
||||
GeometryBuffer g = new GeometryBuffer(100, 1);
|
||||
g.startLine();
|
||||
|
||||
Point[] pts = new Point[10];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pts[i] = new Point(i * 3, (i * i) % 3 * 4);
|
||||
pts[i].x *= 10;
|
||||
pts[i].y *= 10;
|
||||
// System.out.println(pts[i]);
|
||||
g.addPoint(pts[i]);
|
||||
}
|
||||
LineBucket ll = buckets.addLineBucket(0, new LineStyle(Color.BLUE, 2f));
|
||||
ll.addLine(g);
|
||||
Point[] pts = new Point[10];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
pts[i] = new Point(i * 3, (i * i) % 3 * 4);
|
||||
pts[i].x *= 10;
|
||||
pts[i].y *= 10;
|
||||
// System.out.println(pts[i]);
|
||||
g.addPoint(pts[i]);
|
||||
}
|
||||
LineBucket ll = buckets.addLineBucket(0, new LineStyle(Color.BLUE, 2f));
|
||||
ll.addLine(g);
|
||||
|
||||
List<Point> ctrl = BezierPath.cubicSplineControlPoints(pts, 0.1f);
|
||||
List<Point> ctrl = BezierPath.cubicSplineControlPoints(pts, 0.1f);
|
||||
|
||||
g.clear();
|
||||
g.startLine();
|
||||
Point p0 = pts[0];
|
||||
g.clear();
|
||||
g.startLine();
|
||||
Point p0 = pts[0];
|
||||
|
||||
for (int j = 1, k = 0; j < pts.length; j++) {
|
||||
Point p1 = ctrl.get(k++);
|
||||
Point p2 = ctrl.get(k++);
|
||||
Point p3 = pts[j];
|
||||
System.out.println(">>> " + p1 + " " + p2);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
double mu = (i / 10f);
|
||||
Point p = BezierPath.cubicBezier(p0, p1, p2, p3, mu);
|
||||
g.addPoint(p);
|
||||
System.out.println(mu + " " + p);
|
||||
}
|
||||
p0 = p3;
|
||||
}
|
||||
ll = buckets.addLineBucket(1, new LineStyle(Color.CYAN, 2f));
|
||||
ll.addLine(g);
|
||||
for (int j = 1, k = 0; j < pts.length; j++) {
|
||||
Point p1 = ctrl.get(k++);
|
||||
Point p2 = ctrl.get(k++);
|
||||
Point p3 = pts[j];
|
||||
System.out.println(">>> " + p1 + " " + p2);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
double mu = (i / 10f);
|
||||
Point p = BezierPath.cubicBezier(p0, p1, p2, p3, mu);
|
||||
g.addPoint(p);
|
||||
System.out.println(mu + " " + p);
|
||||
}
|
||||
p0 = p3;
|
||||
}
|
||||
ll = buckets.addLineBucket(1, new LineStyle(Color.CYAN, 2f));
|
||||
ll.addLine(g);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void clear() {
|
||||
buckets.clear();
|
||||
setReady(false);
|
||||
}
|
||||
public synchronized void clear() {
|
||||
buckets.clear();
|
||||
setReady(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void update(GLViewport v) {
|
||||
@Override
|
||||
public synchronized void update(GLViewport v) {
|
||||
|
||||
if (mMapPosition.scale == 0)
|
||||
mMapPosition.copy(v.pos);
|
||||
if (mMapPosition.scale == 0)
|
||||
mMapPosition.copy(v.pos);
|
||||
|
||||
if (!isReady()) {
|
||||
compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isReady()) {
|
||||
compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,6 @@
|
||||
*/
|
||||
package org.oscim.test.renderer;
|
||||
|
||||
import static org.oscim.backend.GLAdapter.gl;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.oscim.backend.GL;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.map.Map;
|
||||
@@ -31,6 +25,12 @@ import org.oscim.renderer.GLUtils;
|
||||
import org.oscim.renderer.GLViewport;
|
||||
import org.oscim.renderer.LayerRenderer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import static org.oscim.backend.GLAdapter.gl;
|
||||
|
||||
/*
|
||||
* This is an example how to integrate custom OpenGL drawing routines as map overlay
|
||||
*
|
||||
@@ -40,127 +40,127 @@ import org.oscim.renderer.LayerRenderer;
|
||||
|
||||
public class CustomRenderer extends LayerRenderer {
|
||||
|
||||
private final Map mMap;
|
||||
private final MapPosition mMapPosition;
|
||||
private final Map mMap;
|
||||
private final MapPosition mMapPosition;
|
||||
|
||||
private int mProgramObject;
|
||||
private int hVertexPosition;
|
||||
private int hMatrixPosition;
|
||||
private int mProgramObject;
|
||||
private int hVertexPosition;
|
||||
private int hMatrixPosition;
|
||||
|
||||
private FloatBuffer mVertices;
|
||||
private final float[] mVerticesData = {
|
||||
-200, -200, 1.0f,
|
||||
200, 200, 0,
|
||||
-200, 200, 0.5f,
|
||||
200, -200, 0.5f,
|
||||
};
|
||||
private boolean mInitialized;
|
||||
private FloatBuffer mVertices;
|
||||
private final float[] mVerticesData = {
|
||||
-200, -200, 1.0f,
|
||||
200, 200, 0,
|
||||
-200, 200, 0.5f,
|
||||
200, -200, 0.5f,
|
||||
};
|
||||
private boolean mInitialized;
|
||||
|
||||
public CustomRenderer(Map map) {
|
||||
mMap = map;
|
||||
mMapPosition = new MapPosition();
|
||||
}
|
||||
public CustomRenderer(Map map) {
|
||||
mMap = map;
|
||||
mMapPosition = new MapPosition();
|
||||
}
|
||||
|
||||
// ---------- everything below runs in GLRender Thread ----------
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
if (!mInitialized) {
|
||||
if (!init())
|
||||
return;
|
||||
// ---------- everything below runs in GLRender Thread ----------
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
if (!mInitialized) {
|
||||
if (!init())
|
||||
return;
|
||||
|
||||
mInitialized = true;
|
||||
mInitialized = true;
|
||||
|
||||
// fix current MapPosition
|
||||
mMapPosition.copy(v.pos);
|
||||
// fix current MapPosition
|
||||
mMapPosition.copy(v.pos);
|
||||
|
||||
compile();
|
||||
}
|
||||
}
|
||||
compile();
|
||||
}
|
||||
}
|
||||
|
||||
protected void compile() {
|
||||
// modify mVerticesData and put in FloatBuffer
|
||||
protected void compile() {
|
||||
// modify mVerticesData and put in FloatBuffer
|
||||
|
||||
mVertices.clear();
|
||||
mVertices.put(mVerticesData);
|
||||
mVertices.flip();
|
||||
mVertices.clear();
|
||||
mVertices.put(mVerticesData);
|
||||
mVertices.flip();
|
||||
|
||||
setReady(true);
|
||||
}
|
||||
setReady(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(GLViewport v) {
|
||||
@Override
|
||||
public void render(GLViewport v) {
|
||||
|
||||
// Use the program object
|
||||
GLState.useProgram(mProgramObject);
|
||||
// Use the program object
|
||||
GLState.useProgram(mProgramObject);
|
||||
|
||||
GLState.blend(true);
|
||||
GLState.test(false, false);
|
||||
GLState.blend(true);
|
||||
GLState.test(false, false);
|
||||
|
||||
// unbind previously bound VBOs
|
||||
gl.bindBuffer(GL.ARRAY_BUFFER, 0);
|
||||
// unbind previously bound VBOs
|
||||
gl.bindBuffer(GL.ARRAY_BUFFER, 0);
|
||||
|
||||
// Load the vertex data
|
||||
//mVertices.position(0);
|
||||
gl.vertexAttribPointer(hVertexPosition, 3, GL.FLOAT, false, 0, mVertices);
|
||||
//mVertices.position(2);
|
||||
//GL.vertexAttribPointer(hVertexPosition, 2, GL20.FLOAT, false, 4, mVertices);
|
||||
// Load the vertex data
|
||||
//mVertices.position(0);
|
||||
gl.vertexAttribPointer(hVertexPosition, 3, GL.FLOAT, false, 0, mVertices);
|
||||
//mVertices.position(2);
|
||||
//GL.vertexAttribPointer(hVertexPosition, 2, GL20.FLOAT, false, 4, mVertices);
|
||||
|
||||
GLState.enableVertexArrays(hVertexPosition, -1);
|
||||
GLState.enableVertexArrays(hVertexPosition, -1);
|
||||
|
||||
/* apply view and projection matrices */
|
||||
// set mvp (tmp) matrix relative to mMapPosition
|
||||
// i.e. fixed on the map
|
||||
// set mvp (tmp) matrix relative to mMapPosition
|
||||
// i.e. fixed on the map
|
||||
|
||||
float ratio = 1f / mMap.getWidth();
|
||||
float ratio = 1f / mMap.getWidth();
|
||||
|
||||
v.mvp.setScale(ratio, ratio, 1);
|
||||
v.mvp.multiplyLhs(v.proj);
|
||||
v.mvp.setAsUniform(hMatrixPosition);
|
||||
v.mvp.setScale(ratio, ratio, 1);
|
||||
v.mvp.multiplyLhs(v.proj);
|
||||
v.mvp.setAsUniform(hMatrixPosition);
|
||||
|
||||
// Draw the triangle
|
||||
gl.drawArrays(GL.TRIANGLE_STRIP, 0, 4);
|
||||
// Draw the triangle
|
||||
gl.drawArrays(GL.TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
GLUtils.checkGlError("...");
|
||||
}
|
||||
GLUtils.checkGlError("...");
|
||||
}
|
||||
|
||||
private boolean init() {
|
||||
// Load the vertex/fragment shaders
|
||||
int programObject = GLShader.createProgram(vShaderStr, fShaderStr);
|
||||
private boolean init() {
|
||||
// Load the vertex/fragment shaders
|
||||
int programObject = GLShader.createProgram(vShaderStr, fShaderStr);
|
||||
|
||||
if (programObject == 0)
|
||||
return false;
|
||||
if (programObject == 0)
|
||||
return false;
|
||||
|
||||
// Handle for vertex position in shader
|
||||
hVertexPosition = gl.getAttribLocation(programObject, "a_pos");
|
||||
// Handle for vertex position in shader
|
||||
hVertexPosition = gl.getAttribLocation(programObject, "a_pos");
|
||||
|
||||
hMatrixPosition = gl.getUniformLocation(programObject, "u_mvp");
|
||||
hMatrixPosition = gl.getUniformLocation(programObject, "u_mvp");
|
||||
|
||||
// Store the program object
|
||||
mProgramObject = programObject;
|
||||
// Store the program object
|
||||
mProgramObject = programObject;
|
||||
|
||||
mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4)
|
||||
.order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4)
|
||||
.order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private final static String vShaderStr = "" +
|
||||
"precision mediump float;"
|
||||
+ "uniform mat4 u_mvp;"
|
||||
+ "attribute vec4 a_pos;"
|
||||
+ "varying float alpha;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_Position = u_mvp * vec4(a_pos.xy, 0.0, 1.0);"
|
||||
+ " alpha = a_pos.z;"
|
||||
+ "}";
|
||||
private final static String vShaderStr = "" +
|
||||
"precision mediump float;"
|
||||
+ "uniform mat4 u_mvp;"
|
||||
+ "attribute vec4 a_pos;"
|
||||
+ "varying float alpha;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_Position = u_mvp * vec4(a_pos.xy, 0.0, 1.0);"
|
||||
+ " alpha = a_pos.z;"
|
||||
+ "}";
|
||||
|
||||
private final static String fShaderStr = "" +
|
||||
"precision mediump float;"
|
||||
+ "varying float alpha;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_FragColor = vec4 (alpha, 1.0-alpha, 0.0, 0.7 );"
|
||||
+ "}";
|
||||
private final static String fShaderStr = "" +
|
||||
"precision mediump float;"
|
||||
+ "varying float alpha;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_FragColor = vec4 (alpha, 1.0-alpha, 0.0, 0.7 );"
|
||||
+ "}";
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package org.oscim.test.renderer;
|
||||
|
||||
import static org.oscim.backend.GLAdapter.gl;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.oscim.backend.GL;
|
||||
import org.oscim.backend.canvas.Color;
|
||||
import org.oscim.gdx.GdxMap;
|
||||
@@ -18,195 +14,199 @@ import org.oscim.renderer.GLViewport;
|
||||
import org.oscim.renderer.MapRenderer;
|
||||
import org.oscim.utils.FastMath;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import static org.oscim.backend.GLAdapter.gl;
|
||||
|
||||
public class HexagonRenderTest extends GdxMap {
|
||||
|
||||
@Override
|
||||
protected void createLayers() {
|
||||
mMap.setMapPosition(0, 0, 1 << 4);
|
||||
mMap.layers().add(new GenericLayer(mMap, new HexagonRenderer()));
|
||||
}
|
||||
@Override
|
||||
protected void createLayers() {
|
||||
mMap.setMapPosition(0, 0, 1 << 4);
|
||||
mMap.layers().add(new GenericLayer(mMap, new HexagonRenderer()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new HexagonRenderTest(), null, 400);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GdxMapApp.init();
|
||||
GdxMapApp.run(new HexagonRenderTest(), null, 400);
|
||||
}
|
||||
|
||||
/* This is an example how to integrate custom OpenGL drawing routines as map
|
||||
* overlay
|
||||
* overlay
|
||||
*
|
||||
* based on chapter 2 from:
|
||||
* https://github.com/dalinaum/opengl-es-book-samples/tree/master/Android */
|
||||
|
||||
static class HexagonRenderer extends BucketRenderer {
|
||||
static class HexagonRenderer extends BucketRenderer {
|
||||
|
||||
private int mProgramObject;
|
||||
private int hVertexPosition;
|
||||
private int hMatrixPosition;
|
||||
private int hColorPosition;
|
||||
private int hCenterPosition;
|
||||
private int mProgramObject;
|
||||
private int hVertexPosition;
|
||||
private int hMatrixPosition;
|
||||
private int hColorPosition;
|
||||
private int hCenterPosition;
|
||||
|
||||
//private FloatBuffer mVertices;
|
||||
private boolean mInitialized;
|
||||
private BufferObject mVBO;
|
||||
//private FloatBuffer mVertices;
|
||||
private boolean mInitialized;
|
||||
private BufferObject mVBO;
|
||||
|
||||
int mZoom = -1;
|
||||
float mCellScale = 60 * MapRenderer.COORD_SCALE;
|
||||
int mZoom = -1;
|
||||
float mCellScale = 60 * MapRenderer.COORD_SCALE;
|
||||
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
if (!mInitialized) {
|
||||
if (!init()) {
|
||||
return;
|
||||
}
|
||||
mInitialized = true;
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
if (!mInitialized) {
|
||||
if (!init()) {
|
||||
return;
|
||||
}
|
||||
mInitialized = true;
|
||||
|
||||
compile();
|
||||
mMapPosition.copy(v.pos);
|
||||
}
|
||||
compile();
|
||||
mMapPosition.copy(v.pos);
|
||||
}
|
||||
|
||||
//if (mZoom != v.pos.zoomLevel) {
|
||||
// mMapPosition.copy(v.pos);
|
||||
// mZoom = v.pos.zoomLevel;
|
||||
//}
|
||||
}
|
||||
//if (mZoom != v.pos.zoomLevel) {
|
||||
// mMapPosition.copy(v.pos);
|
||||
// mZoom = v.pos.zoomLevel;
|
||||
//}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void compile() {
|
||||
@Override
|
||||
protected void compile() {
|
||||
|
||||
float[] vertices = new float[12];
|
||||
float[] vertices = new float[12];
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
vertices[i * 2 + 0] = (float) Math.cos(Math.PI * 2 * i / 6) * mCellScale;
|
||||
vertices[i * 2 + 1] = (float) Math.sin(Math.PI * 2 * i / 6) * mCellScale;
|
||||
}
|
||||
FloatBuffer buf = MapRenderer.getFloatBuffer(12);
|
||||
buf.put(vertices);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
vertices[i * 2 + 0] = (float) Math.cos(Math.PI * 2 * i / 6) * mCellScale;
|
||||
vertices[i * 2 + 1] = (float) Math.sin(Math.PI * 2 * i / 6) * mCellScale;
|
||||
}
|
||||
FloatBuffer buf = MapRenderer.getFloatBuffer(12);
|
||||
buf.put(vertices);
|
||||
|
||||
mVBO = BufferObject.get(GL.ARRAY_BUFFER, 0);
|
||||
mVBO.loadBufferData(buf.flip(), 12 * 4);
|
||||
mVBO = BufferObject.get(GL.ARRAY_BUFFER, 0);
|
||||
mVBO.loadBufferData(buf.flip(), 12 * 4);
|
||||
|
||||
setReady(true);
|
||||
}
|
||||
setReady(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(GLViewport v) {
|
||||
@Override
|
||||
public void render(GLViewport v) {
|
||||
|
||||
// Use the program object
|
||||
GLState.useProgram(mProgramObject);
|
||||
// Use the program object
|
||||
GLState.useProgram(mProgramObject);
|
||||
|
||||
GLState.blend(true);
|
||||
GLState.test(false, false);
|
||||
GLState.blend(true);
|
||||
GLState.test(false, false);
|
||||
|
||||
// bind VBO data
|
||||
mVBO.bind();
|
||||
// bind VBO data
|
||||
mVBO.bind();
|
||||
|
||||
// set VBO vertex layout
|
||||
gl.vertexAttribPointer(hVertexPosition, 2, GL.FLOAT, false, 0, 0);
|
||||
// set VBO vertex layout
|
||||
gl.vertexAttribPointer(hVertexPosition, 2, GL.FLOAT, false, 0, 0);
|
||||
|
||||
GLState.enableVertexArrays(hVertexPosition, -1);
|
||||
GLState.enableVertexArrays(hVertexPosition, -1);
|
||||
|
||||
/* apply view and projection matrices */
|
||||
// set mvp (tmp) matrix relative to mMapPosition
|
||||
// i.e. fixed on the map
|
||||
setMatrix(v);
|
||||
v.mvp.setAsUniform(hMatrixPosition);
|
||||
// set mvp (tmp) matrix relative to mMapPosition
|
||||
// i.e. fixed on the map
|
||||
setMatrix(v);
|
||||
v.mvp.setAsUniform(hMatrixPosition);
|
||||
|
||||
final int offset_x = 4;
|
||||
final int offset_y = 16;
|
||||
final int offset_x = 4;
|
||||
final int offset_y = 16;
|
||||
|
||||
float h = (float) (Math.sqrt(3) / 2);
|
||||
for (int y = -offset_y; y < offset_y; y++) {
|
||||
for (int x = -offset_x; x < offset_x; x++) {
|
||||
float xx = x * 2 + (y % 2 == 0 ? 1 : 0);
|
||||
float yy = y * h + h / 2;
|
||||
float h = (float) (Math.sqrt(3) / 2);
|
||||
for (int y = -offset_y; y < offset_y; y++) {
|
||||
for (int x = -offset_x; x < offset_x; x++) {
|
||||
float xx = x * 2 + (y % 2 == 0 ? 1 : 0);
|
||||
float yy = y * h + h / 2;
|
||||
|
||||
gl.uniform2f(hCenterPosition, xx * (mCellScale * 1.5f), yy * mCellScale);
|
||||
gl.uniform2f(hCenterPosition, xx * (mCellScale * 1.5f), yy * mCellScale);
|
||||
|
||||
//float alpha = 1 + (float) Math.log10(FastMath.clamp(
|
||||
// (float) Math.sqrt(xx * xx + yy * yy) / offset_y, 0.0f, 1.0f)) * 2;
|
||||
//float alpha = 1 + (float) Math.log10(FastMath.clamp(
|
||||
// (float) Math.sqrt(xx * xx + yy * yy) / offset_y, 0.0f, 1.0f)) * 2;
|
||||
|
||||
float alpha = (float) Math.sqrt(xx * xx + yy * yy) / offset_y;
|
||||
float alpha = (float) Math.sqrt(xx * xx + yy * yy) / offset_y;
|
||||
|
||||
float fy = (float) (y + offset_y) / (offset_y * 2);
|
||||
float fx = (float) (x + offset_x) / (offset_x * 2);
|
||||
float fz = FastMath.clamp(
|
||||
(float) (x < 0 || y < 0 ? 1 - Math.sqrt(fx * fx + fy
|
||||
* fy)
|
||||
: 0),
|
||||
0,
|
||||
1);
|
||||
float fy = (float) (y + offset_y) / (offset_y * 2);
|
||||
float fx = (float) (x + offset_x) / (offset_x * 2);
|
||||
float fz = FastMath.clamp(
|
||||
(float) (x < 0 || y < 0 ? 1 - Math.sqrt(fx * fx + fy
|
||||
* fy)
|
||||
: 0),
|
||||
0,
|
||||
1);
|
||||
|
||||
int c = 0xff << 24
|
||||
| (int) (0xff * fy) << 16
|
||||
| (int) (0xff * fx) << 8
|
||||
| (int) (0xff * fz);
|
||||
int c = 0xff << 24
|
||||
| (int) (0xff * fy) << 16
|
||||
| (int) (0xff * fx) << 8
|
||||
| (int) (0xff * fz);
|
||||
|
||||
GLUtils.setColor(hColorPosition, c, alpha);
|
||||
GLUtils.setColor(hColorPosition, c, alpha);
|
||||
|
||||
gl.drawArrays(GL.TRIANGLE_FAN, 0, 6);
|
||||
}
|
||||
}
|
||||
gl.drawArrays(GL.TRIANGLE_FAN, 0, 6);
|
||||
}
|
||||
}
|
||||
|
||||
GLUtils.setColor(hColorPosition, Color.DKGRAY, 0.3f);
|
||||
GLUtils.setColor(hColorPosition, Color.DKGRAY, 0.3f);
|
||||
|
||||
for (int y = -offset_y; y < offset_y; y++) {
|
||||
for (int x = -offset_x; x < offset_x; x++) {
|
||||
float xx = x * 2 + (y % 2 == 0 ? 1 : 0);
|
||||
float yy = y * h + h / 2;
|
||||
for (int y = -offset_y; y < offset_y; y++) {
|
||||
for (int x = -offset_x; x < offset_x; x++) {
|
||||
float xx = x * 2 + (y % 2 == 0 ? 1 : 0);
|
||||
float yy = y * h + h / 2;
|
||||
|
||||
gl.uniform2f(hCenterPosition, xx * (mCellScale * 1.5f), yy * mCellScale);
|
||||
gl.drawArrays(GL.LINE_LOOP, 0, 6);
|
||||
}
|
||||
}
|
||||
gl.uniform2f(hCenterPosition, xx * (mCellScale * 1.5f), yy * mCellScale);
|
||||
gl.drawArrays(GL.LINE_LOOP, 0, 6);
|
||||
}
|
||||
}
|
||||
|
||||
GLUtils.checkGlError("...");
|
||||
}
|
||||
GLUtils.checkGlError("...");
|
||||
}
|
||||
|
||||
private boolean init() {
|
||||
// Load the vertex/fragment shaders
|
||||
int programObject = GLShader.createProgram(vShaderStr, fShaderStr);
|
||||
private boolean init() {
|
||||
// Load the vertex/fragment shaders
|
||||
int programObject = GLShader.createProgram(vShaderStr, fShaderStr);
|
||||
|
||||
if (programObject == 0)
|
||||
return false;
|
||||
if (programObject == 0)
|
||||
return false;
|
||||
|
||||
// Handle for vertex position in shader
|
||||
hVertexPosition = gl.getAttribLocation(programObject, "a_pos");
|
||||
// Handle for vertex position in shader
|
||||
hVertexPosition = gl.getAttribLocation(programObject, "a_pos");
|
||||
|
||||
hMatrixPosition = gl.getUniformLocation(programObject, "u_mvp");
|
||||
hMatrixPosition = gl.getUniformLocation(programObject, "u_mvp");
|
||||
|
||||
hColorPosition = gl.getUniformLocation(programObject, "u_color");
|
||||
hColorPosition = gl.getUniformLocation(programObject, "u_color");
|
||||
|
||||
hCenterPosition = gl.getUniformLocation(programObject, "u_center");
|
||||
hCenterPosition = gl.getUniformLocation(programObject, "u_center");
|
||||
|
||||
// Store the program object
|
||||
mProgramObject = programObject;
|
||||
// Store the program object
|
||||
mProgramObject = programObject;
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private final static String vShaderStr = ""
|
||||
+ "#ifdef GLES\n"
|
||||
+ "precision mediump float;\n"
|
||||
+ "#endif\n"
|
||||
+ "uniform mat4 u_mvp;"
|
||||
+ "uniform vec2 u_center;"
|
||||
+ "attribute vec2 a_pos;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_Position = u_mvp * vec4(u_center + a_pos, 0.0, 1.0);"
|
||||
+ "}";
|
||||
private final static String vShaderStr = ""
|
||||
+ "#ifdef GLES\n"
|
||||
+ "precision mediump float;\n"
|
||||
+ "#endif\n"
|
||||
+ "uniform mat4 u_mvp;"
|
||||
+ "uniform vec2 u_center;"
|
||||
+ "attribute vec2 a_pos;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_Position = u_mvp * vec4(u_center + a_pos, 0.0, 1.0);"
|
||||
+ "}";
|
||||
|
||||
private final static String fShaderStr = ""
|
||||
+ "#ifdef GLES\n"
|
||||
+ "precision mediump float;\n"
|
||||
+ "#endif\n"
|
||||
+ "varying float alpha;"
|
||||
+ "uniform vec4 u_color;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_FragColor = u_color;"
|
||||
+ "}";
|
||||
private final static String fShaderStr = ""
|
||||
+ "#ifdef GLES\n"
|
||||
+ "precision mediump float;\n"
|
||||
+ "#endif\n"
|
||||
+ "varying float alpha;"
|
||||
+ "uniform vec4 u_color;"
|
||||
+ "void main()"
|
||||
+ "{"
|
||||
+ " gl_FragColor = u_color;"
|
||||
+ "}";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,30 +23,30 @@ import org.oscim.renderer.bucket.SymbolBucket;
|
||||
import org.oscim.renderer.bucket.SymbolItem;
|
||||
|
||||
public class SymbolRenderLayer extends BucketRenderer {
|
||||
boolean initialize = true;
|
||||
boolean initialize = true;
|
||||
|
||||
public SymbolRenderLayer() {
|
||||
SymbolBucket l = new SymbolBucket();
|
||||
buckets.set(l);
|
||||
public SymbolRenderLayer() {
|
||||
SymbolBucket l = new SymbolBucket();
|
||||
buckets.set(l);
|
||||
|
||||
SymbolItem it = SymbolItem.pool.get();
|
||||
it.billboard = false;
|
||||
SymbolItem it = SymbolItem.pool.get();
|
||||
it.billboard = false;
|
||||
|
||||
try {
|
||||
it.bitmap = CanvasAdapter.getBitmapAsset("jar:symbols/cafe.png");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
it.bitmap = CanvasAdapter.getBitmapAsset("jar:symbols/cafe.png");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
l.addSymbol(it);
|
||||
}
|
||||
}
|
||||
l.addSymbol(it);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
if (initialize) {
|
||||
initialize = false;
|
||||
mMapPosition.copy(v.pos);
|
||||
compile();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void update(GLViewport v) {
|
||||
if (initialize) {
|
||||
initialize = false;
|
||||
mMapPosition.copy(v.pos);
|
||||
compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user