Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core] port matrix and antialiasing cleanup #7444

Merged
merged 4 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/mbgl/style/layers/custom_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct CustomLayerRenderParameters {
double zoom;
double bearing;
double pitch;
double altitude;
double fieldOfView;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ejs": "^2.4.1",
"express": "^4.11.1",
"lodash": "^4.16.4",
"mapbox-gl": "mapbox/mapbox-gl-js#ab836206d415ca3a74257a3066d11a54ab2838cb",
"mapbox-gl": "mapbox/mapbox-gl-js#ef5582dd3bc5c15a3112e875ed66494dab8e9d0b",
"mapbox-gl-style-spec": "mapbox/mapbox-gl-style-spec#49e8b407bdbbe6f7c92dbcb56d3d51f425fc2653",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#da53a81453068f4c2b440f9077d6bd5e7e14ff3d",
"mkdirp": "^0.5.1",
Expand Down
4 changes: 2 additions & 2 deletions platform/darwin/src/MGLGeometry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/** Vertical field of view, measured in degrees, for determining the altitude
of the viewpoint.

TransformState::getProjMatrix() assumes a vertical field of view of
2 arctan ⅓ rad ≈ 36.9°, but MapKit uses a vertical field of view of 30°.
TransformState::getProjMatrix() has a variable vertical field of view that
defaults to 2 arctan ⅓ rad ≈ 36.9° but MapKit uses a vertical field of view of 30°.
flyTo() assumes a field of view of 2 arctan ½ rad. */
const CLLocationDegrees MGLAngularFieldOfView = 30;

Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLOpenGLStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef struct MGLStyleLayerDrawingContext {
double zoomLevel;
CLLocationDirection direction;
CGFloat pitch;
CGFloat perspectiveSkew;
CGFloat fieldOfView;
} MGLStyleLayerDrawingContext;

@interface MGLOpenGLStyleLayer : MGLStyleLayer
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLOpenGLStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void MGLDrawCustomStyleLayer(void *context, const mbgl::style::CustomLayerRender
.zoomLevel = params.zoom,
.direction = mbgl::util::wrap(params.bearing, 0., 360.),
.pitch = static_cast<CGFloat>(params.pitch),
.perspectiveSkew = static_cast<CGFloat>(params.altitude),
.fieldOfView = static_cast<CGFloat>(params.fieldOfView),
};
[layer drawInMapView:layer.mapView withContext:drawingContext];
}
Expand Down
37 changes: 23 additions & 14 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ void TransformState::matrixFor(mat4& matrix, const UnwrappedTileID& tileID) cons
}

