2012-05-04 6 views
0

Привет, я очень новичок в ios и OpenGL. Я пытаюсь следовать инструкциям из книги iPhone 3d программирования. Я просто хочу отобразить серый экран из OpenGL.Окно становится черным в OpenGL

Я действительно не знаю, что я сделал неправильно, и мне нужно понять, почему я не получаю свое окно OpenGL прямо перед тем, как я начну предлагать.

Если я могу помочь дальше просто оставить комментарий :)

GLView.h:

#import <UIKit/UIKit.h> 
#import <OpenGLES/EAGL.h> 
#import <QuartzCore/QuartzCore.h> 
#import <OpenGLES/ES1/gl.h> 
#import <OpenGLES/ES1/glext.h> 

@interface GLView : UIView { 
    // Protected fields 
    EAGLContext* m_context; 
} 

// public fields 
-(void)drawWiew; 

@end 

GLView.mm:

#import "GLView.h" 

@implementation GLView 

+(Class) layerClass { 
    return [CAEAGLLayer class]; 
} 

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

     m_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 
     if (!m_context || ![EAGLContext setCurrentContext:m_context]) { 
      [self release]; 
      return nil; 
     } 

     // Initialization code 
     GLuint framebuffer, renderbuffer; 
     glGenFramebuffersOES(1, &framebuffer); 
     glGenRenderbuffersOES(1, &renderbuffer); 

     glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); 
     glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer); 

     [m_context 
     renderbufferStorage:GL_RENDERBUFFER_OES 
     fromDrawable:eaglLayer]; 

     glFramebufferRenderbufferOES(
            GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, 
            GL_RENDERBUFFER_OES, renderbuffer); 

     glViewport(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));   

     [self drawWiew]; 
    } 
    return self; 
} 

-(void)drawWiew { 
    glClearColor(0.5f, 0.5f, 0.5f, 1); 
    glClear(GL_COLOR_BUFFER_BIT); 

    [m_context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

    NSLog(@"drawView called"); 
} 

-(void)dealloc { 
    if ([EAGLContext currentContext] == m_context) 
     [EAGLContext setCurrentContext:nil]; 

    [m_context release]; 
    [super dealloc]; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

HellowArrowAppDelegate.h:

#import <UIKit/UIKit.h> 
#import "GLView.h" 

@interface HelloArrowAppDelegate : UIResponder <UIApplicationDelegate> { 
    UIWindow* window; 
    GLView* view; 
} 

@property (strong, nonatomic) UIWindow *window; 

@end 

HellowArrowAppD elegate.mm:

#import "HelloArrowAppDelegate.h" 

@implementation HelloArrowAppDelegate 

@synthesize window = _window; 

- (void)dealloc 
{ 
    [_window release]; 
    [super dealloc]; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    _window = [[UIWindow alloc] initWithFrame:screenBounds]; 
    view = [[GLView alloc] initWithFrame:screenBounds]; 

    [_window addSubview:view]; 
    [_window makeKeyWindow]; 

// self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
// // Override point for customization after application launch. 
// self.window.backgroundColor = [UIColor whiteColor]; 
// [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    /* 
    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    */ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    */ 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    /* 
    Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
    */ 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    /* 
    Called when the application is about to terminate. 
    Save data if appropriate. 
    See also applicationDidEnterBackground:. 
    */ 
} 

@end 
+0

Я вставил ваш код, и он отлично работает .. Каков ваш результат? –

ответ

0

OpenGL экраны могут чернеть в зависимости от версии используемой ОС, а также размера окна экрана втягивается. Например, код, который отлично работает в 4.0, 4.1 и 4.3, может отображаться черным в 4.2. Или, если вы удваиваете размер окна OpenGL, он работает. Иногда прошивка мешает - попробуйте возиться с использованием более новой версии ОС и посмотрите, работает ли она лучше.

Смежные вопросы