適用於Android的OpenGL ES教程 - 第一部分 - 設定檢視
2009年12月3日 由每埃裡克·伯格曼 在安卓,嵌入式,Java的 | 90評論
我已經在OpenGL ES 2.0 for android上開始了一個新的更新系列教程。請檢視:OpenGL ES 2.0
我將在Android手機上編寫一些關於使用OpenGL ES的教程。OpenGL ES的理論在不同的裝置上是相同的,因此將它們轉換到另一個平臺應該很容易。
我不能總是記得我找到特定資訊的位置,所以我可能無法總是給你正確的參考。如果您覺得我從您那裡借來的東西但是忘了加你作為參考,請發電子郵件給我。
在程式碼示例中,我將為每個函式提供兩個不同的連結。實際的功能將連結到android文件,之後我還將連結OpenGL文件。像這樣:
1 |
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); |
那麼,讓我們開始吧。
在本教程中,我將向您展示如何設定OpenGL ES檢視,這始終是一個很好的起點。
設定OpenGL ES檢視
設定OpenGL檢視從未如此艱難,在Android上它仍然很容易。開始時只需要兩件事。
GLSurfaceView
GLSurfaceView是Android 1.5中的API類,可幫助您編寫OpenGL ES應用程式。
- 提供粘合程式碼以將OpenGL ES連線到View系統。
- 提供粘合程式碼,使OpenGL ES與Activity生命週期一起工作。
- 可以輕鬆選擇合適的幀緩衝畫素格式。
- 建立和管理單獨的渲染執行緒以啟用平滑動畫。
- 提供易於使用的除錯工具,用於跟蹤OpenGL ES API呼叫和檢查錯誤。
如果您想快速使用OpenGL ES應用程式,那麼您應該從這裡開始。
您需要呼叫的唯一功能是:
1 |
public void setRenderer(GLSurfaceView.Renderer renderer) |
閱讀更多:GLSurfaceView
GLSurfaceView.Renderer
GLSurfaceView.Renderer是一個通用的渲染介面。在您對此渲染器的實現中,您應該將所有呼叫放在渲染幀中。
有三個功能要實現:
1 2 3 4 5 6 7 8 |
// 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緩衝區等等。
onDrawFrame
這是實際繪圖的地方。
onSurfaceChanged
如果您的裝置支援在橫向和縱向之間翻轉,則會在發生此功能時呼叫此功能。你在這裡做的是設定新比率。
閱讀更多:GLSurfaceView.Renderer
把它放在一起
首先,我們建立我們的活動,我們保持乾淨和簡單。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package se.jayway.opengl.tutorial;
import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle;
public class TutorialPartI extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new OpenGLRenderer()); setContentView(view); } } |
我們的渲染器需要更多的工作來設定,看看它,我會更多地解釋程式碼。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package se.jayway.opengl.tutorial;
import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer;
public class OpenGLRenderer implements Renderer { /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax. * microedition.khronos.opengles.GL10, javax.microedition.khronos. * egl.EGLConfig) */ public void onSurfaceCreated(GL10 gl, EGLConfig config) { // 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) */ public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // GL10.GL_DEPTH_BUFFER_BIT); }
/* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax. * microedition.khronos.opengles.GL10, int, int) */ 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 matrix gl.glMatrixMode(GL10.GL_PROJECTION); // Reset the projection matrix gl.glLoadIdentity(); // 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); // Reset the modelview matrix gl.glLoadIdentity(); } } |
全屏
只需在OpenGLDemo類中新增這些行,您就會獲得全屏。
1 2 3 4 5 6 7 |
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) ... // Previous code. } |
這幾乎是您啟動和執行檢視所需的全部內容。如果你編譯並執行它,你會看到一個漂亮的黑屏。