2012-05-04 122 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的屏幕可以根据所使用的OS,以及在屏幕窗口的大小的版本变成黑色被吸入。例如,在4.0,4.1和4.3上运行良好的代码可以在4.2中显示为黑色。或者,如果你将OpenGL窗口的大小加倍,它就可以工作。有时固件会阻碍 - 尝试使用更新版本的操作系统,并查看它是否更好。