Android 平台下OpenGL绘制立方体(2)

图形类 ——本文用所引法绘制

构造方中初始化数据 和渲染器
private void initData() {
//获得 顶点 颜色 和 索引的缓冲数据
//顶点
verBuffer = getFloatBuffer(cubePositions);
//颜色
colorBuffer = getFloatBuffer(color);
//索引
indexBuffer = getShortBuffer(index);
}
private void initShader() {

    //创建着色程序
     mProgram = GLES20.glCreateProgram();
    //关联着色器源码
    GLES20.glAttachShader(mProgram,loadShader(GLES20.GL_VERTEX_SHADER,vertexShaderCode));
    GLES20.glAttachShader(mProgram,loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode));
    //连接程序
    GLES20.glLinkProgram(mProgram);
    //获取 着色器语言中变量的id、

    vPosition = GLES20.glGetAttribLocation(mProgram, "vPosition");
    aColor = GLES20.glGetAttribLocation(mProgram,"aColor");
    vMatrix = GLES20.glGetUniformLocation(mProgram,"vMatrix");
}

//设置 矩阵变换需要用到的 矩阵
public static float[] mProjMatrix = new float[16];//4x4矩阵 投影用
public static float[] mVMatrix = new float[16];//摄像机位置朝向9参数矩阵
public static float[] mMVPMatrix;//最后起作用的总变换矩阵
public static float[] mMMatrix = new float[16];//具体物体的移动旋转矩阵,旋转、平移
public float xAngle = 0;//绕x轴旋转的角度

///矩阵相乘操作

public float[] getMatrix(float[] spec){
    mMVPMatrix = new float[16];
    /**
     * 参数解释
     * float[] result,  变化后的结果矩阵
     * int resultOffset,//结果矩阵开始 索引
     float[] lhs,//左矩阵 即两个矩阵相乘 左边矩阵
     int lhsOffset,  左矩阵起始位置
     float[] rhs, 右矩阵
     int rhsOffset 右矩阵 起始位置

     该方法的意义就是 spec 这个矩阵 通过 左乘 mVMatrix得到 结果矩阵mMVPMatrix
     *
     */
    Matrix.multiplyMM(mMVPMatrix,0,mVMatrix,0,spec,0);

    //该句代码的作用是 合并投影和视口矩阵
    Matrix.multiplyMM(mMVPMatrix,0,mProjMatrix,0,mMVPMatrix,0);
    return mMVPMatrix;
}

//提供 绘制的方法
public void drawSelf() {
    xAngle = (xAngle+1)%360;
    //引入着色程序
    GLES20.glUseProgram(mProgram);
    //初始化总变化矩阵

    /**
     * @param rm returns the result  结果矩阵
     * @param rmOffset index into rm where the result matrix starts 结果起始索引
     * @param a angle to rotate in degrees 旋转角度
     * @param x X axis component  up 向量 x分量
     * @param y Y axis component  up 向量 y分量 注意 y 不可为0 否则看不到效果
     * @param z Z axis component  up 向量 z分量
     */
    Matrix.setRotateM(mMMatrix,0,0,0,1,0);
    //位移 z轴正向位移
    /**
     *  * @param m matrix 平移后 结果
     * @param mOffset index into m where the matrix starts
     * @param x translation factor x 平移的 x分量
     * @param y translation factor y 平移 y 的分量
     * @param z translation factor z 平移 z 的分量
     */
    Matrix.translateM(mMMatrix, 0, 0, 0, 1);
    //旋转 z轴正向位移
    /**
     *  @param m source matrix 旋转结果
     * @param mOffset index into m where the matrix starts
     * @param a angle to rotate in degrees 旋转角
     * @param x X axis component x轴旋转
     * @param y Y axis component y轴旋转
     * @param z Z axis component z轴旋转
     */
    Matrix.rotateM(mMMatrix, 0, xAngle, 1, 1, 1);
    GLES20.glUniformMatrix4fv(vMatrix, 1, false, getMatrix(mMMatrix), 0);
    //设置数据
    GLES20.glVertexAttribPointer(vPosition,
            3, GLES20.GL_FLOAT,
            false, 0, verBuffer);
    GLES20.glVertexAttribPointer(aColor, 4,
            GLES20.GL_FLOAT, false, 0, colorBuffer);
    //****** 开启渲染
    GLES20.glEnableVertexAttribArray(vPosition);
    GLES20.glEnableVertexAttribArray(aColor);
    //绘制
    /**
     * 本次使用 索引绘制法
     *
     *int mode, 绘制模式 GL_TRIANGLES 三角形 GL_LINE_LOOP 线 GL_POINTS 点 GL_TRIANGLE_FAN 扇形 GL_TRIANGLE_STRIP带状胡
     int count,
     int type,
     java.nio.Buffer indices
     */
    GLES20.glDrawElements(
            GLES20.GL_TRIANGLES,36,
            GLES20.GL_UNSIGNED_SHORT,
            indexBuffer);
    //关闭
    GLES20.glDisableVertexAttribArray(vPosition);
    GLES20.glDisableVertexAttribArray(aColor);
}

//最后的使用
public class MainActivity extends AppCompatActivity {

private EGLView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = new EGLView(this);
    setContentView(view);
}

@Override
protected void onResume() {
    super.onResume();
    view.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    view.onPause();
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值