GLFBO.java 4.06 KB
Newer Older
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
1 2 3 4 5 6 7 8
package com.projectseptember.RNGL;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import static android.opengl.GLES20.*;

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
9
public class GLFBO {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    private static final Logger logger = Logger.getLogger(GLFBO.class.getName());

    public final List<GLTexture> color = new ArrayList<>();
    private int handle;
    private int width = 0;
    private int height = 0;

    private static GLTexture initTexture (int width, int height, int attachment) {
        GLTexture texture = new GLTexture();
        texture.bind();
        texture.setShape(width, height);
        glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, texture.handle, 0);
        return texture;
    }

    int initRenderBuffer (int width, int height, int component, int attachment)
    {
        int[] handleArr = new int[1];
        glGenRenderbuffers(1, handleArr, 0);
        int handle = handleArr[0];
        glBindRenderbuffer(GL_RENDERBUFFER, handle);
        glRenderbufferStorage(GL_RENDERBUFFER, component, width, height);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, handle);
        return handle;
    }

    class FBOState {

        private int fbo;
        private int rbo;
        private int tex;

        public FBOState() {
            int[] fbo = new int[1], rbo = new int[1], tex = new int[1];
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, fbo, 0);
            glGetIntegerv(GL_RENDERBUFFER_BINDING, rbo, 0);
            glGetIntegerv(GL_TEXTURE_BINDING_2D, tex, 0);
            this.fbo = fbo[0];
            this.rbo = rbo[0];
            this.tex = tex[0];
        }

        private void restore() {
            glBindFramebuffer(GL_FRAMEBUFFER, fbo);
            glBindRenderbuffer(GL_FRAMEBUFFER, rbo);
            glBindTexture(GL_FRAMEBUFFER, tex);
        }
    }

    public GLFBO() {
        FBOState state = new FBOState();

        int[] handleArr = new int[1];
        glGenFramebuffers(1, handleArr, 0);
        handle = handleArr[0];

        int numColors = 1;

        glBindFramebuffer(GL_FRAMEBUFFER, handle);

        for(int i=0; i<numColors; ++i) {
            color.add(initTexture(width, height, GL_COLOR_ATTACHMENT0 + i));
        }

        state.restore();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        int[] handleArr = new int[] { handle };
        glDeleteFramebuffers(1, handleArr, 0);
    }


    void checkStatus () {
        int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        if(status != GL_FRAMEBUFFER_COMPLETE) {
            switch (status) {
                case GL_FRAMEBUFFER_UNSUPPORTED:
                    logger.severe("Framebuffer unsupported");
                    break;
                case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
                    logger.severe("Framebuffer incomplete attachment");
                    break;
                case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
                    logger.severe("Framebuffer incomplete dimensions");
                    break;
                case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
                    logger.severe("Framebuffer incomplete missing attachment");
                    break;
                default:
                    logger.severe("Failed to create framebuffer: " + status);
            }
        }
    }

    public void bind () {
        glBindFramebuffer(GL_FRAMEBUFFER, handle);
        glViewport(0, 0, width, height);
    }

    public void setShape(int w, int h) {
        if (w == width && h == height) return;
        int[] maxFBOSize = new int[1];
        glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, maxFBOSize, 0);
        if( w < 0 || w > maxFBOSize[0] || h < 0 || h > maxFBOSize[0]) {
            logger.severe("Can't resize framebuffer. Invalid dimensions");
            return;
        }
        width = w;
        height = h;

        FBOState state = new FBOState();

        for (GLTexture clr: color) {
            clr.setShape(w, h);
        }

        glBindFramebuffer(GL_FRAMEBUFFER, handle);
        checkStatus();

        state.restore();
    }
}