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

[core] Update icon shader code #5196

Merged
merged 2 commits into from
Jun 1, 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"aws-sdk": "^2.3.5",
"express": "^4.11.1",
"mapbox-gl-shaders": "mapbox/mapbox-gl-shaders#13aad76282c1b6c4d38fd46f373325dfec69ab01",
"mapbox-gl-shaders": "mapbox/mapbox-gl-shaders#36278b864e60e1dba937a6863064c03d69526854",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#b3441d9a285ffbe9b876677acb13d7df07e5b975",
"node-gyp": "^3.3.1",
"request": "^2.72.0",
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/renderer/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& a
// Update the default matrices to the current viewport dimensions.
state.getProjMatrix(projMatrix);

// The extrusion matrix.
matrix::ortho(extrudeMatrix, 0, state.getWidth(), state.getHeight(), 0, 0, -1);

extrudeScale = {{ (2.0f / state.getWidth()) * state.getAltitude(), -2.0f / state.getHeight() * state.getAltitude() }};
// The extrusion scale.
const float flippedY = state.getViewportMode() == ViewportMode::FlippedY;
extrudeScale = {{ 2.0f / state.getWidth() * state.getAltitude(),
(flippedY ? 2.0f : -2.0f) / state.getHeight() * state.getAltitude() }};

