Java Code Examples for android.opengl.GLES20#glFramebufferTexture2D()

The following examples show how to use android.opengl.GLES20#glFramebufferTexture2D() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GlUtil.java    From PLDroidShortVideo with Apache License 2.0 6 votes vote down vote up
public static void createFBO(int[] fboTex, int[] fboId, int width, int height) {
//generate fbo id
        GLES20.glGenFramebuffers(1, fboId, 0);
//generate texture
        GLES20.glGenTextures(1, fboTex, 0);

//Bind Frame buffer
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId[0]);
//Bind texture
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTex[0]);
//Define texture parameters
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
//Attach texture FBO color attachment
        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, fboTex[0], 0);
//we are done, reset
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    }
 
Example 2
Source File: Framebuffer.java    From Spectaculum with Apache License 2.0 6 votes vote down vote up
public Framebuffer(int width, int height) {
    int[] framebuffer = new int[1];
    GLES20.glGenFramebuffers(1, framebuffer, 0);
    mFramebuffer = framebuffer[0];

    /* Every framebuffer has its own texture attached, because switching between framebuffers
     * is faster and recommended against switching texture attachements of a single framebuffer.
     * http://stackoverflow.com/a/6435997
     * http://stackoverflow.com/a/6767452 (comments!)
     */
    mTargetTexture = Texture2D.generateFloatTexture(width, height);

    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFramebuffer);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
            GLES20.GL_TEXTURE_2D, mTargetTexture.getHandle(), 0);

    checkFramebufferStatus();
}
 
Example 3
Source File: CameraFBORender.java    From AudioVideoCodec with Apache License 2.0 5 votes vote down vote up
/**
 * 创建fbo
 *
 * @param w
 * @param h
 */
private void createFBO(int w, int h) {
    //1. 创建FBO
    int[] fbos = new int[1];
    GLES20.glGenFramebuffers(1, fbos, 0);
    fboId = fbos[0];
    //2. 绑定FBO
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);

    //3. 创建FBO纹理
    int[] textureIds = new int[1];
    //创建纹理
    GLES20.glGenTextures(1, textureIds, 0);
    fboTextureId = textureIds[0];
    //绑定纹理
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTextureId);
    //环绕(超出纹理坐标范围)  (s==x t==y GL_REPEAT 重复)
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
    //过滤(纹理像素映射到坐标点)  (缩小、放大:GL_LINEAR线性)
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    //4. 把纹理绑定到FBO
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
            GLES20.GL_TEXTURE_2D, fboTextureId, 0);

    //5. 设置FBO分配内存大小
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, w, h,
            0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);

    //6. 检测是否绑定成功
    if (GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER)
            != GLES20.GL_FRAMEBUFFER_COMPLETE) {
        Log.d(TAG, "createFBO: 纹理绑定FBO失败");
    }
    //7. 解绑纹理和FBO
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}
 
Example 4
Source File: GLES20Canvas.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
private void setRenderTarget(BasicTexture oldTexture, RawTexture texture) {
    if (oldTexture == null && texture != null) {
        GLES20.glGenFramebuffers(1, mFrameBuffer, 0);
        checkError();
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer[0]);
        checkError();
    } else if (oldTexture != null && texture == null) {
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        checkError();
        GLES20.glDeleteFramebuffers(1, mFrameBuffer, 0);
        checkError();
    }

    if (texture == null) {
        setSize(mScreenWidth, mScreenHeight);
    } else {
        setSize(texture.getWidth(), texture.getHeight());

        if (!texture.isLoaded()) {
            texture.prepare(this);
        }

        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
                texture.getTarget(), texture.getId(), 0);
        checkError();

        checkFramebufferStatus();
    }
}
 
Example 5
Source File: CameraGLRendererBase.java    From Image-Detection-Samples with Apache License 2.0 5 votes vote down vote up
private void initFBO(int width, int height)
{
    Log.d(LOGTAG, "initFBO("+width+"x"+height+")");

    deleteFBO();

    GLES20.glGenTextures(1, texDraw, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texDraw[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    GLES20.glGenTextures(1, texFBO, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texFBO[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    //int hFBO;
    GLES20.glGenFramebuffers(1, FBO, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texFBO[0], 0);
    Log.d(LOGTAG, "initFBO error status: " + GLES20.glGetError());

    int FBOstatus = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (FBOstatus != GLES20.GL_FRAMEBUFFER_COMPLETE)
        Log.e(LOGTAG, "initFBO failed, status: " + FBOstatus);

    mFBOWidth  = width;
    mFBOHeight = height;
}
 
Example 6
Source File: CameraGLRendererBase.java    From FaceDetectDemo with Apache License 2.0 5 votes vote down vote up
private void initFBO(int width, int height)
{
    Log.d(LOGTAG, "initFBO("+width+"x"+height+")");

    deleteFBO();

    GLES20.glGenTextures(1, texDraw, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texDraw[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    GLES20.glGenTextures(1, texFBO, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texFBO[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    //int hFBO;
    GLES20.glGenFramebuffers(1, FBO, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texFBO[0], 0);
    Log.d(LOGTAG, "initFBO error status: " + GLES20.glGetError());

    int FBOstatus = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (FBOstatus != GLES20.GL_FRAMEBUFFER_COMPLETE)
        Log.e(LOGTAG, "initFBO failed, status: " + FBOstatus);

    mFBOWidth  = width;
    mFBOHeight = height;
}
 
Example 7
Source File: CameraGLRendererBase.java    From faceswap with Apache License 2.0 5 votes vote down vote up
private void initFBO(int width, int height)
{
    Log.d(LOGTAG, "initFBO("+width+"x"+height+")");

    deleteFBO();

    GLES20.glGenTextures(1, texDraw, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texDraw[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    GLES20.glGenTextures(1, texFBO, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texFBO[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    //int hFBO;
    GLES20.glGenFramebuffers(1, FBO, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texFBO[0], 0);
    Log.d(LOGTAG, "initFBO error status: " + GLES20.glGetError());

    int FBOstatus = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (FBOstatus != GLES20.GL_FRAMEBUFFER_COMPLETE)
        Log.e(LOGTAG, "initFBO failed, status: " + FBOstatus);

    mFBOWidth  = width;
    mFBOHeight = height;
}
 
Example 8
Source File: CameraGLRendererBase.java    From MOAAP with MIT License 5 votes vote down vote up
private void initFBO(int width, int height)
{
    Log.d(LOGTAG, "initFBO("+width+"x"+height+")");

    deleteFBO();

    GLES20.glGenTextures(1, texDraw, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texDraw[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    GLES20.glGenTextures(1, texFBO, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texFBO[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    //int hFBO;
    GLES20.glGenFramebuffers(1, FBO, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texFBO[0], 0);
    Log.d(LOGTAG, "initFBO error status: " + GLES20.glGetError());

    int FBOstatus = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (FBOstatus != GLES20.GL_FRAMEBUFFER_COMPLETE)
        Log.e(LOGTAG, "initFBO failed, status: " + FBOstatus);

    mFBOWidth  = width;
    mFBOHeight = height;
}
 
Example 9
Source File: GLES20Canvas.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
private void setRenderTarget(BasicTexture oldTexture, RawTexture texture) {
    if (oldTexture == null && texture != null) {
        GLES20.glGenFramebuffers(1, mFrameBuffer, 0);
        checkError();
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer[0]);
        checkError();
    } else if (oldTexture != null && texture == null) {
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        checkError();
        GLES20.glDeleteFramebuffers(1, mFrameBuffer, 0);
        checkError();
    }

    if (texture == null) {
        setSize(mScreenWidth, mScreenHeight);
    } else {
        setSize(texture.getWidth(), texture.getHeight());

        if (!texture.isLoaded()) {
            texture.prepare(this);
        }

        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
                texture.getTarget(), texture.getId(), 0);
        checkError();

        checkFramebufferStatus();
    }
}
 
Example 10
Source File: GlTextureFrameBuffer.java    From sealrtc-android with MIT License 4 votes vote down vote up
/**
 * (Re)allocate texture. Will do nothing if the requested size equals the current size. An
 * EGLContext must be bound on the current thread when calling this function. Must be called at
 * least once before using the framebuffer. May be called multiple times to change size.
 */
public void setSize(int width, int height) {
    if (width == 0 || height == 0) {
        throw new IllegalArgumentException("Invalid size: " + width + "x" + height);
    }
    if (width == this.width && height == this.height) {
        return;
    }
    this.width = width;
    this.height = height;

    // Allocate texture.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
    GLES20.glTexImage2D(
            GLES20.GL_TEXTURE_2D,
            0,
            pixelFormat,
            width,
            height,
            0,
            pixelFormat,
            GLES20.GL_UNSIGNED_BYTE,
            null);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    GlUtil.checkNoGLES2Error("GlTextureFrameBuffer setSize");

    // Attach the texture to the framebuffer as color attachment.
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBufferId);
    GLES20.glFramebufferTexture2D(
            GLES20.GL_FRAMEBUFFER,
            GLES20.GL_COLOR_ATTACHMENT0,
            GLES20.GL_TEXTURE_2D,
            textureId,
            0);

    // Check that the framebuffer is in a good state.
    final int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
        throw new IllegalStateException("Framebuffer not complete, status: " + status);
    }

    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}
 
Example 11
Source File: RecordFBOActivity.java    From grafika with Apache License 2.0 4 votes vote down vote up
/**
 * Prepares the off-screen framebuffer.
 */
private void prepareFramebuffer(int width, int height) {
    GlUtil.checkGlError("prepareFramebuffer start");

    int[] values = new int[1];

    // Create a texture object and bind it.  This will be the color buffer.
    GLES20.glGenTextures(1, values, 0);
    GlUtil.checkGlError("glGenTextures");
    mOffscreenTexture = values[0];   // expected > 0
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mOffscreenTexture);
    GlUtil.checkGlError("glBindTexture " + mOffscreenTexture);

    // Create texture storage.
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);

    // Set parameters.  We're probably using non-power-of-two dimensions, so
    // some values may not be available for use.
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    GlUtil.checkGlError("glTexParameter");

    // Create framebuffer object and bind it.
    GLES20.glGenFramebuffers(1, values, 0);
    GlUtil.checkGlError("glGenFramebuffers");
    mFramebuffer = values[0];    // expected > 0
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFramebuffer);
    GlUtil.checkGlError("glBindFramebuffer " + mFramebuffer);

    // Create a depth buffer and bind it.
    GLES20.glGenRenderbuffers(1, values, 0);
    GlUtil.checkGlError("glGenRenderbuffers");
    mDepthBuffer = values[0];    // expected > 0
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, mDepthBuffer);
    GlUtil.checkGlError("glBindRenderbuffer " + mDepthBuffer);

    // Allocate storage for the depth buffer.
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16,
            width, height);
    GlUtil.checkGlError("glRenderbufferStorage");

    // Attach the depth buffer and the texture (color buffer) to the framebuffer object.
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT,
            GLES20.GL_RENDERBUFFER, mDepthBuffer);
    GlUtil.checkGlError("glFramebufferRenderbuffer");
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
            GLES20.GL_TEXTURE_2D, mOffscreenTexture, 0);
    GlUtil.checkGlError("glFramebufferTexture2D");

    // See if GLES is happy with all this.
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
        throw new RuntimeException("Framebuffer not complete, status=" + status);
    }

    // Switch back to the default framebuffer.
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

    GlUtil.checkGlError("prepareFramebuffer done");
}
 
Example 12
Source File: PhotoFilterView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void drawCustomParamsPass() {
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, renderFrameBuffer[1]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, renderTexture[1], 0);
    GLES20.glClear(0);

    GLES20.glUseProgram(toolsShaderProgram);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, renderTexture[0]);
    GLES20.glUniform1i(sourceImageHandle, 0);
    if (showOriginal) {
        GLES20.glUniform1f(shadowsHandle, 1);
        GLES20.glUniform1f(highlightsHandle, 1);
        GLES20.glUniform1f(exposureHandle, 0);
        GLES20.glUniform1f(contrastHandle, 1);
        GLES20.glUniform1f(saturationHandle, 1);
        GLES20.glUniform1f(warmthHandle, 0);
        GLES20.glUniform1f(vignetteHandle, 0);
        GLES20.glUniform1f(grainHandle, 0);
        GLES20.glUniform1f(fadeAmountHandle, 0);
        GLES20.glUniform3f(highlightsTintColorHandle, 0, 0, 0);
        GLES20.glUniform1f(highlightsTintIntensityHandle, 0);
        GLES20.glUniform3f(shadowsTintColorHandle, 0, 0, 0);
        GLES20.glUniform1f(shadowsTintIntensityHandle, 0);
        GLES20.glUniform1f(skipToneHandle, 1);
    } else {
        GLES20.glUniform1f(shadowsHandle, getShadowsValue());
        GLES20.glUniform1f(highlightsHandle, getHighlightsValue());
        GLES20.glUniform1f(exposureHandle, getExposureValue());
        GLES20.glUniform1f(contrastHandle, getContrastValue());
        GLES20.glUniform1f(saturationHandle, getSaturationValue());
        GLES20.glUniform1f(warmthHandle, getWarmthValue());
        GLES20.glUniform1f(vignetteHandle, getVignetteValue());
        GLES20.glUniform1f(grainHandle, getGrainValue());
        GLES20.glUniform1f(fadeAmountHandle, getFadeValue());
        GLES20.glUniform3f(highlightsTintColorHandle, (tintHighlightsColor >> 16 & 0xff) / 255.0f, (tintHighlightsColor >> 8 & 0xff) / 255.0f, (tintHighlightsColor & 0xff) / 255.0f);
        GLES20.glUniform1f(highlightsTintIntensityHandle, getTintHighlightsIntensityValue());
        GLES20.glUniform3f(shadowsTintColorHandle, (tintShadowsColor >> 16 & 0xff) / 255.0f, (tintShadowsColor >> 8 & 0xff) / 255.0f, (tintShadowsColor & 0xff) / 255.0f);
        GLES20.glUniform1f(shadowsTintIntensityHandle, getTintShadowsIntensityValue());
        boolean skipTone = curvesToolValue.shouldBeSkipped();
        GLES20.glUniform1f(skipToneHandle, skipTone ? 1.0f : 0.0f);
        if (!skipTone) {
            curvesToolValue.fillBuffer();
            GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
            GLES20.glBindTexture(GL10.GL_TEXTURE_2D, curveTextures[0]);
            GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
            GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
            GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
            GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 200, 1, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, curvesToolValue.curveBuffer);
            GLES20.glUniform1i(curvesImageHandle, 1);
        }
    }

    GLES20.glUniform1f(widthHandle, renderBufferWidth);
    GLES20.glUniform1f(heightHandle, renderBufferHeight);
    GLES20.glEnableVertexAttribArray(inputTexCoordHandle);
    GLES20.glVertexAttribPointer(inputTexCoordHandle, 2, GLES20.GL_FLOAT, false, 8, textureBuffer);
    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, 2, GLES20.GL_FLOAT, false, 8, vertexInvertBuffer);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
 
Example 13
Source File: FboTexture.java    From PhotoMovie with Apache License 2.0 4 votes vote down vote up
private void prepareFrameBuffer(int width, int height) {
    GlUtil.checkGlError("prepareFramebuffer start");

    int[] values = new int[1];

    // Create a texture object and bind it.  This will be the color buffer.
    GLES20.glGenTextures(1, values, 0);
    GlUtil.checkGlError("glGenTextures");
    int offscreenTexture = values[0];   // expected > 0
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, offscreenTexture);
    GlUtil.checkGlError("glBindTexture " + offscreenTexture);

    // Create texture storage.
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);

    // Set parameters.  We're probably using non-power-of-two dimensions, so
    // some values may not be available for use.
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    GlUtil.checkGlError("glTexParameter");

    // Create framebuffer object and bind it.
    GLES20.glGenFramebuffers(1, values, 0);
    GlUtil.checkGlError("glGenFramebuffers");
    int framebuffer = values[0];    // expected > 0
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, framebuffer);
    GlUtil.checkGlError("glBindFramebuffer " + framebuffer);

    // Create a depth buffer and bind it.
    GLES20.glGenRenderbuffers(1, values, 0);
    GlUtil.checkGlError("glGenRenderbuffers");
    mRenderBuffer = values[0];    // expected > 0
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, mRenderBuffer);
    GlUtil.checkGlError("glBindRenderbuffer " + mRenderBuffer);

    // Allocate storage for the depth buffer.
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16,
            width, height);
    GlUtil.checkGlError("glRenderbufferStorage");

    // Attach the depth buffer and the texture (color buffer) to the framebuffer object.
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT,
            GLES20.GL_RENDERBUFFER, mRenderBuffer);
    GlUtil.checkGlError("glFramebufferRenderbuffer");
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
            GLES20.GL_TEXTURE_2D, offscreenTexture, 0);
    GlUtil.checkGlError("glFramebufferTexture2D");

    // See if GLES is happy with all this.
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
        throw new RuntimeException("Framebuffer not complete, status=" + status);
    }

    // Switch back to the default framebuffer.
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

    GlUtil.checkGlError("prepareFramebuffer done");
    mFrameBuffer = framebuffer;
    mId = offscreenTexture;
}
 
Example 14
Source File: Framebuffer.java    From PD-classes with GNU General Public License v3.0 4 votes vote down vote up
public void attach( int point, Texture tex ) {
	bind();
	GLES20.glFramebufferTexture2D( GLES20.GL_FRAMEBUFFER, point, GLES20.GL_TEXTURE_2D, tex.id, 0 );
}
 
Example 15
Source File: FBORender.java    From EZFilter with MIT License 4 votes vote down vote up
private void initFBO() {
    // 初始化输出纹理
    if (mTextureOut != null) {
        GLES20.glDeleteTextures(1, mTextureOut, 0);
        mTextureOut = null;
    }
    mTextureOut = new int[1];
    GLES20.glGenTextures(1, mTextureOut, 0);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureOut[0]);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);

    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,
            getWidth(), getHeight(), 0, GLES20.GL_RGBA,
            GLES20.GL_UNSIGNED_BYTE, null);

    // 初始化帧缓冲和深度缓冲
    if (mFrameBuffer != null) {
        GLES20.glDeleteFramebuffers(1, mFrameBuffer, 0);
        mFrameBuffer = null;
    }
    if (mDepthRenderBuffer != null) {
        GLES20.glDeleteRenderbuffers(1, mDepthRenderBuffer, 0);
        mDepthRenderBuffer = null;
    }
    mFrameBuffer = new int[1];
    mDepthRenderBuffer = new int[1];
    GLES20.glGenFramebuffers(1, mFrameBuffer, 0);
    GLES20.glGenRenderbuffers(1, mDepthRenderBuffer, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
            GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
            mTextureOut[0], 0);
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, mDepthRenderBuffer[0]);
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,
            GLES20.GL_DEPTH_COMPONENT16, getWidth(), getHeight());
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,
            GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER,
            mDepthRenderBuffer[0]);
}
 
