istereo

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