我使用这样的代码来设置我的帧缓冲区:
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_);
我想同时渲染到纹理和渲染到渲染缓冲区(在屏幕上查看渲染结果)。但是这段代码不起作用。我认为我不能同时使用glFramebufferTexture2D
和glFramebufferRenderbuffer
。我对吗?我该怎么做?