Example 16
Source File: FrameBuffer.java    From media-for-mobile with Apache License 2.0 4 votes vote down vote up
@Override
public void setResolution(Resolution res) {
    resolution = res;
    if (framebuffer != -1) {
        release();
    }
    int[] glValues = new int[1];

    GLES20.glGenTextures(1, glValues, 0);
    utils.checkEglError("glGenTextures");
    offScreenTexture = glValues[0];

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, offScreenTexture);
    utils.checkEglError("glBindTexture");

    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, res.width(), res.height(), 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    utils.checkEglError("glTexImage2D");

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    GLES20.glGenFramebuffers(1, glValues, 0);
    utils.checkEglError("glGenFramebuffers");

    framebuffer = glValues[0];

    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, framebuffer);
    utils.checkEglError("glBindFramebuffer");

    GLES20.glGenRenderbuffers(1, glValues, 0);
    utils.checkEglError("glGenRenderbuffers");
    depthBuffer = glValues[0];

    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depthBuffer);
    utils.checkEglError("glBindRenderbuffer");

    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, res.width(), res.height());
    utils.checkEglError("glRenderbufferStorage");

    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, depthBuffer);
    utils.checkEglError("glFramebufferRenderbuffer");

    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, offScreenTexture, 0);
    utils.checkEglError("glFramebufferTexture2D");

    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    utils.checkEglError("glCheckFramebufferStatus");



    if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
        throw new RuntimeException("Incomplete framebuffer. Status: " + status);
    }

    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    utils.checkEglError("glBindFramebuffer(0)");
}
 
Example 17
Source File: Painting.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public PaintingData getPaintingData(RectF rect, boolean undo) {
    int minX = (int) rect.left;
    int minY = (int) rect.top;
    int width = (int) rect.width();
    int height = (int) rect.height();

    GLES20.glGenFramebuffers(1, buffers, 0);
    int framebuffer = buffers[0];
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, framebuffer);

    GLES20.glGenTextures(1, buffers, 0);
    int texture = buffers[0];

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);

    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texture, 0);

    GLES20.glViewport(0, 0, (int) size.width, (int) size.height);

    if (shaders == null) {
        return null;
    }
    Shader shader = shaders.get(undo ? "nonPremultipliedBlit" : "blit");
    if (shader == null) {
        return null;
    }
    GLES20.glUseProgram(shader.program);

    Matrix translate = new Matrix();
    translate.preTranslate(-minX, -minY);
    float[] effective = GLMatrix.LoadGraphicsMatrix(translate);
    float[] finalProjection = GLMatrix.MultiplyMat4f(projection, effective);

    GLES20.glUniformMatrix4fv(shader.getUniform("mvpMatrix"), 1, false, FloatBuffer.wrap(finalProjection));

    GLES20.glUniform1i(shader.getUniform("texture"), 0);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTexture());

    GLES20.glClearColor(0, 0, 0, 0);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ZERO);

    GLES20.glVertexAttribPointer(0, 2, GLES20.GL_FLOAT, false, 8, vertexBuffer);
    GLES20.glEnableVertexAttribArray(0);
    GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, 8, textureBuffer);
    GLES20.glEnableVertexAttribArray(1);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

    dataBuffer.limit(width * height * 4);
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, dataBuffer);

    PaintingData data;
    if (undo) {
        data = new PaintingData(null, dataBuffer);
    } else {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.copyPixelsFromBuffer(dataBuffer);

        data = new PaintingData(bitmap, null);
    }

    buffers[0] = framebuffer;
    GLES20.glDeleteFramebuffers(1, buffers, 0);

    buffers[0] = texture;
    GLES20.glDeleteTextures(1, buffers, 0);

    return data;
}
 
Example 18
Source File: OpenGlUtils.java    From TikTok with Apache License 2.0 4 votes vote down vote up
public static Bitmap drawToBitmapByFilter(Bitmap bitmap, GPUImageFilter filter,
                                          int displayWidth, int displayHeight, boolean rotate){
    if(filter == null)
        return null;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] mFrameBuffers = new int[1];
    int[] mFrameBufferTextures = new int[1];
    GLES20.glGenFramebuffers(1, mFrameBuffers, 0);
    GLES20.glGenTextures(1, mFrameBufferTextures, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrameBufferTextures[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffers[0]);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
            GLES20.GL_TEXTURE_2D, mFrameBufferTextures[0], 0);
    GLES20.glViewport(0, 0, width, height);
    filter.onInputSizeChanged(width, height);
    filter.onDisplaySizeChanged(displayWidth, displayHeight);
    int textureId = OpenGlUtils.loadTexture(bitmap, OpenGlUtils.NO_TEXTURE, true);
    if(rotate){
        FloatBuffer gLCubeBuffer = ByteBuffer.allocateDirect(TextureRotationUtil.CUBE.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        gLCubeBuffer.put(TextureRotationUtil.CUBE).position(0);

        FloatBuffer gLTextureBuffer = ByteBuffer
                .allocateDirect(TextureRotationUtil.TEXTURE_NO_ROTATION.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        gLTextureBuffer.put(TextureRotationUtil.getRotation(Rotation.ROTATION_90, true, false)).position(0);
        filter.onDrawFrame(textureId, gLCubeBuffer, gLTextureBuffer);
    }else {
        filter.onDrawFrame(textureId);
    }
    IntBuffer ib = IntBuffer.allocate(width * height);
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    result.copyPixelsFromBuffer(IntBuffer.wrap(ib.array()));
    GLES20.glDeleteTextures(1, new int[]{textureId}, 0);
    GLES20.glDeleteFramebuffers(1, mFrameBuffers, 0);
    GLES20.glDeleteTextures(1, mFrameBufferTextures, 0);
    filter.onInputSizeChanged(displayWidth, displayHeight);
    return result;
}
 
Example 19
Source File: FURenderer.java    From PLDroidShortVideo with Apache License 2.0 4 votes vote down vote up
private void createFBO(int width, int height) {
        if (fboTex != null && (fboWidth != width || fboHeight != height)) {
            deleteFBO();
        }

        fboWidth = width;
        fboHeight = height;

        if (fboTex == null) {
            fboId = new int[4];
            fboTex = new int[4];
            renderBufferId = new int[4];

//generate fbo id
            GLES20.glGenFramebuffers(4, fboId, 0);
//generate texture
            GLES20.glGenTextures(4, fboTex, 0);
//generate render buffer
            GLES20.glGenRenderbuffers(4, renderBufferId, 0);

            for (int i = 0; i < fboId.length; i++) {
//Bind Frame buffer
                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId[i]);
//Bind texture
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTex[i]);
//Define texture parameters
                GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
                GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
                GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
                GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
                GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
//Bind render buffer and define buffer dimension
                GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBufferId[i]);
                GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, width, height);
