The OpenGL ES specification requires that each implementation provide a mechanism that an application can use to create a framebuffer to hold rendered images.【OpenGL ES说明书需要每个执行过程提供一个机制:程序可以用于创建一个帧缓存来保持渲染图片】 On iOS, all framebuffers are implemented using framebuffer objects, which are built-in to OpenGL ES 2.0, and provided on all iOS implementations of OpenGL ES 1.1 by the GL_OES_framebuffer_object
extension.【在IOS,所有的帧缓存用(在OpenGL ES2.0里建立的)帧缓存对象执行,借助GL_OES_framebuffer_object
对象扩展提供所有OpenGL ES1.1的IOS的执行过程】
Framebuffer objects allow your application to precisely control the creation of color, depth, and stencil targets.【帧缓存对象允许你的应用程序精确地控制颜色、深度和模板目标的创建】 You can also create multiple framebuffer objects on an single context, possibly sharing resources between the frame buffers.【你也可以在一个简单的环境里创建多个帧缓存对象,有可能在帧缓存之间共用资源】
To correctly create a framebuffer:【正确地创建帧缓存】
Create a framebuffer object.【创建一个帧缓存对象】
Create one or more targets (renderbuffers or textures), allocate storage for them, and attach each to an attachment point on the framebuffer object.【创建一个或多个目标(渲染缓存或材质),为它们分配内存,并把它们附在帧缓存对象的附着点上】
Test the framebuffer for completeness.【测试帧缓存是否完整】
Depending on what task your application intends to perform, your application configures different objects to attach to the framebuffer object. 【根据你的程序要执行的任务,你的程序设置不同的目标来附着到帧缓存对象上】In most cases, the difference in configuring the framebuffer is in what object is attached to the framebuffer object’s color attachment point:【在大多数案例里,设置帧缓存的不同,在于附着在帧缓存对象的颜色附着点上的对象】
If the framebuffer is used to perform offscreen image processing, attach a renderbuffer. 【如果帧缓存用于执行离屏映像过程,就附一个渲染缓存】See “Creating Offscreen Framebuffer Objects”
If the framebuffer image is used as an input to a later rendering step, attach a texture.【如果帧缓存对象图片是用作输入进一个后面的渲染步骤,附着一个材质】 “Using Framebuffer Objects to Render to a Texture”
If the framebuffer is intended to be displayed to the user, use a special Core Animation-aware renderbuffer.【如果帧缓存要显示给用户,用一个专门的核心动画渲染缓存】 See “Rendering to a Core Animation Layer”
A framebuffer intended for offscreen rendering allocates all of its attachments as OpenGL ES renderbuffers.【一个要离屏渲染的帧缓存把它所有的附着物分配为OpenGL ES渲染缓存】The following code allocates a framebuffer object with color and depth attachments.【下列代码分配了一个带颜色和深度附着物的帧缓存对象】
Create the framebuffer and bind it.【创建帧缓存并绑定它】
//创建一个OpenGL ES用的无符号整形缓存,名为framebuffer GLuint framebuffer; |
//从framebuffer的地址起,分配1个帧缓存的内存,(1个,地址) glGenFramebuffers(1, &framebuffer); |
//把framebuffer绑定在OpenGL ES的帧缓存GL_FRAMEBUFFER上 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
Create a color renderbuffer, allocate storage for it, and attach it to the framebuffer’s color attachment point.【创建一个颜色渲染缓存,为它分配内存,并把它附在帧缓存的颜色附着点上】
//创建一个OpenGL ES用的无符号整形缓存,名为colorRenderbuffer GLuint colorRenderbuffer; |
//从colorRenderbuffer的地址起,分配一个渲染缓存的内存,(1个,地址) glGenRenderbuffers(1, &colorRenderbuffer); |
//把colorRenderbuffer绑定在OpenGL ES的渲染缓存GL_RENDERBUFFER上 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); |
//设定渲染缓存的存储类型(GL型red、green、blue、alpha共8位、宽、高) glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); |
//把GL_RENDERBUFFER里的colorRenderbuffer附在GL_FRAMEBUFFER的GL_COLOR_ATTACHMENT0(颜色附着点0)上 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); |
Create a depth or depth/stencil renderbuffer, allocate storage for it, and attach it to the framebuffer’s depth attachment point.【创建一个深度或深度/模板渲染缓存,给它分配内存,并把它附在帧缓存的深度附着点上】
//创建一个OpenGL ES用的无符号整形缓存,名为depthRenderbuffer GLuint depthRenderbuffer; |
//从depthRenderbuffer的地址起,分配一个渲染缓存的内存,(1个,地址) glGenRenderbuffers(1, &depthRenderbuffer); |
//把depthRenderbuffer绑定在OpenGL ES的渲染缓存GL_RENDERBUFFER上 glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); |
//设定渲染缓存的存储类型(GL_DEPTH_COMPONENT16、宽、高) glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); |
//把GL_RENDERBUFFER里的depthRenderbuffer附在GL_FRAMEBUFFER的GL_DEPTH_ATTACHMENT(深度附着点)上 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer); |
Test the framebuffer for completeness. This test only needs to be performed when the framebuffer’s configuration changes.【测试帧缓存的完整性,只需要在帧缓存的设置改变时执行】
//声明并赋值一个GL枚举变量,赋值为检测GL_FRAMEBUFFER状态的返回值, GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ; |
//如果状态不完全 if(status != GL_FRAMEBUFFER_COMPLETE) { |
//显示语句:创建完全的帧缓存对象失败,状态是... NSLog(@"failed to make complete framebuffer object %x", status); |
} |
The code to create this framebuffer is almost identical to the offscreen example, but now a texture is allocated and attached to the color attachment point.【创建这个帧缓存的代码几乎和离屏的例子一样,但是,现在是一个材质被分配和附着在颜色附着点】
Create the framebuffer object.【创建一个帧缓存对象,见上】
Create the destination texture, and attach it to the framebuffer’s color attachment point.【创建指定的材质,并附在帧缓存的颜色附着点上】
// create the texture【创建一个材质】 |
//声明一个GL的无符号整形变量,叫texture GLuint texture; |
//从texture的地址起,分配一个材质的内存,(1个,地址) glGenTextures(1, &texture); |
//把texture绑定在OpenGL ES的材质GL_TEXTURE_2D上 glBindTexture(GL_TEXTURE_2D, texture); |
//指定材质2D图片的形式() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
//把GL_TEXTURE_2D里的texture,绑定在GL_FRAMEBUFFER里的GL_COLOR_ATTACHMENT0上 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); |
Allocate and attach a depth buffer (as before).【分配并附着一个深度缓冲,如前】
Test the framebuffer for completeness (as before).【测试帧缓存的完整性,如前】
Although this example assumes you are rendering to a color texture, other options are possible. 【虽然这个例子假定你正渲染到一个颜色材质,但是其它选择也可以】For example, using the OES_depth_textureextension, you can attach a texture to the depth attachment point to store depth information from the scene into a texture. 【例如,用OES_depth_texture扩展,你可以附着一个材质到深度附着点来存储场景深度信息进一个材质,(根据我个人的经验,就是用一个灰度图来把平面材质表现出凹凸效果,即不必对表面进行细致的位移改变就能表现出凹凸效果)】You might use this depth information to calculate shadows in the final rendered scene.【你可以用这个深度信息计算最后渲染的场景的影子】
Most applications that draw using OpenGL ES want to display the contents of the framebuffer to the user.【大多数用OpenGL ES绘制的程序要把帧缓存的内容显示给用户】 On iOS, all images displayed on the screen are handled by Core Animation. 【在IOS里,所有显示在屏幕上的图片都有核心动画操作】Every view is backed by a corresponding Core Animation layer. 【每个试图都由一个对应的核心动画层支持】OpenGL ES connects to Core Animation through a special Core Animation layer, a CAEAGLLayer
.【OpenGL ES通过一个专门的核心动画层CAEAGLLayer
与核心动画连接】 A CAEAGLLayer
allows the contents of an OpenGL ES renderbuffer to also act as the contents of a Core Animation layer.【CAEAGLLayer
允许一个OpenGL ES渲染缓存的内容也担当核心动画层的内容】This allows the renderbuffer contents to be transformed and composited with other layer content, including content rendered using UIKit or Quartz. 【这允许渲染缓存的内容被变形和与其它层的内容混合,包括用UIKit和Quartz渲染的内容】Once Core Animation composites the final image, it is displayed on the device’s main screen or an attached external display.【一旦核心动画混合最终图片,它就被显示在设备的主屏或一个附着的外部屏幕上】