iPhone opengl es 2.0自定义构建gl视图失败,但不知道为什么 [英] iphone opengl es 2.0 custom build a gl view failed,but did not know why

查看:140
本文介绍了iPhone opengl es 2.0自定义构建gl视图失败,但不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从UIview扩展构建一个opengl es 2.0视图. 并发送本文: http://www.raywenderlich. com/3664/opengl-es-2-0-for-iphone-tutorial

I want to build a opengl es 2.0 view extend from UIview. And flow this article:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial

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

when I finished first step. everything runs wrong. It did not run as I throught.

这是我的代码:

#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];
    }

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

then I create empty project and do as this:

- (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); 红色.

When app run. it did not displayed a glClearColor(1.0, 0.0, 0.0, 1.0); red color.

我要一遍又一遍地检查这篇文章.我不知道哪里错了. 请帮帮我. 再次感谢.

and I'v check the article again and again.I can not figure out where is wrong. help me please. thanks again.

推荐答案

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

I found I made a mistake in setupFrameBuffer_ method.

glBindRenderbuffer(GL_FRAMEBUFFER, frameBuffer_);

这应该是:

glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer_); 

这篇关于iPhone opengl es 2.0自定义构建gl视图失败,但不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