2017-08-13 23:41:57 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const logging = require("./logging");
|
2017-08-13 23:41:57 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
let scope;
|
2017-08-13 23:41:57 +02:00
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-12 22:24:23 +01:00
|
|
|
scope = require.register("./webgl", {});
|
2017-08-13 23:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scope.copyCanvasToWebgl = function copyCanvasToWebgl(window, canvas, webGLVersion = "webgl"){
|
2019-11-28 01:26:35 +01:00
|
|
|
const webGlCanvas = canvas.cloneNode(true);
|
|
|
|
const context =
|
2017-08-13 23:41:57 +02:00
|
|
|
window.HTMLCanvasElement.prototype.getContext.call(webGlCanvas, webGLVersion) ||
|
|
|
|
window.HTMLCanvasElement.prototype.getContext.call(webGlCanvas, "experimental-" + webGLVersion);
|
|
|
|
if (!context){
|
|
|
|
// unable to get a context...
|
|
|
|
logging.warning("unable to create webgl context.");
|
|
|
|
return {canvas: false, context: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
context.viewport(0, 0, webGlCanvas.width, webGlCanvas.height);
|
2019-11-28 01:26:35 +01:00
|
|
|
|
|
|
|
const program = context.createProgram();
|
|
|
|
|
|
|
|
const vertexShader = context.createShader(context.VERTEX_SHADER);
|
|
|
|
const vertex = "attribute vec4 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_texCoord;\n" +
|
2017-10-06 16:06:31 +02:00
|
|
|
"void main(){\n\tgl_Position = a_position;\n\tv_texCoord = a_texCoord;\n}";
|
2019-11-28 01:26:35 +01:00
|
|
|
context.shaderSource(vertexShader, vertex);
|
|
|
|
context.compileShader(vertexShader);
|
|
|
|
if (!context.getShaderParameter(vertexShader, context.COMPILE_STATUS)){
|
|
|
|
context.deleteShader(vertexShader);
|
2017-08-13 23:41:57 +02:00
|
|
|
logging.warning("webgl: failed to compile vertex shader.");
|
|
|
|
return {canvas: false, context: false};
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
context.attachShader(program, vertexShader);
|
2017-08-13 23:41:57 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const fragmentShader = context.createShader(context.FRAGMENT_SHADER);
|
|
|
|
const fragmenter = "precision mediump float;\nuniform sampler2D u_image;\nvarying vec2 v_texCoord;\n" +
|
2017-10-06 16:06:31 +02:00
|
|
|
"void main(){\n\tgl_FragColor = texture2D(u_image, v_texCoord);\n}";
|
2019-11-28 01:26:35 +01:00
|
|
|
context.shaderSource(fragmentShader, fragmenter);
|
|
|
|
context.compileShader(fragmentShader);
|
|
|
|
if (!context.getShaderParameter(fragmentShader, context.COMPILE_STATUS)){
|
|
|
|
context.deleteShader(fragmentShader);
|
2017-08-13 23:41:57 +02:00
|
|
|
logging.warning("webgl: failed to compile fragmenter shader.");
|
|
|
|
return {canvas: false, context: false};
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
context.attachShader(program, fragmentShader);
|
2017-08-13 23:41:57 +02:00
|
|
|
|
|
|
|
context.linkProgram(program);
|
2019-11-28 01:26:35 +01:00
|
|
|
if (!context.getProgramParameter(program, context.LINK_STATUS)){
|
2017-08-13 23:41:57 +02:00
|
|
|
context.deleteProgram(program);
|
|
|
|
logging.warning("webgl: failed to link program.");
|
|
|
|
return {canvas: false, context: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
context.useProgram(program);
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const positionBuffer = context.createBuffer();
|
2017-08-13 23:41:57 +02:00
|
|
|
context.bindBuffer(context.ARRAY_BUFFER, positionBuffer);
|
|
|
|
context.bufferData(context.ARRAY_BUFFER, new Float32Array([
|
2017-10-05 19:00:00 +02:00
|
|
|
-1, -1,
|
|
|
|
-1, 1,
|
|
|
|
1, -1,
|
|
|
|
1, 1,
|
|
|
|
-1, 1,
|
|
|
|
1, -1
|
2017-08-13 23:41:57 +02:00
|
|
|
]), context.STATIC_DRAW);
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const positionAttributeLocation = context.getAttribLocation(program, "a_position");
|
2017-08-13 23:41:57 +02:00
|
|
|
context.enableVertexAttribArray(positionAttributeLocation);
|
2019-11-28 01:26:35 +01:00
|
|
|
const size = 2; // 2 components per iteration
|
|
|
|
const type = context.FLOAT; // the data is 32bit floats
|
|
|
|
const normalize = false; // don't normalize the data
|
|
|
|
const stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
|
|
|
|
const offset = 0; // start at the beginning of the buffer
|
2017-10-05 19:00:00 +02:00
|
|
|
context.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);
|
2017-08-13 23:41:57 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const texCoordLocation = context.getAttribLocation(program, "a_texCoord");
|
2017-08-13 23:41:57 +02:00
|
|
|
|
|
|
|
// provide texture coordinates for the rectangle.
|
2019-11-28 01:26:35 +01:00
|
|
|
const texCoordBuffer = context.createBuffer();
|
2017-08-13 23:41:57 +02:00
|
|
|
context.bindBuffer(context.ARRAY_BUFFER, texCoordBuffer);
|
|
|
|
context.bufferData(context.ARRAY_BUFFER, new Float32Array([
|
|
|
|
0, 1,
|
|
|
|
0, 0,
|
|
|
|
1, 1,
|
|
|
|
1, 0,
|
|
|
|
0, 0,
|
|
|
|
1, 1
|
|
|
|
]), context.STATIC_DRAW);
|
|
|
|
context.enableVertexAttribArray(texCoordLocation);
|
|
|
|
context.vertexAttribPointer(texCoordLocation, 2, context.FLOAT, false, 0, 0);
|
2019-11-28 01:26:35 +01:00
|
|
|
|
|
|
|
context.bindTexture(context.TEXTURE_2D, context.createTexture());
|
2017-08-13 23:41:57 +02:00
|
|
|
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
|
|
|
|
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
|
|
|
|
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.NEAREST);
|
|
|
|
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.NEAREST);
|
|
|
|
context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, context.RGBA, context.UNSIGNED_BYTE, canvas);
|
2019-11-28 01:26:35 +01:00
|
|
|
|
|
|
|
context.drawArrays(context.TRIANGLES /*primitiveType*/, 0 /*triangleOffset*/, 6 /*count*/);
|
2017-08-13 23:41:57 +02:00
|
|
|
|
|
|
|
return {webGlCanvas, context};
|
|
|
|
};
|
|
|
|
}());
|