在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 構(gòu)造
3D 繪圖基本概念
概述
真正的 3D 圖形
添加顏色
3D 坐標(biāo)變換
構(gòu)造
材質(zhì)渲染

構(gòu)造

在 Andorid 平臺(tái)上構(gòu)造一個(gè) OpenGL View 非常簡(jiǎn)單,主要有兩方面的工作:

GLSurfaceView

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

  • 起到連接 OpenGL ES與Android 的 View 層次結(jié)構(gòu)之間的橋梁作用。
  • 使得 Open GL ES 庫(kù)適應(yīng)于 Anndroid 系統(tǒng)的 Activity 生命周期。
  • 使得選擇合適的 Frame buffer 像素格式變得容易。
  • 創(chuàng)建和管理單獨(dú)繪圖線程以達(dá)到平滑動(dòng)畫效果。
  • 提供了方便使用的調(diào)試工具來跟蹤 OpenGL ES 函數(shù)調(diào)用以幫助檢查錯(cuò)誤。

因此編寫 OpenGL ES 應(yīng)用的起始點(diǎn)是從類 GLSurfaceView 開始,設(shè)置 GLSurfaceView 只需調(diào)用一個(gè)方法來設(shè)置 OpenGLView 用到的 GLSurfaceView.Renderer.

public void  setRenderer(GLSurfaceView.Renderer renderer)  

GLSurfaceView.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)  
  • onSurfaceCreated:在這個(gè)方法中主要用來設(shè)置一些繪制時(shí)不常變化的參數(shù),比如:背景色,是否打開 z-buffer 等。
  • onDrawFrame:定義實(shí)際的繪圖操作。
  • onSurfaceChanged:如果設(shè)備支持屏幕橫向和縱向切換,這個(gè)方法將發(fā)生在橫向 <-> 縱向互換時(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 的初始代碼。