Don't know why GPUImageFilterGroup use flipped frame buffer when drawing the filter in even position

297 views Asked by At

Following is the onDraw method in GPUImageFilterGroup.java.

// GPUImageFilterGroup.java
public void onDraw(final int textureId, final FloatBuffer cubeBuffer,
                   final FloatBuffer textureBuffer) {
    runPendingOnDrawTasks();
    if (!isInitialized() || mFrameBuffers == null || mFrameBufferTextures == null) {
        return;
    }
    if (mMergedFilters != null) {
        int size = mMergedFilters.size();
        int previousTexture = textureId;
        for (int i = 0; i < size; i++) {
            GPUImageFilter filter = mMergedFilters.get(i);
            boolean isNotLast = i < size - 1;
            if (isNotLast) {
                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffers[i]);
                GLES20.glClearColor(1, 0, 0, 0);
            }

            if (i == 0) {
                filter.onDraw(previousTexture, cubeBuffer, textureBuffer);
            } else if (i == size - 1) {
                filter.onDraw(previousTexture, mGLCubeBuffer, (size % 2 == 0) ? mGLTextureFlipBuffer : mGLTextureBuffer);
            } else {
                filter.onDraw(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
            }

            if (isNotLast) {
                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
                previousTexture = mFrameBufferTextures[i];
            }
        }
    }
 }

mGLTextureBuffer stores the positions of the texture.

public static final float TEXTURE_NO_ROTATION[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,
};

mGLTextureFlipBuffer is the upside-down of mGLTextureBuffer in vertical direction.

Just can't figure out why even filter and odd filter use different buffer as following line?

else if (i == size - 1)
    filter.onDraw(previousTexture, mGLCubeBuffer, (size % 2 == 0) ? mGLTextureFlipBuffer : mGLTextureBuffer);
1

There are 1 answers

0
songdowsong On

When you call GPUImage.setupCamera(), you will pass flipHorizontal and flipVertical. So, if you applied even (or odd) number of filters, the texture will be flipped even(or odd) times. If you don't like this, modify this as I do:

    if (i == size - 1) {// the last filter
      filter.onDraw(previousTexture, mGLCubeBuffer, textureBuffer);
    } else {
    //normalTextureBuffer is the not-clipped version of textureBuffer
      filter.onDraw(previousTexture, mGLCubeBuffer, normalTextureBuffer);
    }

Using the code above, you will get a 90-degree-rotated texture whenever you add one filter to the group, thus 4 filters for a 360-degree-rotated(same rotation as the input texture) texture. mGLCubeBuffer and textureBuffer have different rotations differing 90 degrees.
May this help.