繁体   English   中英

iPhone opengl es 2.0自定义构建gl视图失败,但不知道为什么

[英]iphone opengl es 2.0 custom build a gl view failed,but did not know why

我想从UIview构建一个opengl es 2.0视图。 并流这篇文章: http : //www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial

当我完成第一步时。 一切都错了。 它没有按照我的要求运行。

这是我的代码:

#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>


@interface NXGLES2View : UIView
- (void) render;
@end

#import "NXGLES2View.h"

@interface NXGLES2View () 
{
    CAEAGLLayer     *eaglLayer_;
    EAGLContext     *glContext_;
    GLuint          colorRenderBuffer_,frameBuffer_, depthRenderBuffer_;

    CADisplayLink   *displayLink_;
}

- (void) setupGL_;
- (void) tearDownGL_;

- (void) setupLayer_;
- (void) setupContext_;

- (void) setupRenderBuffer_;
- (void) setupFrameBuffer_;
- (void) setupDepthBuffer_;

- (void) setupDisplayLink_;
@end



    @implementation NXGLES2View

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [self setupGL_];
        }
        return self;
    }

    #pragma mark -
    #pragma mark setup step

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

    - (void) setupLayer_{
        eaglLayer_ = (CAEAGLLayer *)self.layer;
        eaglLayer_.opaque = YES;
    }

    - (void) setupContext_{
        glContext_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        if (!glContext_) {
            NSLog(@"failed to create eagl context,abort");
            abort();
        }

        if (![EAGLContext setCurrentContext:glContext_]) {
            NSLog(@"failed to set gl context, abort");
            abort();
        }
    }


    - (void) setupRenderBuffer_{
        glGenRenderbuffers(1, &colorRenderBuffer_);
        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer_);
        [glContext_ renderbufferStorage:GL_RENDERBUFFER fromDrawable:eaglLayer_];  
    }

    - (void) setupDepthBuffer_{
        glGenRenderbuffers(1, &depthRenderBuffer_);
        glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBuffer_);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, self.frame.size.width, self.frame.size.height);   
    }

    - (void) setupFrameBuffer_{
        glGenFramebuffers(1, &frameBuffer_);
        glBindRenderbuffer(GL_FRAMEBUFFER, frameBuffer_);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderBuffer_);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer_);
    }

    - (void) setupDisplayLink_{
        displayLink_ = [CADisplayLink displayLinkWithTarget:self selector:@selector(render)];
        [displayLink_ addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }

    - (void) setupGL_{
        [self setupLayer_];
        [self setupContext_];
        [self setupDepthBuffer_];
        [self setupRenderBuffer_];
        [self setupFrameBuffer_];
    }

    - (void) tearDownGL_{
        glDeleteRenderbuffers(1, &colorRenderBuffer_);
        glDeleteRenderbuffers(1, &depthRenderBuffer_);
        glDeleteFramebuffers(1, &frameBuffer_);
    }


    #ifndef __IPHONE_5_0
    - (void) dealloc{

        [self tearDownGL_];

        [glContext_ release];
        glContext_ = nil;

        [super dealloc];
    }
    #endif


    #pragma mark - 
    #pragma mark draw

    - (void) render{
        glClearColor(1.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);

        [glContext_ presentRenderbuffer:GL_RENDERBUFFER];
    }

然后我创建一个空项目并执行以下操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    NXGLES2View *glview = [[NXGLES2View alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:glview];

    [glview performSelector:@selector(setupDisplayLink_) withObject:nil afterDelay:2.0];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

应用运行时。 它没有显示glClearColor(1.0,0.0,0.0,1.0); 红色。

我会一遍又一遍地检查这篇文章。我不知道哪里出了问题。 请帮帮我。 再次感谢。

我发现我在setupFrameBuffer_方法中犯了一个错误。

glBindRenderbuffer(GL_FRAMEBUFFER, frameBuffer_);

这应该是:

glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer_); 

暂无
暂无

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

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