istereo

annotate src/ES2Renderer.m @ 43:73813c1176de

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