Android OpenGL ES:展示一张2d图片

本文介绍了如何在Android环境中,利用GLSurfaceView和OpenGL ES 2.0进行2D图片的展示。通过创建GLSurfaceView并自定义GLSurfaceView.Renderer,实现了图片的纹理化和渲染。在实现过程中需要注意调整mvp矩阵以防止图片因坐标系转换导致宽高比例变形。
摘要由CSDN通过智能技术生成

前言

本文的环境和条件:GLSurfaceView + OpenGL ES 2.0

代码结构

  1. Activity视图使用GLSurfaceView;
  2. GLSurfaceView使用自定义GLSurfaceView.Render实现,(GLSurfaceView.setRender())
  3. 在Render实现图片纹理化和渲染。

代码Show you the code

自定义的GLSurfaceView.Renderer实现。

public class BitmapRender implements GLSurfaceView.Renderer {
   

    private static final String VERTEX_SHADER =
            "uniform mat4 uMVPMatrix;" +
                    "attribute vec4 aPosition;" +
                    "attribute vec2 aTexCoord;" +
                    "varying vec2 vTexCoord;" +
                    "void main() {" +
                    "  gl_Position = uMVPMatrix * aPosition;" +
                    "  vTexCoord = aTexCoord;" +
                    "}";
    private static final String FRAGMENT_SHADER =
            "precision lowp float;" +
                    "precision highp sampler2D;" +
                    "precision highp int;" +
                    "uniform sampler2D uTextureUnit;" +
                    "varying vec2 vTexCoord;" +
                    "void main() {" +
                    "  gl_FragColor = texture2D(uTextureUnit, vTexCoord);" +
                    "}";
    private static final int COORDS_PRE_VERTEX = 2;
    private static final int COORDS_PRE_TEXTURE = 2;
    // 顶点坐标:世界坐标系
    private static final float[] VERTEX = {
   
            1, 1,  // right, top
            -1, 1, // left, top
            1, -1, // right, bottom
            -1, -1 // left, bottom
    };

    // 纹理坐标
    private static final float[] TEXTUER = {
   
            1, 0.f, // right, top
            0.f, 0.f, // left, top
            1, 1, // right, bottom
            0.f, 1, // left, bottom
    };
    private FloatBuffer mVertexBuffer;
    private FloatBuffer mTextureBuffer;
    private float[] mMVPMatrix = new float[16];
    private int mProgram;
    private int mMVPMatrixHandle;
    private int mPositionHandle;
    private int mTexCoordHandle;
    private Bitmap bitmap;
    private int mBitmapWidth;
    private int mBitmapHeight;
    private int texUnitHandle;
    private int imageTexture = -1;

    public BitmapRender(String imagePath) {
   
        mVertexBuffer = GLESUtils.createFloatBuffer(VERTEX);
        mTextureBuffer = GLESUtils.createFloatBuffer(TEXTUER);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
   
        //GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

        int vertexShader = GLESUtils.createVertexShader(VERTEX_SHADER);
        int fragmentShader = GLESUtils.createFragmentShader(FRAGMENT_SHADER);
        mProgram = GLESUtils.createProgram(vertexShader, fragmentShader);
        // GLES20.glGetAttribLocation方法:获取着
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值