1
UIviewからOpenGL es 2.0ビューの拡張をビルドしたいと考えています。 この記事を流す:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorialiphone opengl es 2.0カスタムビューを作成できませんでしたが、理由がわかりません
最初のステップを終えたとき。すべてがうまくいきません。 私はそれを実行しませんでした。
#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];
}
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
@interface NXGLES2View : UIView
- (void) render;
@end
が、私はこのように空のプロジェクトを作成し、実行します:
- (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); を表示しませんでした。 赤色。
と私は再び何度も記事をチェックします。どこが間違っているのか分かりません。 お願いします。もう一度ありがとうございます。
、あなたはGLKit.httpを使用してオフにはるかに優れている://developer.apple.com/library/ios/#documentation/ GLキット/リファレンス/ GLKit_Collection/Introduction/Introduction.html – logancautrell
ありがとうございます。そう、GLKitは完璧です。しかし、私はどこが間違っているか知りたい。私はiOS上でopengl esに深く欲しいです。 – dinosaur
setupFrameBuffer_ method.glBindRenderbuffer(GL_FRAMEBUFFER、frameBuffer_)に間違いを発見しました。これはglBindFramebuffer(GL_FRAMEBUFFER、frameBuffer_)でなければなりません。 – dinosaur