1. 程式人生 > >Android OpenGL ES 簡明開發教程_真正的3D圖形

Android OpenGL ES 簡明開發教程_真正的3D圖形

  • 摘要:該系列文章均轉載自/content/540025.html由於原文好像無法開啟,正好自己有記錄,所以正好分享出來。Mesh(網格,三角面)是構成空間形體的基本元素,前面的正方形也是由兩個Mesh構成的。本篇將介紹使用Mesh構成四面體,椎體等基本空間形體。Design設計在使用OpenGL框架時一個好的設計原則是使用“CompositePattern”(組合模式),本篇採用如下設計:Mesh首先定義一個基類Mesh,所有空間形體最基本的構成元素為Mesh(三角形網格),其基本
  • 該系列文章均轉載自 

    /content/540025.html 

    由於原文好像無法開啟,正好自己有記錄,所以正好分享出來。 

    Mesh(網格,三角面)是構成空間形體的基本元素,前面的正方形也是由兩個Mesh構成的。本篇將介紹使用Mesh構成四面體,椎體等基本空間形體。
     

    Design設計在使用OpenGL 框架時一個好的設計原則是使用“Composite Pattern”(組合模式),本篇採用如下設計: 

    Android OpenGL ES 簡明開發教程_真正的3D圖形 
    Mesh首先定義一個基類 Mesh,所有空間形體最基本的構成元素為Mesh(三角形網格) ,其基本定義如下: 

    [code]public class Mesh { // Our vertex buffer. private FloatBuffer verticesBuffer = null; // Our index buffer. private ShortBuffer indicesBuffer = null; // The number of indices. private int numOfIndices = -1; // Flat Color private float[] rgba = new float[] { 1.0f, 1.0f, 1.0f, 1.0f }; // Smooth Colors private FloatBuffer colorBuffer = null; // Translate params. public float x = 0; public float y = 0; public float z = 0; // Rotate params. public float rx = 0; public float ry = 0; public float rz = 0; public void draw(GL10 gl) { // Counter-clockwise winding. gl.glFrontFace(GL10.GL_CCW); // Enable face culling. gl.glEnable(GL10.GL_CULL_FACE); // What faces to remove with the face culling. gl.glCullFace(GL10.GL_BACK); // Enabled the vertices buffer for writing and //to be used during // rendering. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // Specifies the location and data format //of an array of vertex // coordinates to use when rendering. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer); // Set flat color gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]); // Smooth color if (colorBuffer != null) { // Enable the color array buffer to be //used during rendering. gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); } gl.glTranslatef(x, y, z); gl.glRotatef(rx, 1, 0, 0); gl.glRotatef(ry, 0, 1, 0); gl.glRotatef(rz, 0, 0, 1); // Point out the where the color buffer is. gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices, GL10.GL_UNSIGNED_SHORT, indicesBuffer); // Disable the vertices buffer. gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // Disable face culling. gl.glDisable(GL10.GL_CULL_FACE); } protected void setVertices(float[] vertices) { // a float is 4 bytes, therefore //we multiply the number if // vertices with 4. ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); verticesBuffer = vbb.asFloatBuffer(); verticesBuffer.put(vertices); verticesBuffer.position(0); } protected void setIndices(short[] indices) { // short is 2 bytes, therefore we multiply //the number if // vertices with 2. ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); ibb.order(ByteOrder.nativeOrder()); indicesBuffer = ibb.asShortBuffer(); indicesBuffer.put(indices); indicesBuffer.position(0); numOfIndices = indices.length; } protected void setColor(float red, float green, float blue, float alpha) { // Setting the flat color. rgba[0] = red; rgba[1] = green; rgba[2] = blue; rgba[3] = alpha; } protected void setColors(float[] colors) { // float has 4 bytes. ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); cbb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(colors); colorBuffer.position(0); }} 

    setVertices 允許子類重新定義頂點座標。 

    setIndices 允許子類重新定義頂點的順序。 

    setColor /setColors允許子類重新定義顏色。 

    x,y,z 定義了平移變換的引數。 

    rx,ry,rz 定義旋轉變換的引數。 

    Plane
    有了Mesh定義之後,再來構造Plane,plane可以有寬度,高度和深度,寬度定義為沿X軸方向的長度,深度定義為沿Z軸方向長度,高度為Y軸方向。 

    Android OpenGL ES 簡明開發教程_真正的3D圖形 

    Segments為形體寬度,高度,深度可以分成的份數。 Segments在構造一個非均勻分佈的Surface特別有用,比如在一個遊戲場景中,構造地貌,使的Z軸的值隨機分佈在-0.1到0.1之間,然後給它渲染好看的材質就可以造成地圖凹凸不平的效果。 

    Android OpenGL ES 簡明開發教程_真正的3D圖形 
    上面圖形中Segments為一正方形,但在OpenGL中我們需要使用三角形,所有需要將Segments分成兩個三角形。為Plane 定義兩個建構函式: 

    // Let you decide the size of the plane but still only one segment. 

    public Plane(float width, float height) 

    // For alla your settings. 

    public Plane(float width, float height, int widthSegments, int heightSegments) 

    比如構造一個1 unit 寬和 1 unit高,並分成4個Segments,使用圖形表示如下: 

    Android OpenGL ES 簡明開發教程_真正的3D圖形
     

    左邊的圖顯示了segments ,右邊的圖為需要建立的Face(三角形)。 

    Plane類的定義如下: 

    [code]public class Plane extends Mesh { public Plane() {this(1, 1, 1, 1); } public Plane(float width, float height) {this(width, height, 1, 1); } public Plane(float width, float height, int widthSegments, int heightSegments) {float[] vertices = new float[(widthSegments + 1) * (heightSegments + 1) * 3];short[] indices = new short[(widthSegments + 1) * (heightSegments + 1) * 6];float xOffset = width / -2;float yOffset = height / -2;float xWidth = width / (widthSegments);float yHeight = height / (heightSegments);int currentVertex = 0;int currentIndex = 0;short w = (short) (widthSegments + 1);for (int y = 0; y < heightSegments + 1; y++) {for (int x = 0; x < widthSegments + 1; x++) { vertices[currentVertex] = xOffset + x * xWidth; vertices[currentVertex + 1] = yOffset + y * yHeight; vertices[currentVertex + 2] = 0; currentVertex += 3; int n = y * (widthSegments + 1) + x; if (y < heightSegments &;&; x < widthSegments) {// Face oneindices[currentIndex] = (short) n;indices[currentIndex + 1] = (short) (n + 1);indices[currentIndex + 2] = (short) (n + w);// Face twoindices[currentIndex + 3] = (short) (n + 1);indices[currentIndex + 4] = (short) (n + 1 + w);indices[currentIndex + 5] = (short) (n + 1 + w - 1);currentIndex += 6; }}}setIndices(indices);setVertices(vertices); }} 

    Cube下面來定義一個正方體(Cube),為簡單起見,這個四面體只可以設定寬度,高度,和深度,沒有和Plane一樣提供Segments支援。 

    [code]public class Cube extends Mesh { public Cube(float width, float height, float depth) {width /= 2;height /= 2;depth /= 2;float vertices[] = { -width, -height, -depth, // 0 width, -height, -depth, // 1 width, height, -depth, // 2 -width, height, -depth, // 3 -width, -height, depth, // 4 width, -height, depth, // 5 width, height, depth, // 6 -width, height, depth, // 7};short indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5, 3, 0, 1, 3, 1, 2, };setIndices(indices);setVertices(vertices); }} 

    GroupGroup可以用來管理多個空間幾何形體,如果把Mesh比作Android的View ,Group可以看作Android的ViewGroup,Android的View的設計也是採用的“Composite Pattern”。 

    Group的主要功能是把針對Group的操作(如draw)分發到Group中的每個成員對應的操作(如draw)。 

    Group定義如下: 

    [code]public class Group extends Mesh { private Vector<Mesh> children = new Vector<Mesh>(); @Override public void draw(GL10 gl) {int size = children.size();for( int i = 0; i < size; i++)children.get(i).draw(gl); } /** * @param location * @param object * @see java.util.Vector#add(int, java.lang.Object) */ public void add(int location, Mesh object) {children.add(location, object); } /** * @param object * @return * @see java.util.Vector#add(java.lang.Object) */ public boolean add(Mesh object) {return children.add(object); } /** * * @see java.util.Vector#clear() */ public void clear() {children.clear(); } /** * @param location * @return * @see java.util.Vector#get(int) */ public Mesh get(int location) {return children.get(location); } /** * @param location * @return * @see java.util.Vector#remove(int) */ public Mesh remove(int location) {return children.remove(location); } /** * @param object * @return * @see java.util.Vector#remove(java.lang.Object) */ public boolean remove(Object object) {return children.remove(object); } /** * @return * @see java.util.Vector#size() */ public int size() {return children.size(); }} 

    Genderer程式碼: 

    [code]public class OpenGLRenderer implements GLSurfaceView.Renderer { private Mesh root; public OpenGLRenderer() {// Initialize our cube.Group group = new Group();Cube cube = new Cube(1, 1, 1);cube.rx = 45;cube.ry = 45;group.add(cube);root = group; } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition * .khronos.opengles.GL10, javax.microedition.khronos.egl.EGLConfig) */ @Override public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig eglConfig) {// Set the background color to black ( rgba ).gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Enable Smooth Shading, default not really needed.gl.glShadeModel(GL10.GL_SMOOTH);// Depth buffer setup.gl.glClearDepthf(1.0f);// Enables depth testing.gl.glEnable(GL10.GL_DEPTH_TEST);// The type of depth testing to do.gl.glDepthFunc(GL10.GL_LEQUAL);// Really nice perspective calculations.gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition. * khronos.opengles.GL10) */ @Override public void onDrawFrame(GL10 gl) {// Clears the screen and depth buffer.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// Replace the current matrix with the identity matrixgl.glLoadIdentity();// Translates 4 units into the screen.gl.glTranslatef(0, 0, -4);// Draw our scene.root.draw(gl); } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition * .khronos.opengles.GL10, int, int) */ @Override public void onSurfaceChanged(GL10 gl, int width, int height) {// Sets the current view port to the new size.gl.glViewport(0, 0, width, height);// Select the projection matrixgl.glMatrixMode(GL10.GL_PROJECTION);// Reset the projection matrixgl.glLoadIdentity();// Calculate the aspect ratio of the windowGLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);// Select the modelview matrixgl.glMatrixMode(GL10.GL_MODELVIEW);// Reset the modelview matrixgl.glLoadIdentity(); }} 

    其它建議上面我們定義裡Mesh, Plane, Cube等基本空間幾何形體,對於構造複雜圖形(如人物),可以預先建立一些通用的幾何形體,如果在組合成較複雜的形體。除了上面的基本形體外,可以建立如Cone,Pryamid, Cylinder等基本形體以備後用。 

    Android OpenGL ES 簡明開發教程_真正的3D圖形 
    本例示例程式碼顯示結果如下: 

    程式碼下載: 

    https://github.com/upperLucky/3DModelDemo 

    Android OpenGL ES 簡明開發教程_真正的3D圖形
  • 以上是Android OpenGL ES 簡明開發教程_真正的3D圖形的內容,更多 簡明 圖形 真正 Android 教程 OpenGL 開發 ES 的內容,請您使用右上方搜尋功能獲取相關資訊。