istereo
diff src/ES1Renderer.m @ 0:1bb950d0976b
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 06 Sep 2011 08:07:14 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ES1Renderer.m Tue Sep 06 08:07:14 2011 +0300 1.3 @@ -0,0 +1,131 @@ 1.4 +// 1.5 +// ES1Renderer.m 1.6 +// istereo 1.7 +// 1.8 +// Created by nuclear on 9/6/11. 1.9 +// Copyright __MyCompanyName__ 2011. All rights reserved. 1.10 +// 1.11 + 1.12 +#import "ES1Renderer.h" 1.13 + 1.14 +@implementation ES1Renderer 1.15 + 1.16 +// Create an OpenGL ES 1.1 context 1.17 +- (id)init 1.18 +{ 1.19 + if ((self = [super init])) 1.20 + { 1.21 + context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 1.22 + 1.23 + if (!context || ![EAGLContext setCurrentContext:context]) 1.24 + { 1.25 + [self release]; 1.26 + return nil; 1.27 + } 1.28 + 1.29 + // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer 1.30 + glGenFramebuffersOES(1, &defaultFramebuffer); 1.31 + glGenRenderbuffersOES(1, &colorRenderbuffer); 1.32 + glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer); 1.33 + glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 1.34 + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer); 1.35 + } 1.36 + 1.37 + return self; 1.38 +} 1.39 + 1.40 +- (void)render 1.41 +{ 1.42 + // Replace the implementation of this method to do your own custom drawing 1.43 + 1.44 + static const GLfloat squareVertices[] = { 1.45 + -0.5f, -0.33f, 1.46 + 0.5f, -0.33f, 1.47 + -0.5f, 0.33f, 1.48 + 0.5f, 0.33f, 1.49 + }; 1.50 + 1.51 + static const GLubyte squareColors[] = { 1.52 + 255, 255, 0, 255, 1.53 + 0, 255, 255, 255, 1.54 + 0, 0, 0, 0, 1.55 + 255, 0, 255, 255, 1.56 + }; 1.57 + 1.58 + static float transY = 0.0f; 1.59 + 1.60 + // This application only creates a single context which is already set current at this point. 1.61 + // This call is redundant, but needed if dealing with multiple contexts. 1.62 + [EAGLContext setCurrentContext:context]; 1.63 + 1.64 + // This application only creates a single default framebuffer which is already bound at this point. 1.65 + // This call is redundant, but needed if dealing with multiple framebuffers. 1.66 + glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer); 1.67 + glViewport(0, 0, backingWidth, backingHeight); 1.68 + 1.69 + glMatrixMode(GL_PROJECTION); 1.70 + glLoadIdentity(); 1.71 + glMatrixMode(GL_MODELVIEW); 1.72 + glLoadIdentity(); 1.73 + glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f); 1.74 + transY += 0.075f; 1.75 + 1.76 + glClearColor(0.5f, 0.5f, 0.5f, 1.0f); 1.77 + glClear(GL_COLOR_BUFFER_BIT); 1.78 + 1.79 + glVertexPointer(2, GL_FLOAT, 0, squareVertices); 1.80 + glEnableClientState(GL_VERTEX_ARRAY); 1.81 + glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors); 1.82 + glEnableClientState(GL_COLOR_ARRAY); 1.83 + 1.84 + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 1.85 + 1.86 + // This application only creates a single color renderbuffer which is already bound at this point. 1.87 + // This call is redundant, but needed if dealing with multiple renderbuffers. 1.88 + glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 1.89 + [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 1.90 +} 1.91 + 1.92 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer 1.93 +{ 1.94 + // Allocate color buffer backing based on the current layer size 1.95 + glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 1.96 + [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer]; 1.97 + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); 1.98 + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); 1.99 + 1.100 + if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) 1.101 + { 1.102 + NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); 1.103 + return NO; 1.104 + } 1.105 + 1.106 + return YES; 1.107 +} 1.108 + 1.109 +- (void)dealloc 1.110 +{ 1.111 + // Tear down GL 1.112 + if (defaultFramebuffer) 1.113 + { 1.114 + glDeleteFramebuffersOES(1, &defaultFramebuffer); 1.115 + defaultFramebuffer = 0; 1.116 + } 1.117 + 1.118 + if (colorRenderbuffer) 1.119 + { 1.120 + glDeleteRenderbuffersOES(1, &colorRenderbuffer); 1.121 + colorRenderbuffer = 0; 1.122 + } 1.123 + 1.124 + // Tear down context 1.125 + if ([EAGLContext currentContext] == context) 1.126 + [EAGLContext setCurrentContext:nil]; 1.127 + 1.128 + [context release]; 1.129 + context = nil; 1.130 + 1.131 + [super dealloc]; 1.132 +} 1.133 + 1.134 +@end