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