rev |
line source |
nuclear@30
|
1 #include <assert.h>
|
nuclear@0
|
2 #import "ES2Renderer.h"
|
nuclear@0
|
3 #include "istereo.h"
|
nuclear@0
|
4
|
nuclear@0
|
5 @implementation ES2Renderer
|
nuclear@0
|
6
|
nuclear@0
|
7 // Create an OpenGL ES 2.0 context
|
nuclear@0
|
8 - (id)init
|
nuclear@0
|
9 {
|
nuclear@0
|
10 if ((self = [super init]))
|
nuclear@0
|
11 {
|
nuclear@0
|
12 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
nuclear@0
|
13
|
nuclear@0
|
14 if (!context || ![EAGLContext setCurrentContext:context])
|
nuclear@0
|
15 {
|
nuclear@0
|
16 [self release];
|
nuclear@0
|
17 return nil;
|
nuclear@0
|
18 }
|
nuclear@0
|
19
|
nuclear@0
|
20 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
|
nuclear@30
|
21 glGenFramebuffers(1, &fbo);
|
nuclear@30
|
22 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@30
|
23
|
nuclear@30
|
24 glGenRenderbuffers(1, &rbuf_color);
|
nuclear@30
|
25 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
|
nuclear@30
|
26 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
|
nuclear@30
|
27
|
nuclear@30
|
28 glGenRenderbuffers(1, &rbuf_depth);
|
nuclear@30
|
29 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@30
|
30 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
|
nuclear@30
|
31 assert(glGetError() == GL_NO_ERROR);
|
nuclear@12
|
32
|
nuclear@0
|
33 init();
|
nuclear@0
|
34 }
|
nuclear@0
|
35
|
nuclear@0
|
36 return self;
|
nuclear@0
|
37 }
|
nuclear@0
|
38
|
nuclear@0
|
39 - (void)render
|
nuclear@0
|
40 {
|
nuclear@0
|
41 redraw();
|
nuclear@0
|
42 // This application only creates a single default framebuffer which is already bound at this point.
|
nuclear@0
|
43 // This call is redundant, but needed if dealing with multiple framebuffers.
|
nuclear@30
|
44 //glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@30
|
45 //glViewport(0, 0, xsz, ysz);
|
nuclear@0
|
46
|
nuclear@0
|
47 // This call is redundant, but needed if dealing with multiple renderbuffers.
|
nuclear@30
|
48 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
|
nuclear@0
|
49 [context presentRenderbuffer:GL_RENDERBUFFER];
|
nuclear@0
|
50 }
|
nuclear@0
|
51
|
nuclear@0
|
52 - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
|
nuclear@0
|
53 {
|
nuclear@0
|
54 // Allocate color buffer backing based on the current layer size
|
nuclear@30
|
55 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
|
nuclear@0
|
56 [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
|
nuclear@30
|
57 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &xsz);
|
nuclear@30
|
58 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &ysz);
|
nuclear@30
|
59
|
nuclear@30
|
60 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@30
|
61 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, xsz, ysz);
|
nuclear@0
|
62
|
nuclear@0
|
63 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
nuclear@0
|
64 {
|
nuclear@0
|
65 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
|
nuclear@0
|
66 return NO;
|
nuclear@0
|
67 }
|
nuclear@30
|
68 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
69
|
nuclear@30
|
70 reshape(xsz, ysz);
|
nuclear@12
|
71
|
nuclear@0
|
72 return YES;
|
nuclear@0
|
73 }
|
nuclear@0
|
74
|
nuclear@30
|
75
|
nuclear@0
|
76 - (void)dealloc
|
nuclear@0
|
77 {
|
nuclear@0
|
78 // Tear down GL
|
nuclear@30
|
79 if (fbo)
|
nuclear@0
|
80 {
|
nuclear@30
|
81 glDeleteFramebuffers(1, &fbo);
|
nuclear@30
|
82 fbo = 0;
|
nuclear@0
|
83 }
|
nuclear@0
|
84
|
nuclear@30
|
85 if (rbuf_color)
|
nuclear@0
|
86 {
|
nuclear@30
|
87 glDeleteRenderbuffers(1, &rbuf_color);
|
nuclear@30
|
88 rbuf_color = 0;
|
nuclear@0
|
89 }
|
nuclear@0
|
90
|
nuclear@0
|
91 if (program)
|
nuclear@0
|
92 {
|
nuclear@0
|
93 glDeleteProgram(program);
|
nuclear@0
|
94 program = 0;
|
nuclear@0
|
95 }
|
nuclear@0
|
96
|
nuclear@0
|
97 // Tear down context
|
nuclear@0
|
98 if ([EAGLContext currentContext] == context)
|
nuclear@0
|
99 [EAGLContext setCurrentContext:nil];
|
nuclear@0
|
100
|
nuclear@0
|
101 [context release];
|
nuclear@0
|
102 context = nil;
|
nuclear@0
|
103
|
nuclear@0
|
104 [super dealloc];
|
nuclear@0
|
105 }
|
nuclear@0
|
106
|
nuclear@0
|
107 @end
|