This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AR_EAGLView.mm
executable file
·473 lines (382 loc) · 15 KB
/
AR_EAGLView.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*==============================================================================
Copyright (c) 2010-2013 QUALCOMM Austria Research Center GmbH.
All Rights Reserved.
Qualcomm Confidential and Proprietary
==============================================================================*/
#import <QuartzCore/QuartzCore.h>
#import "AR_EAGLView.h"
#import "Texture.h"
#import <QCAR/QCAR.h>
#import <QCAR/VideoBackgroundConfig.h>
#import "QCARutils.h"
#ifndef USE_OPENGL1
// *** Note, OpenGL ES 1.x is supported only in the ImageTargets sample ***
#import "ShaderUtils.h"
#define MAKESTRING(x) #x
#import "Shader.fsh"
#import "Shader.vsh"
#endif
@implementation Object3D
@synthesize numVertices;
@synthesize vertices;
@synthesize normals;
@synthesize texCoords;
@synthesize numIndices;
@synthesize indices;
@synthesize texture;
@end
@interface AR_EAGLView (PrivateMethods)
- (void)setFramebuffer;
- (BOOL)presentFramebuffer;
- (void)createFramebuffer;
- (void)deleteFramebuffer;
- (int)loadTextures;
- (void)initRendering;
@end
@implementation AR_EAGLView
@synthesize textureList;
// You must implement this method
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
// test to see if the screen has hi-res mode
- (BOOL) isRetinaEnabled
{
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]
&&
([UIScreen mainScreen].scale == 2.0));
}
// use to allow this view to access loaded textures
- (void) useTextures:(NSMutableArray *)theTextures
{
textures = theTextures;
}
#pragma mark ---- view lifecycle ---
/////////////////////////////////////////////////////////////////
//
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
qUtils = [QCARutils getInstance];
objects3D = [[NSMutableArray alloc] initWithCapacity:2];
textureList = [[NSMutableArray alloc] initWithCapacity:2];
// switch on hi-res mode if available
if ([self isRetinaEnabled])
{
self.contentScaleFactor = 2.0f;
qUtils.contentScalingFactor = self.contentScaleFactor;
}
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
#ifdef USE_OPENGL1
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
qUtils.QCARFlags = QCAR::GL_11;
#else
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
qUtils.QCARFlags = QCAR::GL_20;
#endif
NSLog(@"QCAR OpenGL flag: %d", qUtils.QCARFlags);
if (!context) {
NSLog(@"Failed to create ES context");
}
}
return self;
}
- (void)dealloc
{
[self deleteFramebuffer];
// Tear down context
if ([EAGLContext currentContext] == context) {
[EAGLContext setCurrentContext:nil];
}
[context release];
[objects3D release];
[textureList release];
[super dealloc];
}
- (void)finishOpenGLESCommands
{
// Called in response to applicationWillResignActive. The ARViewController
// stops the render loop, and we now make sure all OpenGL ES commands
// complete before we (potentially) go into the background
if (context) {
[EAGLContext setCurrentContext:context];
glFinish();
}
}
- (void)freeOpenGLESResources
{
// Called in response to applicationDidEnterBackground. Free easily
// recreated OpenGL ES resources
[self deleteFramebuffer];
glFinish();
}
/////////////////////////////////////////////////////////////////
//
- (void)layoutSubviews
{
NSLog(@"EAGLView: layoutSubviews");
// The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
[self deleteFramebuffer];
// Initialisation done once, or once per screen size change
[self initRendering];
}
#pragma mark --- OpenGL essentials ---
/////////////////////////////////////////////////////////////////
//
- (void)createFramebuffer
{
#ifdef USE_OPENGL1
if (context && !defaultFramebuffer) {
[EAGLContext setCurrentContext:context];
// Create default framebuffer object
glGenFramebuffersOES(1, &defaultFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
// Create colour renderbuffer and allocate backing store
glGenRenderbuffersOES(1, &colorRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
// Allocate the renderbuffer's storage (shared with the drawable object)
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &framebufferWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &framebufferHeight);
// Create the depth render buffer and allocate storage
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, framebufferWidth, framebufferHeight);
// Attach colour and depth render buffers to the frame buffer
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
// Leave the colour render buffer bound so future rendering operations will act on it
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
}
#else
if (context && !defaultFramebuffer) {
[EAGLContext setCurrentContext:context];
// Create default framebuffer object
glGenFramebuffers(1, &defaultFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
// Create colour render buffer and allocate backing store
glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
// Allocate the renderbuffer's storage (shared with the drawable object)
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
// Create the depth render buffer and allocate storage
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, framebufferWidth, framebufferHeight);
// Attach colour and depth render buffers to the frame buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
// Leave the colour render buffer bound so future rendering operations will act on it
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
}
#endif
}
/////////////////////////////////////////////////////////////////
//
- (void)deleteFramebuffer
{
if (context) {
[EAGLContext setCurrentContext:context];
#ifdef USE_OPENGL1
if (defaultFramebuffer) {
glDeleteFramebuffersOES(1, &defaultFramebuffer);
defaultFramebuffer = 0;
}
if (colorRenderbuffer) {
glDeleteRenderbuffersOES(1, &colorRenderbuffer);
colorRenderbuffer = 0;
}
if (depthRenderbuffer) {
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
#else
if (defaultFramebuffer) {
glDeleteFramebuffers(1, &defaultFramebuffer);
defaultFramebuffer = 0;
}
if (colorRenderbuffer) {
glDeleteRenderbuffers(1, &colorRenderbuffer);
colorRenderbuffer = 0;
}
if (depthRenderbuffer) {
glDeleteRenderbuffers(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
#endif
}
}
/////////////////////////////////////////////////////////////////
//
- (void)setFramebuffer
{
if (context) {
[EAGLContext setCurrentContext:context];
if (!defaultFramebuffer) {
// Perform on the main thread to ensure safe memory allocation for
// the shared buffer. Block until the operation is complete to
// prevent simultaneous access to the OpenGL context
[self performSelectorOnMainThread:@selector(createFramebuffer) withObject:self waitUntilDone:YES];
}
#ifdef USE_OPENGL1
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
#else
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
#endif
}
}
/////////////////////////////////////////////////////////////////
//
- (BOOL)presentFramebuffer
{
BOOL success = FALSE;
if (context) {
[EAGLContext setCurrentContext:context];
#ifdef USE_OPENGL1
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
#else
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
#endif
success = [context presentRenderbuffer:GL_RENDERBUFFER];
}
return success;
}
/////////////////////////////////////////////////////////////////
// TEMPLATE - this is app specific and
// expected to be overridden in EAGLView.mm
- (void) setup3dObjects
{
for (int i=0; i < [textures count]; i++)
{
Object3D *obj3D = [[Object3D alloc] init];
obj3D.numVertices = 0;
obj3D.vertices = nil;
obj3D.normals = nil;
obj3D.texCoords = nil;
obj3D.numIndices = 0;
obj3D.indices = nil;
obj3D.texture = [textures objectAtIndex:i];
[objects3D addObject:obj3D];
[obj3D release];
}
}
////////////////////////////////////////////////////////////////////////////////
// Initialise OpenGL 2.x shaders
- (void)initShaders
{
#ifndef USE_OPENGL1
// OpenGL 2 initialisation
shaderProgramID = ShaderUtils::createProgramFromBuffer(vertexShader, fragmentShader);
if (0 < shaderProgramID) {
vertexHandle = glGetAttribLocation(shaderProgramID, "vertexPosition");
normalHandle = glGetAttribLocation(shaderProgramID, "vertexNormal");
textureCoordHandle = glGetAttribLocation(shaderProgramID, "vertexTexCoord");
mvpMatrixHandle = glGetUniformLocation(shaderProgramID, "modelViewProjectionMatrix");
texSampler2DHandle = glGetUniformLocation(shaderProgramID,"texSampler2D");
}
else {
NSLog(@"Could not initialise augmentation shader");
}
#endif
}
////////////////////////////////////////////////////////////////////////////////
// Initialise OpenGL rendering
- (void)initRendering
{
if (renderingInited)
return;
// Define the clear colour
glClearColor(0.0f, 0.0f, 0.0f, QCAR::requiresAlpha() ? 0.0f : 1.0f);
// Generate the OpenGL texture objects
for (int i = 0; i < [textures count]; ++i) {
GLuint nID;
Texture* texture = [textures objectAtIndex:i];
glGenTextures(1, &nID);
[texture setTextureID: nID];
glBindTexture(GL_TEXTURE_2D, nID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [texture width], [texture height], 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)[texture pngData]);
}
// set up objects using the above textures.
[self setup3dObjects];
if (QCAR::GL_20 & qUtils.QCARFlags) {
[self initShaders];
}
renderingInited = YES;
}
////////////////////////////////////////////////////////////////////////////////
// Draw the current frame using OpenGL
//
// This code is a TEMPLATE for the subclassing EAGLView to complete
//
// The subclass override of this method is called by QCAR when it wishes to render the current frame to
// the screen.
//
// *** QCAR will call the subclassed method on a single background thread ***
- (void)renderFrameQCAR
{
#ifdef renderFrameQCAR_TEMPLATE
[self setFramebuffer];
// Clear colour and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render video background and retrieve tracking state
QCAR::State state = QCAR::Renderer::getInstance().begin();
QCAR::Renderer::getInstance().drawVideoBackground();
if (QCAR::GL_11 & qUtils.QCARFlags) {
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
glEnable(GL_DEPTH_TEST);
// We must detect if background reflection is active and adjust the culling direction.
// If the reflection is active, this means the pose matrix has been reflected as well,
// therefore standard counter clockwise face culling will result in "inside out" models.
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
if(QCAR::Renderer::getInstance().getVideoBackgroundConfig().mReflection == QCAR::VIDEO_BACKGROUND_REFLECTION_ON)
glFrontFace(GL_CW); //Front camera
else
glFrontFace(GL_CCW); //Back camera
for (int i = 0; i < state.getNumActiveTrackables(); ++i) {
// Render using the appropriate version of OpenGL
if (QCAR::GL_11 & qUtils.QCARFlags){
////////////////////////////////////////////////
// In subclass, draw augmentations in OpenGL ES 1.1 here
////////////////////////////////////////////////
}
#ifndef USE_OPENGL1
else {
////////////////////////////////////////////////
// In subclass, draw augmentations in OpenGL ES 2.0 here
////////////////////////////////////////////////
}
#endif
}
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
if (QCAR::GL_11 & qUtils.QCARFlags) {
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
QCAR::Renderer::getInstance().end();
[self presentFramebuffer];
#endif //renderFrameQCAR_TEMPLATE
}
@end