OpenGL ES Programming Guide for iOS 第二章

第二章 iOS上的OpenGL ES

OpenGL ES提供程序接口原函数以透过图形加速硬件管线绘制。OpenGL消耗图形命令产生影像用以显示给用户,或进一步处理OpenGL ES以外的检索。

OpenGL ES规范明确声明了每个函数的行为。参考下面步骤以理解大部份OpenGL ES命令:

·参考25“Determining OpenGL ESCapabilities” 获得各类别的详细实践信息。

·读写状态变量声明于规范中。OpenGL ES的状态通常表示当前配置的图形管线。例如,OpenGL ES1.1使用状态变量,广泛地配置灯光,材料和固定功能管线进行计算。

·建立,修改及推毁OpenGL ES对象。OpenGL ES对象不是Objective-C对象;它们是透过OpenGL ES程序接口操作的资源。参考17See “OpenGL ES Objects Encapsulate Resources on Behalf ofYour Application” 获得更多讯息。

·顶点需透过绘图原函数提交至管线加工,组合,或著作为祯缓存影格。

2.1 该选择哪一个版本的OpenGL ES?

当你设计OpenGL ES应用程序时,必须回答一个关键问题:你的应用要支持OpenGL ES2.0OpenGL ES 1.1或是两者兼有。

·OpenGL ES 2.0OpenGL ES1.1更强大灵活,是新应用的最佳选择。可更清晰,简洁的自定义顶点及著色器,以及有更好的效能。在OpenGL ES1.1执行相同的计算,往往需要多个渲染通道或更复杂的状态配置,容易掩盖算法目的。如果算法变复杂,OpenGL ES2.0的著色器更能清晰及简洁的表达运算,以及更高的效能。建制OpenGL ES2.0应用程序需要较多工作,默认情况下,需重新为OpenGL ES1.1创建一些基础配置。

·OpenGL ES1.1提供了一些固定功能管线,包括一些不错的基础3D应用功能,转换和光照,及混合至祯缓存。如果应用需求简单,用OpenGL ES1.1可较少编码。若需要支持所有iOS装置,就只能选择OpenGL ES1.1

如果你为了就设备相容性选择实践OpenGL ES1.1为主,可以考虑增加一个OpenGL ES 2.0的渲染选项,OpenGL ES2.0有更大的能力及优势发挥新的iOS装置的图形处理器。

重要如果你的应用不支持OpenGL ES 1.1与2.0绘图路径,将会限制应用能选择的装置版本。参考 iOS App Programming Guild中的”Declaring the Required Device Capabilities”章节。

2.2.了解OpenGLES的架构

OpenGL ES运作在几个关键原则上。为了设计高效的OpenGL ES应用程序,需要了解底层架构。

2.2.1. 客户端-服务器模型

OpenGL ES使用客户端-服务器模型,如图1.1。当应用程序呼叫OpenGL ES函数时,它会与OpenGL ES客户端交谈。客户端程序到函数调用完毕,必要时转化为OpenGL服务器命令。客户端-服务器模型允许工作处理被分为客户端与服务器端的函数调用。客户端,服务器,及之间的沟通路径的本质,OpenGL ES各别具体实践;在iOS终至上,工作是介于CPU与专用的图形处理器之间。

图1.1 OpenGL ES 客户端-服务器模型

2.2.2. OpenGL ES 依赖于具体平台的关键功能库

OpenGL ES规范声明了OpenGL ES如何工作,但是没有声明管理OpenGL ES与主操作系统之间交互的功能。相反地,规范假设每个实践都提供配置渲染内文与系统祯缓存的配置功能。渲染内文储存OpenGL ES状态机的内部状态。渲染内文每个OpenGLES应用程序各自维护自己的状态数据副本,不用担心会影响其他程序。参考20页的Configuring OpenGL ES Contexts。也可以在单一应用中使用多个宣染内文。系统祯缓存是OpenGL ES绘制命令的终点,也与主操作系统的图形子系统有关。iOS不提供系统祯缓存。相反地,iOS扩展了OpenGLES的祯缓存对象,并允许与CoreAnimation共享数据。参考18页的Framebuffer Objects are theOnly Rendering Target on iOS获得更多有关祯缓存对象的信息,28页的Drawing With OpenGL ES详细讨论了在应用中创建与使用祯缓存。

2.2.3. 命令可以异步执行

OpenGL ES客户端-服务器模型的一个好处是OpenGL ES函数可以在请求操作完成前将控制权还给应用程序。假如OpenGL ES在将控制权还给应用前,需等每个函数执行完毕,CPU与GPU将会步调一致,应用会丧失很多并行机会。在iOS中,推迟执行绘图命令很常见。推迟几个绘图命令,并同时处理,图形硬件可以删除隐藏的表面于执行昂贵的片段计算之前。许多OpenGL ES函数或明或暗送刷新命令给图形装置。其他刷新图形处理器的OpenGL命令会等到某些或全部命令完成。尽可能避免设计出客户-服务端同步的应用。

2.2.4. 命令可以依序执行

若客户端应用程序照相同顺序执行函数,OpenGL ES保证函数调用的结果与宣染内文相同。当你的应用调用OpenGL ES函数,可以假设这些函数结果是有效的,即时命令尚未执行完毕。

2.2.5. 呼叫时参数会被复制

为了让命令异步处理,应用调用函数时需要的参数数据必须在控制权返回应用前被复制。若参数是个指向应用程序内存中的顶点数据数组指针,OpenGL ES必须在返回前复制顶点数据。这有一些重要意义。首先,应用可以自由改变自己的内存无论是否调用OpenGL ES,因为OpenGL ES绝不会和应用同时存取内存。再来,复制及格式化数据,以便图形装置可读取,并增加每个函数呼叫开销。为了最好的效能,应用需声明为了图形装置最佳化的数据格式,并且应该使用缓冲对象隐藏应用与OpenGL ES之间的内存管理及复制。

2.2.6. 定义在规范中具扩充功能实践方法

一個OpenGLES實現可以擴展OpenGLES規范的兩種方法之一。首先实现需满足规范规定的最低要求,如应用程序可存取的纹理大小,纹理单元数量。一个OpenGL ES实现是自由支持较大变量:如一个大型纹理或更多纹理单元。其次,OpenGL ES扩充允许一个实践提供新的OpenGL ES函数其常量。扩充允许实践添加全新特性。Apple实践许多扩充功能,允许应用程序利用硬件特性来改善应用程序的效能。不同的iOS装置与版本的实际硬件限制与提供的扩充功能清单会有所不同。应用需测试运行能力,以确保行为一致。

2.3.应用程序中常用OpenGL ES中具代表性封装对象资源

对象是不透明的容器,在应用中用来保存配置状态或是渲染器所需的数据。因为只有对象的访问是通过程序的API,在应用中配置对象时,可以选择不同策略的OpenGL ES实践。它可以存储在数据格式或是徒刑处理器最佳化的内存位置中。对象的另一个好处是重复使用,允许应用一次或多次配置对象。

OpenGL ES最重要的对象类型包括:

l纹理(texture)是个图像,可被绘图管线采样。通常纹理被用来映射到原语上的彩色图像,但也可映射到其他数据,如一般映射或是预计算的照明讯息。章节Best Practices for Workingwith Texture Data讨论在iOS上使用纹理(texture)这重要议题。

l缓存(buffer)对象在应用中是一个拥有OpenGL ES储存数据的区块内存。缓存被用来精确控制应用跟OpenGL ES之间复制数据的过程。举例来说,假设你提供顶点数组给,每次提交绘制呼叫都必需复制数据。相比之下,若你的应用将数据储存在顶点缓存对象(vertex buffer object),当应用提交命令修改顶点缓存对象的内文时,数据才被复制。使用缓存来管理顶点数据,可显著提高应用效能。

l顶点数组(vertex array)对象持有的是图型管线读取的顶点属性配置。许多应用渲染实体需要不同的管线配置。通过储存顶点数组配置,避免重新配置管线的成本,并可能允许执行特定的顶点配置优化处理。

