圖片和圖形之定義形狀(11)

原文

概要


能夠在OpenGL ES視圖的上下文中定義要繪製的形狀是創建高端圖形傑作的第一步。使用OpenGL ES進行繪製可能會有點棘手,但您不知道OpenGL ES如何定義圖形對象的一些基本知識。

本課講解了與Android設備屏幕相關的OpenGL ES座標系,定義形狀的基礎知識,形狀表面以及定義三角形和正方形。

定義一個三角形


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剔除面部特徵進行繪製。有關面和剔除的更多信息,請參閱 OpenGL ES開發人員指南

定義一個正方形


在OpenGL中定義三角形非常簡單,但是如果您想要稍微複雜一點,該怎麼辦?說,一個正方形?有很多方法可以做到這一點,但在OpenGL ES中繪製這種形狀的典型路徑是使用兩個三角形繪製在一起:
圖片和圖形之定義形狀(11)
圖1.使用兩個三角形繪製一個正方形

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

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創建更復雜的形狀的過程。一般來說,您可以使用三角形集合來繪製對象。在下一課中,您將學習如何在屏幕上繪製這些形狀。

    Lastest Update:2018.04.25

聯繫我

QQ:94297366
微信打賞:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公衆號推薦:

圖片和圖形之定義形狀(11)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章