{"size": 3660, "ext": "js", "max_stars_repo_path": "sleeping-better-at-night/assets/player/gl/TSDGLFrameBuffer.js", "max_stars_repo_name": "tomaszlakomygp/tomaszlakomygp.github.io", "max_stars_repo_stars_event_min_datetime": "2017-11-14T15:34:03.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T13:11:05.000Z", "max_issues_repo_path": "sleeping-better-at-night/assets/player/gl/TSDGLFrameBuffer.js", "max_issues_repo_name": "tomaszlakomygp/tomaszlakomygp.github.io", "max_issues_repo_issues_event_min_datetime": "2017-06-12T16:36:39.000Z", "max_issues_repo_issues_event_max_datetime": "2020-09-15T10:35:37.000Z", "max_forks_repo_path": "sleeping-better-at-night/assets/player/gl/TSDGLFrameBuffer.js", "max_forks_repo_name": "tomaszlakomygp/tomaszlakomygp.github.io", "max_forks_repo_forks_event_min_datetime": "2017-06-22T13:35:23.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-04T02:22:09.000Z"}
{"max_stars_count": 63, "max_issues_count": 37, "max_forks_count": 22, "avg_line_length": 31.2820512821, "max_line_length": 129, "alphanum_fraction": 0.65}
/*
 * TSDGLFrameBuffer.js
 * Keynote HTML Player
 *
 * Created by Tungwei Cheng
 * Copyright (c) 2018 Apple Inc. All rights reserved.
 */

var TSDGLFrameBuffer = Class.create({
    initialize: function(gl, size, textureCount) {
        this.gl = gl;

        // framebuffer size
        this.size = size;

        // number of framebuffer-attachable texture images
        this.textureCount = textureCount;

        // current texture index
        this.currentTextureIndex = 0;

        // create framebuffer-attachable texture images
        this.setupFramebuffer(gl, size, textureCount);
    },

    setupFramebuffer: function(gl, size, textureCount) {
        // create and bind frame buffer object
        var buffer = this.buffer = gl.createFramebuffer();

        gl.bindFramebuffer(gl.FRAMEBUFFER, buffer);

        var textures = this.textures = [];

        // set up framebuffer texture(s)
        for (var i = 0; i < textureCount; i++) {
            var texture = gl.createTexture();

            // bind texture
            gl.bindTexture(gl.TEXTURE_2D, texture);

            gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
            gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);

            // setup texture parameters
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);

            // specify the texture size for memory allocation
            gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size.width, size.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

            // unbind texture
            gl.bindTexture(gl.TEXTURE_2D, null);

            textures.push(texture);
        }

        // bind current texture to the framebuffer
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textures[this.currentTextureIndex], 0);

        // unbind framebuffer
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    },

    currentGLTexture: function() {
        var texture = this.textures[this.currentTextureIndex];

        return texture;
    },

    setCurrentTextureToNext: function() {
        var textureCount = this.textureCount;

        if (this.textureCount > 0) {
            var currentTextureIndex = this.currentTextureIndex;
            var nextTextureIndex = (currentTextureIndex + 1) % textureCount;

            this.currentTextureIndex = nextTextureIndex;

            // bind the framebuffer to the next texture
            this.bindFramebuffer();
        }
    },

    bindFramebuffer: function() {
        var gl = this.gl;

        //bind framebuffer
        gl.bindFramebuffer(gl.FRAMEBUFFER, this.buffer);

        // bind current texture
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.textures[this.currentTextureIndex], 0);
    },

    currentGLFramebuffer: function() {
        var gl = this.gl;
        var framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);

        return framebuffer;
    },

    unbindFramebufferAndBindGLFramebuffer: function(currentGLFramebuffer) {
        var gl = this.gl;
        gl.bindFramebuffer(gl.FRAMEBUFFER, currentGLFramebuffer);
    }

});

TSDGLFrameBuffer.currentGLFramebuffer = function(gl) {
	// use getParameter in WebGL as there is no getIntegerv implementation
    var framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);

    // this will return null if drawing buffer is the display default
    return framebuffer;
};
