Android OpenGL ES(十一)绘制一个20面体 .
生活随笔
收集整理的這篇文章主要介紹了
Android OpenGL ES(十一)绘制一个20面体 .
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前面介紹了OpenGL ES所有能夠繪制的基本圖形,點,線段和三角形。其它所有復雜的2D或3D圖形都是由這些基本圖形構成。
本例介紹如何使用三角形構造一個正20面體。一個正20面體,有12個頂點,20個面,30條邊構成:
創建一個DrawIcosahedron Activity, 定義20面體的12個頂點,和20個面如下:
?
static final float X=.525731112119133606f; static final float Z=.850650808352039932f;static float vertices[] = new float[]{-X, 0.0f, Z, X, 0.0f, Z, -X, 0.0f, -Z, X, 0.0f, -Z,0.0f, Z, X, 0.0f, Z, -X, 0.0f, -Z, X, 0.0f, -Z, -X,Z, X, 0.0f, -Z, X, 0.0f, Z, -X, 0.0f, -Z, -X, 0.0f};static short indices[] = new short[]{0,4,1, 0,9,4, 9,5,4, 4,5,8, 4,8,1,8,10,1, 8,3,10, 5,3,8, 5,2,3, 2,7,3,7,10,3, 7,6,10, 7,11,6, 11,0,6, 0,1,6,6,1,10, 9,0,11, 9,11,2, 9,2,5, 7,2,11 };
?
OpenGL ES缺省使用同一種顏色來繪制圖形,為了能夠更好的顯示3D效果,我們為每個頂點隨機定義一些顏色如下:
float[] colors = {0f, 0f, 0f, 1f,0f, 0f, 1f, 1f,0f, 1f, 0f, 1f,0f, 1f, 1f, 1f,1f, 0f, 0f, 1f,1f, 0f, 1f, 1f,1f, 1f, 0f, 1f,1f, 1f, 1f, 1f,1f, 0f, 0f, 1f,0f, 1f, 0f, 1f,0f, 0f, 1f, 1f,1f, 0f, 1f, 1f};
?
將頂點,面,顏色存放到Buffer中以提高性能:
private FloatBuffer vertexBuffer;private FloatBuffer colorBuffer;private ShortBuffer indexBuffer;ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); vertexBuffer = vbb.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0);ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); cbb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(colors); colorBuffer.position(0);ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); ibb.order(ByteOrder.nativeOrder()); indexBuffer = ibb.asShortBuffer(); indexBuffer.put(indices); indexBuffer.position(0);
?
看一看DrawScene代碼:
?
public void DrawScene(GL10 gl) {super.DrawScene(gl);gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);gl.glLoadIdentity();gl.glTranslatef(0, 0, -4);gl.glRotatef(angle, 0, 1, 0);gl.glFrontFace(GL10.GL_CCW);gl.glEnable(GL10.GL_CULL_FACE);gl.glCullFace(GL10.GL_BACK);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);gl.glEnableClientState(GL10.GL_COLOR_ARRAY);gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,GL10.GL_UNSIGNED_SHORT, indexBuffer);gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisable(GL10.GL_CULL_FACE);angle++;}
?
用來繪制20面體使用了gl.glDrawElements:
public abstract void glDrawElements(int mode, int count, int type, Buffer indices) ,可以重新定義頂點的順序,頂點的順序由indices Buffer 指定。
本例會顯示一個繞Y軸不斷旋轉的20面體,如何旋轉模型將在后面的坐標系統和坐標變換中介紹.
完整代碼:
MainActiviy.java
package com.example.gltest;import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer;import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.Window; import android.view.WindowManager;public class MainActivity extends Activityimplements IOpenGLDemo{private FloatBuffer vertexBuffer;private FloatBuffer colorBuffer;private ShortBuffer indexBuffer;private GLSurfaceView mGLSurfaceView;static final float X=.525731112119133606f;static final float Z=.850650808352039932f;static float vertices[] = new float[]{-X, 0.0f, Z, X, 0.0f, Z, -X, 0.0f, -Z, X, 0.0f, -Z,0.0f, Z, X, 0.0f, Z, -X, 0.0f, -Z, X, 0.0f, -Z, -X,Z, X, 0.0f, -Z, X, 0.0f, Z, -X, 0.0f, -Z, -X, 0.0f};static short indices[] = new short[]{0,4,1, 0,9,4, 9,5,4, 4,5,8, 4,8,1,8,10,1, 8,3,10, 5,3,8, 5,2,3, 2,7,3,7,10,3, 7,6,10, 7,11,6, 11,0,6, 0,1,6,6,1,10, 9,0,11, 9,11,2, 9,2,5, 7,2,11 };float[] colors = {0f, 0f, 0f, 1f,0f, 0f, 1f, 1f,0f, 1f, 0f, 1f,0f, 1f, 1f, 1f,1f, 0f, 0f, 1f,1f, 0f, 1f, 1f,1f, 1f, 0f, 1f,1f, 1f, 1f, 1f,1f, 0f, 0f, 1f,0f, 1f, 0f, 1f,0f, 0f, 1f, 1f,1f, 0f, 1f, 1f};/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);vbb.order(ByteOrder.nativeOrder());vertexBuffer = vbb.asFloatBuffer();vertexBuffer.put(vertices);vertexBuffer.position(0);ByteBuffer cbb= ByteBuffer.allocateDirect(colors.length * 4);cbb.order(ByteOrder.nativeOrder());colorBuffer = cbb.asFloatBuffer();colorBuffer.put(colors);colorBuffer.position(0);ByteBuffer ibb= ByteBuffer.allocateDirect(indices.length * 2);ibb.order(ByteOrder.nativeOrder());indexBuffer = ibb.asShortBuffer();indexBuffer.put(indices);indexBuffer.position(0);this.requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);mGLSurfaceView = new GLSurfaceView(this);mGLSurfaceView.setRenderer(new OpenGLRenderer(this));setContentView(mGLSurfaceView);}// public void DrawScene(GL10 gl) { // gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f); // // Clears the screen and depth buffer. // gl.glClear(GL10.GL_COLOR_BUFFER_BIT // | GL10.GL_DEPTH_BUFFER_BIT); // // }public void DrawScene(GL10 gl) {// super.DrawScene(gl); gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);gl.glLoadIdentity();gl.glTranslatef(0, 0, -4);float angle=0;gl.glRotatef(angle, 0, 1, 0);gl.glFrontFace(GL10.GL_CCW);gl.glEnable(GL10.GL_CULL_FACE);gl.glCullFace(GL10.GL_BACK);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);gl.glEnableClientState(GL10.GL_COLOR_ARRAY);gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,GL10.GL_UNSIGNED_SHORT, indexBuffer);gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisable(GL10.GL_CULL_FACE);angle++;}@Overrideprotected void onResume() {// Ideally a game should implement// onResume() and onPause()// to take appropriate action when the//activity looses focus super.onResume();mGLSurfaceView.onResume();}@Overrideprotected void onPause() {// Ideally a game should implement onResume()//and onPause()// to take appropriate action when the//activity looses focus super.onPause();mGLSurfaceView.onPause();}}
其他類似。
?
?
?
?
轉載于:https://www.cnblogs.com/Anita9002/p/4452612.html
總結
以上是生活随笔為你收集整理的Android OpenGL ES(十一)绘制一个20面体 .的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 妇科检查要多少钱啊?
- 下一篇: “凭莺为向杨花道”下一句是什么