2010-11-12 52 views
0

我跟着這個頁面上的教程:爲什麼我沒有得到一個灰色的背景,這個節目

http://iphone-3d-programming.labs.oreilly.com/ch01.html

我下到的地方說,「編譯和構建,你現在應該看到部分純粹的灰色屏幕,華友世界!「但是,當我運行程序時,我只是看到一個黑屏。

這些都是什麼文件看起來像:

HelloArrowAppDelegate.h

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

@interface HelloArrowAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *m_window; 
    GLView* m_view; 
} 

@end 

HelloArrowAppDelegate.mm

#import "HelloArrowAppDelegate.h" 

@implementation HelloArrowAppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch. 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    m_window = [[UIWindow alloc] initWithFrame: screenBounds]; 
    m_view = [[GLView alloc] initWithFrame:screenBounds]; 

    [m_window addSubview: m_view]; 
    [m_window makeKeyAndVisible]; 

    return YES; 
} 

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

@end 

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 { 
    EAGLContext* m_context; 
} 

-(void) drawView; 

@end 

GLView.mm

#import "GLView.h" 

@implementation GLView 

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

    [m_context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 

+ (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; 
     } 

     // OpenGL Initialization 
     GLuint framebuffer, renderbuffer; 
     glGenFramebuffersOES(1, &framebuffer); 
     glGenFramebuffersOES(1, &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 drawView]; 
    } 
    return self; 
} 

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

    [m_context release]; 
    [super dealloc]; 
} 

@end 
+0

不知道這是一個問題,但在視圖實際附加到窗口之前,您正在繪製它似乎很奇怪。如果將以下內容添加到-application:didFinishLaunchingWithOptions:call?的末尾,會發生什麼情況? '[m_view performSelector:@selector(drawView)withObject:nil afterDelay:0.0];' – 2010-11-14 21:36:00

回答

2

initWithFrame不正確。你想要生成一個幀緩衝區和一個渲染緩衝區並將它們連接起來。相反,您會生成兩個幀緩衝區並完全忽略一個。你也應該在你的類中保留對它們的引用(變量'renderbuffer'和'framebuffer'),因爲除非你想泄漏內存,否則你需要稍後刪除它們。

沒有固定的第二個問題,我建議:

- (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; 
       } 

     // these should be in the class so that we can release them later, 
     // this will leak resources 
     GLuint framebuffer, renderbuffer; 

     // generate and bind a framebuffer 
     glGenFramebuffersOES(1, &framebuffer); 
     glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); 

     // generate a colour renderbuffer; this example doesn't seem to want 
     // e.g. a depth buffer, but if it did then you'd generate and add one 
     // of those here also 

     // generate and bind 
     glGenRenderbuffersOES(1, &renderbuffer); 
     glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer); 

     // get storage from the layer 
     [m_context 
     renderbufferStorage:GL_RENDERBUFFER_OES 
     fromDrawable: eaglLayer]; 

     // link to the framebuffer 
     glFramebufferRenderbufferOES(
                                 GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, 
                                 GL_RENDERBUFFER_OES, renderbuffer); 

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

     [self drawView]; 
    } 
    return self; 
} 

認沽幀緩衝和渲染的地方,你可以在相關的時刻再次得到他們,你還應該:

- (void)dealloc { 

    if(renderbuffer) glDeleteRenderbuffersOES(1, &renderbuffer); 
    if(framebuffer) glDeleteFramebuffersOES(1, &framebuffer); 

    if ([EAGLContext currentContext] == m_context) { 
       [EAGLContext setCurrentContext:nil]; 
    } 

    [m_context release]; 
    [super dealloc]; 
} 

我已經根據您提供的代碼對此進行測試。我看到灰色的屏幕。將調用更改爲glClearColor會更改屏幕的顏色,因此GL上下文顯然很有用。

0

內部消除可能很難任何代碼來告訴你笑

你的灰色窗口來自

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

    [m_context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 

檢查,這被稱爲correclty

+0

我檢查並調用它。 – 2010-11-12 17:39:12

0

最可能的原因是,遵循教程時錯過了一些東西。或者他們錯了。無論哪一個最可能:-)

所以下一個階段是調試並找出出了什麼問題。大多數情況下,您可能錯過了添加顯示內容的代碼行。我會首先查看代碼的這一部分,並將其與本教程進行比較。

如果這樣做不起作用,那麼我會把你的代碼作爲備份,然後開始剝離它,直到你擁有絕對少量的代碼。然後在這裏發佈。沒有一些代碼,我們不能告訴你什麼是錯的。

+0

我加了代碼。我沒有注意到任何遺漏。 – 2010-11-13 18:48:44

0

我想嘗試以下內容添加到您的上述代碼:

- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
     CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer; 
     eaglLayer.opaque = YES; 
     eaglLayer.drawableProperties = 
     [NSDictionary dictionaryWithObjectsAndKeys: 
      [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, 
      kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 


... 
} 

在代碼中我用我定義了一個drawableProperties,但你似乎丟失了。

1

我遇到了這個問題,並通過確保在@implementation行後面實現圖層類來修復它。 didFinishLaunchingWithOptions:

@implementation GLView 

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

我通過擺脫線

m_window = [[UIWindow alloc] initWithFrame: screenBounds]; 
應用

拿到了灰色背景文件中的方法HelloArrowAppDelegate.h

相關問題