istereo

view src/ES2Renderer.m @ 34:634c7f7c1f0f

the text blur distance was too much man
author John Tsiombikas <nuclear@mutantstargoat.com>
date Fri, 09 Sep 2011 00:31:54 +0300
parents fe1cb1c567cc
children ff055bff6a15
line source
1 #include <assert.h>
2 #import "ES2Renderer.h"
3 #include "istereo.h"
5 @implementation ES2Renderer
7 // Create an OpenGL ES 2.0 context
8 - (id)init
9 {
10 if ((self = [super init]))
11 {
12 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
14 if (!context || ![EAGLContext setCurrentContext:context])
15 {
16 [self release];
17 return nil;
18 }
20 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
21 glGenFramebuffers(1, &fbo);
22 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
24 glGenRenderbuffers(1, &rbuf_color);
25 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
26 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
28 glGenRenderbuffers(1, &rbuf_depth);
29 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
30 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
31 assert(glGetError() == GL_NO_ERROR);
33 init();
34 }
36 return self;
37 }
39 - (void)render
40 {
41 redraw();
42 // This application only creates a single default framebuffer which is already bound at this point.
43 // This call is redundant, but needed if dealing with multiple framebuffers.
44 //glBindFramebuffer(GL_FRAMEBUFFER, fbo);
45 //glViewport(0, 0, xsz, ysz);
47 // This call is redundant, but needed if dealing with multiple renderbuffers.
48 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
49 [context presentRenderbuffer:GL_RENDERBUFFER];
50 }
52 - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
53 {
54 // Allocate color buffer backing based on the current layer size
55 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
56 [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
57 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &xsz);
58 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &ysz);
60 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
61 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, xsz, ysz);
63 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
64 {
65 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
66 return NO;
67 }
68 assert(glGetError() == GL_NO_ERROR);
70 reshape(xsz, ysz);
72 return YES;
73 }
76 - (void)dealloc
77 {
78 // Tear down GL
79 if (fbo)
80 {
81 glDeleteFramebuffers(1, &fbo);
82 fbo = 0;
83 }
85 if (rbuf_color)
86 {
87 glDeleteRenderbuffers(1, &rbuf_color);
88 rbuf_color = 0;
89 }
91 if (program)
92 {
93 glDeleteProgram(program);
94 program = 0;
95 }
97 // Tear down context
98 if ([EAGLContext currentContext] == context)
99 [EAGLContext setCurrentContext:nil];
101 [context release];
102 context = nil;
104 [super dealloc];
105 }
107 @end