istereo

view 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 source
1 //
2 // ES1Renderer.m
3 // istereo
4 //
5 // Created by nuclear on 9/6/11.
6 // Copyright __MyCompanyName__ 2011. All rights reserved.
7 //
9 #import "ES1Renderer.h"
11 @implementation ES1Renderer
13 // Create an OpenGL ES 1.1 context
14 - (id)init
15 {
16 if ((self = [super init]))
17 {
18 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
20 if (!context || ![EAGLContext setCurrentContext:context])
21 {
22 [self release];
23 return nil;
24 }
26 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
27 glGenFramebuffersOES(1, &defaultFramebuffer);
28 glGenRenderbuffersOES(1, &colorRenderbuffer);
29 glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
30 glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
31 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
32 }
34 return self;
35 }
37 - (void)render
38 {
39 // Replace the implementation of this method to do your own custom drawing
41 static const GLfloat squareVertices[] = {
42 -0.5f, -0.33f,
43 0.5f, -0.33f,
44 -0.5f, 0.33f,
45 0.5f, 0.33f,
46 };
48 static const GLubyte squareColors[] = {
49 255, 255, 0, 255,
50 0, 255, 255, 255,
51 0, 0, 0, 0,
52 255, 0, 255, 255,
53 };
55 static float transY = 0.0f;
57 // This application only creates a single context which is already set current at this point.
58 // This call is redundant, but needed if dealing with multiple contexts.
59 [EAGLContext setCurrentContext:context];
61 // This application only creates a single default framebuffer which is already bound at this point.
62 // This call is redundant, but needed if dealing with multiple framebuffers.
63 glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
64 glViewport(0, 0, backingWidth, backingHeight);
66 glMatrixMode(GL_PROJECTION);
67 glLoadIdentity();
68 glMatrixMode(GL_MODELVIEW);
69 glLoadIdentity();
70 glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
71 transY += 0.075f;
73 glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
74 glClear(GL_COLOR_BUFFER_BIT);
76 glVertexPointer(2, GL_FLOAT, 0, squareVertices);
77 glEnableClientState(GL_VERTEX_ARRAY);
78 glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
79 glEnableClientState(GL_COLOR_ARRAY);
81 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
83 // This application only creates a single color renderbuffer which is already bound at this point.
84 // This call is redundant, but needed if dealing with multiple renderbuffers.
85 glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
86 [context presentRenderbuffer:GL_RENDERBUFFER_OES];
87 }
89 - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
90 {
91 // Allocate color buffer backing based on the current layer size
92 glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
93 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
94 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
95 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
97 if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
98 {
99 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
100 return NO;
101 }
103 return YES;
104 }
106 - (void)dealloc
107 {
108 // Tear down GL
109 if (defaultFramebuffer)
110 {
111 glDeleteFramebuffersOES(1, &defaultFramebuffer);
112 defaultFramebuffer = 0;
113 }
115 if (colorRenderbuffer)
116 {
117 glDeleteRenderbuffersOES(1, &colorRenderbuffer);
118 colorRenderbuffer = 0;
119 }
121 // Tear down context
122 if ([EAGLContext currentContext] == context)
123 [EAGLContext setCurrentContext:nil];
125 [context release];
126 context = nil;
128 [super dealloc];
129 }
131 @end