矩阵变换,我们需要将矩阵传递给片元着色器,然后和位置相乘,得到最终的位置,矩阵可以做什么
- 平移
- 缩放
- 旋转
我们需要做矩阵变换,我们需要将数据传递进去,还有就是计算得到矩阵。
完整代码:
package com.example.myapplication.learn.shape;
import android.opengl.GLES20;
import android.opengl.Matrix;
import com.example.myapplication.learn.shape.base.BaseGameScreen;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class TriangleMatrix extends BaseGameScreen {
private final String vertexShaderCode =
"attribute vec4 vPosition;" +
"uniform mat4 vMatrix;" +
"void main() {" +
" gl_Position = vMatrix*vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
static final int COORDS_PER_VERTEX = 3;
private FloatBuffer vertexBuffer;
static float triangleCoords[] = {
0.5f, 0.5f, 0.0f, // top
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f // bottom right
};
private int mProgram ;
//顶点个数
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
//顶点之间的偏移量
private final int vertexStride = COORDS_PER_VERTEX * 4; // 每个顶点四个字节
//设置颜色,依次为红绿蓝和透明通道
float color[] = { 1.0f, 1.0f, 1.0f, 1.0f };
private int mPositionHandle;
private int mColorHandle;
private int mMatrixHandler;
public TriangleMatrix(){
}
public void create(){
ByteBuffer bb = ByteBuffer.allocateDirect(
triangleCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(triangleCoords);
vertexBuffer.position(0);
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode);
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram,vertexShader);
GLES20.glAttachShader(mProgram,fragmentShader);
GLES20.glLinkProgram(mProgram);
}
//相机位置
private float[] mViewMatrix=new float[16];
//透视
private float[] mProjectMatrix=new float[16];
//变换矩阵
private float[] mMVPMatrix=new float[16];
public void surfaceChange(int width,int height){
float ratio=(float)width/height;
// 设置相机类型
Matrix.frustumM(mProjectMatrix,0,-ratio,ratio,-1,1,3,7);
// 设置相机位置
Matrix.setLookAtM(mViewMatrix, 0,
0, 0, 7.0f,
0f, 0f, 0f,
0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix,0,mProjectMatrix,0,mViewMatrix,0);
}
@Override
public void dispose() {
}
@Override
public void render() {
//程序加入到环境里面
GLES20.glUseProgram(mProgram);
mMatrixHandler = GLES20.glGetUniformLocation(mProgram,"vMatrix");
//设置
GLES20.glUniformMatrix4fv(mMatrixHandler,1,false,mMVPMatrix,0);
//获取位置句柄
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
GLES20.glEnableVertexAttribArray(mPositionHandle);
//获得句柄
//准备三角形的坐标数据
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
//获取片元着色器的vColor成员的句柄
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
//设置绘制三角形的颜色
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
//绘制三角形
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
//禁止顶点数组的句柄
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
重点的几句,修改片元着色器,然后 得到位置,将矩阵传递,至于怎样得到矩阵,后面会有章节说明。可以对比一下案例一.
网友评论