Android OpenGL ES 繪圖方法參數解析

來源:互聯網
上載者:User

OpenGL ES提供了兩類方法來繪製一個空間幾何圖形:
public abstract void glDrawArrays(int mode, int first, int count) 使用VetexBuffer 來繪製,頂點的順序由vertexBuffer中的順序指定。
public abstract void glDrawElements(int mode, int count, int type, Buffer indices) ,可以重新定義頂點的順序,頂點的順序由indices Buffer 指定。
其中mode 為上述解釋頂點的模式。
前面說過頂點一般使用數組來定義,並使用Buffer來儲存以提高繪圖效能,參見Android OpenGL ES 開發中的Buffer使用
如下面定義三個頂點座標,並把它們存放在FloatBuffer 中:
float[] vertexArray = new float[]{
 -0.8f , -0.4f * 1.732f , 0.0f ,
0.8f , -0.4f * 1.732f , 0.0f ,
0.0f , 0.4f * 1.732f , 0.0f ,
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(0);
有了頂點的定義,下面就可以通過開啟OpenGL ES管道(Pipeline)的相應開關將頂點參數傳給OpenGL 庫:
開啟頂點開關和關閉頂點開關的方法如下:
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
在開啟頂點開關後,將頂點座標傳給OpenGL 管道的方法為:glVertexPointer:
public void glVertexPointer(int size,int type,int stride,Buffer pointer)
size: 每個頂點座標維數,可以為2,3,4。
type: 頂點的資料類型,可以為GL_BYTE, GL_SHORT, GL_FIXED,或 GL_FLOAT,預設為浮點類型GL_FLOAT。
stride: 每個相鄰頂點之間在數組中的間隔(位元組數),預設為0,表示頂點儲存之間無間隔。
pointer: 儲存頂點的數組。
應用用上可以般頂點的顏色值存放在對應頂點後面,如,RGB 採用4 位元組表示,此時相鄰頂點就不是連續存放的,stride 值為4

對應頂點除了可以為其定義座標外,還可以指定顏色,材質,法線(用於光照處理)等。
glEnableClientState 和 glDisableClientState 可以控制的pipeline開關可以有:GL_COLOR_ARRAY (顏色) ,GL_NORMAL_ARRAY (法線), GL_TEXTURE_COORD_ARRAY (材質), GL_VERTEX_ARRAY(頂點), GL_POINT_SIZE_ARRAY_OES等。
對應的傳入顏色,頂點,材質,法線的方法如下:
glColorPointer(int size,int type,int stride,Buffer pointer)
glVertexPointer(int size, int type, int stride, Buffer pointer)
glTexCoordPointer(int size, int type, int stride, Buffer pointer)
glNormalPointer(int type, int stride, Buffer pointer)
如果需要使用三角形來構造複雜圖形,可以使用GL_TRIANGLE_STRIP或GL_TRIANGLE_FAN模式,另外一種是通過定義頂點序列:
如定義了一個正方形:
對應的頂點和buffer 定義代碼:
private short[] indices = { 0, 1, 2, 0, 2, 3 };
//To gain some performance we also put this ones in a byte buffer.
// short is 2 bytes, therefore we multiply the number if vertices with 2.
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
ShortBuffer indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
定義三角形的頂點的順序很重要 在拼接曲面的時候,用來定義面的頂點的順序非常重要,因為頂點的順序定義了面的朝向(前向或是後向),為了擷取繪製的高效能,一般情況不會繪製面的前面和後面,只繪製面的“前面”。雖然“前面”“後面”的定義可以應人而易,但一般為所有的“前面”定義統一的頂點順序(順時針或是逆時針方向)。
下面代碼設定逆時針方法為面的“前面”:
gl.glFrontFace(GL10.GL_CCW);
開啟 忽略“後面”設定:
gl.glEnable(GL10.GL_CULL_FACE);
明確指明“忽略“哪個面的代碼如下:
gl.glCullFace(GL10.GL_BACK);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.