OpenGL ES:矩阵变换(平移、旋转、缩放)

总的变换矩阵:
mvpMatrix = projectionMatrix * viewMatrix * modelMatrix;

投影矩阵(projectrionMatrix):

    //根据屏幕宽高计算
    float ratio = (float) width / height;
    //计算投影矩阵
    Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

视图矩阵(viewMatrix):

    float[] resultTransform = new float[16];
    //设置观察视角 eye相机坐标 center 目标坐标 up 相机正上方 向量vuv(相机头部指向)
    Matrix.setLookAtM(mViewMatrix, 0,
            0, 0, 3,
            0f, 0f, 0f,
            0f, 1f, 0.0f);

模型矩阵(modelMatrix):

modelMatrix=translateMatrix * scaleMatrix * rotateMatrix

            private final float[] mBaseMatrix = new float[]{
            1f, 0f, 0f, 0f,
            0f, 1f, 0f, 0f,
            0f, 0f, 1f, 0f,
            0f, 0f, 0f, 1f
		   };
		    // x y z 向量设置绕着哪个轴旋转
            Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0f, 0, 1f);
            // x y z 代表缩放到比例
            Matrix.scaleM(mScaleMatrix,0,mBaseMatrix,0,1f,1f,1f);
            //x y z 代表位移量
            Matrix.translateM(mTranslateMatrix,0,mBaseMatrix,0,0.5f,0.5f,0.0f);

使用如下函数使得2个矩阵合并:

    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

顶点着色器:

#version 300 es
//变换矩阵 统一变量
uniform mat4 uMVPMatrix;
//顶点
layout (location = 0) in vec4 vPosition;
//纹理坐标
layout (location = 1) in vec2 aTextureCoord;
//输出纹理坐标(s,t)
out vec2 vTexCoord;
void main() {
     gl_Position  = uMVPMatrix * vPosition;
     vTexCoord = aTextureCoord;
}

片段着色器:

#version 300 es
//声明精度
precision mediump float;
//纹理采样器 对应一个纹理单元
uniform sampler2D texture0;
uniform sampler2D texture1;
//接收刚才顶点着色器传入的纹理坐标(s,t)
in vec2 vTexCoord;
out vec4 vFragColor;
void main() {
     vFragColor = mix(texture(texture0,vTexCoord),texture(texture1,vTexCoord),0.5f);
}

你可能感兴趣的:(OpenGL,ES)