繁体   English   中英

渲染到纹理并同时渲染到渲染缓冲区

[英]Render to texture and render to renderbuffer at the same time

我使用这样的代码来设置我的帧缓冲:

glGenRenderbuffers(1, &colorBuffer_) ;

glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer_);

if (!colorBuffer_)
{
    NSLog(@"glGenRenderbuffers() failed");
    break;
}

[self.context renderbufferStorage:GL_RENDERBUFFER fromDrawable:drawable_];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width_);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height_);

glGenFramebuffers(1, &fbo);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);

if (!fbo)
{
    NSLog(@"glGenFramebuffers() failed");
    break;
}

CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, self.context, NULL, &textureCache_);
if (err)
{
    NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d", err);
}
CFDictionaryRef empty; // empty value for attr value.
CFMutableDictionaryRef attrs;
empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary
                           NULL,
                           NULL,
                           0,
                           &kCFTypeDictionaryKeyCallBacks,
                           &kCFTypeDictionaryValueCallBacks);
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                  1,
                                  &kCFTypeDictionaryKeyCallBacks,
                                  &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attrs,
                     kCVPixelBufferIOSurfacePropertiesKey,
                     empty);

CVPixelBufferCreate(kCFAllocatorDefault,
                    (int)width_,
                    (int)height_,
                    kCVPixelFormatType_32BGRA,
                    attrs,
                    &renderTarget_);

err = CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
                                                    textureCache_, renderTarget_,
                                                    NULL, // texture attributes
                                                    GL_TEXTURE_2D,
                                                    GL_RGBA, // opengl format
                                                    (int)width_,
                                                    (int)height_,
                                                    GL_BGRA, // native iOS format
                                                    GL_UNSIGNED_BYTE,
                                                    0,
                                                    &renderTexture_);
if (err)
{
    NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d", err);
}

CFRelease(attrs);
CFRelease(empty);

glBindTexture(CVOpenGLESTextureGetTarget(renderTexture_), CVOpenGLESTextureGetName(renderTexture_));
checkForErrors();

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

NSLog(@"%u", CVOpenGLESTextureGetName(renderTexture_));

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture_), 0);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer_);

我想渲染纹理并同时渲染到renderbuffer(以在屏幕上查看渲染结果)。 但是这段代码不起作用。 我想我不能同时使用glFramebufferTexture2DglFramebufferRenderbuffer 我对吗? 我该怎么做?

你是对的,你不能将纹理和渲染缓冲区附加到同一个连接点,以自动渲染到两者。

只需将其渲染到纹理,然后在屏幕上绘制一个屏幕大小的纹理四边形来显示它。 当然,请记住在渲染时将纹理解除绑定( glBindTexture(GL_TEXTURE_2D, 0) ),此时您不会。

或者通过像往常一样将结果渲染到屏幕并使用glCopyTexSubImage2D将这些结果复制到纹理中来glCopyTexSubImage2D 但最终你不会绕过副本,无论是间接绘制纹理四边形还是直接帧缓冲到纹理副本。

编辑:您也可以使用多个渲染目标解决此问题,方法是将纹理和渲染缓冲区附加到不同的颜色附件,并在片段着色器中的多个通道上输出相同的结果颜色(使用gl_FragData[i]而不是gl_FragColor )。 但我不确定这是否真的会给你带来任何东西,而且它需要你的着色器才能知道双重渲染。 最后我不确定ES实际上是否支持多重渲染目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM