OpenGL UIView只显示黑色

时间:2014-04-08 01:46:03

标签: ios uiview opengl-es

我正在关注这个OpenGL ES 2.0教程http://www.raywenderlich.com/3664/opengl-tutorial-for-ios-opengl-es-2-0,我正试图让屏幕清除glClearColor,但我看到的只是黑色。通过查看我的GLView可以看出原因吗?

#import "GLView.h"

@implementation GLView

+ (Class)layerClass
{
    return [CAEAGLLayer class];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupLayer];
        [self setupContext];
        [self setupRenderBuffer];
        [self setupFrameBuffer];
        [self render];
    }
    return self;
}

- (void)setupLayer
{
    m_eaglLayer = (CAEAGLLayer *)self.layer;
    m_eaglLayer.opaque = YES;
}

- (void)setupContext
{
    m_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!m_context) {
        NSLog(@"Failed to initialize OpenGLES 2.0 context");
        exit(1);
    }

    if (![EAGLContext setCurrentContext:m_context]) {
        NSLog(@"Failed to set current OpenGL context");
        exit(1);
    }
}

- (void)setupRenderBuffer
{
    glGenRenderbuffers(1, &m_colorRenderBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderBuffer);
    [m_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:m_eaglLayer];
}

- (void)setupFrameBuffer
{
    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRenderBuffer);
}

-(void)render
{
    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, self.frame.size.width, self.frame.size.height);
    [m_context presentRenderbuffer:GL_RENDERBUFFER];

}

@end

1 个答案:

答案 0 :(得分:0)

我发现了问题。我从未在app delegate中初始化UIWindow。