Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. drawImage(
  2.         tex, texWidth, texHeight,
  3.         srcX, srcY, srcWidth, srcHeight,
  4.         dstX, dstY, dstWidth, dstHeight, srcRotation) {
  5.     var gl;
  6.     var matrix, matrix2;
  7.     var texMatrix;
  8.  
  9.     if (dstX === undefined) {
  10.       dstX = srcX;
  11.       srcX = 0;
  12.     }
  13.     if (dstY === undefined) {
  14.       dstY = srcY;
  15.       srcY = 0;
  16.     }
  17.     if (srcWidth === undefined) {
  18.       srcWidth = texWidth;
  19.     }
  20.     if (srcHeight === undefined) {
  21.       srcHeight = texHeight;
  22.     }
  23.     if (dstWidth === undefined) {
  24.       dstWidth = srcWidth;
  25.       srcWidth = texWidth;
  26.     }
  27.     if (dstHeight === undefined) {
  28.       dstHeight = srcHeight;
  29.       srcHeight = texHeight;
  30.     }
  31.     if (srcRotation === undefined) {
  32.       srcRotation = 0;
  33.     }
  34.  
  35.     gl = this._gl;
  36.     gl.bindTexture(gl.TEXTURE_2D, tex);
  37.     gl.useProgram(this._program);
  38.  
  39.     gl.bindBuffer(gl.ARRAY_BUFFER, this._positionBuffer);
  40.     gl.enableVertexAttribArray(this._locations.position);
  41.     gl.vertexAttribPointer(this._locations.position, 2, gl.FLOAT, false, 0, 0);
  42.     gl.bindBuffer(gl.ARRAY_BUFFER, this._texcoordBuffer);
  43.     gl.enableVertexAttribArray(this._locations.texcoord);
  44.     gl.vertexAttribPointer(this._locations.texcoord, 2, gl.FLOAT, false, 0, 0);
  45.  
  46.     matrix = mat4.create();
  47.     matrix2 = mat4.create();
  48.     mat4.ortho(matrix, 0, 1000, 1000, 0, -1, 1);
  49.     mat4.translate(matrix2, matrix, [dstX, dstY, 0]);
  50.     mat4.copy(matrix, matrix2);
  51.     mat4.scale(matrix2, matrix, [dstWidth, dstHeight, 1]);
  52.     mat4.copy(matrix, matrix2);
  53.     gl.drawingBufferHeight = 320;
  54.     gl.uniformMatrix4fv(this._locations.matrix, false, matrix);
  55.     texMatrix = mat4.create();
  56.     mat4.fromScaling(texMatrix, [1 / texWidth, 1 / texHeight, 1]);
  57.     mat4.translate(matrix2, texMatrix, [texWidth * 0.5, texHeight * 0.5, 0]);
  58.     mat4.copy(texMatrix, matrix2);
  59.     mat4.rotateZ(matrix2, texMatrix, srcRotation);
  60.     mat4.copy(texMatrix, matrix2);
  61.     mat4.translate(matrix2, texMatrix, [texWidth * -0.5, texHeight * -0.5, 0]);
  62.     mat4.translate(texMatrix, matrix2, [srcX, srcY, 0]);
  63.     mat4.scale(matrix2, texMatrix, [srcWidth, srcHeight, 1]);
  64.     mat4.copy(texMatrix, matrix2);
  65.  
  66.     gl.uniformMatrix4fv(this._locations.textureMatrix, false, texMatrix);
  67.     gl.uniform1i(this._locations.texture, 0);
  68.     gl.drawArrays(gl.TRIANGLES, 0, 6);
  69.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement