纹理到纹理的拷贝(一)

class TextureCopy {
            private int mProgram;
            private FloatBuffer mTexVertices;
            private FloatBuffer mPosVertices;

            private int mTexSamplerHandle;
            private int mTexCoordHandle;
            private int mPosCoordHandle;

            private static final String VERTEX_SHADER =
                    "attribute vec4 a_position;\n" +
                            "attribute vec2 a_texcoord;\n" +
                            "varying vec2 v_texcoord;\n" +
                            "void main() {\n" +
                            "  gl_Position = a_position;\n" +
                            "  v_texcoord = a_texcoord;\n" +
                            "}\n";

            private static final String FRAGMENT_SHADER =
                    "precision mediump float;\n" +
                            "uniform sampler2D tex_sampler;\n" +
                            "varying vec2 v_texcoord;\n" +
                            "void main() {\n" +
                            "  gl_FragColor = texture2D(tex_sampler, v_texcoord);\n" +
                            "}\n";
            // 左右翻转
            // private final float[] TEX_VERTICES = {
            //      1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f
            // };

            // 上下翻转
            // private final float[] TEX_VERTICES = {
            //      0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
            // };
            //不变换
            private final float[] TEX_VERTICES = {
                    0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f
            };

            private final float[] POS_VERTICES = {
                    -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f
            };

            private static final int FLOAT_SIZE_BYTES = 4;
            private int[] fFrame = new int[1];

            public int init() {
                // Create program
                mProgram = GLToolbox.createProgram(VERTEX_SHADER, FRAGMENT_SHADER);

                // Bind attributes and uniforms
                mTexSamplerHandle = GLES20.glGetUniformLocation(mProgram,
                        "tex_sampler");
                mTexCoordHandle = GLES20.glGetAttribLocation(mProgram, "a_texcoord");
                mPosCoordHandle = GLES20.glGetAttribLocation(mProgram, "a_position");

                // Setup coordinate buffers
                mTexVertices = ByteBuffer.allocateDirect(
                        TEX_VERTICES.length * FLOAT_SIZE_BYTES)
                        .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mTexVertices.put(TEX_VERTICES).position(0);
                mPosVertices = ByteBuffer.allocateDirect(
                        POS_VERTICES.length * FLOAT_SIZE_BYTES)
                        .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mPosVertices.put(POS_VERTICES).position(0);

                GLES20.glGenFramebuffers(1, fFrame, 0);
                return 0;
            }

            public int copy(int inTexture, int outTexture, int imageWidth, int imageHeight) {
                GLES20.glUseProgram(mProgram);
                GLToolbox.checkGlError("glUseProgram");

                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fFrame[0]);
                //为FrameBuffer挂载Texture[1]来存储颜色
                GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
                        GLES20.GL_TEXTURE_2D, outTexture, 0);

                GLES20.glViewport(0, 0, imageWidth, imageHeight);
                GLToolbox.checkGlError("glViewport");

                GLES20.glDisable(GLES20.GL_BLEND);

                GLES20.glVertexAttribPointer(mTexCoordHandle, 2, GLES20.GL_FLOAT, false,
                        0, mTexVertices);
                GLES20.glEnableVertexAttribArray(mTexCoordHandle);
                GLES20.glVertexAttribPointer(mPosCoordHandle, 2, GLES20.GL_FLOAT, false,
                        0, mPosVertices);
                GLES20.glEnableVertexAttribArray(mPosCoordHandle);
                GLToolbox.checkGlError("vertex attribute setup");

                GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
                GLToolbox.checkGlError("glActiveTexture");
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, inTexture);//把已经处理好的Texture传到GL上面
                GLToolbox.checkGlError("glBindTexture");
                GLES20.glUniform1i(mTexSamplerHandle, 0);

                GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
                return 0;
            }

            public void release() {
                GLES20.glDeleteFramebuffers(1, fFrame, 0);
            }
        };
       

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值