istereo

view src/ES2Renderer.m @ 39:ff055bff6a15

copyright statements and stuff
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 11 Sep 2011 09:03:18 +0300
parents 8dd271942543
children
line source
1 /*
2 Stereoscopic tunnel for iOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 /* XXX this file is mostly generated from Xcode and therefore sucks ass */
22 #include <assert.h>
23 #import "ES2Renderer.h"
24 #include "istereo.h"
26 @implementation ES2Renderer
28 // Create an OpenGL ES 2.0 context
29 - (id)init
30 {
31 if ((self = [super init]))
32 {
33 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
35 if (!context || ![EAGLContext setCurrentContext:context])
36 {
37 [self release];
38 return nil;
39 }
41 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
42 glGenFramebuffers(1, &fbo);
43 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
45 glGenRenderbuffers(1, &rbuf_color);
46 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
47 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
49 glGenRenderbuffers(1, &rbuf_depth);
50 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
51 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
52 assert(glGetError() == GL_NO_ERROR);
54 init();
55 }
57 return self;
58 }
60 - (void)render
61 {
62 redraw();
63 // This application only creates a single default framebuffer which is already bound at this point.
64 // This call is redundant, but needed if dealing with multiple framebuffers.
65 //glBindFramebuffer(GL_FRAMEBUFFER, fbo);
66 //glViewport(0, 0, xsz, ysz);
68 // This call is redundant, but needed if dealing with multiple renderbuffers.
69 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
70 [context presentRenderbuffer:GL_RENDERBUFFER];
71 }
73 - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
74 {
75 // Allocate color buffer backing based on the current layer size
76 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
77 [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
78 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &xsz);
79 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &ysz);
81 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
82 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, xsz, ysz);
84 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
85 {
86 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
87 return NO;
88 }
89 assert(glGetError() == GL_NO_ERROR);
91 reshape(xsz, ysz);
93 return YES;
94 }
97 - (void)dealloc
98 {
99 // Tear down GL
100 if (fbo)
101 {
102 glDeleteFramebuffers(1, &fbo);
103 fbo = 0;
104 }
106 if (rbuf_color)
107 {
108 glDeleteRenderbuffers(1, &rbuf_color);
109 rbuf_color = 0;
110 }
112 if (program)
113 {
114 glDeleteProgram(program);
115 program = 0;
116 }
118 // Tear down context
119 if ([EAGLContext currentContext] == context)
120 [EAGLContext setCurrentContext:nil];
122 [context release];
123 context = nil;
125 [super dealloc];
126 }
128 @end