基于Android的OpenGL ES 2.0学习笔记(2)

接(1)章


这次尝试画一个三角形,并且加上一个旋转动画的效果。以下为三角类的代码:

public class CZTriangle implements ICZgles {
    private static final String TAG = "CZPoint";

    private static final String VERTEX_SHADER
            = "attribute vec4 a_Position;"
            + "uniform mat4 u_rotate;"
            + "void main() {"
            + "    gl_Position = u_rotate * a_Position;"
            + "}";
    private static final String FRAGMENT_SHADER
            = "void main() {"
            + "    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
            + "}";

    private int mAPosition;
    private int mURotate;
    private int mUFragmentColor;
    private FloatBuffer mFB;

    private float[] mVer = {
            0.0f, 0.0f, 0.0f,
            0.5f, -0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f
    };

    private float angle = 0.0f;

    @Override
    public void init(int program, int vertexShader, int fragmentShader) {
        //mUFragmentColor = GLES20.glGetUniformLocation(program, "u_FragmentColor");
        ByteBuffer bb = ByteBuffer.allocateDirect(mVer.length * 4);
        bb.order(ByteOrder.nativeOrder());
        mFB = bb.asFloatBuffer();
        mFB.put(mVer);
        mFB.position(0);

        int[] vb = new int[1];
        GLES20.glGenBuffers(1, vb, 0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vb[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, 4 * mFB.limit(), mFB, GLES20.GL_STATIC_DRAW);
        mAPosition = GLES20.glGetAttribLocation(program, "a_Position");
        mURotate = GLES20.glGetUniformLocation(program, "u_rotate");
        GLES20.glVertexAttribPointer(mAPosition, 3, GLES20.GL_FLOAT, false, 0, 0);
        //GLES20.glVertexAttribPointer(mAPosition, 3, GLES20.GL_FLOAT, false, 0, mFB);
    }

    @Override
    public String getVertexShader() {
        return VERTEX_SHADER;
    }

    @Override
    public String getFragmentShader() {
        return FRAGMENT_SHADER;
    }

    @Override
    public void draw() {
        float[] matrix = new float[16];
        Matrix.setRotateM(matrix, 0, angle, 0.0f, 0.0f, 1.0f);
        angle += 1.0f;
        if (angle > 360.0f) {
            angle = 0.0f;
        }
        GLES20.glUniformMatrix4fv(mURotate, 1, false, matrix, 0);
        //GLES20.glUniform4f(mUFragmentColor, 1.0f, 0.0f, 0.0f, 1.0f);
        //GLES20.glVertexAttribPointer(mAPosition, 3, GLES20.GL_FLOAT, false, 0, mFB);
        GLES20.glEnableVertexAttribArray(mAPosition);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
        GLES20.glDisableVertexAttribArray(mAPosition);
    }
}

opengl es 2.0并没有提供移动旋转等运动的api,都需要定义四维矩阵和当前坐标相乘去实现相应的效果,也就是我们常说的齐次坐标。至于如何定义出这个四维矩阵就不在这篇文章里细说了。

可以通过Matrix这个类来辅助定义各种矩阵类型,例如旋转的话可以使用以下方法:

Matrix.setRotateM(长度为16的数组, 偏移量, 旋转角度, 旋转轴的x, 旋转轴的y, 旋转轴的z);

得到相应的矩阵后传给shader和当前坐标矩阵相乘


同时这次大概也熟悉了一下2.0的api,感觉大部分api都和shader联系的太紧密了,不结合底层的东西很难说清楚,还得继续深入学习。

总之感觉opengl es的编程思想并非面向对象,很多东西太低层,搞起来不太适应。


WebGL虽然也是基于opengl es,但是有个three js这么个脚本,很多东西做起来比android容易上手

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. The Android robot is reproduced from work created and shared by Google and is used according to terms described in the Creative Commons 3.0 Attribution License (http://creativecommons.org/licenses/by/3.0/us/legalcode). The unit circle image in Figure 43, from http://en.wikipedia.org/wiki/File:Unit_circle.svg, is used according to the terms described in the Creative Commons Attribution-ShareAlike license, located at http://creativecommons.org/licenses/by-sa/3.0/legalcode. Day skybox and night skybox courtesy of Jockum Skoglund, also known as hipshot, hipshot@zfight.com,http://www.zfight.com. The image of the trace capture button is created and shared by the Android Open Source Project and is used according to terms described in the Creative Commons 2.5 Attribution License.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值