istereo

diff src/ES2Renderer.m @ 0:1bb950d0976b

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 06 Sep 2011 08:07:14 +0300
parents
children 9890940948f7
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ES2Renderer.m	Tue Sep 06 08:07:14 2011 +0300
     1.3 @@ -0,0 +1,93 @@
     1.4 +#import "ES2Renderer.h"
     1.5 +#include "istereo.h"
     1.6 +
     1.7 +@implementation ES2Renderer
     1.8 +
     1.9 +// Create an OpenGL ES 2.0 context
    1.10 +- (id)init
    1.11 +{
    1.12 +    if ((self = [super init]))
    1.13 +    {
    1.14 +        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    1.15 +
    1.16 +        if (!context || ![EAGLContext setCurrentContext:context])
    1.17 +        {
    1.18 +            [self release];
    1.19 +            return nil;
    1.20 +        }
    1.21 +
    1.22 +        // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
    1.23 +        glGenFramebuffers(1, &defaultFramebuffer);
    1.24 +        glGenRenderbuffers(1, &colorRenderbuffer);
    1.25 +        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    1.26 +        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    1.27 +        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
    1.28 +		
    1.29 +		init();
    1.30 +    }
    1.31 +
    1.32 +    return self;
    1.33 +}
    1.34 +
    1.35 +- (void)render
    1.36 +{
    1.37 +	redraw();
    1.38 +    // This application only creates a single default framebuffer which is already bound at this point.
    1.39 +    // This call is redundant, but needed if dealing with multiple framebuffers.
    1.40 +    //glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    1.41 +    //glViewport(0, 0, backingWidth, backingHeight);
    1.42 +
    1.43 +    // This call is redundant, but needed if dealing with multiple renderbuffers.
    1.44 +    //glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    1.45 +    [context presentRenderbuffer:GL_RENDERBUFFER];
    1.46 +}
    1.47 +
    1.48 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
    1.49 +{
    1.50 +    // Allocate color buffer backing based on the current layer size
    1.51 +    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    1.52 +    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
    1.53 +    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
    1.54 +    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
    1.55 +
    1.56 +    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    1.57 +    {
    1.58 +        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
    1.59 +        return NO;
    1.60 +    }
    1.61 +
    1.62 +    return YES;
    1.63 +}
    1.64 +
    1.65 +- (void)dealloc
    1.66 +{
    1.67 +    // Tear down GL
    1.68 +    if (defaultFramebuffer)
    1.69 +    {
    1.70 +        glDeleteFramebuffers(1, &defaultFramebuffer);
    1.71 +        defaultFramebuffer = 0;
    1.72 +    }
    1.73 +
    1.74 +    if (colorRenderbuffer)
    1.75 +    {
    1.76 +        glDeleteRenderbuffers(1, &colorRenderbuffer);
    1.77 +        colorRenderbuffer = 0;
    1.78 +    }
    1.79 +
    1.80 +    if (program)
    1.81 +    {
    1.82 +        glDeleteProgram(program);
    1.83 +        program = 0;
    1.84 +    }
    1.85 +
    1.86 +    // Tear down context
    1.87 +    if ([EAGLContext currentContext] == context)
    1.88 +        [EAGLContext setCurrentContext:nil];
    1.89 +
    1.90 +    [context release];
    1.91 +    context = nil;
    1.92 +
    1.93 +    [super dealloc];
    1.94 +}
    1.95 +
    1.96 +@end