无法使用 glGenFramebuffers 生成帧缓冲区
Posted
技术标签:
【中文标题】无法使用 glGenFramebuffers 生成帧缓冲区【英文标题】:Can't generate framebuffer with glGenFramebuffers 【发布时间】:2012-11-02 05:16:16 【问题描述】:glGenFramebuffers 不分配帧缓冲区对象!我无法让这个小程序运行。
代码:
/* ----- AppDelegate.h ----- */
#import <UIKit/UIKit.h>
#import "GLView.h"
@interface AppDelegate : NSObject <UIApplicationDelegate>
@private
UIWindow *window;
GLView *view;
@end
/* ----- AppDelegate.h ----- */
#import "AppDelegate.h"
@implementation AppDelegate
- (void)dealloc
[view release];
[window release];
[super dealloc];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
CGRect frame = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:frame];
view = [[GLView alloc] initWithFrame:frame];
[window addSubview:view];
[window makeKeyAndVisible];
return YES;
@end
/* ----- GLView.h ----- */
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
@interface GLView : UIView
@private
EAGLContext* context;
GLuint color_render_buffer;
GLuint depth_render_buffer;
GLuint frame_buffer;
-(void)render:(CADisplayLink*)displayLink;
@end
/* ----- GLView.mm ----- */
#import "GLView.h"
@implementation GLView
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
CAEAGLLayer* eaglLayer = (CAEAGLLayer*)self.layer;
eaglLayer.opaque = YES;
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if(!context && ![EAGLContext setCurrentContext:context])
[self release];
return nil;
int width = (int)frame.size.width;
int height = (int)frame.size.height;
glGenFramebuffers(1, &frame_buffer);
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer);
if(glIsFramebuffer(frame_buffer) == GL_FALSE)
//NSLog(@"%i", frame_buffer);
NSLog(@"frame_buffer is GL_FALSE!");
glGenRenderbuffers(1, &color_render_buffer);
glBindRenderbuffer(GL_RENDERBUFFER, color_render_buffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:eaglLayer];
if(glIsRenderbuffer(color_render_buffer) == GL_FALSE)
NSLog(@"%i", color_render_buffer);
NSLog(@"color_render_buffer is GL_FALSE");
glGenRenderbuffers(1, &depth_render_buffer);
glBindRenderbuffer(GL_RENDERBUFFER, depth_render_buffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
if(glIsRenderbuffer(depth_render_buffer) == GL_FALSE)
NSLog(@"depth_render_buffer is GL_FALSE");
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_render_buffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_render_buffer);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
#ifdef DEBUG_LOG
NSLog(@"Framebuffer is not complete!");
exit(1);
#endif
[[CADisplayLink displayLinkWithTarget:self selector:@selector(render:)] addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
return self;
+(Class)layerClass
return [CAEAGLLayer class];
-(void)dealloc
if([EAGLContext currentContext] == context)
[EAGLContext setCurrentContext:nil];
[context release];
[super dealloc];
-(void)render:(CADisplayLink*)displayLink
/* Rendering */
[context presentRenderbuffer:GL_RENDERBUFFER];
@end
我总是得到“frame_buffer is GL_FALSE!”打印到控制台... frame_buffer 始终为 0(窗口系统默认帧缓冲区) 是否需要将任何设置添加到 plist 中?还是我错过了什么?
请帮忙。
【问题讨论】:
【参考方案1】:你永远不会调用 setCurrentContext: when context != nil. 变化:
if(!context && ![EAGLContext setCurrentContext:context])
收件人:
if(!context || ![EAGLContext setCurrentContext:context])
【讨论】:
短路评估就是这里发生的事情。您的代码首先检查 (!context)。上下文是正确的,因此执行第二个任期没有意义。此外,首先评估最左边的条件会在您的代码中产生维护风险 - 如果有人决定稍后在您的 if 语句中添加另一个子句怎么办?以上是关于无法使用 glGenFramebuffers 生成帧缓冲区的主要内容,如果未能解决你的问题,请参考以下文章