如何在 iPhone OpenGL ES 2.0 中的渲染和呈现帧缓冲区之间切换?

发布于 2024-10-16 02:01:33 字数 239 浏览 4 评论 0原文

iPhone OpenGL ES 2.0..

  • 第一帧,渲染到我的帧缓冲区,然后呈现它(因为它默认在模板 OpenGL ES 应用程序中工作)。
  • 在下一帧上,我想使用渲染的帧缓冲区作为着色器的输入,同时渲染到另一个帧缓冲区并呈现第二个帧缓冲区。
  • 下一帧,我想使用framebuffer2作为着色器的输入,同时再次渲染到第一个帧缓冲区。
  • 重复

我该怎么做?

iPhone OpenGL ES 2.0..

  • First frame, render to my framebuffer then present it (as it works by default in the template OpenGL ES application).
  • On the next frame, I want to use that rendered framebuffer as an input in to my shaders, while rendering to another framebuffer and presenting that 2nd framebuffer.
  • The next frame, I want to use framebuffer2 as input in to my shaders, while rendering to the first framebuffer again.
  • Repeat

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-10-23 02:01:33

您应该能够使用如下代码设置一个具有纹理支持的渲染缓冲区:

// Offscreen position framebuffer object
glGenFramebuffers(1, &positionFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);

glGenRenderbuffers(1, &positionRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, positionRenderbuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, FBO_WIDTH, FBO_HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, positionRenderbuffer); 

// Offscreen position framebuffer texture target
glGenTextures(1, &positionRenderTexture);
glBindTexture(GL_TEXTURE_2D, positionRenderTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FBO_WIDTH, FBO_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionRenderTexture, 0);

切换缓冲区就像使用这样的代码一样简单:

glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);        
glViewport(0, 0, FBO_WIDTH, FBO_HEIGHT);

然后,您可以渲染到该缓冲区并通过将其传递到一个简单的纹理中来显示生成的纹理着色器将其显示在矩形几何体中。该纹理也可以输入到着色器中,该着色器渲染到另一个由纹理支持的类似渲染缓冲区中,依此类推。

如果您需要进行一些基于 CPU 的处理或读出,可以使用 glReadPixels() 从此屏外渲染缓冲区中提取像素。

有关示例,您可以在此处此处。前者处理来自相机的视频帧,其中一项设置允许视频直通,同时在屏外渲染缓冲区中进行处理。后一个示例在某个点渲染为立方体贴图纹理,然后使用该纹理在茶壶上进行环境映射。

You should be able to set up a renderbuffer that has a texture backing it using code like the following:

// Offscreen position framebuffer object
glGenFramebuffers(1, &positionFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);

glGenRenderbuffers(1, &positionRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, positionRenderbuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, FBO_WIDTH, FBO_HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, positionRenderbuffer); 

// Offscreen position framebuffer texture target
glGenTextures(1, &positionRenderTexture);
glBindTexture(GL_TEXTURE_2D, positionRenderTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FBO_WIDTH, FBO_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionRenderTexture, 0);

Switching buffers is as simple as using code like this:

glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);        
glViewport(0, 0, FBO_WIDTH, FBO_HEIGHT);

You can then render to that buffer and display the resulting texture by passing it into a simple shader that displays it within a rectangular piece of geometry. That texture can also be fed into a shader which renders into another similar renderbuffer that is backed by a texture, and so on.

If you need to do some CPU-based processing or readout, you can use glReadPixels() to pull in the pixels from this offscreen renderbuffer.

For examples of this, you can try out my sample applications here and here. The former does processing of video frames from a camera, with one of the settings allowing for a passthrough of the video while doing processing in an offscreen renderbuffer. The latter example renders into a cube map texture at one point, then uses that texture to do environment mapping on a teapot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文