summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2016-11-03 15:22:21 +0200
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2016-11-07 09:38:04 +0100
commit505bf7db7a33f977a3339528bbae17bdfa30e2e0 (patch)
treefceaab6babc8dcb65304305a72c5358d50253998
parentee073f6b96f5e70a283def9cd2acebbae29b97d1 (diff)
downloadqtlocation-mapboxgl-505bf7db7a33f977a3339528bbae17bdfa30e2e0.tar.gz
[android] update custom layer example with invalidation example
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/customlayer/CustomLayerActivity.java9
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer.java3
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_custom_layer.xml9
-rw-r--r--platform/android/src/example_custom_layer.cpp35
4 files changed, 52 insertions, 4 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/customlayer/CustomLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/customlayer/CustomLayerActivity.java
index 778ab0ad04..ea8e39275f 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/customlayer/CustomLayerActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/customlayer/CustomLayerActivity.java
@@ -146,6 +146,15 @@ public class CustomLayerActivity extends AppCompatActivity {
case R.id.action_update_layer:
updateLayer();
return true;
+ case R.id.action_set_color_red:
+ ExampleCustomLayer.setColor(1, 0, 0, 1);
+ return true;
+ case R.id.action_set_color_green:
+ ExampleCustomLayer.setColor(0, 1, 0, 1);
+ return true;
+ case R.id.action_set_color_blue:
+ ExampleCustomLayer.setColor(0, 0, 1, 1);
+ return true;
default:
return super.onOptionsItemSelected(item);
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer.java
index 2901060459..a75061bc9b 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer.java
@@ -6,6 +6,9 @@ public class ExampleCustomLayer {
}
public native static long createContext();
+
+ public native static void setColor(float red, float green, float blue, float alpha);
+
public static long InitializeFunction;
public static long RenderFunction;
public static long DeinitializeFunction;
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_custom_layer.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_custom_layer.xml
index 43465cce62..ec1f756333 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_custom_layer.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_custom_layer.xml
@@ -5,4 +5,13 @@
<item
android:id="@+id/action_update_layer"
android:title="Update layer (invalidate)" />
+ <item
+ android:id="@+id/action_set_color_red"
+ android:title="Red" />
+ <item
+ android:id="@+id/action_set_color_green"
+ android:title="Green" />
+ <item
+ android:id="@+id/action_set_color_blue"
+ android:title="Blue" />
</menu> \ No newline at end of file
diff --git a/platform/android/src/example_custom_layer.cpp b/platform/android/src/example_custom_layer.cpp
index 516a8f2cd5..0fb0287f3d 100644
--- a/platform/android/src/example_custom_layer.cpp
+++ b/platform/android/src/example_custom_layer.cpp
@@ -1,14 +1,17 @@
#include <jni.h>
#include <GLES2/gl2.h>
+#include <mbgl/platform/log.hpp>
+
#include <mbgl/style/layers/custom_layer.hpp>
static const GLchar * vertexShaderSource = "attribute vec2 a_pos; void main() { gl_Position = vec4(a_pos, 0, 1); }";
-static const GLchar * fragmentShaderSource = "void main() { gl_FragColor = vec4(0, 1, 0, 1); }";
+static const GLchar * fragmentShaderSource = "uniform vec4 fill_color; void main() { gl_FragColor = fill_color; }";
class ExampleCustomLayer {
public:
~ExampleCustomLayer() {
+ mbgl::Log::Info(mbgl::Event::General, "~ExampleCustomLayer");
if (program) {
glDeleteBuffers(1, &buffer);
glDetachShader(program, vertexShader);
@@ -20,6 +23,7 @@ public:
}
void initialize() {
+ mbgl::Log::Info(mbgl::Event::General, "Initialize");
program = glCreateProgram();
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
@@ -32,6 +36,7 @@ public:
glAttachShader(program, fragmentShader);
glLinkProgram(program);
a_pos = glGetAttribLocation(program, "a_pos");
+ fill_color = glGetUniformLocation(program, "fill_color");
GLfloat background[] = { -1,-1, 1,-1, -1,1, 1,1 };
glGenBuffers(1, &buffer);
@@ -40,12 +45,15 @@ public:
}
void render() {
+ mbgl::Log::Info(mbgl::Event::General, "Render");
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(a_pos);
glVertexAttribPointer(a_pos, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
+ glUniform4fv(fill_color, 1, color);
+
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@@ -54,35 +62,54 @@ public:
GLuint fragmentShader = 0;
GLuint buffer = 0;
GLuint a_pos = 0;
+ GLuint fill_color = 0;
+
+ static GLfloat color[];
};
+GLfloat ExampleCustomLayer::color[] = { 0.0f, 1.0f, 0.0f, 1.0f };
+
jlong JNICALL nativeCreateContext(JNIEnv*, jobject) {
- return reinterpret_cast<jlong>(new ExampleCustomLayer());
+ mbgl::Log::Info(mbgl::Event::General, "nativeCreateContext");
+ return reinterpret_cast<jlong>(new ExampleCustomLayer());
+}
+
+void JNICALL nativeSetColor(JNIEnv*, jobject, jfloat red, jfloat green, jfloat blue, jfloat alpha) {
+ mbgl::Log::Info(mbgl::Event::General, "nativeSetColor: %.2f, %.2f, %.2f, %.2f", red, green, blue, alpha);
+ ExampleCustomLayer::color[0] = red;
+ ExampleCustomLayer::color[1] = green;
+ ExampleCustomLayer::color[2] = blue;
+ ExampleCustomLayer::color[3] = alpha;
}
void nativeInitialize(void *context) {
+ mbgl::Log::Info(mbgl::Event::General, "nativeInitialize");
reinterpret_cast<ExampleCustomLayer*>(context)->initialize();
}
void nativeRender(void *context, const mbgl::style::CustomLayerRenderParameters& /*parameters*/) {
+ mbgl::Log::Info(mbgl::Event::General, "nativeRender");
reinterpret_cast<ExampleCustomLayer*>(context)->render();
}
void nativeDenitialize(void *context) {
+ mbgl::Log::Info(mbgl::Event::General, "nativeDeinitialize");
delete reinterpret_cast<ExampleCustomLayer*>(context);
}
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
+ mbgl::Log::Info(mbgl::Event::General, "OnLoad");
JNIEnv *env = nullptr;
vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
jclass customLayerClass = env->FindClass("com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer");
JNINativeMethod methods[] = {
- {"createContext", "()J", reinterpret_cast<void *>(&nativeCreateContext)}
+ {"createContext", "()J", reinterpret_cast<void *>(&nativeCreateContext)},
+ {"setColor", "(FFFF)V", reinterpret_cast<void *>(&nativeSetColor)}
};
- if (env->RegisterNatives(customLayerClass, methods, 1) < 0) {
+ if (env->RegisterNatives(customLayerClass, methods, 2) < 0) {
env->ExceptionDescribe();
return JNI_ERR;
}