l著色器程序(Shader programs),或著称作著色器(shaders),也是对象。OpenGL ES2.0应用可创建顶点与片段著色器分别进行顶点或片段的计算。

l染缓存(renderbuffer)是简单的2D图型影像的特殊格式。这种格式通常被定义为颜色,深度或模板数据。渲染缓存通常是不单独使用,而是与祯缓存(framebuffer)一起使用。

l祯缓存(Framebuffer)对象是图型管线的最终目的。祯缓存对象只是个容器,内含纹理(texture)染缓存(renderbuffer),本身创建渲染器的完整配置。在之后的章节Drawing With OpenGL ES, 讨论在iOS应用中创建及使用祯缓存的策略。

虽然在OpenGL ES中的每个对象类型都有它自己的函数来操纵它,所有的对象共享一个类似的编程模型:

1. 产生对象身份

身份是个普通的整数,用来表示某个特定对象实例。每当你需要一个新对象,呼叫OpenGL ES创建一个新的身份。创建对象身份不代表实际创建对象,只是配置个参考。

2.绑定对象到OpenGLES内文

大部分OpenGL ES函数隐含作用于对象,而不是要求每次呼叫时明确指定对象身份。你设置对象并绑定配置到内文。每个不同类型的对象使用不同的绑定函数。在第一次绑定一个对象身分时,OpenGL ES会配置及初始化对象。

3.修改对象状态

你的应用程序使一或多个函数调用配置对象。举例来说,在绑定纹理对象后,通常会配置纹理过滤及读取图像数据。改变一个对象可能是昂贵的,因为他可能需要提交新的数据给图型硬件。这是合理的,创建及配置对象一次,并在应用程序中不要改变它。

4. 使用对象

一但创建并配置了染场景所需全部对象,你要替管线绑定对象并执行一或多个绘图功能。OpenGL ES照原语将对象储存的数据渲染出来。结果会送到绑定的祯缓存对象。

5. 删除对象

当对象不用时,应用应该要将其删除。对象被删除后,内容会被摧毁,对象身分会被回收。

2.4.只宣染在iOS上的侦缓存对象(Framebuffer)

祯缓存(framebuffer)对象是渲染命令的目标。OpenGL ES2.0提供的祯缓存对象是核心规范中的一部分;在OpenGLES1.1中由OES_framebuffer_object拓展提供。因威祯缓存对象只在iOS上渲染,苹果保证OES_framebuffer_object拓展会被实践在所有

实践OpenGL ES1.1的iOS上。如图1-2,祯缓存对象提供颜色深度或模版数据存储将图像连结至祯缓存。最常见的图像连结对象是渲染缓存(renderbuffer)。然而,OpenGL ES纹理可以被连接到祯缓存的颜色附着点,而让图像直接渲染成纹理。
之后,纹理可以作为未来的渲染命令的输入。

图1-2,祯缓存与颜色及深度渲缓存


建立祯缓存需依循下列步骤:

1. 产生及连结一个祯缓存对象。

2. 产生,连结,及配置图像。

3. 连结图像到祯缓存的附著点

4. 若需更多图像则重复2-3

5. 测试祯缓存完整性。完整性的规则声明在OpenGL ES规范中。这些规则确保了祯缓存及其附著点。


