OpenGL ES on iPhone OS

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中的每個對象都有自己的函數來控制,這些對象也公用標準的模型:

  1. 生成對象標識

    你應該給想要創建的對象生成一個標識,這個標識類似於指針。當程序要操作一個對象的時候,用標識來指定那個對象應該工作。

    注意,創建對象的標識並沒有給對象分配內存,它僅僅是給對象一個引用。

  2. 綁定對象到OpenGL ES context。

    每種類型對象都有一個方法來綁定對象到context。一次只可以在一種類型的對象上進行操作,通過綁定對象來選定他。你第一次綁定對象的標識的時候,OpenGL ES給對象分配了內存和初始化( The first time you bind to an object identifier, OpenGL ES allocates memory and initializes that object. )。

  3. 修改對象的狀態

    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.

  4. 渲染對象

    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.

  5. 刪除對象

    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 with attachments.

典型的創建framebuffer的過程是:

  1. 生成和綁定framebuffer對象。

  2. 生成、綁定、配置一個圖像。

  3. 連接圖像到framebuffer

  4. 對其它圖像重複第2和3步。

  5. 測試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

Core Animation-based renderbuffer

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章