(一)建立OpenGL ES 环境(Building an OpenGL ES Environment)

原文链接:https://developer.android.com/training/graphics/opengl/environment.html
示例代码

In order to draw graphics with OpenGL ES in your Android application, you must create a view container for them. One of the more straight-forward ways to do this is to implement both a GLSurfaceView and a GLSurfaceView.Renderer. A GLSurfaceView is a view container for graphics drawn with OpenGL and GLSurfaceView.Renderer controls what is drawn within that view. For more information about these classes, see the OpenGL ES developer guide.
为了在Android应用中使用OpenGL ES进行绘图,你必须建立一个view容器。最直接的一个方法就是直接实现GLSurfaceView和GLSurfaceView.Renderer这两个类。GLSurfaceView是一个OpenGL画图所使用的view容器,而GLSurfaceView.Renderer是控制在这个view里面画什么。你可以查看OpenGL ES 开发指导获取更多关于这些类的信息。

GLSurfaceView is just one way to incorporate OpenGL ES graphics into your application. For a full-screen or near-full screen graphics view, it is a reasonable choice. Developers who want to incorporate OpenGL ES graphics in a small portion of their layouts should take a look at TextureView. For real, do-it-yourself developers, it is also possible to build up an OpenGL ES view using SurfaceView, but this requires writing quite a bit of additional code.
在你的应用中使用OpenGL ES画图,GLSurfaceView只是其中的一种方式。绘制一个全屏或近乎全屏的图像,采用GLSurfaceView是合理的选择。如果开发者想使用OpenGL ES在一小部分布局中绘制图像,那么应该考虑TextureView。其实,你可以在SurfaceView的基础上手动实现一个OpenGL ES view, 但是这需要写许多额外的代码。

This lesson explains how to complete a minimal implementation of GLSurfaceView and GLSurfaceView.Renderer in a simple application activity.
这一课主要是讲在一个简单的activity里面,以小量的代码实现GLSurfaceView and GLSurfaceView这两个类。

Declare OpenGL ES Use in the Manifest

在AndroidManifest.xml中声明OpenGL ES的使用

In order for your application to use the OpenGL ES 2.0 API, you must add the following declaration to your manifest:
为了在你的应用中使用OpenGL ES 2.0 API,你必须把这个声明加到你的AndroidManifest.xml.

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

If your application uses texture compression, you must also declare which compression formats your app supports, so that it is only installed on compatible devices.
为你的应用中使用了texture压缩,你必须声明你的应用支持的压缩格式,以便于你的应用只在支持这些格式的手机上才能安装。

<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />

For more information about texture compression formats, see the OpenGL developer guide.
查看OpenGL开发文档,获取更多与texture压缩格式相关的信息。

Create an Activity for OpenGL ES Graphics

建立一个Activity

Android applications that use OpenGL ES have activities just like any other application that has a user interface. The main difference from other applications is what you put in the layout for your activity. While in many applications you might use TextView, Button and ListView, in an app that uses OpenGL ES, you can also add a GLSurfaceView.
一个使用OpenGL ES的应用和其他应用一样有activities和用户界面。与其他应用的主要不同是你在布局里的使用的控件。在其他的(非OpenGL ES)应用的布局里,你可能会使用TextView, Button and ListView。在一个使用OpenGL ES的应用的布局中,你还可以使用GLSurfaceView。

The following code example shows a minimal implementation of an activity that uses a GLSurfaceView as its primary view:
接下的示例代码展示了用少量必要的代码实现在一个activity里面使用GLSurfaceView作为主要的view。

public class OpenGLES20Activity extends Activity {

    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }
}
  • Note: OpenGL ES 2.0 requires Android 2.2 (API Level 8) or higher, so make sure your Android project targets that API or higher.

  • 注意:OpenGL ES 2.0 需要Android 2.2(API Level 8)及以上,所以确保你的应用minSdkVersion在8 及以上。

Build a GLSurfaceView Object

创建一个GLSurfaceView对象

