Android OpenGL ES 簡明開發教程 02

來源:互聯網
上載者:User

在Andorid平台上構造一個OpenGL View非常簡單,主要有兩方面的工作:

GLSurfaceView

Android平台提供的OpenGL ES API主要定義在包android.opengl ,javax.microedition.khronos.egl ,javax.microedition.khronos.opengles ,java.nio 等幾個包中,其中類GLSurfaceView 為這些包中的核心類:

  • 起到串連OpenGL ES與Android 的View階層之間的橋樑作用。
  • 使得Open GL ES庫適應於Anndroid系統的Activity生命週期。
  • 使得選擇合適的Frame buffer像素格式變得容易。
  • 建立和管理單獨繪圖線程以達到平滑動畫效果。
  • 提供了方便使用的調試工具來跟蹤OpenGL ES函數調用以協助檢查錯誤。

因此編寫OpenGL ES應用的起始點是從類GLSurfaceView開始,設定GLSurfaceView只需調用一個方法來設定OpenGLView用到的GLSurfaceView.Renderer.

public void  setRenderer(GLSurfaceView.Renderer renderer)

GLSurfaceView.Renderer

GLSurfaceView.Renderer定義了一個統一圖形繪製的介面,它定義了如下三個介面函數:

// Called when the surface is created or recreated.public void onSurfaceCreated(GL10 gl, EGLConfig config)// Called to draw the current frame.public void onDrawFrame(GL10 gl)// Called when the surface changed size.public void onSurfaceChanged(GL10 gl, int width, int height)
  • onSurfaceCreated : 在這個方法中主要用來設定一些繪製時不常變化的參數,比如:背景色,是否開啟 z-buffer等。
  • onDrawFrame:  定義實際的繪圖操作。
  • onSurfaceChanged: 如果裝置支援螢幕橫向和縱向切換,這個方法將發生在橫向<->縱向互換時。此時可以重新設定繪製的縱橫比率。

有了上面的基本定義,可以寫出一個OpenGL ES應用的通用架構。

建立一個新的Android 項目:OpenGLESTutorial, 在項目在添加兩個類TutorialPartI.java 和OpenGLRenderer.java.

具體代碼如下:

TutorialPartI.java

public class TutorialPartI extends Activity { // Called when the activity is first created.  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW) getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW) GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new OpenGLRenderer()); setContentView(view); }}

OpenGLRenderer.java

public class OpenGLRenderer implements Renderer { public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Set the background color to black ( rgba ). gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs. // Enable Smooth Shading, default not really needed. gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs. // Depth buffer setup. gl.glClearDepthf(1.0f);// OpenGL docs. // Enables depth testing. gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs. // The type of depth testing to do. gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs. // Really nice perspective calculations. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs. GL10.GL_NICEST); } public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs. GL10.GL_DEPTH_BUFFER_BIT); } public void onSurfaceChanged(GL10 gl, int width, int height) { // Sets the current view port to the new size. gl.glViewport(0, 0, width, height);// OpenGL docs. // Select the projection matrix gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs. // Reset the projection matrix gl.glLoadIdentity();// OpenGL docs. // Calculate the aspect ratio of the window GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f); // Select the modelview matrix gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs. // Reset the modelview matrix gl.glLoadIdentity();// OpenGL docs. }}

編譯後運行,螢幕顯示一個黑色的全屏。這兩個類定義了Android OpenGL ES應用的最基本的類和方法,可以看作是OpenGL ES的”Hello ,world”應用,後面將逐漸豐富這個例子來畫出3D圖型。

架構代碼下載:可以作為你自己的OpenGL
3D 的初始代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.