为什么我在这个程序中没有灰色背景 [英] Why am I not getting a grey background in this program

查看:154
本文介绍了为什么我在这个程序中没有灰色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照此页面上的教程:



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



我到了部分,它说,编译和构建,你现在应该看到一个灰色的屏幕。Hurray!但是,当我运行程序,我只是得到一个黑屏。



这些文件是什么样子:



HelloArrowAppDelegate.h

  #import< UIKit / UIKit.h> 
#importGLView.h

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

@end

HelloArrowAppDelegate.mm

  #importHelloArrowAppDelegate.h

@implementation HelloArrowAppDelegate

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

//应用程序启动后覆盖自定义点。
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

  #importGLView.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初始化
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


解决方案>

initWithFrame不正确。你想生成一个framebuffer和一个renderbuffer并链接两者。相反,您生成两个帧缓冲区并完全忽略一个。你还应该保留对它们的引用(变量'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;
}

//这些应该在类中,以便我们以后可以释放它们,
//这将泄漏资源
GLuint framebuffer,renderbuffer;

//生成和绑定帧缓冲区
glGenFramebuffersOES(1,& framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES,framebuffer);

//生成一个颜色renderbuffer;这个例子似乎不想要
//例如一个深度缓冲区,但如果它那么你会生成并添加一个
//这里的

//生成并绑定
glGenRenderbuffersOES(1,& renderbuffer );
glBindRenderbufferOES(GL_RENDERBUFFER_OES,renderbuffer);

//从图层获取存储
[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;
}

将framebuffer和renderbuffer放到你可以在相关时刻再次访问的地方,您还应该:

   - (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上下文会工作。


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

解决方案

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.

这篇关于为什么我在这个程序中没有灰色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