istereo

annotate src/ES2Renderer.m @ 13:fe1cb1c567cc

at long last
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 07 Sep 2011 08:33:55 +0300
parents 9890940948f7
children 8dd271942543
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@12 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@13 59 reshape(backingWidth, backingHeight);
nuclear@12 60
nuclear@0 61 return YES;
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 - (void)dealloc
nuclear@0 65 {
nuclear@0 66 // Tear down GL
nuclear@0 67 if (defaultFramebuffer)
nuclear@0 68 {
nuclear@0 69 glDeleteFramebuffers(1, &defaultFramebuffer);
nuclear@0 70 defaultFramebuffer = 0;
nuclear@0 71 }
nuclear@0 72
nuclear@0 73 if (colorRenderbuffer)
nuclear@0 74 {
nuclear@0 75 glDeleteRenderbuffers(1, &colorRenderbuffer);
nuclear@0 76 colorRenderbuffer = 0;
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 if (program)
nuclear@0 80 {
nuclear@0 81 glDeleteProgram(program);
nuclear@0 82 program = 0;
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 // Tear down context
nuclear@0 86 if ([EAGLContext currentContext] == context)
nuclear@0 87 [EAGLContext setCurrentContext:nil];
nuclear@0 88
nuclear@0 89 [context release];
nuclear@0 90 context = nil;
nuclear@0 91
nuclear@0 92 [super dealloc];
nuclear@0 93 }
nuclear@0 94
nuclear@0 95 @end