// The native matrix is a 1:1 matrix that paints the coordinates at the
// same screen position as the vertex specifies.
Expand Down
1 change: 0 additions & 1 deletion src/mbgl/renderer/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class Painter : private util::noncopyable {

mat4 projMatrix;
mat4 nativeMatrix;
mat4 extrudeMatrix;

std::array<float, 2> extrudeScale;

Expand Down
68 changes: 24 additions & 44 deletions src/mbgl/renderer/painter_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,28 @@ void Painter::renderSDF(SymbolBucket &bucket,
{
mat4 vtxMatrix = translatedMatrix(matrix, translate, tileID, translateAnchor);

bool skewed = rotationAlignment == RotationAlignmentType::Map;
mat4 exMatrix;
float s;
float gammaScale;

if (skewed) {
matrix::identity(exMatrix);
s = tileID.pixelsToTileUnits(1, state.getZoom());
gammaScale = 1.0f / std::cos(state.getPitch());
} else {
exMatrix = extrudeMatrix;
s = state.getAltitude();
gammaScale = 1.0f;
matrix::rotate_z(exMatrix, exMatrix, state.getNorthOrientationAngle());
}
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;
float fontScale = fontSize / sdfFontSize;
matrix::scale(exMatrix, exMatrix, fontScale, fontScale, 1.0f);

float scale = fontScale;
std::array<float, 2> exScale = extrudeScale;
bool alignedWithMap = rotationAlignment == RotationAlignmentType::Map;
float gammaScale = 1.0f;

if (alignedWithMap) {
scale *= tileID.pixelsToTileUnits(1, state.getZoom());
exScale.fill(scale);
gammaScale /= std::cos(state.getPitch());
} else {
exScale = {{ exScale[0] * scale, exScale[1] * scale }};
}

config.program = sdfShader.getID();
sdfShader.u_matrix = vtxMatrix;
sdfShader.u_exmatrix = exMatrix;
sdfShader.u_extrude_scale = exScale;
sdfShader.u_texsize = texsize;
sdfShader.u_skewed = skewed;
sdfShader.u_skewed = alignedWithMap;
sdfShader.u_texture = 0;

// adjust min/max zooms for variable font sies
Expand Down Expand Up @@ -183,35 +177,21 @@ void Painter::renderSymbol(SymbolBucket& bucket,
mat4 vtxMatrix =
translatedMatrix(matrix, paint.iconTranslate, tileID, paint.iconTranslateAnchor);

bool skewed = layout.iconRotationAlignment == RotationAlignmentType::Map;
mat4 exMatrix;
float s;

if (skewed) {
matrix::identity(exMatrix);
s = tileID.pixelsToTileUnits(1, state.getZoom());
float scale = fontScale;
std::array<float, 2> exScale = extrudeScale;
const bool alignedWithMap = layout.iconRotationAlignment == RotationAlignmentType::Map;
if (alignedWithMap) {
scale *= tileID.pixelsToTileUnits(1, state.getZoom());
exScale.fill(scale);
} else {
exMatrix = extrudeMatrix;
matrix::rotate_z(exMatrix, exMatrix, state.getNorthOrientationAngle());
s = state.getAltitude();
exScale = {{ exScale[0] * scale, exScale[1] * scale }};
}
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);

// 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.getHeight(), 2) / 4.0f * (1.0f + std::pow(state.getAltitude(), 2)));
float x = state.getHeight() / 2.0f * std::tan(state.getPitch());
float extra = (topedgelength + x) / topedgelength - 1;

config.program = iconShader->getID();
iconShader->u_matrix = vtxMatrix;
iconShader->u_exmatrix = exMatrix;
iconShader->u_extrude_scale = exScale;
iconShader->u_texsize = {{ float(activeSpriteAtlas->getWidth()) / 4.0f, float(activeSpriteAtlas->getHeight()) / 4.0f }};
iconShader->u_skewed = skewed;
iconShader->u_extra = extra;
iconShader->u_skewed = alignedWithMap;
iconShader->u_texture = 0;

// adjust min/max zooms for variable font sies
Expand Down
17 changes: 8 additions & 9 deletions src/mbgl/shader/icon_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ class IconShader : public Shader {

void bind(GLbyte *offset) final;

UniformMatrix<4> u_matrix = {"u_matrix", *this};
UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this};
Uniform<GLfloat> u_zoom = {"u_zoom", *this};
Uniform<GLfloat> u_opacity = {"u_opacity", *this};
Uniform<std::array<GLfloat, 2>> u_texsize = {"u_texsize", *this};
Uniform<GLint> u_skewed = {"u_skewed", *this};
Uniform<GLfloat> u_extra = {"u_extra", *this};
Uniform<GLint> u_texture = {"u_texture", *this};
Uniform<GLint> u_fadetexture = {"u_fadetexture", *this};
UniformMatrix<4> u_matrix = {"u_matrix", *this};
Uniform<std::array<GLfloat, 2>> u_extrude_scale = {"u_extrude_scale", *this};
Uniform<GLfloat> u_zoom = {"u_zoom", *this};
Uniform<GLfloat> u_opacity = {"u_opacity", *this};
Uniform<std::array<GLfloat, 2>> u_texsize = {"u_texsize", *this};
Uniform<GLint> u_skewed = {"u_skewed", *this};
Uniform<GLint> u_texture = {"u_texture", *this};
Uniform<GLint> u_fadetexture = {"u_fadetexture", *this};

protected:
GLint a_offset = -1;
Expand Down
22 changes: 11 additions & 11 deletions src/mbgl/shader/sdf_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class SDFShader : public Shader {
public:
SDFShader(gl::GLObjectStore&);

UniformMatrix<4> u_matrix = {"u_matrix", *this};
UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this};
Uniform<std::array<GLfloat, 4>> u_color = {"u_color", *this};
Uniform<GLfloat> u_opacity = {"u_opacity", *this};
Uniform<std::array<GLfloat, 2>> u_texsize = {"u_texsize", *this};
Uniform<GLfloat> u_buffer = {"u_buffer", *this};
Uniform<GLfloat> u_gamma = {"u_gamma", *this};
Uniform<GLfloat> u_zoom = {"u_zoom", *this};
Uniform<GLint> u_skewed = {"u_skewed", *this};
Uniform<GLint> u_texture = {"u_texture", *this};
Uniform<GLint> u_fadetexture = {"u_fadetexture", *this};
UniformMatrix<4> u_matrix = {"u_matrix", *this};
Uniform<std::array<GLfloat, 2>> u_extrude_scale = {"u_extrude_scale", *this};
Uniform<std::array<GLfloat, 4>> u_color = {"u_color", *this};
Uniform<GLfloat> u_opacity = {"u_opacity", *this};
Uniform<std::array<GLfloat, 2>> u_texsize = {"u_texsize", *this};
Uniform<GLfloat> u_buffer = {"u_buffer", *this};
Uniform<GLfloat> u_gamma = {"u_gamma", *this};
Uniform<GLfloat> u_zoom = {"u_zoom", *this};
Uniform<GLint> u_skewed = {"u_skewed", *this};
Uniform<GLint> u_texture = {"u_texture", *this};
Uniform<GLint> u_fadetexture = {"u_fadetexture", *this};

protected:
GLint a_offset = -1;
Expand Down