没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Android OpenGL ES 简明开发教程小结
1. Android OpenGL ES 简明开发教程一:概述
2. Android OpenGL ES 简明开发教程二:构造 OpenGL ES View
3. Android OpenGL ES 简明开发教程三:3D 绘图基本概念
4. Android OpenGL ES 简明开发教程四:3D 坐标变换
5. Android OpenGL ES 简明开发教程五:添加颜色
6. Android OpenGL ES 简明开发教程六: 真正的 3D 图形
7. Android OpenGL ES 简明开发教程七:材质渲染
Android OpenGL ES 简明开发教程一:概述
ApiDemos 的 Graphics 示例中含有 OpenGL ES 例子,OpenGL ES 主要用来
开发 3D 图形应用的。OpenGL ES (OpenGL for Embedded Systems) 是
OpenGL 三维图形 API 的子集,针对手机、PDA 和游戏主机等嵌入式设备而
设计。
下面是维基百科中对应 OpenGL ES 的简介:
OpenGL ES 是从 OpenGL 裁剪定制而来的,去除了 glBegin/glEnd,四边形
(GL_QUADS)、多边形(GL_POLYGONS)等复杂图元等许多非绝对必要的
特性。经过多年发展,现在主要有两个版本,OpenGL ES 1.x 针对固定管线硬
件的,OpenGL ES 2.x 针对可编程管线硬件。OpenGL ES 1.0 是以 OpenGL 1.3
规范为基础的,OpenGL ES 1.1 是以 OpenGL 1.5 规范为基础的,它们分别又
支持 common 和 common lite 两种 profile。lite profile 只支持定点实数,而
common profile 既支持定点数又支持浮点数。 OpenGL ES 2.0 则是参照
OpenGL 2.0 规范定义的,common profile 发布于 2005-8,引入了对可编程管
线的支持。
在解析 Android ApiDemos 中 OpenGL ES 示例前,有必要对 OpenGL ES 开发
单独做个简明开发教程,可以帮助从未接触过 3D 开发的程序员了解 OpenGL 的
开发的基本概念和方法,很多移动手机平台都提供了对 OpenGL ES 开发包的支
持,因此尽管这里使用 Android 平台介绍 OpenGL ES ,但基本概念和步骤同样适
用于其它平台。
简明开发教程主要参考 Jayway Team Blog 中 OpenGL ES 开发教程 , 这是一
个写的比较通俗易懂的开发教程,适合 OpenGL ES 初学者。
除了这个 OpenGL ES 简明开发教程外,以后将专门针对 OpenGL ES 写个由浅
入深的开发教程,尽请关注。
Android OpenGL ES 简明开发教程二:构造 OpenGL
ES View
在 Andorid 平台上构造一个 OpenGL View 非常简单,主要有两方面的工作:
GLSurfaceView
Android 平台提供的 OpenGL ES API 主要定义在包
android.opengl ,javax.microedition.khronos.egl ,javax.microedition.khronos.o
pengles ,java.nio 等几个包中,其中类 GLSurfaceView 为这些包中的核心类:
起到连接 OpenGL ES 与 Android 的 View 层次结构之间的桥梁作用。
使得 Open GL ES 库适应于 Anndroid 系统的 Activity 生命周期。
使得选择合适的 Frame buffer 像素格式变得容易。
创建和管理单独绘图线程以达到平滑动画效果。
提供了方便使用的调试工具来跟踪 OpenGL ES 函数调用以帮助检查错
误。
因此编写 OpenGL ES 应用的起始点是从类 GLSurfaceView 开始,设置
GLSurfaceView 只需调用一个方法来设置 OpenGLView 用到的
GLSurfaceView.Renderer.
帮助
1
public void setRenderer(GLSurfaceView.Renderer renderer)
GLSurfaceView.Renderer
GLSurfaceView.Renderer 定义了一个统一图形绘制的接口,它定义了如下三个
接口函数:
帮助
1
2
3
4
5
6
7
8
// 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 : 在这个方法中主要用来设置一些绘制时不常变化的
参数,比如:背景色,是否打开 z-buffer 等。
onDrawFrame: 定义实际的绘图操作。
onSurfaceChanged: 如果设备支持屏幕横向和纵向切换,这个方法将发
生在横向<->纵向互换时。此时可以重新设置绘制的纵横比率。
有了上面的基本定义,可以写出一个 OpenGL ES 应用的通用框架。
创建一个新的 Android 项目:OpenGLESTutorial, 在项目在添加两个类
TutorialPartI.java 和 OpenGLRenderer.java.
具体代码如下:
TutorialPartI.java
帮助
1
2
public class TutorialPartI extends Activity {
// Called when the activity is first created.
3
4
5
6
7
8
9
10
11
12
13
14
@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
帮助
1
2
3
4
5
6
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.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
剩余69页未读,继续阅读
嗨皮
- 粉丝: 121
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页