新聞中心
原文鏈接:http://docs.eoeandroid.com/training/graphics/OpenGL/shapes.html

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:申請域名、雅安服務器托管、營銷軟件、網(wǎng)站建設、陸良網(wǎng)站維護、網(wǎng)站推廣。
在OpenGL ES視圖中定義所要繪制的圖形,是創(chuàng)建高質(zhì)量圖形的第一步。使用OpenGL ES繪制圖形時,如果不了解怎樣基于OpenGL ES定義圖形對象,將會是一件棘手的事。 這節(jié)課將介紹以Android設備屏幕為基準的OpenGL ES坐標系統(tǒng),定義圖形的基本方法,圖形輪廓,以及定義三角形、方形。
定義三角形 Define a Triangle
OpenGL ES允許可以在三維坐標上定義你要繪制的對象。所以,在繪制三角形前,你要定義好它的坐標。在OpenGL中,定義坐標最典型的方法,就是定義坐標定點的一組浮點型數(shù)據(jù)。為了提高效率,你可以把這些坐標值寫進一組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定義了以下的默認坐標系統(tǒng):[0,0,0] (X,Y,Z)作為GLSurfaceView圖像的中點,[1,1,0]是圖像的右上角頂點,[-1,-1,0]是左下角頂點。如果需要該坐標系統(tǒng)的圖片,請移步OpenGL ES開發(fā)指南。 請注意,圖形的坐標是按逆時針方向定義的,繪制的順序是非常重要的,因為它定義圖形的正面以及反面,正面可以被直接繪制,而反面你可能選擇以OpenGL ES消除面方法使其不被繪制出來。想要獲取更多關于面與消除的信息,請查看OpenGL ES開發(fā)指南。
定義方形 Define a Square
在OpenGL中,定義三角形是非常簡單的,但你是否想要來點高難度的?比如,方形?要定義方形,有很多種方法,其中典型的方法就是把兩個三角形畫在一起:
圖1.使用兩個三角形繪制方形
同樣,你需要按照逆時針方向定義代表方形的兩個三角形的坐標頂點,并把值寫到ByteBuffer。為了避免每個三角形都定義坐標產(chǎn)生兩種坐標系統(tǒng),使用繪制列表告訴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繪制更復雜的形狀。一般來說,都是使用好幾個三角形來繪制圖形對象。在下節(jié)課,你將學習如何把這些圖像畫在屏幕上。
網(wǎng)頁名稱:如何使用OpenGL繪制更復雜的形狀
分享路徑:http://www.fisionsoft.com.cn/article/dpdjcsc.html


咨詢
建站咨詢
