定義形狀

為應用程式建立高階圖形時,首先要能夠定義要在 OpenGL ES 檢視畫面中繪製的形狀。如果不知道 OpenGL ES 預期您定義圖形物件的基本概念,則使用 OpenGL ES 進行繪圖可能會有點困難。

本課程將說明與 Android 裝置螢幕相關的 OpenGL ES 座標系統、定義形狀、形狀錶面以及定義三角形和正方形的基本知識。

定義三角形

OpenGL ES 可讓您使用三維空間中的座標定義繪製的物件。因此,您必須先定義三角形的座標,才能繪製三角形。在 OpenGL 中,一般做法是定義座標的浮點數端點陣列。為求最大效率,您會將這些座標寫入 ByteBuffer,並傳遞至 OpenGL ES 圖形管道進行處理。

Kotlin

// number of coordinates per vertex in this array
const val COORDS_PER_VERTEX = 3
var triangleCoords = floatArrayOf(     // 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
)

class Triangle {

    // Set color with red, green, blue and alpha (opacity) values
    val color = floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f)

    private var vertexBuffer: FloatBuffer =
            // (number of coordinate values * 4 bytes per float)
            ByteBuffer.allocateDirect(triangleCoords.size * 4).run {
                // use the device hardware's native byte order
                order(ByteOrder.nativeOrder())

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

Java

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 中繪製這類形狀的典型路徑就是同時使用兩個繪製的三角形:

圖 1 用兩個三角形繪製正方形。

再次提醒您,您應該以逆時針順序定義代表此形狀的兩個三角形的頂點,然後將值放入 ByteBuffer 中。為避免定義每個三角形共用的兩個座標,請使用繪圖清單,告知 OpenGL ES 圖形管道如何繪製這些頂點。以下是形狀程式碼:

Kotlin

// number of coordinates per vertex in this array
const val COORDS_PER_VERTEX = 3
var squareCoords = floatArrayOf(
        -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
)

class Square2 {

    private val drawOrder = shortArrayOf(0, 1, 2, 0, 2, 3) // order to draw vertices

    // initialize vertex byte buffer for shape coordinates
    private val vertexBuffer: FloatBuffer =
            // (# of coordinate values * 4 bytes per float)
            ByteBuffer.allocateDirect(squareCoords.size * 4).run {
                order(ByteOrder.nativeOrder())
                asFloatBuffer().apply {
                    put(squareCoords)
                    position(0)
                }
            }

    // initialize byte buffer for the draw list
    private val drawListBuffer: ShortBuffer =
            // (# of coordinate values * 2 bytes per short)
            ByteBuffer.allocateDirect(drawOrder.size * 2).run {
                order(ByteOrder.nativeOrder())
                asShortBuffer().apply {
                    put(drawOrder)
                    position(0)
                }
            }
}

Java

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 建立更複雜的形狀。一般來說,您會使用三角形集合來繪製物件。在下一堂課程中,您將學習如何在畫面上繪製這些形狀。