Android OpenGL ES 開發(三): OpenGL ES 定義形狀

在上篇文章,咱們可以配置好基本的Android OpenGL 使用的環境。可是若是咱們不瞭解OpenGL ES如何定義圖像的一些基本知識就使用OpenGL ES進行繪圖仍是有點棘手的。因此可以在OpenGL ES的View裏面定義要繪製的形狀是進行高端繪圖操做的第一步。
本文主要作的事情就是爲了講解Android設備屏幕相關的OpenGL ES座標系統,定義形狀,形狀面的基礎知識,以及定義三角形和正方形。java

1、定義三角形

OpenGL ES容許你使用三維空間座標系定義繪製的圖像,因此你在繪製一個三角形以前必需要先定義它的座標。在OpenGL中,這樣作的典型方法是爲座標定義浮點數的頂點數組。
爲了得到最大的效率,能夠將這些座標寫入ByteBuffer,並傳遞到OpenGL ES圖形管道進行處理。數組

public class Triangle {

    private FloatBuffer vertexBuffer;

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float triangleCoords[] = {   // in counterclockwise order:
             0.0f,  0.622008459f, 0.0f, // top
            -0.5f, -0.311004243f, 0.0f, // bottom left
             0.5f, -0.311004243f, 0.0f  // bottom right
    };

    // Set color with red, green, blue and alpha (opacity) values
    float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

    public Triangle() {
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float)
                triangleCoords.length * 4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

        // create a floating point buffer from the ByteBuffer
        vertexBuffer = bb.asFloatBuffer();
        // add the coordinates to the FloatBuffer
        vertexBuffer.put(triangleCoords);
        // set the buffer to read the first coordinate
        vertexBuffer.position(0);
    }
}

默認狀況下,OpenGL ES採用座標系,[0,0,0](X,Y,Z)指定GLSurfaceView框架的中心,[1,1,0]是框架的右上角,[ - 1,-1,0]是框架的左下角。 有關此座標系的說明,請參閱OpenGL ES開發人員指南。框架

請注意,此圖形的座標以逆時針順序定義。 繪圖順序很是重要,由於它定義了哪一面是您一般想要繪製的圖形的正面,以及背面。關於這塊相關的更多的內容,能夠去查看一下相關的OpenGL ES 文檔。this

2、定義正方形

能夠看到,在OpenGL裏面定義一個三角形很簡單。可是若是你想要獲得一個更復雜一點的東西呢?好比一個正方形?可以找到不少辦法來做到這一點,可是在OpenGL裏面繪製這個圖形的方式是將兩個三角形畫在一塊兒。

一樣,你應該以逆時針的順序爲這兩個表明這個形狀的三角形定義頂點,並將這些值放在一個ByteBuffer中。 爲避免定義每一個三角形共享的兩個座標兩次,請使用圖紙列表告訴OpenGL ES圖形管道如何繪製這些頂點。 這是這個形狀的代碼:code

public class Square {

    private FloatBuffer vertexBuffer;
    private ShortBuffer drawListBuffer;

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float squareCoords[] = {
            -0.5f,  0.5f, 0.0f,   // top left
            -0.5f, -0.5f, 0.0f,   // bottom left
             0.5f, -0.5f, 0.0f,   // bottom right
             0.5f,  0.5f, 0.0f }; // top right

    private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices

    public Square() {
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
        // (# of coordinate values * 4 bytes per float)
                squareCoords.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(squareCoords);
        vertexBuffer.position(0);

        // initialize byte buffer for the draw list
        ByteBuffer dlb = ByteBuffer.allocateDirect(
        // (# of coordinate values * 2 bytes per short)
                drawOrder.length * 2);
        dlb.order(ByteOrder.nativeOrder());
        drawListBuffer = dlb.asShortBuffer();
        drawListBuffer.put(drawOrder);
        drawListBuffer.position(0);
    }
}

這個例子讓你瞭解用OpenGL建立更復雜的形狀的過程。 通常來講,您使用三角形的集合來繪製對象。下面的文章裏面,將講述如何在屏幕上繪製這些形狀。對象

相關文章
相關標籤/搜索