//Attach texture FBO color attachment
                GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, fboTex[i], 0);
//Attach render buffer to depth attachment
                GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderBufferId[i]);
//we are done, reset
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
                GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, 0);
                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
            }
        }
    }
 
Example 20
Source File: TwoPassFilter.java    From AndroidFastImageProcessing with MIT License votes vote down vote up
private void initFBO() {
		if(firstPassFrameBuffer != null) {
			GLES20.glDeleteFramebuffers(1, firstPassFrameBuffer, 0);
			firstPassFrameBuffer = null;
		}
		if(firstPassTextureOut != null) {
			GLES20.glDeleteTextures(1, firstPassTextureOut, 0);
			firstPassTextureOut = null;
		}
		if(firstPassDepthRenderBuffer != null) {
			GLES20.glDeleteRenderbuffers(1, firstPassDepthRenderBuffer, 0);
			firstPassDepthRenderBuffer = null;
		}
		firstPassFrameBuffer = new int[1];
		firstPassTextureOut = new int[1];
		firstPassDepthRenderBuffer = new int[1];
		GLES20.glGenFramebuffers(1, firstPassFrameBuffer, 0);
		GLES20.glGenRenderbuffers(1, firstPassDepthRenderBuffer, 0);
		GLES20.glGenTextures(1, firstPassTextureOut, 0);
		
		GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, firstPassFrameBuffer[0]);
		
		GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, firstPassTextureOut[0]);
		GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, getWidth(), getHeight(), 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5, null);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
		GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, firstPassTextureOut[0], 0);
		
		GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, firstPassDepthRenderBuffer[0]);
		GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, getWidth(), getHeight());
		GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, firstPassDepthRenderBuffer[0]);
		
		int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
		if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
			throw new RuntimeException(this+": Failed to set up render buffer with status "+status+" and error "+GLES20.glGetError());
		}
	}