我是靠谱客的博主 丰富电脑,这篇文章主要介绍OpenGL ES Android 基础(一),现在分享给大家,希望可以做个参考。

一、OpenGL ES 的环境搭建
在Android中使用OpenGL ES 绘制图形,首先需要创建一个视图容器。实现这一点的最直接的方法之一是实现GLSurfaceViewGLSurfaceView.RendererGLSurfaceView是使用OpenGL绘制图形的视图容器。GLSurfaceView.Renderer控制在该视图中绘制的图形。
GLSurfaceView只是将OpenGL ES图形融入到应用程序中的一种方法。对于全屏或近全屏幕图形视图,这是一个合理的选择。想要在其布局的一小部分中集成OpenGL ES图形的开发人员应该看看TextureView。对于真正的,自己动手的开发人员,也可以使用SurfaceView构建OpenGL ES视图,但这需要编写相当多的附加代码。

复制代码
1
2
3
1、在Manifest清单文件中声明OpenGL ES 的使用。
复制代码
1
2
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
复制代码
1
2
2、为OponGL ES图形创建一个Activity
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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); } }
复制代码
1
2
3
3、构建GLSurfaceView对象 GLSurfaceView是一个专门绘制OpenGL ES图形的视图。它本身内容并不多。设置GLSurfaceView.Renderer来控制对象进行实际绘制。实际上,这个对象的代码很少,你可能会试图跳过扩展它,并创建一个未修改的GLSurfaceView实例,但不要这样做。您需要继承此类才能捕获“触摸事件响应”。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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); // Render the view only when there is a change in the drawing data setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } }

4、构建Renderer类
在使用OpenGL ES的应用程序中,GLSurfaceView.Renderer类或渲染器的实现在哪里开始变得有趣。该类控制与它相关联的GLSurfaceView中绘制的内容。在Android系统中,渲染器中有三种方法可以通过GLSurfaceView进行绘制:

复制代码
1
2
3
4
onSurfaceCreated() - 调用一次用于设置视图的OpenGL ES环境。 onDrawFrame() - 调用每次重绘的视图。 onSurfaceChanged() - 如果视图的几何变化,例如当设备的屏幕方向更改时调用。

这是OpenGL ES渲染器的一个非常基本的实现,它只是在GLSurfaceView中绘制黑色背景:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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); } }

最后

以上就是丰富电脑最近收集整理的关于OpenGL ES Android 基础(一)的全部内容,更多相关OpenGL内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部