介紹GLSurfaceView

來源:互聯網
上載者:User
介紹GLSurfaceView

android.opengl.GLSurfaceView類讓你更容易地使用OpenGL ES渲染你的應用程式,其主要通過一下幾點:

1、提供粘合代碼把OpenGL ES串連到你的視圖系統

2、提供粘合代碼使得OpenGL ES按照Acticity(活動)的生命週期工作

3、使它容易選擇一款合適的架構緩衝區像素格式

4、建立和管理一個獨立的渲染線程,產生平滑的動畫

5、提供更容易使用的調試工具來跟蹤OpenGL ES 的API 並能找出錯誤。

GLSurfaceView是一個很好的基類對於構建一個使用OpenGL ES進行部分或全部渲染的應用程式。一個2D或3D的動作遊戲就是一個很好的例子,例如一個2D或3D的可視化應用如Google地圖。
 
以下是一個簡單的GLSurfaceView的應用:

一個最簡單的OpenGL ES應用代碼如下:

package  com.javaeye.googlers

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class ClearActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mGLView = new GLSurfaceView(this);

        mGLView.setRenderer(new ClearRenderer());

        setContentView(mGLView);

    }

    @Override
    protected void onPause() {

        super.onPause();

        mGLView.onPause();

    }

    @Override
    protected void onResume() {

        super.onResume();

        mGLView.onResume();

    }

    private GLSurfaceView mGLView;

}

class ClearRenderer implements GLSurfaceView.Renderer {

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        // Do nothing special.

    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {

        gl.glViewport(0, 0, w, h);

    }

    public void onDrawFrame(GL10 gl) {

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    }

}

 

這個程式並沒有做太多東西:它在每幀是清除螢幕到黑色。但是它是一個完整的OpenGL應用程式,正確地按照Activity(活動)的生命週期實現。當活動暫停渲染它也暫停渲染,活動恢複它也恢複。你可以把這個例子作為一個基本的互動的樣本程式。僅僅更多地調用了ClearRenderer.onDrawFrame() 方法。注意你甚至不需要子類化一個GLSurfaceView視圖。

 

GLSurfaceView.Renderer 有三個方法:

onSurfaceCreated() :在開始渲染的時候被調用,無論什麼時候OpenGL ES 渲染不得不重新被建立。(渲染是典型的丟失並重新建立當活動被暫停或恢複。)該方法一個建立長生命週期OpenGL資源(如材質)的好地方。

onSurfaceChanged():該方法在surface大小改變時被調用。這是設定你opengl視圖端的好地方。如果相機是固定的,不會圍著情境移動,你也可以在這裡設定你的相機。

onDrawFrame():每幀的時候該方法都會被調用,這個用於畫情境是可靠的。你完全可以通過調用glClear方法開清楚幀緩衝,接著通過其他的opengl ES來調用畫當前的情境。

 

使用者如何輸入?

假如你想做一個可以互動的程式(如遊戲),通常你會實現GLSurfaceView子類,因為這是很容易擷取使用者輸入事件。以下代碼是一個清晰的長例子展示給你怎樣做到這個:

 

package com.javaeye.googlers;

 

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;

 

import android.app.Activity;

import android.content.Context;

import android.opengl.GLSurfaceView;

import android.os.Bundle;

import android.view.MotionEvent;

 

public class ClearActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mGLView = new ClearGLSurfaceView(this);

        setContentView(mGLView);

    }

 

    @Override

    protected void onPause() {

        super.onPause();

        mGLView.onPause();

    }

 

    @Override

    protected void onResume() {

        super.onResume();

        mGLView.onResume();

    }

 

    private GLSurfaceView mGLView;

}

 

class ClearGLSurfaceView extends GLSurfaceView {

    public ClearGLSurfaceView(Context context) {

        super(context);

        mRenderer = new ClearRenderer();

        setRenderer(mRenderer);

    }

 

    public boolean onTouchEvent(final MotionEvent event) {

        queueEvent(new Runnable(){

            public void run() {

                mRenderer.setColor(event.getX() / getWidth(),

                        event.getY() / getHeight(), 1.0f);

            }});

            return true;

        }

 

        ClearRenderer mRenderer;

}

 

class ClearRenderer implements GLSurfaceView.Renderer {

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        // Do nothing special.

    }

 

    public void onSurfaceChanged(GL10 gl, int w, int h) {

        gl.glViewport(0, 0, w, h);

    }

 

    public void onDrawFrame(GL10 gl) {

        gl.glClearColor(mRed, mGreen, mBlue, 1.0f);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    }

 

    public void setColor(float r, float g, float b) {

        mRed = r;

        mGreen = g;

        mBlue = b;

    }

 

    private float mRed;

    private float mGreen;

    private float mBlue;

}

 

這個應用每幀都在清楚螢幕。當你點擊螢幕時,它清除顏色基於你觸屏時間的X、Y座標。注意在 ClearGLSurfaceView.onTouchEvent()中使用queueEvent()。queueEvent()方法被安全地用於在UI線程和渲染線程之間進行交流。如果你願意,你還可以使用一些其他的java線程間交流技術,例如Renderer 類本身的同步方法。然而,queueing 事件經常是一種用於處理線程間資訊交流的更簡單方式。

 

 

其他的GLSurfaceView樣本:

如果你厭煩了上面的樣本,你還可以從android的ApiDemo中找到更經典的樣本,所有的openGL ES樣本都是用GLSurfaceView視圖轉變的:

 

GLSurfaceView - 一個旋轉的三角形

Kube - 一個魔方例子

Translucent GLSurfaceView - 展示在一個透明的背景上顯示3d動畫

Textured Triangle - 顯示一個帶紋理的3D三角形

Sprite Text - 展示怎樣用材質畫出文字並混合進一個3d的情境中

Touch Rotate - 展示怎樣旋轉一個3D物體來響應使用者的輸入

聯繫我們

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