Android官方开发文档Training系列课程中文版:OpenGL绘图之图形定义
原文地址:http://android.xsoftlab.net/training/graphics/opengl/shapes.html
使用OpenGL繪制圖形的第一步就是要定義一個圖形。如果不清楚OpenGL如何繪制自定義圖形的相關基礎知識時,那么使用OpenGL一定要仔細。
這節課將會簡單講述OpenGl ES的坐標系統,及定義圖形的基礎。本章中以三角形、正方形舉例做說明。
定義三角形
OpenGL ES允許用戶使用三維空間定義繪制的對象。所以,在繪制三角形之前,必須先定義它的坐標。在OpenGL中,最典型的方式就是以浮點型的數字定義一個頂點坐標數組。為了提高效率,應該將這些數據寫入到一個ByteBuffer對象中,它隨后會被送入OpenGL ES圖形管道中,等待處理。
public class Triangle {private FloatBuffer vertexBuffer;// number of coordinates per vertex in this arraystatic 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 left0.5f, -0.311004243f, 0.0f // bottom right};// Set color with red, green, blue and alpha (opacity) valuesfloat color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };public Triangle() {// initialize vertex byte buffer for shape coordinatesByteBuffer bb = ByteBuffer.allocateDirect(// (number of coordinate values * 4 bytes per float)triangleCoords.length * 4);// use the device hardware's native byte orderbb.order(ByteOrder.nativeOrder());// create a floating point buffer from the ByteBuffervertexBuffer = bb.asFloatBuffer();// add the coordinates to the FloatBuffervertexBuffer.put(triangleCoords);// set the buffer to read the first coordinatevertexBuffer.position(0);} }默認情況下,OpenGL ES的坐標系統將[0,0,0](X,Y,Z)點作為GLSurfaceView的中心。[1,1,0]點是框架的右上角,[-1,-1,0]點是框架的左下角。有關該坐標系統的演示信息,請參見OpenGL ES開發指南。
注意這里定義圖形坐標點是以逆時針方向定義的。知道這個繪制順序是很重要的,因為它決定了圖形的哪一面應該是朝著用戶的,哪一面應該是被繪制的,以及朝著用戶的另一面的繪制信息,它還可以選擇不使用OpenGL繪制朝著用戶的這一面等功能。有關更多轉向及選擇繪制的相關信息,請參見OpenGL ES開發指南。
定義正方形
定義一個三角形是很容易的,但是定義一些稍微比較復雜一點的圖形會怎么樣?比如定義一個正方形?這里提供了幾種實現方式,但是典型的方式就是將兩個三角形繪制到一起,然后形成一個正方形:
再提醒一次,定義圖形的時候應該以逆時針方向定義這兩個三角形的頂點。為了避免兩個三角形都重復定義兩個坐標點,應該使用繪制列表來告訴OpenGL ES繪制管道如何繪制這些頂點。下面是這個圖形的代碼:
public class Square {private FloatBuffer vertexBuffer;private ShortBuffer drawListBuffer;// number of coordinates per vertex in this arraystatic final int COORDS_PER_VERTEX = 3;static float squareCoords[] = {-0.5f, 0.5f, 0.0f, // top left-0.5f, -0.5f, 0.0f, // bottom left0.5f, -0.5f, 0.0f, // bottom right0.5f, 0.5f, 0.0f }; // top rightprivate short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw verticespublic Square() {// initialize vertex byte buffer for shape coordinatesByteBuffer 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 listByteBuffer 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);} }這個示例展現了如何創建復合型圖形的方法。一般情況下,會以三角形堆疊的方式來繪制對象。在下節課中,你會學習到如何在屏幕上繪制這些圖形。
總結
以上是生活随笔為你收集整理的Android官方开发文档Training系列课程中文版:OpenGL绘图之图形定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (设计模式)简单工厂模式之通过配置文件动
- 下一篇: WSDL基础知识