Skip to content

Commit

Permalink
clean up pattern code (#3512)
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner authored Nov 1, 2016
1 parent 6fd85d0 commit 525b15a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
6 changes: 3 additions & 3 deletions js/render/draw_background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const setPattern = require('./set_pattern');
const pattern = require('./pattern');

module.exports = drawBackground;

Expand All @@ -21,7 +21,7 @@ function drawBackground(painter, sourceCache, layer) {
let program;
if (image) {
program = painter.useProgram('fillPattern');
setPattern(image, painter, program);
pattern.prepare(image, painter, program);
painter.tileExtentPatternVAO.bind(gl, program, painter.tileExtentBuffer);
} else {
program = painter.useProgram('fill', painter.basicFillProgramConfiguration);
Expand All @@ -35,7 +35,7 @@ function drawBackground(painter, sourceCache, layer) {

for (const coord of coords) {
if (image) {
setPattern.setTile({coord, tileSize}, painter, program, true);
pattern.setTile({coord, tileSize}, painter, program);
}
gl.uniformMatrix4fv(program.u_matrix, false, painter.transform.calculatePosMatrix(coord));
gl.drawArrays(gl.TRIANGLE_STRIP, 0, painter.tileExtentBuffer.length);
Expand Down
7 changes: 4 additions & 3 deletions js/render/draw_extrusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const vec3 = require('gl-matrix').vec3;
const Buffer = require('../data/buffer');
const VertexArrayObject = require('./vertex_array_object');
const StructArrayType = require('../util/struct_array');
const setPattern = require('./set_pattern');
const pattern = require('./pattern');

module.exports = draw;

Expand Down Expand Up @@ -159,8 +159,9 @@ function drawExtrusion(painter, source, layer, coord) {
programConfiguration.setUniforms(gl, program, layer, {zoom: painter.transform.zoom});

if (image) {
setPattern(image, painter, program);
setPattern.setTile(tile, painter, program, true);
pattern.prepare(image, painter, program);
pattern.setTile(tile, painter, program);
gl.uniform1f(program.u_height_factor, -Math.pow(2, coord.z) / tile.tileSize / 8);
}

setMatrix(program, painter, coord, tile, layer);
Expand Down
6 changes: 3 additions & 3 deletions js/render/draw_fill.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const setPattern = require('./set_pattern');
const pattern = require('./pattern');

module.exports = drawFill;

Expand Down Expand Up @@ -92,9 +92,9 @@ function setFillProgram(programId, usePattern, painter, layerData, layer, tile,
program = painter.useProgram(`${programId}Pattern`);
painter.gl.uniform1f(program.u_opacity, layer.paint['fill-opacity']);
if (firstTile || program !== prevProgram) {
setPattern(layer.paint['fill-pattern'], painter, program);
pattern.prepare(layer.paint['fill-pattern'], painter, program);
}
setPattern.setTile(tile, painter, program, false);
pattern.setTile(tile, painter, program);
}
painter.gl.uniformMatrix4fv(program.u_matrix, false, painter.translatePosMatrix(
coord.posMatrix, tile,
Expand Down
12 changes: 3 additions & 9 deletions js/render/set_pattern.js → js/render/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

const pixelsToTileUnits = require('../source/pixels_to_tile_units');

module.exports = setPattern;

function setPattern(image, painter, program) {
exports.prepare = function (image, painter, program) {
const gl = painter.gl;

const imagePosA = painter.spriteAtlas.getPosition(image.from, true);
Expand All @@ -24,9 +22,9 @@ function setPattern(image, painter, program) {

gl.activeTexture(gl.TEXTURE0);
painter.spriteAtlas.bind(gl, true);
}
};

setPattern.setTile = function (tile, painter, program, isExtruded) {
exports.setTile = function (tile, painter, program) {
const gl = painter.gl;

gl.uniform1f(program.u_tile_units_to_pixels, 1 / pixelsToTileUnits(tile, 1, painter.transform.tileZoom));
Expand All @@ -40,8 +38,4 @@ setPattern.setTile = function (tile, painter, program, isExtruded) {
// split the pixel coord into two pairs of 16 bit numbers. The glsl spec only guarantees 16 bits of precision.
gl.uniform2f(program.u_pixel_coord_upper, pixelX >> 16, pixelY >> 16);
gl.uniform2f(program.u_pixel_coord_lower, pixelX & 0xFFFF, pixelY & 0xFFFF);

if (isExtruded) {
gl.uniform1f(program.u_height_factor, -numTiles / tile.tileSize / 8);
}
};

0 comments on commit 525b15a

Please sign in to comment.