注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛安卓而產(chǎn)生了翻譯的念頭,純屬個(gè)人興趣愛好。
原文鏈接: http://developer.android.com/training/graphics/opengl/shapes.html
在一個(gè)OpenGL ES視圖的上下文中定義形狀,是創(chuàng)建你的杰作所需要的第一步。在不知道關(guān)于OpenGL ES如何期望你來(lái)定義圖形對(duì)象的基本知識(shí)的時(shí)候,通過(guò)OpenGL ES 繪圖可能會(huì)有些困難。
這節(jié)課將解釋OpenGL ES相對(duì)于Android設(shè)備屏幕的坐標(biāo)系,定義形狀和形狀表面的基本知識(shí),如定義一個(gè)三角形和一個(gè)矩形。
一). 定義一個(gè)三角形
OpenGL ES允許你使用三維空間的坐標(biāo)來(lái)定義繪畫對(duì)象。所以在你能畫三角形之前,你必須先定義它的坐標(biāo)。在OpenGL 中,典型的辦法是以浮點(diǎn)數(shù)的形式為坐標(biāo)定義一個(gè)頂點(diǎn)數(shù)組。為了讓效率最大化,你可以將坐標(biāo)寫入一個(gè) ByteBuffer ,它將會(huì)傳入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 ); } }
默認(rèn)情況下,OpenGL ES會(huì)假定一個(gè)坐標(biāo)系,在這個(gè)坐標(biāo)系中,[0, 0, 0](X, Y, Z)對(duì)應(yīng)的是
GLSurfaceView
框架的中心。[1, 1, 0]對(duì)應(yīng)的是框架的右上角,[-1, -1, 0]對(duì)應(yīng)的則是左下角。如果想要看此坐標(biāo)系的插圖說(shuō)明,可以閱讀
OpenGL ES
開發(fā)手冊(cè)。
注意到這個(gè)形狀的坐標(biāo)是以逆時(shí)針順序定義的。繪制的順序非常關(guān)鍵,因?yàn)樗x了那一面是形狀的正面(你希望繪制的一面),以及背面(你可以使用OpenGL ES的 cull face特性來(lái)讓它不要繪制 )。更多關(guān)于該方面的信息, 可以閱讀 OpenGL ES 開發(fā)手冊(cè)。
二). 定義一個(gè)矩形
在OpenGL中定義三角形非常簡(jiǎn)單,那么你是否想要增加一些復(fù)雜性呢?比如,定義一個(gè)矩形?有很多方法可以用來(lái)定義矩形,不過(guò)在OpenGL ES中最典型的辦法是使用兩個(gè)三角形拼接在一起:
圖1. 使用兩個(gè)三角形畫一個(gè)矩形
再一次地,你需要以逆時(shí)針的形式為三角形頂點(diǎn)定義坐標(biāo)來(lái)表現(xiàn)這個(gè)圖形,并將值放入一個(gè)
ByteBuffer
中。為了避免由兩個(gè)三角形共享的頂點(diǎn)被重復(fù)定義,可以使用一個(gè)繪制列表來(lái)告訴OpenGL ES圖形處理流程應(yīng)該如何畫這些頂點(diǎn)。下面是代碼樣例:
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 ); } }
該樣例給了你一個(gè)如何使用OpenGL創(chuàng)建復(fù)雜圖形的啟發(fā),通常來(lái)說(shuō),你需要使用三角形的集合來(lái)繪制對(duì)象。在下一節(jié)課中,你將學(xué)習(xí)如何在屏幕上畫這些形狀。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
