开发者

OpenGL ES iPhone switching from IB to initWithFrame

Hey guys, I am writing a custom view for doing OpenGLES rendering. I got it working when I had a view in interface builder and set the class to my rendering view, but now I switched to creating the view with initWithFrame. (Note I can set the background color of the view and see it I just can't render anything in OpenGL not even the clear color) Everything seems to be getting called开发者_开发问答 and the layer class seems ok too it just for some reason does not work outside of directly creating it with InterfaceBuilder. Any Ideas?

I have this setup code:

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

- (id)initWithCoder:(NSCoder*)coder 
{
    if (self = [super initWithCoder:coder]) 
    {
        [self setupView];
    }
    return self;

}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupView];
    }   
    return self;
}

- (void)setupView
{
    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

    eaglLayer.opaque = YES;
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!_context || ![EAGLContext setCurrentContext:_context])
    {
        NSLog(@"Error could not set context");
        [self release];
    }

    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(renderView:)];
    _displayLink.paused = YES;
    _displayLink.frameInterval = FPS;

    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

    [EAGLContext setCurrentContext:_context];

    [self setup];
}

And then some layout code.

- (void)layoutSubviews 
{
    [EAGLContext setCurrentContext:_context];
    [self destroyBuffers];
    if (![self createBuffers]){
        NSLog(@"Failed to create framebuffer!");
    }

    [self resumeRendering];
}

And some rendering code:

- (void)renderView:(CADisplayLink*)sender
{
    [EAGLContext setCurrentContext:_context];

        glBindFramebuffer(GL_FRAMEBUFFER, _viewFrameBuffer);

        glClearColor(0.5, 0.5, 0.5, 1.0);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     



        GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_COLOR_ATTACHMENT0};
        glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);


        glBindRenderbuffer(GL_RENDERBUFFER, _viewRenderBuffer);


    [_context presentRenderbuffer:GL_RENDERBUFFER];

}

I can't figure out why nothing is rendering as all the same things are getting called. If anyone can help me out that would be awesome!


I maybe speculating too much here but switching from IB to initWithFrame has a step omitted, that is loadView in your viewController, which is where you would instantiate your custom view class instead of a standard view class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