Android OpenGL ES: 第一個程式

來源:互聯網
上載者:User

標籤:android   opengl es   三維   圖形   

1. 概要

OpenGL是案頭環境下的繪製,渲染三維圖形的API。

OpenGL ES是在Android環境下的OpenGL。

在Android中OpenGL需要在GLSurfaceView中渲染,渲染控制函數在GLSurfaceView.Renderer中。接下來會介紹如何建立第一個OpenGL程式


2. 配置AndroidManifest

(1)聲明OpenGL ES API
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
(2)如果在APP中用到紋理,需要聲明採用紋理的格式
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />


<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.android.firstglsurfaceview">    <uses-feature android:glEsVersion="0x00020000" />    <uses-sdk android:targetSdkVersion="11"/>    <application            android:label="@string/app_name">        <activity android:name="FirstGLActivity"                android:theme="@android:style/Theme.NoTitleBar.Fullscreen"                android:launchMode="singleTask"                android:configChanges="orientation|keyboardHidden">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

3. 建立一個OpenGL Project3.1 建立Activity

只需要在Activity中設定一個GLSurfaceView即可。

FirstGLSurfaceView繼承於GLSurfaceView。

public class FirstGLActivity extends Activity {    private FirstGLSurfaceView mGLView;    @Override    protected void onCreate(Bundle icicle) {        super.onCreate(icicle);        mGLView = new FirstGLSurfaceView(getApplication());        setContentView(mGLView);    }    @Override    protected void onPause() {        super.onPause();        mGLView.onPause();    }    @Override    protected void onResume() {        super.onResume();        mGLView.onResume();    }}

3.2 建立GLSurfaceView

此部分需要聲明OpenGL版本,設定Renderer

class FirstGLSurfaceView extends GLSurfaceView {    private final FirstGLRenderer mRenderer;    public MyGLSurfaceView(Context context){        super(context);        // Create an OpenGL ES 2.0 context        setEGLContextClientVersion(2);        mRenderer = new MyGLRenderer();        // Set the Renderer for drawing on the GLSurfaceView        setRenderer(mRenderer);    }}

3.3 建立Render

GLSurfaceView.Renderer是OpenGL ES最重要的一部分,設定OpenGL環境,繪製OpenGL都是在Renderer中完成。

Render實現了GLSurfaceView.Renderer介面,需要實現3個函數:
onSurfaceCreated() - Surface建立時會觸發此事件,在此事件中需要完成OpenGL的初始化工作。
onDrawFrame() - 更新OpenGL繪製的內容
onSurfaceChanged() - 當Surface大小改變時觸發此事件,比如方向改變等。

如果您瞭解案頭環境下OpenGL編程,那麼我們可以將其與GLUT建立的OpenGL程式對應起來(參見第一個OpenGL程式)。

onSurfaceCreated  --- init

onDrawFrame  --- display


?

public class FirstGLRenderer implements GLSurfaceView.Renderer {    public void onSurfaceCreated(GL10 unused, EGLConfig config) {        // Set the background frame color        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);    }    public void onDrawFrame(GL10 unused) {        // Redraw background color        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);    }    public void onSurfaceChanged(GL10 unused, int width, int height) {        GLES20.glViewport(0, 0, width, height);    }}



4. 參考文獻

[1] Google Android API, http://developer.android.com/training/graphics/opengl/environment.html

Android OpenGL ES: 第一個程式

聯繫我們

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