istereo

annotate src/ES2Renderer.m @ 10:f72f96c93972

bah
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 08:22:18 +0300
parents
children 9890940948f7
rev   line source
nuclear@0 1 #import "ES2Renderer.h"
nuclear@0 2 #include "istereo.h"
nuclear@0 3
nuclear@0 4 @implementation ES2Renderer
nuclear@0 5
nuclear@0 6 // Create an OpenGL ES 2.0 context
nuclear@0 7 - (id)init
nuclear@0 8 {
nuclear@0 9 if ((self = [super init]))
nuclear@0 10 {
nuclear@0 11 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
nuclear@0 12
nuclear@0 13 if (!context || ![EAGLContext setCurrentContext:context])
nuclear@0 14 {
nuclear@0 15 [self release];
nuclear@0 16 return nil;
nuclear@0 17 }
nuclear@0 18
nuclear@0 19 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
nuclear@0 20 glGenFramebuffers(1, &defaultFramebuffer);
nuclear@0 21 glGenRenderbuffers(1, &colorRenderbuffer);
nuclear@0 22 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
nuclear@0 23 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
nuclear@0 24 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
nuclear@0 25
nuclear@0 26 init();
nuclear@0 27 }
nuclear@0 28
nuclear@0 29 return self;
nuclear@0 30 }
nuclear@0 31
nuclear@0 32 - (void)render
nuclear@0 33 {
nuclear@0 34 redraw();
nuclear@0 35 // This application only creates a single default framebuffer which is already bound at this point.
nuclear@0 36 // This call is redundant, but needed if dealing with multiple framebuffers.
nuclear@0 37 //glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
nuclear@0 38 //glViewport(0, 0, backingWidth, backingHeight);
nuclear@0 39
nuclear@0 40 // This call is redundant, but needed if dealing with multiple renderbuffers.
nuclear@0 41 //glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
nuclear@0 42 [context presentRenderbuffer:GL_RENDERBUFFER];
nuclear@0 43 }
nuclear@0 44
nuclear@0 45 - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
nuclear@0 46 {
nuclear@0 47 // Allocate color buffer backing based on the current layer size
nuclear@0 48 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
nuclear@0 49 [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
nuclear@0 50 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
nuclear@0 51 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
nuclear@0 52
nuclear@0 53 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
nuclear@0 54 {
nuclear@0 55 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
nuclear@0 56 return NO;
nuclear@0 57 }
nuclear@0 58
nuclear@0 59 return YES;
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 - (void)dealloc
nuclear@0 63 {
nuclear@0 64 // Tear down GL
nuclear@0 65 if (defaultFramebuffer)
nuclear@0 66 {
nuclear@0 67 glDeleteFramebuffers(1, &defaultFramebuffer);
nuclear@0 68 defaultFramebuffer = 0;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 if (colorRenderbuffer)
nuclear@0 72 {
nuclear@0 73 glDeleteRenderbuffers(1, &colorRenderbuffer);
nuclear@0 74 colorRenderbuffer = 0;
nuclear@0 75 }
nuclear@0 76
nuclear@0 77 if (program)
nuclear@0 78 {
nuclear@0 79 glDeleteProgram(program);
nuclear@0 80 program = 0;
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 // Tear down context
nuclear@0 84 if ([EAGLContext currentContext] == context)
nuclear@0 85 [EAGLContext setCurrentContext:nil];
nuclear@0 86
nuclear@0 87 [context release];
nuclear@0 88 context = nil;
nuclear@0 89
nuclear@0 90 [super dealloc];
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 @end