Android OpenGL/NeHe 教材的第一課
Android簡介使用方法開發工具開始寫程式視覺化介面視覺元件對話元件核心物件事件處理資料儲存查詢功能影音功能繪圖功能網路功能衛星地圖特殊功能資源管理裝置管理系統核心問題與回答刷機升級常用軟體Eclipse教學錄影訊息相關網站參考文獻最新修改簡體版English |
執行結果程式原始碼// 本範例修改自 Savas Ziplies 的 nehe-android-ports // 來源:http://insanitydesign.com/wp/projects/nehe-android-ports/ package ccc.android; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; public class GL1 extends Activity { private GLSurfaceView glSurface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); glSurface = new GLSurfaceView(this); glSurface.setRenderer(new GLRenderer()); setContentView(glSurface); } @Override protected void onResume() { super.onResume(); glSurface.onResume(); } @Override protected void onPause() { super.onPause(); glSurface.onPause(); } public class GLRenderer implements Renderer { private Triangle triangle; private Square square; public GLRenderer() { triangle = new Triangle(); square = new Square(); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background gl.glClearDepthf(1.0f); //Depth Buffer Setup gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do //Really Nice Perspective Calculations gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); } public void onDrawFrame(GL10 gl) { //Clear Screen And Depth Buffer gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); //Reset The Current Modelview Matrix gl.glTranslatef(0.0f, -1.2f, -6.0f); //Move down 1.2 Unit And Into The Screen 6.0 square.draw(gl); //Draw the square gl.glTranslatef(0.0f, 2.5f, 0.0f); //Move up 2.5 Units triangle.draw(gl); //Draw the triangle } /** * If the surface changes, reset the view */ public void onSurfaceChanged(GL10 gl, int width, int height) { if(height == 0) { //Prevent A Divide By Zero By height = 1; //Making Height Equal One } gl.glViewport(0, 0, width, height); //Reset The Current Viewport gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix gl.glLoadIdentity(); //Reset The Projection Matrix //Calculate The Aspect Ratio Of The Window GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix gl.glLoadIdentity(); //Reset The Modelview Matrix } } public class Triangle { private FloatBuffer vertexBuffer; private float vertices[] = { 0.0f, 1.0f, 0.0f, //Top -1.0f, -1.0f, 0.0f, //Bottom Left 1.0f, -1.0f, 0.0f //Bottom Right }; public Triangle() { ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); byteBuf.order(ByteOrder.nativeOrder()); vertexBuffer = byteBuf.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); } public void draw(GL10 gl) { //Set the face rotation gl.glFrontFace(GL10.GL_CW); //Point to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); //Enable vertex buffer gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //Draw the vertices as triangle strip gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3); //Disable the client state before leaving gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } } public class Square { private FloatBuffer vertexBuffer; private float vertices[] = { -1.0f, -1.0f, 0.0f, //Bottom Left 1.0f, -1.0f, 0.0f, //Bottom Right -1.0f, 1.0f, 0.0f, //Top Left 1.0f, 1.0f, 0.0f //Top Right }; public Square() { // ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); byteBuf.order(ByteOrder.nativeOrder()); vertexBuffer = byteBuf.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); } public void draw(GL10 gl) { //Set the face rotation gl.glFrontFace(GL10.GL_CW); //Point to our vertex buffer gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); //Enable vertex buffer gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //Draw the vertices as triangle strip gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3); //Disable the client state before leaving gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } } } |
page revision: 2, last edited: 10 Jan 2011 08:26
Post preview:
Close preview