关于OpenGL ES
Open Graphic Library(OpenGL),它的作用是将2D、3D数据形象化。它是一个多用途的,开放标准的图形库。这个库支持的应用有:2D、3D数字内容创作;机械及建筑设计;虚拟原型;飞行模拟;电视游戏等。你可以使用它去配置一个3D图形管道并向它提交数据。顶点将被转换、光线将组装成原始的,光栅化的用以创建一幅2D图像。OpenGL是为将函数调用转换成底层图形硬件可以识别的命令而专门设计的。由于这些硬件专门用于处理图形命令,OpenGL绘图通常很快。
OpenGL for Embedded System(OpenGL ES)是OpenGL的一个简化版本,为了保证学习和使用的简易性,它去除了一些冗余的功能。
初步印象
OpenGL给予一个App驾驭底层图形处理器的能力。iOS设备的GPU能够执行精确的2D、3D绘图,复杂的阴影计算到每一个像素以生成最终的图像。当你的App设计需求里需要最直接而全面的去访问GPU硬件资源时,OpenGL ES是你应该使用的。典型的使用OpenGL的地方包括电视游戏、模拟3D图像的呈现。
OpenGL ES是一组低级的、硬件为主的API。尽管它提供了最强大的、最灵活的图形处理工具,但它也拥有陡峭的学习曲线,同时对你App的综合设计有比较大的影响。对于那些从事专门研究而需要很高的图形性能的app,iOS提供了若干高级框架。
Sprite Kit Framework:提供了一个专门为创造2D游戏而优化的硬件加速动画系统。(See SpriteKit Programming Guide)
Core Image Framework:提供实时滤镜、分析、视频图像。(See Core Image Programming Guide)
核心动画为所有的iOS App提供硬件加速图形渲染和动画基础设施,配合一个简单的说明程序模型使实现复杂的用户界面动画变得十分容易。(See Core Animation Programming Guide)
通过使用UIKit Framework的一些功能,你可以添加动画、物理特效、或者其他的一些特别的效果到Cocoa Touch用户界面。
在iOS系统下OpenGL ES是一组平台无关的API
由于OpenGL ES是一组基于C的API,它具有很强的可移植性和被广泛支持。作为C API,它与Objective-C Cocoa Touch apps无缝融合。OpenGL ES规格并没有定义一个窗口层。取而代之的是,主机操作系统必须提供一个创建OpenGL ES渲染上下文(rendering context)的函数,用来接受命令。还有帧缓冲(framebuffer),任何绘图命令的结果都将写入到它当中。在iOS系统下使用OpenGL ES需要使用iOS的类来创建和呈现绘图表面,然后使用平台无关的API去渲染它的内容。
相关章节:CheckList for Building OpenGL ES Apps for iOS, Configuring OpenGL ES Context
GLKit提供绘图表面以及动画支持
Views and View Controllers, define by the UIKit framework,控制iOS视觉内容的呈现。GLKit framework提供这些类的OpenGL ES-aware版本。当你开发一个OpenGL ES的App时,你使用GLKView对象去渲染你的OpenGL ES内容。你可以使用GLKViewController去管理你的view和支持内容动画(support animating its content)
相关章节:Drawing With OpenGL ES and GLKit
iOS支持多种渲染目标
除了绘制内容填充整个屏幕或者视图层次的一部分,你也可以把OpenGL ES帧缓冲(framebuffer)对象用作其他的渲染策略。iOS实现标准的OpenGL ES帧缓冲(framebuffer)对象,在一个OpenGL ES场景中,它可以用来渲染到一个屏幕外的缓冲(offscreen buffer)或者到一个其他地方需要用到的纹理上。除此之外,iOS下的OpenGL ES支持渲染到一个核心动画层(Core Animation Layer)(the CAEAGLLayer class),这个层可以与其他层联合起来创建你的App的用户界面或其他的视觉内容。
相关章节:Drawing to Other Rendering Destinations
App需要额外的性能调整
图形处理器是为图形操作而优化的并行设备,为了让你的App获得更好的性能,你必须仔细的设计你的App去向OpenGL ES提供数据以便图形硬件和你的App能够并行运行。一个缺乏优化的App总是使CPU和GPU等待对方程序命令执行完毕。
你应该设计你的App能够有效使用OpenGL ES的API,当你完成编译你的App时,使用Instruments去优化你的App的性能,如果OpenGL ES成为瓶颈,利用这个指南所提供的信息来优化你的App的性能。
Xcode提供一系列可以帮助你提高你的OpenGL ES App性能的工具。
相关章节:OpenGL ES Design Guidelines, Best Practices for Working with Vertex Data, Best Practices for Working with Texture Data, Best Practices for Shaders, Tuning Your OpenGL ES App
OpenGL ES不可以用于后台App
那些在后台运行的App不可以调用OpenGL ES函数,当你的App在后台的时候访问图形处理器,它将自动被iOS终止。为了防止这个,你的App应该在进入后台之前清空掉所有之前提交给OpenGL ES的挂起的命令,并且直到回到前台,都不要调用OpenGL ES。
相关章节:Multitasking, High Resolution, and Other iOS Features
OpenGL ES在多线程App中设置了其他的限制
利用并发性设计App对提高App表现有很大的帮助。如果你打算添加并发到OpenGL ES App中,你必须保证它不会在两个不同的线程中同时访问同一个上下文(context)。
相关章节:Concurrency and OpenGL ES
如何使用这份文档
先读这三篇文章:Checklist for Building OpenGL ES Apps for iOS, Configuring OpenGL ES Contexts, Drawing with OpenGL ES and GLKit.这三章提供了OpenGL ES 是如何整合到iOS中的一个概述,并提供了创建你的第一个OpenGL ES App以及在iOS上运行所需要的所有细节。
如果你熟悉iOS下OpenGL ES的使用,读Drawing to Other Rendering Destinations和Multitasking, High Resolution, and Other iOS Features,他们是与平台相关的重要的指南。开发者熟悉iOS 5.0以前的OpenGL ES的使用应该读Drawing with OpenGL ES and GLKit获取OpenGL ES流式开发新功能的细节。
最后,请读:OpenGL ES Design Guidelines, Tuning Your OpenGL ES App。阅读随后的章节去掌握更深层次的方法设计高效率的OpenGL ES应用。
除非有额外的说明,这本书的OpenGL ES示例代码是以OpenGL ES 3.0为基础。如果你使用其他的OpenGL ES版本,你在使用示例代码时可能需要做些更改
前提条件
在尝试使用OpenGL ES之前,你应该熟悉通常的iOS应用的结构。阅读Start Developing iOS App Today。
这份文档不是一份完整的教程或者是多平台OpenGL ES API的一份参考,想了解更多关于OpenGL ES,查看下列参考资料。
参照
OpenGL® ES 3.0 Programming Guide, published by Addison-Wesley, provides a comprehensive introduction to OpenGL ES concepts.
OpenGL® Shading Language, Third Edition, also published by Addison-Wesley, provides many shading algorithms useable in your OpenGL ES app. You may need to modify some of these algorithms to run efficiently on mobile graphics processors.
OpenGL ES API Registry is the official repository for the OpenGL ES specifications, the OpenGL ES shading language specifications, and documentation for OpenGL ES extensions.
OpenGL ES Framework Reference describes the platform-specific functions and classes provided by Apple to integrate OpenGL ES into iOS.
iOS Device Compatibility Reference provides more detailed information on the hardware and software features available to your app.
GLKit Framework Reference describes a framework provided by Apple to make it easier to develop OpenGL ES 2.0 and 3.0 apps.