istereo

view 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 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 return YES;
60 }
62 - (void)dealloc
63 {
64 // Tear down GL
65 if (defaultFramebuffer)
66 {
67 glDeleteFramebuffers(1, &defaultFramebuffer);
68 defaultFramebuffer = 0;
69 }
71 if (colorRenderbuffer)
72 {
73 glDeleteRenderbuffers(1, &colorRenderbuffer);
74 colorRenderbuffer = 0;
75 }
77 if (program)
78 {
79 glDeleteProgram(program);
80 program = 0;
81 }
83 // Tear down context
84 if ([EAGLContext currentContext] == context)
85 [EAGLContext setCurrentContext:nil];
87 [context release];
88 context = nil;
90 [super dealloc];
91 }
93 @end