function createDoubleBuffer(gl, width, height) {
var output = {
// An array of textures, containing only two textures.
textures: [],
// An integer index telling which one is the read buffer.
readBufferIndex: 0,
// Return the read buffer.
getReadBuffer: function() {
return this.textures[this.readBufferIndex];
},
// Return the write buffer.
getWriteBuffer: function() {
return this.textures[1 - this.readBufferIndex];
},
// Exchange the roles of the buffers.
swap: function() {
this.readBufferIndex = 1 - this.readBufferIndex;
}
};
// Allocate the buffers.
output.textures.push(createFloatTexture(gl, width, height));
output.textures.push(createFloatTexture(gl, width, height));
return output;
}
var buffer = createDoubleBuffer(gl, 512, 512);
function drawToBufferAndSwap(gl, fbo, doubleBuffer, drawFunc) {
// Bind the FBO.
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
// Attach the write buffer to the color attachment.
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
doubleBuffer.getWriteBuffer(),
0);
// Let the input function draw as necessary.
drawFunc();
// Detach the write buffer.
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
null,
0);
// Unbind the FBO.
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// Swap the buffers.
doubleBuffer.swap();
// After this point, what was written to the write buffer is now in the read buffer and is ready for the next step.
}
// Draw scene to frame buffer.
drawToBufferAndSwap(gl, fbo, buffer, function() {
gl.clearColor(0.75, 0.75, 0.75, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Code elided for brevity.
gl.flush();
});
if ($("#blurXCheckBox").is(":checked")) {
drawToBufferAndSwap(gl, fbo, buffer, function() {
gl.useProgram(blurXProgram);
// Code elided for brevity.
drawFullScreenQuad(gl, blurXProgram);
gl.useProgram(null);
gl.flush();
});
}
if ($("#blurYCheckBox").is(":checked")) {
drawToBufferAndSwap(gl, fbo, buffer, function() {
gl.useProgram(blurYProgram);
// Code elided for brevity.
drawFullScreenQuad(gl, blurYProgram);
gl.useProgram(null);
gl.flush();
});
}
// Copy pixel from read buffer to the monitor.
{
gl.useProgram(textureCopyProgram);
if (textureCopyProgram.texture != null) {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, buffer.getReadBuffer());
gl.uniform1i(textureCopyProgram.texture, 0);
}
drawFullScreenQuad(gl, textureCopyProgram);
}
Currently the "Convert to sRGB" checkbox is not functioning. Can you enable sRGB conversion by adding a pass in the post processing? The shader for sRGB conversion is already there.