2010-11-12 69 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