在 Andorid 平臺(tái)上構(gòu)造一個(gè) OpenGL View 非常簡(jiǎn)單,主要有兩方面的工作:
Android 平臺(tái)提供的 OpenGL ES API 主要定義在包android.opengl,javax.microedition.khronos.egl,javax.microedition.khronos.opengles,java.nio 等幾個(gè)包中,其中類 GLSurfaceView 為這些包中的核心類:
因此編寫 OpenGL ES 應(yīng)用的起始點(diǎn)是從類 GLSurfaceView 開始,設(shè)置 GLSurfaceView 只需調(diào)用一個(gè)方法來設(shè)置 OpenGLView 用到的 GLSurfaceView.Renderer.
public void setRenderer(GLSurfaceView.Renderer renderer)
GLSurfaceView.Renderer 定義了一個(gè)統(tǒng)一圖形繪制的接口,它定義了如下三個(gè)接口函數(shù):
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)
<-> 縱向互換時(shí)。此時(shí)可以重新設(shè)置繪制的縱橫比率。 有了上面的基本定義,可以寫出一個(gè) OpenGL ES 應(yīng)用的通用框架。
創(chuàng)建一個(gè)新的 Android 項(xiàng)目:OpenGLESTutorial, 在項(xiàng)目在添加兩個(gè)類 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.
}
}
編譯后運(yùn)行,屏幕顯示一個(gè)黑色的全屏。這兩個(gè)類定義了 Android OpenGL ES 應(yīng)用的最基本的類和方法,可以看作是 OpenGL ES 的”Hello ,world”應(yīng)用,后面將逐漸豐富這個(gè)例子來畫出 3D 圖型。
框架代碼下載:可以作為你自己的 OpenGL 3D 的初始代碼。