summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mbgl/platform/default/glfw_view.hpp5
-rw-r--r--platform/default/glfw_view.cpp52
2 files changed, 57 insertions, 0 deletions
diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp
index 2f74404201..ae5bf340d5 100644
--- a/include/mbgl/platform/default/glfw_view.hpp
+++ b/include/mbgl/platform/default/glfw_view.hpp
@@ -52,6 +52,9 @@ private:
makeSpriteImage(int width, int height, float pixelRatio);
void nextOrientation();
+ void toggleClipMasks();
+
+ void renderClipMasks();
void addRandomPointAnnotations(int count);
void addRandomShapeAnnotations(int count);
@@ -81,6 +84,8 @@ private:
int fbHeight;
float pixelRatio;
+ bool showClipMasks = false;
+
double lastX = 0, lastY = 0;
double lastClick = -1;
diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp
index 3173bfddee..a4e8d7059f 100644
--- a/platform/default/glfw_view.cpp
+++ b/platform/default/glfw_view.cpp
@@ -6,6 +6,7 @@
#include <mbgl/gl/gl_values.hpp>
#include <mbgl/gl/gl_helper.hpp>
#include <mbgl/platform/log.hpp>
+#include <mbgl/platform/platform.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/chrono.hpp>
@@ -136,6 +137,9 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
if (!mods)
view->map->resetPosition();
break;
+ case GLFW_KEY_C:
+ view->toggleClipMasks();
+ break;
case GLFW_KEY_S:
if (view->changeStyleCallback)
view->changeStyleCallback();
@@ -232,6 +236,11 @@ void GLFWView::nextOrientation() {
}
}
+void GLFWView::toggleClipMasks() {
+ showClipMasks = !showClipMasks;
+ map->update(mbgl::Update::Repaint);
+}
+
void GLFWView::addRandomCustomPointAnnotations(int count) {
std::vector<mbgl::PointAnnotation> points;
@@ -439,9 +448,52 @@ void GLFWView::beforeRender() {
}
void GLFWView::afterRender() {
+ if (showClipMasks) {
+ renderClipMasks();
+ }
+
glfwSwapBuffers(window);
}
+void GLFWView::renderClipMasks() {
+ // Read the stencil buffer
+ auto pixels = std::make_unique<uint8_t[]>(fbWidth * fbHeight);
+ glReadPixels(0, // GLint x
+ 0, // GLint y
+ fbWidth, // GLsizei width
+ fbHeight, // GLsizei height
+ GL_STENCIL_INDEX, // GLenum format
+ GL_UNSIGNED_BYTE, // GLenum type
+ pixels.get() // GLvoid * data
+ );
+
+ // Scale the Stencil buffer to cover the entire color space.
+ auto it = pixels.get();
+ auto end = it + fbWidth * fbHeight;
+ const auto factor = 255.0f / *std::max_element(it, end);
+ for (; it != end; ++it) {
+ *it *= factor;
+ }
+
+ using namespace mbgl::gl;
+ Preserve<PixelZoom> pixelZoom;
+ Preserve<RasterPos> rasterPos;
+ Preserve<StencilTest> stencilTest;
+ Preserve<DepthTest> depthTest;
+ Preserve<Program> program;
+ Preserve<ColorMask> colorMask;
+
+ MBGL_CHECK_ERROR(glPixelZoom(1.0f, 1.0f));
+ MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, -1.0f));
+ MBGL_CHECK_ERROR(glDisable(GL_STENCIL_TEST));
+ MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST));
+ MBGL_CHECK_ERROR(glUseProgram(0));
+ MBGL_CHECK_ERROR(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
+ MBGL_CHECK_ERROR(glWindowPos2i(0, 0));
+
+ MBGL_CHECK_ERROR(glDrawPixels(fbWidth, fbHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels.get()));
+}
+
void GLFWView::report(float duration) {
frames++;
frameTime += duration;