void TransformState::getProjMatrix(mat4& projMatrix) const {
double halfFov = std::atan(0.5 / getAltitude());
double topHalfSurfaceDistance = std::sin(halfFov) * getAltitude() /
std::sin(M_PI / 2.0f - getPitch() - halfFov);
// Calculate z value of the farthest fragment that should be rendered.
double farZ = std::cos(M_PI / 2.0f - getPitch()) * topHalfSurfaceDistance + getAltitude();

matrix::perspective(projMatrix, 2.0f * std::atan((size.height / 2.0f) / getAltitude()),
double(size.width) / size.height, 0.1, farZ);
// Find the distance from the center point [width/2, height/2] to the
// center top point [width/2, 0] in Z units, using the law of sines.
// 1 Z unit is equivalent to 1 horizontal px at the center of the map
// (the distance between[width/2, height/2] and [width/2 + 1, height/2])
const double halfFov = getFieldOfView() / 2.0;
const double groundAngle = M_PI / 2.0 + getPitch();
const double topHalfSurfaceDistance = std::sin(halfFov) * getCameraToCenterDistance() / std::sin(M_PI - groundAngle - halfFov);

matrix::translate(projMatrix, projMatrix, 0, 0, -getAltitude());

// After the rotateX, z values are in pixel units. Convert them to
// altitude unites. 1 altitude unit = the screen height.
// Calculate z distance of the farthest fragment that should be rendered.
const double furthestDistance = std::cos(M_PI / 2 - getPitch()) * topHalfSurfaceDistance + getCameraToCenterDistance();
// Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance`
const double farZ = furthestDistance * 1.01;

matrix::perspective(projMatrix, getFieldOfView(), double(size.width) / size.height, 1, farZ);

const bool flippedY = viewportMode == ViewportMode::FlippedY;
matrix::scale(projMatrix, projMatrix, 1, flippedY ? 1 : -1,
1.0f / (rotatedNorth() ? size.width : size.height));
matrix::scale(projMatrix, projMatrix, 1, flippedY ? 1 : -1, 1);

matrix::translate(projMatrix, projMatrix, 0, 0, -getCameraToCenterDistance());

using NO = NorthOrientation;
switch (getNorthOrientation()) {
Expand Down Expand Up @@ -164,8 +169,12 @@ float TransformState::getAngle() const {
return angle;
}

float TransformState::getAltitude() const {
return altitude;
float TransformState::getFieldOfView() const {
return fov;
}

float TransformState::getCameraToCenterDistance() const {
return 0.5 * size.height / std::tan(fov / 2.0);
}

float TransformState::getPitch() const {
Expand Down
9 changes: 7 additions & 2 deletions src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class TransformState {

// Rotation
float getAngle() const;
float getAltitude() const;
float getFieldOfView() const;
float getCameraToCenterDistance() const;
float getPitch() const;

// State
Expand Down Expand Up @@ -109,7 +110,11 @@ class TransformState {
double x = 0, y = 0;
double angle = 0;
double scale = 1;
double altitude = 1.5;
// This fov value is somewhat arbitrary. The altitude of the camera used
// to be defined as 1.5 screen heights above the ground, which was an
// arbitrary choice. This is the fov equivalent to that value calculated with:
// `fov = 2 * arctan((height / 2) / (height * 1.5))`
double fov = 0.6435011087932844;
double pitch = 0.0;

// cache values for spherical mercator math
Expand Down
22 changes: 9 additions & 13 deletions src/mbgl/programs/line_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@ template <class Values, class...Args>
Values makeValues(const LinePaintProperties::Evaluated& properties,
const RenderTile& tile,
const TransformState& state,
const std::array<float, 2>& pixelsToGLUnits,
Args&&... args) {

mat2 antialiasingMatrix;
matrix::identity(antialiasingMatrix);
matrix::scale(antialiasingMatrix, antialiasingMatrix, 1.0, std::cos(state.getPitch()));
matrix::rotate(antialiasingMatrix, antialiasingMatrix, state.getAngle());

// calculate how much longer the real world distance is at the top of the screen
// than at the middle of the screen.
float topedgelength = std::sqrt(std::pow(state.getSize().height, 2.0f) / 4.0f * (1.0f + std::pow(state.getAltitude(), 2.0f)));
float x = state.getSize().height / 2.0f * std::tan(state.getPitch());

return Values {
uniforms::u_matrix::Value{
tile.translatedMatrix(properties.get<LineTranslate>(),
Expand All @@ -39,21 +30,22 @@ Values makeValues(const LinePaintProperties::Evaluated& properties,
uniforms::u_gapwidth::Value{ properties.get<LineGapWidth>() },
uniforms::u_blur::Value{ properties.get<LineBlur>() },
uniforms::u_offset::Value{ properties.get<LineOffset>() },
uniforms::u_antialiasingmatrix::Value{ antialiasingMatrix },
uniforms::u_ratio::Value{ 1.0f / tile.id.pixelsToTileUnits(1.0, state.getZoom()) },
uniforms::u_extra::Value{ (topedgelength + x) / topedgelength - 1.0f },
uniforms::u_gl_units_to_pixels::Value{{{ 1.0f / pixelsToGLUnits[0], 1.0f / pixelsToGLUnits[1] }}},
std::forward<Args>(args)...
};
}

LineProgram::UniformValues
LineProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
const RenderTile& tile,
const TransformState& state) {
const TransformState& state,
const std::array<float, 2>& pixelsToGLUnits) {
return makeValues<LineProgram::UniformValues>(
properties,
tile,
state,
pixelsToGLUnits,
uniforms::u_color::Value{ properties.get<LineColor>() }
);
}
Expand All @@ -63,6 +55,7 @@ LineSDFProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
float pixelRatio,
const RenderTile& tile,
const TransformState& state,
const std::array<float, 2>& pixelsToGLUnits,
const LinePatternPos& posA,
const LinePatternPos& posB,
float dashLineWidth,
Expand All @@ -84,6 +77,7 @@ LineSDFProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
properties,
tile,
state,
pixelsToGLUnits,
uniforms::u_color::Value{ properties.get<LineColor>() },
uniforms::u_patternscale_a::Value{ scaleA },
uniforms::u_patternscale_b::Value{ scaleB },
Expand All @@ -99,6 +93,7 @@ LinePatternProgram::UniformValues
LinePatternProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
const RenderTile& tile,
const TransformState& state,
const std::array<float, 2>& pixelsToGLUnits,
const SpriteAtlasPosition& posA,
const SpriteAtlasPosition& posB) {
std::array<float, 2> sizeA {{
Expand All @@ -115,6 +110,7 @@ LinePatternProgram::uniformValues(const LinePaintProperties::Evaluated& properti
properties,
tile,
state,
pixelsToGLUnits,
uniforms::u_pattern_tl_a::Value{ posA.tl },
uniforms::u_pattern_br_a::Value{ posA.br },
uniforms::u_pattern_tl_b::Value{ posB.tl },
Expand Down
17 changes: 8 additions & 9 deletions src/mbgl/programs/line_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ namespace uniforms {
MBGL_DEFINE_UNIFORM_SCALAR(float, u_ratio);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_width);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_gapwidth);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_extra);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_offset);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_tex_y_a);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_tex_y_b);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_sdfgamma);
MBGL_DEFINE_UNIFORM_SCALAR(float, u_fade);
MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_patternscale_a);
MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_patternscale_b);
MBGL_DEFINE_UNIFORM_MATRIX(double, 2, u_antialiasingmatrix);
MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_gl_units_to_pixels);
} // namespace uniforms

struct LineAttributes : gl::Attributes<
Expand Down Expand Up @@ -93,17 +92,17 @@ class LineProgram : public Program<
uniforms::u_gapwidth,
uniforms::u_blur,
uniforms::u_offset,
uniforms::u_antialiasingmatrix,
uniforms::u_ratio,
uniforms::u_extra,
uniforms::u_gl_units_to_pixels,
uniforms::u_color>>
{
public:
using Program::Program;

static UniformValues uniformValues(const style::LinePaintProperties::Evaluated&,
const RenderTile&,
const TransformState&);
const TransformState&,
const std::array<float, 2>& pixelsToGLUnits);
};

