在OpenGL ES視圖中定義所要繪制的圖形,是創建高質量圖形的第一步。使用OpenGL ES繪制圖形時,如果不了解怎樣基于OpenGL ES定義圖形對象,將會是一件棘手的事。本文將會為大家介紹以Android設備屏幕為基準的OpenGL ES坐標系統,定義圖形的基本方法,圖形輪廓,以及定義三角形、方形。
原文鏈接:http://docs.eoeandroid.com/training/graphics/opengl/shapes.html
在OpenGL ES視圖中定義所要繪制的圖形,是創建高質量圖形的第一步。使用OpenGL ES繪制圖形時,如果不了解怎樣基于OpenGL ES定義圖形對象,將會是一件棘手的事。 這節課將介紹以Android設備屏幕為基準的OpenGL ES坐標系統,定義圖形的基本方法,圖形輪廓,以及定義三角形、方形。
定義三角形 Define a Triangle
OpenGL ES允許可以在三維坐標上定義你要繪制的對象。所以,在繪制三角形前,你要定義好它的坐標。在OpenGL中,定義坐標最典型的方法,就是定義坐標定點的一組浮點型數據。為了提高效率,你可以把這些坐標值寫進一組ByteBuffer,它將會傳遞給OpenGL ES圖形管道進行處理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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開發指南。
定義方形 Define a Square
在OpenGL中,定義三角形是非常簡單的,但你是否想要來點高難度的?比如,方形?要定義方形,有很多種方法,其中典型的方法就是把兩個三角形畫在一起:

圖1.使用兩個三角形繪制方形
同樣,你需要按照逆時針方向定義代表方形的兩個三角形的坐標頂點,并把值寫到ByteBuffer。為了避免每個三角形都定義坐標產生兩種坐標系統,使用繪制列表告訴OpenGL ES圖像管道如何繪制這些頂點,下面是該種形狀繪制方法的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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繪制更復雜的形狀。一般來說,都是使用好幾個三角形來繪制圖形對象。在下節課,你將學習如何把這些圖像畫在屏幕上。