1. 程式人生 > >Android OpenGL ES 簡明開發教程二 構造OpenGL ES View

Android OpenGL ES 簡明開發教程二 構造OpenGL ES View

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

在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 的初始程式碼。


           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述