class LinePatternProgram : public Program<
Expand All @@ -117,9 +116,8 @@ class LinePatternProgram : public Program<
uniforms::u_gapwidth,
uniforms::u_blur,
uniforms::u_offset,
uniforms::u_antialiasingmatrix,
uniforms::u_ratio,
uniforms::u_extra,
uniforms::u_gl_units_to_pixels,
uniforms::u_pattern_tl_a,
uniforms::u_pattern_br_a,
uniforms::u_pattern_tl_b,
Expand All @@ -135,6 +133,7 @@ class LinePatternProgram : public Program<
static UniformValues uniformValues(const style::LinePaintProperties::Evaluated&,
const RenderTile&,
const TransformState&,
const std::array<float, 2>& pixelsToGLUnits,
const SpriteAtlasPosition& posA,
const SpriteAtlasPosition& posB);
};
Expand All @@ -150,9 +149,8 @@ class LineSDFProgram : public Program<
uniforms::u_gapwidth,
uniforms::u_blur,
uniforms::u_offset,
uniforms::u_antialiasingmatrix,
uniforms::u_ratio,
uniforms::u_extra,
uniforms::u_gl_units_to_pixels,
uniforms::u_color,
uniforms::u_patternscale_a,
uniforms::u_patternscale_b,
Expand All @@ -169,6 +167,7 @@ class LineSDFProgram : public Program<
float pixelRatio,
const RenderTile&,
const TransformState&,
const std::array<float, 2>& pixelsToGLUnits,
const LinePatternPos& posA,
const LinePatternPos& posB,
float dashLineWidth,
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/programs/symbol_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Values makeValues(const style::SymbolPropertyValues& values,
extrudeScale.fill(tile.id.pixelsToTileUnits(1, state.getZoom()) * scale);
} else {
extrudeScale = {{
pixelsToGLUnits[0] * scale * state.getAltitude(),
pixelsToGLUnits[1] * scale * state.getAltitude()
pixelsToGLUnits[0] * scale * state.getCameraToCenterDistance(),
pixelsToGLUnits[1] * scale * state.getCameraToCenterDistance()
}};
}

