[core] Added Viewport mode

Satisfies embedding platforms that requires the viewport coordinate
systems to be set according to its standards e.g. viewport mirrored
vertically.
This commit is contained in:
Bruno de Oliveira Abinader
2016-05-11 14:10:17 +03:00
parent 659c2568ec
commit c13dd32242
14 changed files with 85 additions and 33 deletions

View File

@@ -34,7 +34,8 @@ public:
explicit Map(View&, FileSource&,
MapMode mapMode = MapMode::Continuous,
GLContextMode contextMode = GLContextMode::Unique,
ConstrainMode constrainMode = ConstrainMode::HeightOnly);
ConstrainMode constrainMode = ConstrainMode::HeightOnly,
ViewportMode viewportMode = ViewportMode::Default);
~Map();
// Register a callback that will get called (on the render thread) when all resources have
@@ -117,11 +118,15 @@ public:
// North Orientation
void setNorthOrientation(NorthOrientation);
NorthOrientation getNorthOrientation() const;
// Constrain mode
void setConstrainMode(ConstrainMode);
ConstrainMode getConstrainMode() const;
// Viewport mode
void setViewportMode(ViewportMode);
ViewportMode getViewportMode() const;
// Size
uint16_t getWidth() const;
uint16_t getHeight() const;

View File

@@ -29,6 +29,13 @@ enum class ConstrainMode : EnumType {
WidthAndHeight,
};
// Satisfies embedding platforms that requires the viewport coordinate systems
// to be set according to its standards.
enum class ViewportMode : EnumType {
Default,
FlippedY,
};
enum class MapDebugOptions : EnumType {
NoDebug = 0,
TileBorders = 1 << 1,

View File

@@ -96,6 +96,7 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_)
printf("- Press `N` to reset north\n");
printf("- Press `R` to toggle any available `night` style class\n");
printf("- Press `Z` to cycle through north orientations\n");
printf("- Prezz `X` to cycle through the viewport modes\n");
printf("- Press `A` to cycle through Mapbox offices in the world + dateline monument\n");
printf("\n");
printf("- Press `1` through `6` to add increasing numbers of point annotations for testing\n");

View File

@@ -380,7 +380,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
// setup mbgl map
mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource;
_mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None);
_mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None, mbgl::ViewportMode::Default);
[self validateTileCacheSize];
// start paused if in IB

View File

@@ -261,7 +261,7 @@ public:
[[NSFileManager defaultManager] removeItemAtURL:legacyCacheURL error:NULL];
mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource;
_mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None);
_mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None, mbgl::ViewportMode::Default);
[self validateTileCacheSize];
// Install the OpenGL layer. Interface Builder’s synchronous drawing means

View File

