您的位置:首页 > 运维架构

OpenGL ES on iPhone OS

2010-04-25 00:18 295 查看

OpenGL ES on iPhone OS

OpenGL ES(OpenGL for Embedded Systems) 是OpenGL的简化版本,它去掉了一些多余的功能,旨在提供一个在移动设备上可以容易实现的编程接口。OpenGL ES允许你的程序配置一个传统的3D图形管线,并提交数据给OpenGL,然后由OpenGL完成变换和光照,装配成图元,以及光栅化为2D图像。

现在OpenGL ES有两个版本:

OpenGL ES 1.1 用完善的固定功能管线实现了一个标准的图形管线。固定功能管线实现了传统的光照和光栅化模型。允许打开和配置管线上的各种部分来完成指定的工作;也可以关闭一些功能,来改善程序的速度。

OpenGL ES 2.0 共用了许多OpenGL ES 1.1上的函数,但是删除了所有固定管线的功能,取而代之的是一个一般目的的基于shader的管线。shader允许你创建自己的顶点属性,以及直接在硬件上执行定制的顶点和片段函数,允许你的程序可以完全定制应用于每定点和每片段上的操作。

Apple的硬件支持这两个版本的OpenGL ES。

本章的剩余部分给出了iPhone图形硬件和OpenGL ES的一个概述,并阐述了它们如何相适应。

iPhone Graphics Overview



Core Animation是基本的iPhone图形子系统。你的程序中的每个
UIView对象 都由Core Animation层支持。随着各层更新它们的内容,它们就被Core Animation变为动画和混合,并显示出来。这个过程在
iPhone Application Programming Guide
中的
Window and Views
一节详细描述


同iPhone上其它的图形系统一样,OpenGL ES也是Core Animation的一个客户机(client)。要使用OpenGL ES渲染屏幕,程序首先要创建一个UIView,它由一个特定的Core Animation层——CAEAGLLayer对象来支持。CAEAGLLayer可以察觉到(is aware of)OpenGL ES,并且可以用于创建作为Core Animation一部份的渲染目标。当你的程序完成了一帧的渲染后, you present the contents of the
CAEAGLLayer
object, where they are composited with the data from other views.

关于如何创建一个CAEAGLLayer对象并显示你渲染的图像的完整讨论见 “Working with OpenGL ES Contexts and Framebuffers.”

尽管你可以使用OpenGL ES层和非OpenGL ES层来组成场景,实际上,你可以只使用OpenGL ES来获得更高表现。关于用OpenGL ES的内容与非OpenGL ES混合的的内容见 “Displaying Your Results.”

Overview of OpenGL ES

OpenGL ES提供了一个用于提交几何给硬件加速的渲染管线的过程(Procedural)API。OpenGL ES命令提交给rendering context,在那里使用这些命令来生成可以显示给用户的图像。 Most commands in OpenGL ES perform one of the following actions:

读取OpenGL ES context的状态:典型的应用是确定OpenGL ES的能力。详见 “Determining OpenGL ES Capabilities”

改变 OpenGL ES context的状态变量:典型的应用是为未来的操作配置管线。

创建、修改、删除OpenGL ES对象:详见下一节。

提交几何用于渲染。顶点数据提交给管线,然后处理、装配成图元,光栅化给framebuffer

An OpenGL ES implementation is allowed to extend the OpenGL ES specification, either by offering limits higher than the minimum required (such as allowing larger textures) or by extending the API through extensions . Apple uses the extensions mechanism to provide a number of critical extensions that help provide great performance in iPhone OS. For example, Apple offers a texture compression extension that makes it easier to fit your textures into the memory available on iPhone. Note that the limits and extensions offered by iPhone OS may vary depending on the hardware. Your application must test the capabilities at runtime and alter its behavior to match what is available. For more information on how to do this, see “Determining OpenGL ES Capabilities.”

OpenGL ES Objects

As described above, OpenGL ES offers a number of objects that can be created and configured to help create your scene. All of these objects are managed for you by OpenGL ES. Some of the most important object types include:

如前文所述,OpenGL ES提供了一些对象,通过创建和配置它们来帮助你创建场景。主要的对象类型包括:

texture(纹理) 是可以被几何管线采样的图像。This is typically used to map a color image onto your geometry but can also be used to map other data onto the geometry (for example, normals or lighting information). “Best Practices for Working with Texture Data” discusses critical topics for using textures on Apple’s OpenGL ES implementation.

buffer(缓冲区) 是属于OpenGL ES的内存,你可以向它读写数据。 The most common use for a buffer is to hold vertex data that your application wants to submit to the graphics hardware. Because this buffer is owned by the OpenGL ES implementation, it can optimize the placement and format of the data in this buffer in order to more efficiently process vertices, particularly when the data does not change from frame to frame. Using buffers to manage your vertex data can significantly boost the performance of your application.

shader(着色器) 也是对象。 OpenGL ES 2.0可以创建shader,通过编译和链接代码到shader,可以指定shader来处理顶点和片段数据 。

renderbuffer(渲染缓冲区) 是个特殊格式的见到2D图像。 This format may be defined as color data, but it could also be depth or stencil information. Renderbuffers are not usually used alone, but are instead collected and used as part of a framebuffer.

framebuffer(帧缓冲区) 是渲染管线的最终产物。 A framebuffer object is really just a container that attaches textures and renderbuffers to itself to create a complete destination for rendering. Framebuffer objects are part of the OpenGL ES 2.0 standard, and Apple also implements them on OpenGL ES 1.1 with the
OES_framebuffer_object
“Working with OpenGL ES Contexts and Framebuffers,” describes strategies for creating and using framebuffers in iPhone OS applications. extension. Framebuffers are used extensively on the iPhone and are described in more detail below. A later chapter。

尽管OpenGL ES中的每个对象都有自己的函数来控制,这些对象也公用标准的模型:

生成对象标识

你应该给想要创建的对象生成一个标识,这个标识类似于指针。当程序要操作一个对象的时候,用标识来指定那个对象应该工作。

注意,创建对象的标识并没有给对象分配内存,它仅仅是给对象一个引用。

绑定对象到OpenGL ES context。

每种类型对象都有一个方法来绑定对象到context。一次只可以在一种类型的对象上进行操作,通过绑定对象来选定他。你第一次绑定对象的标识的时候,OpenGL ES给对象分配了内存和初始化( The first time you bind to an object identifier, OpenGL ES allocates memory and initializes that object. )。

修改对象的状态

Commands implicitly operate on the currently bound object. After binding the object, your application makes one or more OpenGL ES calls to configure the object. For example, after binding to a texture, your application makes an additional call to actually load the texture image.

渲染对象

Once you’ve created and configured your objects, you can start drawing your geometry. As you submit vertices, the currently bound objects are used to render your output. In the case of shaders, the current shader is used to calculate the final results. Other objects may be involved at various stages of the pipeline.

删除对象

Finally, when you are done with an object, your application deletes it. When an object is deleted, its contents and object identifier are recycled.

在iPhone OS上,OpenGL ES对象由sharegroup 对象管理。多个rendering context可以被配置为使用用一个sharegroup。 The two rendering contexts can then use the same data (for example, a texture) to actually share a single texture object. Sharegroups are covered in “EAGLSharegroup.”

Framebuffers

framebuffer对象是渲染命令的目标。通常,framebuffer由平台定义的接口创建。每个平台都提供它自己的函数来创建可以绘制到窗口的framebuffer。 The
OES_framebuffer_object
extended OpenGL ES to provide a standard mechanism to create and configure framebuffers that rendered to offscreen renderbuffers or to textures. Apple不提供创建framebuffer的平台接口,所有的framebuffer对象都是用
OES_framebuffer_object创建的。在OpenGL ES 2.0,这些函数是核心规范的一部分。


Framebuffer objects provide storage for color, depth and/or stencil data by attaching images to the framebuffer, as shown in Figure 1-1 . The most common image attachment is a renderbuffer. However, a texture can also be attached to the color attachment of a framebuffer, allowing an image to be drawn, and then later texture mapped onto other geometry.

Figure 1-1 Framebuffer with color, depth, and stencil buffers



典型的创建framebuffer的过程是:

生成和绑定framebuffer对象。

生成、绑定、配置一个图像。

连接图像到framebuffer

对其它图像重复第2和3步。

测试framebuffer的完整性。完整性的规则见OpenGL规范中,这些规则确保framebuffer以及其附属物都是良好定义的。

Apple extends framebuffer objects by allowing the color renderbuffer’s storage to be allocated so that it is shared with a Core Animation layer. This data can be presented, where it is combined with other Core Animation data and presented to the screen. See “Working with OpenGL ES Contexts and Framebuffers” for more information.

iPhone OS Classes

所有的OpenGL ES实现都需要一个平台具体的代码来创建rendering context以及用它来绘制场景。 iPhone OS 用EAGL来完成这项工作,EAGL是一个 Objective-C 接口. This section highlights the classes and protocols of the EAGL API, 本节强调EAGL API的类和协议,详见“Working with OpenGL ES Contexts and Framebuffers.”

EAGLContext

The
EAGLContext
class defines the rendering context that is the target of all OpenGL ES commands. Your application creates and initializes an
EAGLContext
object and makes it the current target of commands. As OpenGL ES commands are executed, they are typically be stored in a queue maintained by the context and later executed to render the final image.

The
EAGLContext
class also provides a method to present images to Core Animation for display.

EAGLSharegroup

Every
EAGLContext
object contains a reference to an
EAGLSharegroup
object. Whenever an object is allocated by OpenGL ES for that context, the object is actually allocated and maintained by the sharegroup. This division of responsibility is useful because it is possible to create two or more contexts that use the same sharegroup. In this scenario, objects that are allocated by one rendering context can be used by another, as shown in Figure 1-2 .

Figure 1-2 Two contexts sharing OpenGL objects



Using a sharegroup allows two or more contexts to share OpenGL resources without duplicating that data for each context. Resources on a mobile device are scarcer than those on desktop hardware. By sharing textures, shaders and other objects, your application makes better use of available resources.

An application can also implement resource-sharing mechanism by using a single context and creating one framebuffer object for each rendering destination. The application switches the current target for rendering commands as needed, without changing the current context.

EAGLDrawable Protocol

Your application does not directly implement the
EAGLDrawable
protocol on any objects. An
EAGLContext
object recognizes that objects that implement this protocol can allocate storage for a renderbuffer that can later be presented by the user. Only renderbuffers that are allocated using the drawable object can be presented in this way.

In iPhone OS, this protocol is implemented only by the
CAEAGLLayer
class, to associate OpenGL ES renderbuffers with the Core Animation graphics system.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: