核心类:
package test.com.opengles3_1;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.widget.GridLayout;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
/**
* //三角形
* Created by hbin on 2016/7/26.
*/
//三角形
public class Triangle
{
public static float[] mProjMatrix = new float[16];//4x4矩阵 投影用
public static float[] mVMatrix = new float[16];//摄像机位置朝向9参数矩阵
public static float[] mMVPMatrix;//最后起作用的总变换矩阵
int mProgram;//自定义渲染管线程序id
int muMVPMatrixHandle;//总变换矩阵引用id
int maPositionHandle; //顶点位置属性引用id
int maColorHandle; //顶点颜色属性引用id
String mVertexShader;//顶点着色器
String mFragmentShader;//片元着色器
static float[] mMMatrix = new float[16];//具体物体的移动旋转矩阵,旋转、平移
FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
FloatBuffer mColorBuffer;//顶点着色数据缓冲
int vCount=0;
float xAngle=0;//绕x轴旋转的角度
public Triangle(MyTDView mv)
{
//初始化顶点坐标与着色数据
initVertexData();
//初始化shader
initShader(mv);
}
public void initVertexData()
{
//顶点坐标数据的初始化
vCount=3;
final float UNIT_SIZE=0.2f;
// final float UNIT_SIZE=0.1f;
float vertices[]=new float[]
{
-4*UNIT_SIZE,0,0,
0,-4*UNIT_SIZE,0,
4*UNIT_SIZE,0,0
// -4*UNIT_SIZE,0,1*UNIT_SIZE,
// 0,-4*UNIT_SIZE,1*UNIT_SIZE,
// 4*UNIT_SIZE,0,1*UNIT_SIZE
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
float colors[]=new float[]
{
1,1,1,0,
0,0,1,0,
0,1,0,0
};
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
cbb.order(ByteOrder.nativeOrder());
mColorBuffer = cbb.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
}
//初始化shader
public void initShader(MyTDView mv)
{
//加载顶点着色器的脚本内容
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex.sh", mv.getResources());
//加载片元着色器的脚本内容