Expand Down Expand Up @@ -75,9 +75,9 @@ static SymbolSDFProgram::UniformValues makeSDFValues(const style::SymbolProperty
// The default gamma value has to be adjust for the current pixelratio so that we're not
// drawing blurry font on retina screens.
const float gammaBase = 0.105 * values.sdfScale / values.paintSize / pixelRatio;
const float gammaScale = values.pitchAlignment == AlignmentType::Map
const float gammaScale = (values.pitchAlignment == AlignmentType::Map
? 1.0 / std::cos(state.getPitch())
: 1.0;
: 1.0) / state.getCameraToCenterDistance();

return makeValues<SymbolSDFProgram::UniformValues>(
values,
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/renderer/painter_circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void Painter::renderCircle(PaintParameters& parameters,
uniforms::u_scale_with_map::Value{ scaleWithMap },
uniforms::u_extrude_scale::Value{ scaleWithMap
? std::array<float, 2> {{
pixelsToGLUnits[0] * state.getAltitude(),
pixelsToGLUnits[1] * state.getAltitude()
pixelsToGLUnits[0] * state.getCameraToCenterDistance(),
pixelsToGLUnits[1] * state.getCameraToCenterDistance()
}}
: pixelsToGLUnits }
},
Expand Down
5 changes: 4 additions & 1 deletion src/mbgl/renderer/painter_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void Painter::renderLine(PaintParameters& parameters,
frame.pixelRatio,
tile,
state,
pixelsToGLUnits,
posA,
posB,
layer.impl->dashLineWidth,
Expand All @@ -72,6 +73,7 @@ void Painter::renderLine(PaintParameters& parameters,
properties,
tile,
state,
pixelsToGLUnits,
*posA,
*posB));

Expand All @@ -80,7 +82,8 @@ void Painter::renderLine(PaintParameters& parameters,
LineProgram::uniformValues(
properties,
tile,
state));
state,
pixelsToGLUnits));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/style/layers/custom_layer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void CustomLayer::Impl::render(const TransformState& state) const {
parameters.zoom = state.getZoom();
parameters.bearing = -state.getAngle() * util::RAD2DEG;
parameters.pitch = state.getPitch();
parameters.altitude = state.getAltitude();
parameters.fieldOfView = state.getFieldOfView();

renderFn(context, parameters);
}
Expand Down
14 changes: 7 additions & 7 deletions test/map/transform.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ TEST(Transform, Padding) {

const LatLng shiftedCenter = transform.getLatLng(padding);
ASSERT_NE(trueCenter.latitude, shiftedCenter.latitude);
ASSERT_DOUBLE_EQ(trueCenter.longitude, shiftedCenter.longitude);
ASSERT_NEAR(trueCenter.longitude, shiftedCenter.longitude, 1e-9);
ASSERT_DOUBLE_EQ(manualShiftedCenter.latitude, shiftedCenter.latitude);
ASSERT_DOUBLE_EQ(manualShiftedCenter.longitude, shiftedCenter.longitude);
}
Expand Down Expand Up @@ -401,12 +401,12 @@ TEST(Transform, Antimeridian) {

const LatLng coordinateSanFrancisco { 37.7833, -122.4167 };
ScreenCoordinate pixelSF = transform.latLngToScreenCoordinate(coordinateSanFrancisco);
ASSERT_DOUBLE_EQ(151.79409149185352, pixelSF.x);
ASSERT_DOUBLE_EQ(383.76774094913071, pixelSF.y);
ASSERT_NEAR(151.79409149185352, pixelSF.x, 1e-2);
ASSERT_NEAR(383.76774094913071, pixelSF.y, 1e-2);

transform.setLatLng({ 0, -181 });
ScreenCoordinate pixelSFBackwards = transform.latLngToScreenCoordinate(coordinateSanFrancisco);
ASSERT_DOUBLE_EQ(666.63617954008976, pixelSFBackwards.x);
ASSERT_NEAR(666.63617954008976, pixelSFBackwards.x, 1e-2);
ASSERT_DOUBLE_EQ(pixelSF.y, pixelSFBackwards.y);

transform.setLatLng({ 0, 179 });
Expand All @@ -417,12 +417,12 @@ TEST(Transform, Antimeridian) {
const LatLng coordinateWaikiri{ -16.9310, 179.9787 };
transform.setLatLngZoom(coordinateWaikiri, 10);
ScreenCoordinate pixelWaikiri = transform.latLngToScreenCoordinate(coordinateWaikiri);
ASSERT_DOUBLE_EQ(500.00000000007759, pixelWaikiri.x);
ASSERT_DOUBLE_EQ(500, pixelWaikiri.y);
ASSERT_NEAR(500, pixelWaikiri.x, 1e-2);
ASSERT_NEAR(500, pixelWaikiri.y, 1e-2);

transform.setLatLng({ coordinateWaikiri.latitude, 180.0213 });
ScreenCoordinate pixelWaikiriForwards = transform.latLngToScreenCoordinate(coordinateWaikiri);
ASSERT_DOUBLE_EQ(437.95953728819512, pixelWaikiriForwards.x);
ASSERT_NEAR(437.95953728819512, pixelWaikiriForwards.x, 1e-2);
ASSERT_DOUBLE_EQ(pixelWaikiri.y, pixelWaikiriForwards.y);
LatLng coordinateFromPixel = transform.screenCoordinateToLatLng(pixelWaikiriForwards);
ASSERT_NEAR(coordinateWaikiri.latitude, coordinateFromPixel.latitude, 0.000001);
Expand Down
2 changes: 2 additions & 0 deletions test/util/tile_cover.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ TEST(TileCover, WorldZ0) {
TEST(TileCover, Pitch) {
Transform transform;
transform.resize({ 512, 512 });
// slightly offset center so that tile order is better defined
transform.setLatLng({ 0.01, -0.01 });
transform.setZoom(2);
transform.setPitch(40.0 * M_PI / 180.0);

Expand Down