2012-08-12 65 views
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; 
} 
+0

同样的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

回答

0

试试这个one..i得到回答

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     _eaglLayer = (CAEAGLLayer *)self.layer; 
     _eaglLayer.opaque = YES; 

     _context =[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

     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); 
     }  // Initialization code 

     glGenRenderbuffers(1, &_colorRenderBuffer); 
     glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer); 
     [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer]; 

     GLuint framebuffer; 
     glGenFramebuffers(1, &framebuffer); 
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 
     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer); 


     glClearColor(1.0,1,0,1.0); 
     glClear(GL_COLOR_BUFFER_BIT); 
     [_context presentRenderbuffer:GL_RENDERBUFFER]; 
    } 
    return self; 
} 
+(Class)layerClass{ 
    return [CAEAGLLayer class]; 
} 
-(void)dealloc 
{ 
    [_context release]; 
    _context = nil; 
    [super dealloc]; 
}