A GLSurfaceView is a specialized view where you can draw OpenGL ES graphics. It does not do much by itself. The actual drawing of objects is controlled in the GLSurfaceView.Renderer that you set on this view. In fact, the code for this object is so thin, you may be tempted to skip extending it and just create an unmodified GLSurfaceView instance, but don’t do that. You need to extend this class in order to capture touch events, which is covered in the Responding to Touch Events lesson.
GLSurfaceView是一个专门用来绘制OpenGL ES图画的view. 这个类本身并不做太多绘制相关的工作。你为GLSurfaceView设置的GLSurfaceView.Renderer这个类才是实际上负责绘制物体的类。实际上,与GLSurfaceView相关的代码很少,以至于你不想继承这个类,只想创建一个没有任何修改的GLSurfaceView实例,但是不要这么做!如果你想截获点击事件(在点击事件响应的课程里讲过),你需要继承(extend) GLSurfaceView这个类。

The essential code for a GLSurfaceView is minimal, so for a quick implementation, it is common to just create an inner class in the activity that uses it:
实现继承GLSurfaceView的代码量很少,快速的实现方式是在使用GLSurfaceView的activity里面建立一个继承GLSurfaceView的内部类。

class MyGLSurfaceView extends GLSurfaceView {

    private final MyGLRenderer 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);
    }
}

One other optional addition to your GLSurfaceView implementation is to set the render mode to only draw the view when there is a change to your drawing data using the GLSurfaceView.RENDERMODE_WHEN_DIRTY setting:
另外,你可以设render模式为GLSurfaceView.RENDERMODE_WHEN_DIRTY ,只有绘制数据有改变时才绘制这个view.

// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

This setting prevents the GLSurfaceView frame from being redrawn until you call requestRender(), which is more efficient for this sample app.
这种设置可以避免GLSurfaceView自动重新绘制帧(frame), 除非你调用方法requestRender(), 对于这个示例应用来说,这样更加高效。

Build a Renderer Class

创建一个Renderer类

The implementation of the GLSurfaceView.Renderer class, or renderer, within an application that uses OpenGL ES is where things start to get interesting. This class controls what gets drawn on the GLSurfaceView with which it is associated. There are three methods in a renderer that are called by the Android system in order to figure out what and how to draw on a GLSurfaceView:

让一个使用OpenGL ES的应用变得有趣地方在于GLSurfaceView.Renderer类的实现。GLSurfaceView.Renderer这个类控制在与它相关的GLSurfaceView上绘制什么内容。为了弄清楚需要绘制什么以及怎么样绘制,android系统会调用GLSurfaceView.Renderer类的三个方法。

  • onSurfaceCreated() - Called once to set up the view's OpenGL ES environment.
    onSurfaceCreated() - 在设置view的OpenGL ES 环境时会调用一次。

  • onDrawFrame() - Called for each redraw of the view.
    onDrawFrame() - 每次重新绘制时会调用到。

  • onSurfaceChanged() - Called if the geometry of the view changes, for example when the device's screen orientation changes.
    onSurfaceChanged() - 在view的形状改变时会被调用,比如:手机屏幕旋转后。

Here is a very basic implementation of an OpenGL ES renderer, that does nothing more than draw a black background in the GLSurfaceView:
这里有一个基本的 OpenGL ES renderer实现,作用是只在GLSurfaceView里画一个黑色的背景。

public class MyGLRenderer 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);
    }
}

That’s all there is to it! The code examples above create a simple Android application that displays a black screen using OpenGL. While this code does not do anything very interesting, by creating these classes, you have laid the foundation you need to start drawing graphic elements with OpenGL.
结束!上面这些示例代码创建了一个简单的应用,在这个应用里面,采用OpenGL显示了一个画色的背景。虽然,这些代码没实现什么有意思的事情,但是,通过实现这些类,你打下了进一步使用OpenGL绘制图画的基础。

  • Note: You may wonder why these methods have a GL10 parameter, when you are using the OpengGL ES 2.0 APIs. These method signatures are simply reused for the 2.0 APIs to keep the Android framework code simpler.
    注意:你可能会奇怪为什么在使用OpengGL ES 2.0 API时会有一些GL10的参数。为了使Android 框架的代码更简洁,在OpengGL ES 2.0中重用了1.0中的方法签名。

If you are familiar with the OpenGL ES APIs, you should now be able to set up a OpenGL ES environment in your app and start drawing graphics. However, if you need a bit more help getting started with OpenGL, head on to the next lessons for a few more hints.
如果你熟悉 OpenGL ES API,现在你就可以在你的应用中设置一个 OpenGL ES环境,开始绘图了。如果你需要更多有关OpenGL入门的帮助,可以看下一课

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容