Skip to content

Commit

Permalink
Circle map style: use GL quads or GL points opensciencemap#122
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Dec 8, 2016
1 parent 4cd1146 commit b6dc72b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
4 changes: 4 additions & 0 deletions vtm-playground/src/org/oscim/test/CircleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.oscim.test;

import org.oscim.backend.GLAdapter;
import org.oscim.backend.canvas.Color;
import org.oscim.core.GeometryBuffer;
import org.oscim.gdx.GdxMap;
Expand Down Expand Up @@ -73,6 +74,9 @@ protected void createLayers() {
}

public static void main(String[] args) {
// Draw circles with quads or points
GLAdapter.CIRCLE_QUADS = false;

GdxMapApp.init();
GdxMapApp.run(new CircleTest());
}
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions vtm/resources/assets/shaders/circle_quad.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifdef GLES
precision highp float;
#endif
uniform mat4 u_mvp;
attribute vec2 a_pos;
varying vec2 v_pos;
void main() {
gl_Position = u_mvp * vec4(a_pos, 0.0, 1.0);
v_pos = a_pos;
}

$$

#ifdef GLES
precision highp float;
#endif
uniform vec4 u_color;
uniform float u_scale;
varying vec2 v_pos;
void main() {
gl_FragColor = u_color;
}
6 changes: 6 additions & 0 deletions vtm/src/org/oscim/backend/GLAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand Down Expand Up @@ -35,6 +36,11 @@ public class GLAdapter {
*/
public static boolean NO_BUFFER_SUB_DATA = false;

/**
* Draw circles with quads or points.
*/
public static boolean CIRCLE_QUADS = false;

public static void init(GL gl20) {
gl = gl20;
}
Expand Down
59 changes: 47 additions & 12 deletions vtm/src/org/oscim/renderer/bucket/CircleBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CircleBucket extends RenderBucket {
public CircleStyle circle;

public CircleBucket(int level) {
super(RenderBucket.CIRCLE, true, false);
super(RenderBucket.CIRCLE, true, GLAdapter.CIRCLE_QUADS);
this.level = level;
}

Expand All @@ -49,29 +49,58 @@ public void addCircle(GeometryBuffer geom) {
float x = geom.getPointX(0);
float y = geom.getPointY(0);

vertexItems.add((short) (x * COORD_SCALE), (short) (y * COORD_SCALE));
indiceItems.add((short) numVertices++);
numIndices++;
if (GLAdapter.CIRCLE_QUADS) {
// Create quad
vertexItems.add((short) ((x + circle.radius) * COORD_SCALE), (short) ((y - circle.radius) * COORD_SCALE));
int ne = numVertices++;
vertexItems.add((short) ((x - circle.radius) * COORD_SCALE), (short) ((y - circle.radius) * COORD_SCALE));
int nw = numVertices++;
vertexItems.add((short) ((x - circle.radius) * COORD_SCALE), (short) ((y + circle.radius) * COORD_SCALE));
int sw = numVertices++;
vertexItems.add((short) ((x + circle.radius) * COORD_SCALE), (short) ((y + circle.radius) * COORD_SCALE));
int se = numVertices++;

indiceItems.add((short) ne);
numIndices++;
indiceItems.add((short) nw);
numIndices++;
indiceItems.add((short) sw);
numIndices++;

indiceItems.add((short) sw);
numIndices++;
indiceItems.add((short) se);
numIndices++;
indiceItems.add((short) ne);
numIndices++;
} else {
// Use point
vertexItems.add((short) (x * COORD_SCALE), (short) (y * COORD_SCALE));
indiceItems.add((short) numVertices++);
numIndices++;
}
}

public static class Renderer {
static Shader shader;

static boolean init() {
shader = new Shader("circle");
shader = new Shader(GLAdapter.CIRCLE_QUADS ? "circle_quad" : "circle_point");
return true;
}

public static class Shader extends GLShader {
int uMVP, uColor, uScale, aPos;

Shader(String shaderFile) {
gl.enable(GL.VERTEX_PROGRAM_POINT_SIZE);
if (!GLAdapter.CIRCLE_QUADS)
gl.enable(GL.VERTEX_PROGRAM_POINT_SIZE);

// OpenGL needs GLSL version 120
String version = null;
if (GLAdapter.GDX_DESKTOP_QUIRKS)
if (!GLAdapter.CIRCLE_QUADS && GLAdapter.GDX_DESKTOP_QUIRKS) {
// OpenGL requires GLSL version 120 for gl_PointCoord
version = "120";
}

if (!createVersioned(shaderFile, version))
return;
Expand Down Expand Up @@ -106,10 +135,16 @@ public static RenderBucket draw(RenderBucket b, GLViewport v) {
gl.vertexAttribPointer(s.aPos, 2, GL.SHORT,
false, 0, cb.vertexOffset);

gl.drawElements(GL.POINTS,
cb.numIndices,
GL.UNSIGNED_SHORT,
cb.indiceOffset);
if (GLAdapter.CIRCLE_QUADS)
gl.drawElements(GL.TRIANGLES,
cb.numIndices,
GL.UNSIGNED_SHORT,
cb.indiceOffset);
else
gl.drawElements(GL.POINTS,
cb.numIndices,
GL.UNSIGNED_SHORT,
cb.indiceOffset);
}

return b;
Expand Down

0 comments on commit b6dc72b

Please sign in to comment.