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
/
EAGLView.mm
executable file
·253 lines (213 loc) · 8.96 KB
/
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
/*==============================================================================
Copyright (c) 2010-2013 QUALCOMM Austria Research Center GmbH.
All Rights Reserved.
Qualcomm Confidential and Proprietary
==============================================================================*/
// Subclassed from AR_EAGLView
#import "EAGLView.h"
#import "Teapot.h"
#import "apple_sliced.h"
#import "banana.h"
#import "Texture.h"
#import <QCAR/Renderer.h>
#import <QCAR/VideoBackgroundConfig.h>
#import "QCARutils.h"
#ifndef USE_OPENGL1
#import "ShaderUtils.h"
#endif
namespace {
// Teapot texture filenames
const char* textureFilenames[] = {
"apple_sliced.jpg",
"banana.jpg"
};
// Model scale factor //gumoz
// const float kObjectScale = 3.0f;
const float kObjectScale = 120.0f;
}
@implementation EAGLView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// create list of textures we want loading - ARViewController will do this for us
int nTextures = sizeof(textureFilenames) / sizeof(textureFilenames[0]);
for (int i = 0; i < nTextures; ++i)
[textureList addObject: [NSString stringWithUTF8String:textureFilenames[i]]];
}
return self;
}
//- (void) setup3dObjects
//{
// // build the array of objects we want drawn and their texture
// // in this example we have 3 targets and require 3 models
// // but using the same underlying 3D model of a teapot, differentiated
// // by using a different texture for each
//
// for (int i=0; i < [textures count]; i++)
// {
// Object3D *obj3D = [[Object3D alloc] init];
//
// obj3D.numVertices = NUM_TEAPOT_OBJECT_VERTEX;
// obj3D.vertices = teapotVertices;
// obj3D.normals = teapotNormals;
// obj3D.texCoords = teapotTexCoords;
//
// obj3D.numIndices = NUM_TEAPOT_OBJECT_INDEX;
// obj3D.indices = teapotIndices;
//
// obj3D.texture = [textures objectAtIndex:i];
//
// [objects3D addObject:obj3D];
// [obj3D release];
// }
//}
- (void) setup3dObjects
{
// build the array of objects we want drawn and their texture
// in this example we have 3 targets and require 3 models
// but using the same underlying 3D model of a teapot, differentiated
// by using a different texture for each
for (int i=0; i < [textures count]; i++)
{
Object3D *obj3D = [[Object3D alloc] init];
if(i == 0)
{
obj3D.numVertices = apple_slicedNumVerts;
obj3D.vertices = apple_slicedVerts;
obj3D.normals = apple_slicedNormals;
obj3D.texCoords = apple_slicedTexCoords;
// obj3D.numIndices = NUM_TEAPOT_OBJECT_INDEX;
// obj3D.indices = teapotIndices;
} else
{
obj3D.numVertices = bananaNumVerts;
obj3D.vertices = bananaVerts;
obj3D.normals = bananaNormals;
obj3D.texCoords = bananaTexCoords;
}
obj3D.texture = [textures objectAtIndex:i];
[objects3D addObject:obj3D];
[obj3D release];
}
}
// called after QCAR is initialised but before the camera starts
- (void) postInitQCAR
{
// Here we could make a QCAR::setHint call to set the maximum
// number of simultaneous targets
// QCAR::setHint(QCAR::HINT_MAX_SIMULTANEOUS_IMAGE_TARGETS, 2);
}
// modify renderFrameQCAR here if you want a different 3D rendering model
////////////////////////////////////////////////////////////////////////////////
// Draw the current frame using OpenGL
//
// This method is called by QCAR when it wishes to render the current frame to
// the screen.
//
// *** QCAR will call this method on a single background thread ***
- (void)renderFrameQCAR
{
[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();
//NSLog(@"active trackables: %d", state.getNumActiveTrackables());
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.getNumTrackableResults(); ++i) {
// Get the trackable
const QCAR::TrackableResult* result = state.getTrackableResult(i);
const QCAR::Trackable& trackable = result->getTrackable();
QCAR::Matrix44F modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(result->getPose());
// Choose the texture based on the target name
int targetIndex = 0; // "stones"
if (!strcmp(trackable.getName(), "uia"))
targetIndex = 1;
// else if (!strcmp(trackable.getName(), "tarmac"))
// targetIndex = 2;
// int targetIndex = 0;
//gumoz
NSLog(@"objects3D %@, %d", objects3D, targetIndex);
Object3D *obj3D = [objects3D objectAtIndex:targetIndex];
// Render using the appropriate version of OpenGL
if (QCAR::GL_11 & qUtils.QCARFlags) {
// Load the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(qUtils.projectionMatrix.data);
// Load the model-view matrix
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(modelViewMatrix.data);
glTranslatef(0.0f, 0.0f, -kObjectScale);
glScalef(kObjectScale, kObjectScale, kObjectScale);
// Draw object
glBindTexture(GL_TEXTURE_2D, [obj3D.texture textureID]);
glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*)obj3D.texCoords);
glVertexPointer(3, GL_FLOAT, 0, (const GLvoid*)obj3D.vertices);
glNormalPointer(GL_FLOAT, 0, (const GLvoid*)obj3D.normals);
//gumoz
// glDrawElements(GL_TRIANGLES, obj3D.numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)obj3D.indices);
glDrawArrays(GL_TRIANGLES, 0, obj3D.numVertices);
}
#ifndef USE_OPENGL1
else {
// OpenGL 2
QCAR::Matrix44F modelViewProjection;
ShaderUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale, &modelViewMatrix.data[0]);
ShaderUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale, &modelViewMatrix.data[0]);
ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0], &modelViewMatrix.data[0], &modelViewProjection.data[0]);
glUseProgram(shaderProgramID);
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.vertices);
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.normals);
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.texCoords);
glEnableVertexAttribArray(vertexHandle);
glEnableVertexAttribArray(normalHandle);
glEnableVertexAttribArray(textureCoordHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, [obj3D.texture textureID]);
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
//gumoz
// glDrawElements(GL_TRIANGLES, obj3D.numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)obj3D.indices);
glDrawArrays(GL_TRIANGLES, 0, obj3D.numVertices);
ShaderUtils::checkGlError("EAGLView renderFrameQCAR");
}
#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);
}
#ifndef USE_OPENGL1
else {
glDisableVertexAttribArray(vertexHandle);
glDisableVertexAttribArray(normalHandle);
glDisableVertexAttribArray(textureCoordHandle);
}
#endif
QCAR::Renderer::getInstance().end();
[self presentFramebuffer];
}
@end