Why am I not getting a grey background in this program

本秂侑毒 提交于 2019-12-10 12:15:23

问题


I followed the tutorial on this page:

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

I got down to the part where it says, "Compile and build and you should now see a solid gray screen. Hurray!" However, when I ran the program, I just get a black screen.

These are what the files look like:

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

回答1:


The initWithFrame is incorrect. You want to generate a framebuffer and a renderbuffer and link the two. Instead you generate two framebuffers and completely ignore one. You should also keep the references to them (the variables 'renderbuffer' and 'framebuffer') in your class, as you'll need to delete them later unless you want to leak memory.

Without fixing the second issue, I recommend:

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

Put framebuffer and renderbuffer somewhere you can get to them again at the relevant moment and you should also:

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

I've tested this against the code you provide. I get the grey screen. Changing the call to glClearColor changes the colour of the screen, so clearly the GL context is working.




回答2:


I ran into this issue and fixed it in my case by making sure the layer class was implemented right after the @implementation line.

@implementation GLView

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



回答3:


I got the gray background by getting rid of the line

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

in application:didFinishLaunchingWithOptions: method in the file HelloArrowAppDelegate.h




回答4:


The most likely cause is that you missed something when following the tutorial. Or they got it wrong. Whichever is most likely :-)

So the next stage is to debug and figure out what went wrong. Mostly likely you have missed a line of code which adds stuff to the display. I'd look in that section of your code first and compare it to the tutorial.

If that doesn't work then I'd take a copy of you code as a back up, then start stripping stuff out of it until you have the absolute minimal amount of code. Then post that here. Without some code we cannot tell you what is wrong.




回答5:


whithout any code it might be difficult to tell you lol

your gray window comes from

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

    [m_context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

check that this is called correclty




回答6:


I'd try add the following to your above code:

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


...
}

In code I use I define a drawableProperties but you seem to be missing it.



来源:https://stackoverflow.com/questions/4161708/why-am-i-not-getting-a-grey-background-in-this-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!