Android OpenGL ES基本用法(10),多个surface绘制同一个共享纹理


目录


在这里插入图片描述
在上一篇的基础上实现共享纹理。

MainActivity

 private void initView() {
        picGlSurfaceView = (PicGlSurfaceView) findViewById(R.id.picGlSurfaceView);
        llContent = (LinearLayout) findViewById(R.id.llContent);
        //
        picGlSurfaceView.getPicRender().setOnRenderCreateListener(new PicRender.OnRenderCreateListener() {
            @Override
            public void onCreate(final int textureId) {

                //需要在主线程中
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (llContent.getChildCount() > 0) {
                            llContent.removeAllViews();
                        }
                        //动态添加
                        for (int i = 0; i < 3; i++) {
                            PicGlSurfaceViewMulti picSurface = new PicGlSurfaceViewMulti(MediaCodecTestActivity.this);
                            //设置共享的纹理
                            picSurface.setTextureId(textureId);
                            //使用自己的surface所以传入null,使用共享的eglcontext
                            picSurface.setSurfaceAndEglContext(null, picGlSurfaceView.getEglContext());

                            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 300);
                            layoutParams.weight = 1;
                            layoutParams.topMargin = 10;
                            layoutParams.leftMargin = 10;
                            layoutParams.rightMargin = 10;
                            llContent.addView(picSurface, layoutParams);
                        }

                    }
                });
            }
        });


    }

PicGlSurfaceViewMulti

package com.example.opengl.eglpic;

import android.content.Context;
import android.util.AttributeSet;

import com.example.opengl.egl.MyGlSurfaceView;

public class PicGlSurfaceViewMulti extends MyGlSurfaceView {

    private PicRenderMulti picRenderMulti;

    public PicGlSurfaceViewMulti(Context context) {
        this(context,null);
    }

    public PicGlSurfaceViewMulti(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PicGlSurfaceViewMulti(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        picRenderMulti = new PicRenderMulti(context);
        setRender(picRenderMulti);
    }

    public void setTextureId(int textureId){
        if (picRenderMulti != null) {
            picRenderMulti.setTextureId(textureId);
        }
    }
}

PicRenderMulti

package com.example.opengl.eglpic;

import android.content.Context;
import android.opengl.GLES20;

import com.example.opengl.R;
import com.example.opengl.egl.MyGlSurfaceView;
import com.example.opengl.egl.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class PicRenderMulti implements MyGlSurfaceView.EGLRender {

    private Context context;

    private float[] vertexData = {
            -1f, -1f,
            1f, -1f,
            -1f, 1f,
            1f, 1f
    };
    private FloatBuffer vertexBuffer;

    private float[] fragmentData = {
            0f, 1f,
            1f, 1f,
            0f, 0f,
            1f, 0f
    };
    private FloatBuffer fragmentBuffer;

    private int program;
    private int vPosition;
    private int fPosition;

    private int sampler;

    private int vboId;

    private int textureId;
    private int index;

    public void setTextureId(int textureId) {
        this.textureId = textureId;
        this.index = index;
    }

    public PicRenderMulti(Context context) {
        this.context = context;

        vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
                .put(vertexData);
        vertexBuffer.position(0);

        fragmentBuffer = ByteBuffer.allocateDirect(fragmentData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
                .put(fragmentData);
        fragmentBuffer.position(0);
    }

    @Override
    public void onSurfaceCreated() {
        String vertexSource = ShaderUtil.readRawTxt(context, R.raw.vertex_shader);
        String fragmentSource = ShaderUtil.readRawTxt(context, R.raw.fragment_shader);

        program = ShaderUtil.createProgram(vertexSource, fragmentSource);

        vPosition = GLES20.glGetAttribLocation(program, "v_Position");
        fPosition = GLES20.glGetAttribLocation(program, "f_Position");
        sampler = GLES20.glGetUniformLocation(program, "sTexture");

        int[] vbos = new int[1];
        GLES20.glGenBuffers(1, vbos, 0);
        vboId = vbos[0];

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4 + fragmentData.length * 4, null, GLES20.GL_STATIC_DRAW);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexData.length * 4, vertexBuffer);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4, fragmentData.length * 4, fragmentBuffer);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }

    @Override
    public void onSurfaceChanged(int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame() {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glClearColor(1f, 0f, 0f, 1f);

        GLES20.glUseProgram(program);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);


        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);

        GLES20.glEnableVertexAttribArray(vPosition);
        GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 8,
                0);

        GLES20.glEnableVertexAttribArray(fPosition);
        GLES20.glVertexAttribPointer(fPosition, 2, GLES20.GL_FLOAT, false, 8,
                vertexData.length * 4);

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

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,下面是一个在一个surface渲染多个纹理的简单例子: 首先,我们需要在OpenGL ES中创建多个纹理对象,并将它们绑定到不同的纹理单元。例如,我们可以创建两个纹理对象,并将它们绑定到纹理单元0和1: ``` GLuint texture1, texture2; glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1); // 设置纹理参数和加载纹理数据 glGenTextures(1, &texture2); glBindTexture(GL_TEXTURE_2D, texture2); // 设置纹理参数和加载纹理数据 // 将纹理分别绑定到纹理单元0和1 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2); ``` 然后,我们需要在着色器中使用采样器(sampler2D)类型的uniform变量来接收纹理单元的值,并在渲染时使用这些采样器来采样纹理。例如,我们可以定义两个采样器uniform变量,并在片段着色器中使用它们来采样纹理: ``` // 顶点着色器 attribute vec4 position; attribute vec2 texCoord; varying vec2 v_texCoord; void main() { gl_Position = position; v_texCoord = texCoord; } // 片段着色器 varying vec2 v_texCoord; uniform sampler2D texture1; uniform sampler2D texture2; void main() { vec4 color1 = texture2D(texture1, v_texCoord); vec4 color2 = texture2D(texture2, v_texCoord); gl_FragColor = color1 * color2; } ``` 最后,在绘制之前,我们需要将纹理单元的值传递给着色器中的采样器uniform变量。例如,我们可以使用glUniform1i函数将纹理单元0和1的值分别传递给采样器uniform变量texture1和texture2: ``` // 绘制代码 glUseProgram(program); glUniform1i(glGetUniformLocation(program, "texture1"), 0); glUniform1i(glGetUniformLocation(program, "texture2"), 1); // 绑定顶点数据和纹理坐标数据 glDrawArrays(GL_TRIANGLES, 0, vertexCount); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值