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.
Here is my code:
#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;
}
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.
I found I made a mistake in setupFrameBuffer_ method.
this should be :