OpenGL® Programming Guide, Sixth Edition OpenGL is a powerful software interface used to produce high-quality, computergenerated images and interactive applications using 2D and 3D objects, bitmaps, and color images. The OpenGL® Programming Guide, Sixth Edition, provides definitive and comprehensive information on OpenGL and the OpenGL Utility Library. The previous edition covered OpenGL through Version 2.0. This sixth edition of the best-selling “red book” describes the latest features of OpenGL Version 2.1. You will find clear explanations of OpenGL functionality and many basic computer graphics techniques, such as building and rendering 3D models; interactively viewing objects from different perspective points; and using shading, lighting, and texturing effects for greater realism. In addition, this book provides in-depth coverage of advanced techniques, including texture mapping, antialiasing, fog and atmospheric effects, NURBS, image processing, and more. The text also explores other key topics such as enhancing performance, OpenGL extensions, and cross-platform techniques. This sixth edition has been updated to include the newest features of OpenGL Version 2.1, including: Using server-side pixel buffer objects for fast pixel rectangle download and retrieval Discussion of the sRGB texture format Expanded discussion of the OpenGL Shading Language This edition continues the discussion of the OpenGL Shading Language (GLSL) and explains the mechanics of using this language to create complex graphics effects and boost the computational power of OpenGL. The OpenGL Technical Library provides tutorial and reference books for OpenGL. The Library enables programmers to gain a practical understanding of OpenGL and shows them how to unlock its full potential. Originally developed by SGI, the Library continues to evolve under the auspices of the OpenGL Architecture Review Board (ARB) Steering Group (now part of the Khronos Group), an industry consortium responsible for guiding the evolution of OpenGL and related technologies
OpenGL® ES™ is the industry’s leading software interface and graphics library for rendering sophisticated 3D graphics on handheld and embedded devices. The newest version, OpenGL ES 3.0, makes it possible to create stunning visuals for new games and apps, without compromising device performance or battery life. In the OpenGL ES 3.0 Programming Guide, 2nd Edition, the authors cover the entire API and Shading Language. They carefully introduce OpenGL ES 3.0 features such as shadow mapping, instancing, multiple render targets, uniform buffer objects, texture compression, program binaries, and transform feedback. Through detailed, downloadable C-based code examples, you’ll learn how to set up and program every aspect of the graphics pipeline. Step by step, you’ll move from introductory techniques all the way to advanced per-pixel lighting and particle systems. Throughout, you’ll find cutting-edge tips for optimizing performance, maximizing efficiency with both the API and hardware, and fully leveraging OpenGL ES 3.0 in a wide spectrum of applications. All code has been built and tested on iOS 7, Android 4.3, Windows (OpenGL ES 3.0 Emulation), and Ubuntu Linux, and the authors demonstrate how to build OpenGL ES code for each platform. Coverage includes EGL API: communicating with the native windowing system, choosing configurations, and creating rendering contexts and surfaces Shaders: creating and attaching shader objects; compiling shaders; checking for compile errors; creating, linking, and querying program objects; and using source shaders and program binaries OpenGL ES Shading Language: variables, types, constructors, structures, arrays, attributes, uniform blocks, I/O variables, precision qualifiers, and invariance Geometry, vertices, and primitives: inputting geometry into the pipeline, and assembling it into primitives 2D/3D, Cubemap, Array texturing: creation, loading, and rendering; texture wrap modes, filtering, and formats; compressed textures, sampler objects, immutable textures, pixel unpack buffer objects, and mipmapping Fragment shaders: multitexturing, fog, alpha test, and user clip planes Fragment operations: scissor, stencil, and depth tests; multisampling, blending, and dithering Framebuffer objects: rendering to offscreen surfaces for advanced effects Advanced rendering: per-pixel lighting, environment mapping, particle systems, image post-processing, procedural textures, shadow mapping, terrain, and projective texturing Sync objects and fences: synchronizing within host application and GPU execution This edition of the book includes a color insert of the OpenGL ES 3.0 API and OpenGL ES Shading Language 3.0 Reference Cards created by Khronos. The reference cards contain a complete list of all of the functions in OpenGL ES 3.0 along with all of the types, operators, qualifiers, built-ins, and functions in the OpenGL ES Shading Language. Table of Contents Chapter 1. Introduction to OpenGL ES 3.0 Chapter 2. Hello Triangle: An OpenGL ES 3.0 Example Chapter 3. An Introduction to EGL Chapter 4. Shaders and Programs Chapter 5. OpenGL ES Shading Language Chapter 6. Vertex Attributes, Vertex Arrays, and Buffer Objects Chapter 7. Primitive Assembly and Rasterization Chapter 8. Vertex Shaders Chapter 9. Texturing Chapter 10. Fragment Shaders Chapter 11. Fragment Operations Chapter 12. Framebuffer Objects Chapter 13. Sync Objects and Fences Chapter 14. Advanced Programming with OpenGL ES 3.0 Chapter 15. State Queries Chapter 16. OpenGL ES Platforms Appendix A. GL_HALF_FLOAT Appendix B. Built-In Functions Appendix C. ES Framework API Book Details Title: OpenGL ES 3.0 Programming Guide, 2nd Edition Author: Aaftab Munshi, Budirijanto Purnomo, Dan Ginsburg, Dave Shreiner Length: 560 pages Edition: 2 Language: English Publisher: Addison-Wesley Professional Publication Date: 2014-03-10 ISBN-10: 0321933885 ISBN-13: 9780321933881
Includes Complete Coverage of the OpenGL® Shading Language! Today’s OpenGL software interface enables programmers to produce extraordinarily high-quality computer-generated images and interactive applications using 2D and 3D objects, color images, and programmable shaders. OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 4.3, Eighth Edition, has been almost completely rewritten and provides definitive, comprehensive information on OpenGL and the OpenGL Shading Language. This edition of the best-selling “Red Book” describes the features through OpenGL version 4.3. It also includes updated information and techniques formerly covered in OpenGL® Shading Language (the “Orange Book”). For the first time, this guide completely integrates shader techniques, alongside classic, functioncentric techniques. Extensive new text and code are presented, demonstrating the latest in OpenGL programming techniques. OpenGL® Programming Guide, Eighth Edition, provides clear explanations of OpenGL functionality and techniques, including processing geometric objects with vertex, tessellation, and geometry shaders using geometric transformations and viewing matrices; working with pixels and texture maps through fragment shaders; and advanced data techniques using framebuffer objects and compute shaders. New OpenGL features covered in this edition include Best practices and sample code for taking full advantage of shaders and the entire shading pipeline (including geometry and tessellation shaders) Integration of general computation into the rendering pipeline via compute shaders Techniques for binding multiple shader programs at once during application execution Latest GLSL features for doing advanced shading techniques Additional new techniques for optimizing graphics program performance
OpenGL开发指南英文第九版 What This Guide Contains This guide contains the following chapters: • Chapter 1 , “Introduction to OpenGL ,” provides a glimpse into what OpenGL can do. It also presents a simple OpenGL programand explains the essential programming details you need to know for the subsequent chapters. • Chapter 2 , “Shader Fundamentals ,” discusses the major features of the OpenGL Shading Language and SPIR-V, demonstrating how to initialize and use themwithin an application. • Chapter 3 , “Drawing with OpenGL ,” describes the various methods for rendering geometry using OpenGL, as well as some optimization techniques for making rendering more efficient. • Chapter 4 , “Color, Pixels, and Fragments ,” explains OpenGL’s processing of color, including how pixels are processed, how buffers are managed, and rendering techniques focused on pixel processing. • Chapter 5 , “Viewing Transformations, Culling, Clipping, and Feedback ,” details the operations for presenting a three-dimensional scene on a two-dimensional computer screen, including the mathematics and shader operations for the various types of geometric projection. • Chapter 6 , “Textures and Framebuffers ,” discusses combining geometric model s and imagery for creating realistic, highly detailed three-dimensional models. • Chapter 7 , “Light and Shadow ,” describes simulating illumination effects for computer graphics, focusing on implementing those techniques in programmable shaders. • Chapter 8 , “Procedural Texturing ,” details the generation of textures and other surface effects using programmable shaders for inc reased realismand other rendering effects. • Chapter 9 , “Tessellation Shaders ,” explains OpenGL’s shader facility for managing and tessellating geometric surfaces. • Chapter 10 , “Geometry Shaders ,” describe an additional technique for modifying geometric primitives within the OpenGL rendering pipeline using shaders. • Chapter 11 , “Memory ,” demonstrates techniques using OpenGL’s framebuffer and buffer memories for advanced rendering techniques and nongraphical uses. • Chapter 12 , “Compute Shaders ,” introduces the newest shader stage, which integrates general computation into the OpenGL rendering pipeline.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值