Frame Buffer Object (csb.app)

Webgl Draw_Buffers mrt_A873054267 -CSDN blog

FBO

  • process
    • texture
      • Texture map 1, 2
    • framebuffer
      • CreateFramebuffer Create window
      • BindFramebuffer uses Windows
      • FramebufferTexture2D uses a map
      • DrawBuffersWEBGL create
    • renderrbuffer
      • CreateRenderbuffer creates the render
      • BindRenderbuffer uses rendering
      • RenderbufferStorage Precision
    • both
      • framebufferRenderbuffer
function initFramebufferObject(gl,ext) {
    var framebuffer, texture,texture2, depthBuffer;
    framebuffer = gl.createFramebuffer();
    {
        texture = gl.createTexture(); // Create a texture object
        gl.bindTexture(gl.TEXTURE_2D, texture); // Bind the object to target
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        framebuffer.texture = texture; // Store the texture object
    }
    {
        texture2 = gl.createTexture(); // Create a texture object
        gl.bindTexture(gl.TEXTURE_2D, texture2); // Bind the object to target
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        framebuffer.texture2 = texture2; // Store the texture object
    }
    gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, ext.COLOR_ATTACHMENT0_WEBGL, gl.TEXTURE_2D, texture, 0);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, ext.COLOR_ATTACHMENT1_WEBGL, gl.TEXTURE_2D, texture2, 0);
    ext.drawBuffersWEBGL([
        ext.COLOR_ATTACHMENT0_WEBGL, // gl_FragData[0]
        ext.COLOR_ATTACHMENT1_WEBGL, // gl_FragData[1]
    ]);
    {
        depthBuffer = gl.createRenderbuffer(); // Create a renderbuffer object
        gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer); // Bind the object to target
        gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT);
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
    }
    {  // Unbind the frame buffer
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
        gl.bindTexture(gl.TEXTURE_2D, null);
        gl.bindRenderbuffer(gl.RENDERBUFFER, null);
    }
    return framebuffer;
}
Copy the code

Vertex data

function initVertexBuffers(gl) {
    var vertices = new Float32Array([ / / square
        -0.5.0.5,   -0.5, -0.5.0.5.0.5.0.5, -0.5
    ]);
    var n = 4; // The number of vertices
    var vertexBuffer = gl.createBuffer();
    if(! vertexBuffer) {console.log('Failed to create the buffer object');
        return -1;
    }
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
    if (a_Position < 0) {
        console.log('Failed to get the storage location of a_Position');
        return -1;
    }
    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false.0.0);
    gl.enableVertexAttribArray(a_Position);
    return n;
}
Copy the code

The main function

function main() {
    var canvas = document.getElementById('webgl');
    var gl = getWebGLContext(canvas);
    var ext = gl.getExtension('WEBGL_draw_buffers');
    if(! gl) {console.log('Failed to get the rendering context for WebGL');
        return;
    }
   let initProgram =  initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)
    var fbo = initFramebufferObject(gl,ext); / / get fbo
    var n = initVertexBuffers(gl); / / get
    gl.viewport(0.0, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT); // Set a viewport for FBO
    gl.clearColor(0.2.0.2.0.4.1.0); // Set clear color (the color is slightly changed)
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);  // Clear FBO
    if (n < 0) {
        console.log('Failed to set the positions of the vertices');
        return;
    }
    gl.clearColor(0.0.0.1);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);              // Change the drawing destination to FBO
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
    gl.bindFramebuffer(gl.FRAMEBUFFER, null);        // Change the drawing destination to color buffer
    gl.viewport(0.0, canvas.width, canvas.height);  // Set the size of viewport back to that of <canvas>
    let newProgram =  initShaders(gl, m_VSHADER_SOURCE, m_FSHADER_SOURCE);
    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(gl.TEXTURE_2D, fbo.texture2); // Try texture2 instead of texture
    initVertexBuffers(gl);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}
Copy the code

Fbo multi Texture GLSL

  var VSHADER_SOURCE =
    "attribute vec4a_Position; \n" + "void main() {\n" +
    "  gl_Position= a_Position; \n" + "}\n";// Fragment shader program
  var FSHADER_SOURCE =
    "#extension GL_EXT_draw_buffers : require\n" +
    "void main() {\n" +
    "  gl_FragData[0] = vec4(1.0.0.0.0.0.1.0); \n" + "gl_FragData[1] = vec4(0.0.1.0.0.0.1.0); \n" + "}\n";Copy the code

GLSL showing texture color

  var m_VSHADER_SOURCE =
    "attribute vec4a_Position; \n" + "void main() {\n" +
    "  gl_Position= a_Position; \n" + "}\n";// Sampler, texture coordinate, texture shader element
  var m_FSHADER_SOURCE =
    "#ifdef GL_ES\n" +
    "precision mediump float; \n" + "#endif\n" +
    "uniform sampler2Du_Sampler; \n" + "void main() {\n" +
    "  gl_FragColor = texture2D(u_Sampler, vec2(0.5.0.5)); \n" + "}\n";Copy the code