0
我試圖用OpenGLES 2.0顯示綠色屏幕,但是當我運行它時,它只是一個黑色屏幕。我不確定是否正確設置了nib文件,但是當我在appdelgate中添加glView的子視圖時,它將從默認的白色屏幕切換到黑色屏幕,因此OpenGLView可能會出現問題。OpenGLES objective-c屏幕沒有彈出
OpenGLView.m
#import "OpenGLView.h"
@interface openGLView()
@end
@implementation openGLView
+(Class)layerClass {
return [CAEAGLLayer class];
}
-(void)setupLayer {
_eaglLayer = (CAEAGLLayer*) self.layer;
_eaglLayer.opaque = YES;
}
-(void)setupContext{
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2;
_context = [[EAGLContext alloc] initWithAPI:api];
if (!_context) {
NSLog(@"failed to initialize openGLES 2.0 context");
exit(1);
}
if (![EAGLContext setCurrentContext:_context]) {
NSLog(@"Failed to set current OpenGL Context");
exit(1);
}
}
-(void)setupRenderBuffer {
glGenRenderbuffers(1, &_colorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
}
-(void)setupFrameBuffer {
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
}
-(void)render {
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupLayer];
[self setupContext];
[self setupRenderBuffer];
[self setupFrameBuffer];
[self render];
}
return self;
}
-(void) dealloc {
[_context release];
_context = Nil;
[super dealloc];
}
@end
AppDelegate.m
@implementation AppDelegate
@synthesize glView = _glview;
- (void)dealloc
{
[_glview release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.glView = [[[openGLView alloc] initWithFrame:screenBounds] autorelease];
[self.window addSubview:_glview];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
return YES;
}
同樣的bug在下面討論 http://stackoverflow.com/questions/7924435/iphone-opengl-es-2-0-custom-build-a-gl-view-failedbut-did-not-知道,爲什麼?RQ = 1 – dhaya 2013-04-20 11:31:44