[OpenGL ES] 正交投影

我只是一个虾纸丫 提交于 2020-11-24 12:26:45

在OpenGL中就需要用到矩形来改变顶点坐标的范围,最后再归一化就可以了。
顶点着色器中添加矩阵

vertex_shader_m.glsl

attribute vec4 v_Position;
attribute vec2 f_Position;
varying vec2 ft_Position;
uniform mat4 u_Matrix;
void main() {
    ft_Position = f_Position;
    gl_Position = v_Position * u_Matrix;
}

然后根据图形宽高和屏幕宽高计算(?)的长度

orthoM(float[] m, int mOffset, float left, float right, float bottom, float top, float near, float far)
Matrix.orthoM(matrix, 0, -width / ((height / 702f * 526f)),  width / ((height / 702f * 526f)), -1f, 1f, -1f, 1f);
Matrix.orthoM(matrix, 0, -1, 1, - height / ((width / 526f * 702f)),  height / ((width / 526f * 702f)), -1f, 1f);

使用

GLES20.glUniformMatrix4fv(umatrix, 1, false, matrix, 0);


GGLTextureRender
package com.example.opengldemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.util.Log;

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

public class GGLTextureRender implements MyGLSurfaceView.MyGLRender {

    private Context context;
    //顶点坐标
    private final float[] vertexData = {
            -1f, -1f,
            1f, -1f,
            -1f, 1f,
            1f, 1f
    };

    //纹理坐标
    private final float[] fragmentData = {
            //fbo坐标
            0f, 0f,
            1f, 0f,
            0f, 1f,
            1f, 1f

//            0f, 1f,
//            1f, 1f,
//            0f, 0f,
//            1f, 0f
    };

    private FloatBuffer vertexBuffer;
    private FloatBuffer fragmentBuffer;

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

        fboRender = new FboRender(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);
    }

    private int program;

    private int vPosition;
    private int fPosition;

    private int textureid;
    private int sampler;

    private int vboId;
    private int fboId;

    private int imgTextureId;

    private FboRender fboRender;

    /**************************正交投影-START*********************************************/
    private int umatrix;
    private float[] matrix = new float[16];
    /**************************正交投影-END***********************************************/

    @Override
    public void onSufaceCreated() {

        fboRender.onCreate();

        /**************************正交投影-START*********************************************/
        //更换shader
        String vertexSource = GShaderUtil.getRawResource(context, R.raw.vertex_shader_m);
        /**************************正交投影-END***********************************************/

        String fragmentSource = GShaderUtil.getRawResource(context, R.raw.fragment_shader);
        program = GShaderUtil.createProgram(vertexSource, fragmentSource);

        if (program > 0) {
            //顶点坐标
            vPosition = GLES20.glGetAttribLocation(program, "v_Position");
            //纹理坐标
            fPosition = GLES20.glGetAttribLocation(program, "f_Position");

            sampler = GLES20.glGetUniformLocation(program,"sTexture");

            /**************************正交投影-START*********************************************/
            //获取矩阵
            umatrix =  GLES20.glGetUniformLocation(program,"u_Matrix");
            /**************************正交投影-END***********************************************/


            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);

            int [] fbos = new int[1];
            GLES20.glGenBuffers(1, fbos, 0);
            fboId = fbos[0];
            //绑定
            GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,fboId);

            //生成纹理
            int[] textureIds = new int[1];
            GLES20.glGenTextures(1, textureIds, 0);
            textureid = textureIds[0];

            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureid);
            GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
            GLES20.glUniform1i(sampler, 0);

            //设置环绕过滤方法
            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);

            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);

            //设置FBO分配内存大小
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 900, 1600, 0,
                    GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
            //把纹理绑定到FBO
            GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
                    GLES20.GL_TEXTURE_2D, textureid, 0);
            //检查FBO绑定是否成功
            if(GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER) != GLES20.GL_FRAMEBUFFER_COMPLETE){
                Log.e("godv", "fbo error");
            }else {
                Log.e("godv", "fbo success");
            }

            //解绑纹理
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

            //解绑
            GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
            imgTextureId = loadTexture(R.drawable.androids);
        }
    }

    @Override
    public void onSufaceChanged(int width, int height) {
        //900 * 1600
        GLES20.glViewport(0,0,width,height);
        fboRender.onChange(width, height);

        /**************************正交投影-START*********************************************/
        if (width > height){
            Matrix.orthoM(matrix, 0, -width / ((height / 702f * 526f)),
                    width / ((height / 702f * 526f)), -1f, 1f, -1f, 1f);
        }else {
            Matrix.orthoM(matrix, 0, -1, 1, - height / ((width / 526f * 702f)),
                    height / ((width / 526f * 702f)), -1f, 1f);
        }
        /**************************正交投影-END***********************************************/

    }

    @Override
    public void onDrawFrame() {

        //绑定FBO  离屏渲染    =0关掉
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);

        //清屏
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        //使用颜色清屏
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

        //使用program
        GLES20.glUseProgram(program);

        /**************************正交投影-START*********************************************/
        //使用
        GLES20.glUniformMatrix4fv(umatrix, 1, false, matrix, 0);
        /**************************正交投影-END***********************************************/

        //绑定FBO纹理id
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, imgTextureId);

        //绑定VBO
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);

        //使用顶点坐标
        GLES20.glEnableVertexAttribArray(vPosition);
        //传0 从VBO中取值
        GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 8, 0);

        GLES20.glEnableVertexAttribArray(fPosition);

        //VBO
        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);

        //解绑VBO
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

        //解绑FBO
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        fboRender.onDraw(textureid);

    }

    /*
    返回图片数据的纹理
     */
        private int loadTexture(int src){
            //创建纹理
            int[] textureIds = new int[1];
            GLES20.glGenTextures(1, textureIds, 0);
            //绑定纹理
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
            //设置环绕过滤方法
            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);

            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);

            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),src);
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
            //解绑
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
            return textureIds[0];
        }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!