@@ -34,7 +34,7 @@ enum class RenderState {
class Map::Impl : public Style::Observer {
public:
Impl(View&, FileSource&, MapMode, GLContextMode, ConstrainMode);
Impl(View&, FileSource&, MapMode, GLContextMode, ConstrainMode, ViewportMode);
void onResourceLoaded() override;
void onResourceError(std::exception_ptr) override;
@@ -76,16 +76,16 @@ public:
bool loading = false;
};
Map::Map(View& view, FileSource& fileSource, MapMode mapMode, GLContextMode contextMode, ConstrainMode constrainMode)
: impl(std::make_unique<Impl>(view, fileSource, mapMode, contextMode, constrainMode)) {
Map::Map(View& view, FileSource& fileSource, MapMode mapMode, GLContextMode contextMode, ConstrainMode constrainMode, ViewportMode viewportMode)
: impl(std::make_unique<Impl>(view, fileSource, mapMode, contextMode, constrainMode, viewportMode)) {
view.initialize(this);
update(Update::Dimensions);
}
Map::Impl::Impl(View& view_, FileSource& fileSource_, MapMode mode_, GLContextMode contextMode_, ConstrainMode constrainMode_)
Map::Impl::Impl(View& view_, FileSource& fileSource_, MapMode mode_, GLContextMode contextMode_, ConstrainMode constrainMode_, ViewportMode viewportMode_)
: view(view_),
fileSource(fileSource_),
transform(view, constrainMode_),
transform(view, constrainMode_, viewportMode_),
mode(mode_),
contextMode(contextMode_),
pixelRatio(view.getPixelRatio()),
@@ -638,6 +638,17 @@ ConstrainMode Map::getConstrainMode() const {
return impl->transform.getConstrainMode();
}
#pragma mark - Viewport mode
void Map::setViewportMode(mbgl::ViewportMode mode) {
impl->transform.setViewportMode(mode);
update(Update::Repaint);
}
ViewportMode Map::getViewportMode() const {
return impl->transform.getViewportMode();
}
#pragma mark - Projection
double Map::getMetersPerPixelAtLatitude(double lat, double zoom) const {

View File

@@ -36,9 +36,9 @@ static double _normalizeAngle(double angle, double anchorAngle)
return angle;
}
Transform::Transform(View &view_, ConstrainMode constrainMode)
Transform::Transform(View &view_, ConstrainMode constrainMode, ViewportMode viewportMode)
: view(view_)
, state(constrainMode)
, state(constrainMode, viewportMode)
{
}
@@ -555,6 +555,16 @@ ConstrainMode Transform::getConstrainMode() const {
return state.getConstrainMode();
}
#pragma mark - Viewport mode
void Transform::setViewportMode(mbgl::ViewportMode mode) {
state.viewportMode = mode;
}
ViewportMode Transform::getViewportMode() const {
return state.getViewportMode();
}
#pragma mark - Transition
void Transform::startTransition(const CameraOptions& camera,

View File

@@ -20,7 +20,7 @@ class View;
class Transform : private util::noncopyable {
public:
Transform(View&, ConstrainMode);
Transform(View&, ConstrainMode, ViewportMode);
// Map view
bool resize(std::array<uint16_t, 2> size);
@@ -132,11 +132,15 @@ public:
// North Orientation
void setNorthOrientation(NorthOrientation);
NorthOrientation getNorthOrientation() const;
// Constrain mode
void setConstrainMode(ConstrainMode);
ConstrainMode getConstrainMode() const;
// Viewport mode
void setViewportMode(ViewportMode);
ViewportMode getViewportMode() const;
// Transitions
bool inTransition() const;
Update updateTransitions(const TimePoint& now);

View File

@@ -7,8 +7,9 @@
namespace mbgl {
TransformState::TransformState(ConstrainMode constrainMode_)
TransformState::TransformState(ConstrainMode constrainMode_, ViewportMode viewportMode_)
: constrainMode(constrainMode_)
, viewportMode(viewportMode_)
{
}
@@ -39,7 +40,8 @@ void TransformState::getProjMatrix(mat4& projMatrix) const {
// After the rotateX, z values are in pixel units. Convert them to
// altitude unites. 1 altitude unit = the screen height.
matrix::scale(projMatrix, projMatrix, 1, -1, 1.0f / (rotatedNorth() ? getWidth() : getHeight()));
const bool flippedY = viewportMode == ViewportMode::FlippedY;
matrix::scale(projMatrix, projMatrix, 1, flippedY ? 1 : -1, 1.0f / (rotatedNorth() ? getWidth() : getHeight()));
using NO = NorthOrientation;
switch (getNorthOrientation()) {
@@ -89,6 +91,12 @@ ConstrainMode TransformState::getConstrainMode() const {
return constrainMode;
}
#pragma mark - ViewportMode
ViewportMode TransformState::getViewportMode() const {
return viewportMode;
}
#pragma mark - Position
LatLng TransformState::getLatLng(LatLng::WrapMode wrapMode) const {

View File

@@ -18,7 +18,7 @@ class TransformState {
friend class Transform;
public:
TransformState(ConstrainMode = ConstrainMode::HeightOnly);
TransformState(ConstrainMode = ConstrainMode::HeightOnly, ViewportMode = ViewportMode::Default);
// Matrix
void matrixFor(mat4&, const UnwrappedTileID&) const;
@@ -31,10 +31,13 @@ public:
// North Orientation
NorthOrientation getNorthOrientation() const;
double getNorthOrientationAngle() const;
// Constrain mode
ConstrainMode getConstrainMode() const;
// Viewport mode
ViewportMode getViewportMode() const;
// Position
LatLng getLatLng(LatLng::WrapMode = LatLng::Unwrapped) const;
double pixel_x() const;
@@ -99,6 +102,7 @@ private:
private:
ConstrainMode constrainMode;
ViewportMode viewportMode;
// animation state
bool rotating = false;

View File

@@ -51,7 +51,8 @@ void Painter::renderSDF(SymbolBucket &bucket,
gammaScale = 1.0f;
matrix::rotate_z(exMatrix, exMatrix, state.getNorthOrientationAngle());
}
matrix::scale(exMatrix, exMatrix, s, s, 1);
const bool flippedY = !skewed && state.getViewportMode() == ViewportMode::FlippedY;
matrix::scale(exMatrix, exMatrix, s, flippedY ? -s : s, 1);
// If layerStyle.size > bucket.info.fontSize then labels may collide
float fontSize = paintSize;
@@ -217,7 +218,8 @@ void Painter::renderSymbol(SymbolBucket& bucket,
matrix::rotate_z(exMatrix, exMatrix, state.getNorthOrientationAngle());
s = state.getAltitude();
}
matrix::scale(exMatrix, exMatrix, s, s, 1);
const bool flippedY = !skewed && state.getViewportMode() == ViewportMode::FlippedY;
matrix::scale(exMatrix, exMatrix, s, flippedY ? -s : s, 1);
matrix::scale(exMatrix, exMatrix, fontScale, fontScale, 1.0f);

View File

@@ -8,7 +8,7 @@ using namespace mbgl;
TEST(Transform, InvalidScale) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
ASSERT_DOUBLE_EQ(0, transform.getLatLng().latitude);
ASSERT_DOUBLE_EQ(0, transform.getLatLng().longitude);
@@ -48,7 +48,7 @@ TEST(Transform, InvalidScale) {
TEST(Transform, InvalidLatLng) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
ASSERT_DOUBLE_EQ(0, transform.getLatLng().latitude);
ASSERT_DOUBLE_EQ(0, transform.getLatLng().longitude);
@@ -84,7 +84,7 @@ TEST(Transform, InvalidLatLng) {
TEST(Transform, InvalidBearing) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
ASSERT_DOUBLE_EQ(0, transform.getLatLng().latitude);
ASSERT_DOUBLE_EQ(0, transform.getLatLng().longitude);
@@ -111,7 +111,7 @@ TEST(Transform, PerspectiveProjection) {
MockView view;
LatLng loc;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
transform.setScale(2 << 9);
transform.setPitch(0.9);
@@ -142,7 +142,7 @@ TEST(Transform, PerspectiveProjection) {
TEST(Transform, UnwrappedLatLng) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
transform.setScale(2 << 9);
transform.setPitch(0.9);
@@ -175,7 +175,7 @@ TEST(Transform, ConstrainHeightOnly) {
MockView view;
LatLng loc;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
transform.setScale(std::pow(2, util::MAX_ZOOM));
@@ -194,7 +194,7 @@ TEST(Transform, ConstrainWidthAndHeight) {
MockView view;
LatLng loc;
Transform transform(view, ConstrainMode::WidthAndHeight);
Transform transform(view, ConstrainMode::WidthAndHeight, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
transform.setScale(std::pow(2, util::MAX_ZOOM));
@@ -211,7 +211,7 @@ TEST(Transform, ConstrainWidthAndHeight) {
TEST(Transform, Anchor) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
const LatLng latLng { 10, -100 };
@@ -312,7 +312,7 @@ TEST(Transform, Anchor) {
TEST(Transform, Padding) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
ASSERT_DOUBLE_EQ(0, transform.getLatLng().latitude);
@@ -350,7 +350,7 @@ TEST(Transform, Padding) {
TEST(Transform, MoveBy) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
transform.setLatLngZoom({ 0, 0 }, 10);
@@ -378,7 +378,7 @@ TEST(Transform, MoveBy) {
TEST(Transform, Antimeridian) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
transform.setLatLngZoom({ 0, 0 }, 1);
@@ -422,7 +422,7 @@ TEST(Transform, Antimeridian) {
TEST(Transform, Camera) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({{ 1000, 1000 }});
const LatLng latLng1 { 45, 135 };

View File

@@ -25,7 +25,7 @@ public:
StubFileSource fileSource;
StubStyleObserver observer;
MockView view;
Transform transform { view, ConstrainMode::HeightOnly };
Transform transform { view, ConstrainMode::HeightOnly, ViewportMode::Default };
TransformState transformState;
Worker worker { 1 };
gl::TexturePool texturePool;

View File

@@ -30,7 +30,7 @@ TEST(TileCover, WorldZ0) {
TEST(TileCover, Pitch) {
MockView view;
Transform transform(view, ConstrainMode::HeightOnly);
Transform transform(view, ConstrainMode::HeightOnly, ViewportMode::Default);
transform.resize({ { 512, 512 } });
transform.setZoom(2);
transform.setPitch(40.0 * M_PI / 180.0);