Ejemplo n.º 1
0
WebGlBaseRenderer.prototype.renderTile = function(tile, texture, layer, layerZ) {

  var gl = this.gl;
  var shaderProgram = this.shaderProgram;
  var constantBuffers = this.constantBuffers;
  var vtMatrix = this.vtMatrix;
  var translateVector = this.translateVector;
  var scaleVector = this.scaleVector;

  translateVector[0] = tile.centerX();
  translateVector[1] = tile.centerY();
  translateVector[2] = -0.5;

  scaleVector[0] = tile.scaleX();
  scaleVector[1] = tile.scaleY();
  scaleVector[2] = 1;

  mat4.copy(vtMatrix, layer.view().projection());
  mat4.rotateX(vtMatrix, vtMatrix, tile.rotX());
  mat4.rotateY(vtMatrix, vtMatrix, tile.rotY());
  mat4.translate(vtMatrix, vtMatrix, translateVector);
  mat4.scale(vtMatrix, vtMatrix, scaleVector);

  gl.uniformMatrix4fv(shaderProgram.vtMatrix, false, vtMatrix);

  setDepth(gl, shaderProgram, layerZ, tile.z);

  setTexture(gl, shaderProgram, texture);

  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, constantBuffers.vertexIndices);
  gl.drawElements(gl.TRIANGLES, vertexIndices.length, gl.UNSIGNED_SHORT, 0);
};
Ejemplo n.º 2
0
function rectToViewport(rect, resultParameters, resultViewportMatrix) {
  // Horizontal axis.
  var offsetX = rect.left;
  var totalWidth = rect.totalWidth;
  var clampedOffsetX = clamp(offsetX, 0, totalWidth);

  var widthWithoutDiscarded = rect.width - (clampedOffsetX - offsetX);
  var maxWidth = totalWidth - clampedOffsetX;

  var clampedWidth = clamp(widthWithoutDiscarded, 0, maxWidth);

  resultParameters.offsetX = clampedOffsetX;
  resultParameters.width = clampedWidth;

  // Vertical axis.
  var offsetY = rect.totalHeight - rect.bottom;
  var totalHeight = rect.totalHeight;
  var clampedOffsetY = clamp(offsetY, 0, totalHeight);

  var heightWithoutDiscarded = rect.height - (clampedOffsetY - offsetY);
  var maxHeight = totalHeight - clampedOffsetY;

  var clampedHeight = clamp(heightWithoutDiscarded, 0, maxHeight);

  resultParameters.offsetY = clampedOffsetY;
  resultParameters.height = clampedHeight;

  // Compensation matrix for shader.
  // This matrix is used to scale and offset the vertices by the necessary
  // amount to compensate the viewport clamping.

  // Scaling is easy. Just revert the scaling that a smaller viewport would
  // cause.
  scaleVector[0] = rect.width / clampedWidth;
  scaleVector[1] = rect.height / clampedHeight;
  scaleVector[2] = 1;

  // Translating is more complicated. The center of the view will be at
  // the center of the viewport, but it should actually be offset according
  // to rect. One translation is be required to compensate for clamping at the
  // beginning of the viewport and another for clamping at the end of the
  // viewport. The two are calculated and subtracted.

  // Horizontal axis translation compensation.
  var leftClampCompensation = clampedOffsetX - offsetX;

  var rightmost = offsetX + rect.width;
  var clampedRightmost = clampedOffsetX + clampedWidth;
  var rightClampCompensation = rightmost - clampedRightmost;

  // Vertical axis translation compensation.
  var bottomClampCompensation = clampedOffsetY - offsetY;

  var topmost = offsetY + rect.height;
  var clampedTopmost = clampedOffsetY + clampedHeight;
  var topClampCompensation = topmost - clampedTopmost;

  // Divide by the viewport size to convert from pixels to viewport coordinates.
  translateVector[0] = (rightClampCompensation - leftClampCompensation)/clampedWidth;
  translateVector[1] = (topClampCompensation - bottomClampCompensation)/clampedHeight;
  translateVector[2] = 0;

  var viewportMatrix = resultViewportMatrix;
  mat4.identity(viewportMatrix);
  mat4.translate(viewportMatrix, viewportMatrix, translateVector);
  mat4.scale(viewportMatrix, viewportMatrix, scaleVector);
}