{
const canvas = html`<canvas width=600 height=500></canvas>`;
yield canvas;
const gl = canvas.getContext('webgl2');
const vertexShader = createShader(gl,gl.VERTEX_SHADER,vertexShaderCode.value)
const fragmentShader = createShader(gl,gl.FRAGMENT_SHADER,fragmentShaderCode.value)
const program = createProgram(gl,vertexShader, fragmentShader);
gl.useProgram(program);
var positionAttrLocation = gl.getAttribLocation(program,'a_position');
var texCoordAttribLocation = gl.getAttribLocation(program,'a_texCoord');
var resolutionLocation = gl.getUniformLocation(program, 'u_resolution');
var imageLocation = gl.getUniformLocation(program, 'u_image');
var vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const positionBuffer = gl.createBuffer();
gl.enableVertexAttribArray(positionAttrLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
var size = 2;
var type = gl.FLOAT;
var norm = false;
var stride = 0;
var offset = 0;
gl.vertexAttribPointer(positionAttrLocation, size, type, norm, stride, offset)
// provide texture coordinates for the rectangle
var texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0,
]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(texCoordAttribLocation);
var size = 2; // 2 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var norm = false; // don't normalize the data
var stride = 0;
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer(texCoordAttribLocation,size,type,norm,stride,offset)
// Create texture and put the image in it
var originalImageTexture = createAndSetupTexture(gl);
// Upload image into texture
var mipLevel = 0; // the largest mip
var internalFormat = gl.RGBA // format we want in the texture
var srcFormat = gl.RGBA // format of the data
var srcType = gl.UNSIGNED_BYTE // type of supplied data
gl.texImage2D(gl.TEXTURE_2D,
mipLevel,
internalFormat,
srcFormat,
srcType,
image
);
// create 2 textures and attach them to framebuffers
var textures = [];
var framebuffers = [];
for(var ii=0; ii<2; ii++){
var texture = createAndSetupTexture(gl);
textures.push(texture);
// make the texture the same size as the image
var mipLevel = 0; // Largest mil
var internalFormat = gl.RGBA;
var border = 0;
var srcFormat = gl.RGBA;
var srcype = gl.UNSIGNED_BYTEl
var data = null;
gl.texImage2D(gl.TEXTURE_2D, mipLevel, internalFormat, image.width,
image.height, border, srcFormat, srcType, data
);
// Create framebuffer
var fbo = gl.createFramebuffer();
framebuffers.push(fbo);
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
// Attach a texture to it
var attachmentPoint = gl.COLOR_ATTACHMENT0;
gl.framebufferTexture2D(gl.FRAMEBUFFER,attachmentPoint, gl.TEXTURE_2D, texture, mipLevel);
}
var kernelLocation = gl.getUniformLocation(program, "u_kernel[0]");
var kernelWeightLocation = gl.getUniformLocation(program, "u_kernelWeight");
var flipYLocation = gl.getUniformLocation(program,'u_flipY');
/*
var edgeDetectKernel = kernelValue;
// set the kernel and it's weight
gl.uniform1fv(kernelLocation, edgeDetectKernel);
gl.uniform1f(kernelWeightLocation, computeKernelWeight(edgeDetectKernel));
*/
webglUtils.resizeCanvasToDisplaySize(gl.canvas)
// Tell webgl how to convert from clip space to pixels
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Clear the canvas
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// bind attributes and buffers
gl.bindVertexArray(vao);
// Start with the original image on unit 0
gl.activeTexture(gl.TEXTURE0 + 0)
gl.bindTexture(gl.TEXTURE_2D, originalImageTexture);
// Pass in the canvas resolution so we can convert from
// pixels top clip space in shader
gl.uniform2f(resolutionLocation, gl.canvas.width, gl.canvas.height);
// Tell the shader to get the texture from texture unit
gl.uniform1i(imageLocation, 0);
// don't y flip images while drawing to the textures
gl.uniform1f(flipYLocation, 1);
// Bind position buffer
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Set rect same size as image
setRectangle(gl, 0, 0, image.width, image.height);
kernelValues.forEach((name,i)=>{
setFramebuffer(framebuffers[i%2], image.width, image.height,gl, resolutionLocation);
drawWithKernel(gl,kernelLocation, kernelWeightLocation, name);
gl.bindTexture(gl.TEXTURE_2D, textures[i % 2])
})
gl.uniform1f(flipYLocation, -1);
setFramebuffer(null, gl.canvas.width, gl.canvas.height,gl, resolutionLocation);
drawWithKernel(gl,kernelLocation,kernelWeightLocation,'normal');
invalidation.then(d=>{
gl.deleteProgram(program);
gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);
})
}