gpt4 book ai didi

ios - OpenGL 中的离屏帧缓冲区

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:29 47 4
gpt4 key购买 nike

我正在使用 OpenGL 创建一个 iPhone 游戏,我想在屏幕外帧缓冲区上绘图,然后将该帧缓冲区用作在实际屏幕上绘图的纹理。我的代码基于 Apple's和 GLSprite 示例,但似乎我在切换绘图目标时做得不对,因为我只得到一个空白纹理。我正在使用的代码如下。怎么了?即时渲染纹理的最佳方式是什么?

下面的 Creating an Offscreen Framebuffer 给出了 8cd6 错误代码。

创建屏幕帧缓冲区

glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}

创建离屏帧缓冲区

glGenFramebuffers(1, &offscreenFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, offscreenFramebuffer);

glGenTextures(1,&framebufferTexture);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"ERROR: %x", status);
}

绘图,循环:

glBindTexture(GL_TEXTURE_2D,textureFromFile); //Switch to a texture from a file
glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreenFramebuffer); //Switch to the offscreen framebuffer
//Render the texture to be used later
glBindTexture(GL_TEXTURE_2D,framebufferTexture); //Switch to the offscreen framebuffer's texture
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); //Switch to the screen framebuffer
//Do the drawing using the texture rendered just before, and present this to the screen.

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

最佳答案

问题很简单:您忘记了在屏幕 上绘图。你知道,默认帧缓冲区,当你执行 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); 时你得到的是你在编写这段代码之前渲染得很好的东西。

没有所谓的“屏幕上”帧缓冲区对象。根据定义,所有用户定义的 FBO 都是屏幕外的。

你根本不应该有这个 viewFramebuffer FBO。相反,您应该绘制到您的纹理,然后使用该纹理绘制到默认帧缓冲区。

关于ios - OpenGL 中的离屏帧缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9058359/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com